Skip to content

Commit

Permalink
add native tunnel module
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoby committed Jul 23, 2024
1 parent b3fbef7 commit c3cd141
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/actions/gradle/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ runs:

- name: Build Android ${{ inputs.buildType }} Bundle
shell: bash
run: ./gradlew :app:assemble${{ inputs.buildType }} :app:bundle${{ inputs.buildType }}
run: ./gradlew assemble${{ inputs.buildType }} bundle${{ inputs.buildType }}
env:
RELEASE_KEYSTORE: ${{ runner.workspace }}/ziti-mobile.jks
RELEASE_KEYSTORE_PASSWORD: ${{ inputs.keystorePassword }}
45 changes: 42 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,47 @@ plugins {
alias(libs.plugins.kotlin.android)
}

version = parent.version

def vc = versionCode
def getCommitHash = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'log', '-1', '--format=%h'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null
}
}

def getVersionName = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null
}
}

def getVersionCode = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine("git", "rev-list", "--first-parent", "--count", "origin/main")
standardOutput = stdout
}
return Integer.parseInt(stdout.toString().trim())
}

def gitHash = getCommitHash()
def gitBranch = getVersionName()
version = getVersionName()

android {
namespace = "org.openziti.mobile"
Expand All @@ -28,7 +66,7 @@ android {
defaultConfig {
applicationId "org.openziti.mobile"
minSdkVersion 26
versionCode vc
versionCode getVersionCode()
versionName "${project.version}"

buildConfigField "String", "GIT_COMMIT", "\"${gitHash}\""
Expand Down Expand Up @@ -73,6 +111,7 @@ android {

dependencies {
implementation fileTree(include: ["*.jar"], dir: "libs")
implementation(project(":tunnel"))

implementation(libs.ziti.android) {
transitive = true
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/openziti/mobile/ZitiMobileEdgeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ package org.openziti.mobile

import android.app.Application
import android.content.Intent
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import org.openziti.android.Ziti

import org.openziti.tunnel.Tunnel

/**
*
*/
class ZitiMobileEdgeApp: Application() {
init {
Log.i(this.javaClass.simpleName, "native[tlsuv]: ${Tunnel().tlsuvVersion()}")
Log.i(this.javaClass.simpleName, "native[ziti]: ${Tunnel().zitiSdkVersion()}")
Log.i(this.javaClass.simpleName, "native[ziti-tunnel]: ${Tunnel().zitiTunnelVersion()}")
}
companion object {
val ROUTE_CHANGE = "route_change"
lateinit var app: ZitiMobileEdgeApp
Expand Down
87 changes: 0 additions & 87 deletions build.gradle

This file was deleted.

12 changes: 12 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2021 NetFoundry. All rights reserved.
*/

// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
alias(libs.plugins.android.app) apply(false)
alias(libs.plugins.android.lib) apply(false)
alias(libs.plugins.kotlin.android) apply(false)
}
group = "org.openziti"
5 changes: 5 additions & 0 deletions buildSrc/git.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* Copyright (c) 2024 NetFoundry. All rights reserved.
*/


1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junit-

[plugins]
android-app = { id = "com.android.application", version.ref = "android" }
android-lib = { id = "com.android.library", version.ref = "android" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

[bundles]
Expand Down
9 changes: 9 additions & 0 deletions tunnel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.20)

project(
ziti-native-deps
DESCRIPTION "OpenZiti tunneler SDK"
HOMEPAGE_URL "https://github.com/openziti/ziti-tunneler-sdk-c"
LANGUAGES C CXX
)

File renamed without changes.
23 changes: 17 additions & 6 deletions ziti-native-tunnel/build.gradle.kts → tunnel/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
import com.android.build.gradle.internal.tasks.factory.dependsOn

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
alias(libs.plugins.android.lib)
alias(libs.plugins.kotlin.android)
}

val localProperties = gradleLocalProperties(parent!!.projectDir, providers)

val zitiSdkDir = localProperties["ziti.dir"]
println("ziti-sdk = ${zitiSdkDir}")

val cmakeArgs = mutableListOf("-DDEPS_DIR=${project.buildDir.path}/cmake")
localProperties["ziti.dir"]?.let { cmakeArgs.add("-DZITI_SDK_DIR=$it") }
localProperties["tlsuv.dir"]?.let { cmakeArgs.add("-Dtlsuv_DIR=$it") }
localProperties["tunnel.dir"]?.let { cmakeArgs.add("-Dtunnel_DIR=$it")}

android {
namespace = "org.openziti.tunnel"
compileSdk = 34

defaultConfig {
minSdk = 29
minSdk = 26

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
externalNativeBuild {
cmake {
cppFlags("")
arguments(*cmakeArgs.toTypedArray())
}
}
}
Expand Down Expand Up @@ -44,7 +57,6 @@ android {
}

dependencies {

implementation(libs.core.ktx)
implementation(libs.appcompat)
implementation(libs.material)
Expand All @@ -63,6 +75,5 @@ presets.forEach { triplet ->
val task = tasks.register<Exec>("build-native-deps-${triplet}") {
commandLine("cmake", "--preset", triplet)
}

buildNative.dependsOn(task)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

message(NOTICE "dependencies built in ${DEPS_DIR}")
message(NOTICE "building for ${ANDROID_ABI}")
if (${ANDROID_ABI} STREQUAL "arm64-v8a")
set(deps_abi_path "${DEPS_DIR}/arm64-android/vcpkg_installed/arm64-android")
elseif (${ANDROID_ABI} STREQUAL "armeabi-v7a")
set(deps_abi_path "${DEPS_DIR}/arm-android/vcpkg_installed/arm-neon-android")
elseif (${ANDROID_ABI} STREQUAL "x86")
set(deps_abi_path "${DEPS_DIR}/x86-android/vcpkg_installed/x86-android")
elseif (${ANDROID_ABI} STREQUAL "x86_64")
set(deps_abi_path "${DEPS_DIR}/x64-android/vcpkg_installed/x64-android")
endif ()

set(CMAKE_FIND_ROOT_PATH "${deps_abi_path};${CMAKE_FIND_ROOT_PATH}")

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("tunnel")

set(ZITI_BUILD_TESTS off)
set(ZITI_BUILD_PROGRAMS off)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
Expand All @@ -34,4 +51,19 @@ add_library(${CMAKE_PROJECT_NAME} SHARED
target_link_libraries(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log)
log)

if (tunnel_DIR)
add_subdirectory(${tunnel_DIR} ${CMAKE_CURRENT_BINARY_DIR}/tunnel-sdk)
else (tunnel_DIR)
include(FetchContent)
FetchContent_Declare(tun-sdk
GIT_REPOSITORY http://github.com/openziti/ziti-tunnel-sdk-c.git
GIT_TAG main
)
FetchContent_MakeAvailable(tun-sdk)
endif ()
target_link_libraries(${CMAKE_PROJECT_NAME}
ziti-tunnel-sdk-c
ziti-tunnel-cbs-c
)
47 changes: 47 additions & 0 deletions tunnel/src/main/cpp/tunnel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <jni.h>
#include <string>

#include <tlsuv/tlsuv.h>
#include <ziti/ziti.h>
#include <ziti/ziti_tunnel.h>

static jstring JNICALL tlsuvVersion(JNIEnv *env, jobject /* this */);
static jstring JNICALL zitiSdkVersion(JNIEnv *env, jobject self);
static jstring JNICALL zitiTunnelVersion(JNIEnv *env, jobject self);

extern "C"
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}

// Find your class. JNI_OnLoad is called from the correct class loader context for this to work.
jclass c = env->FindClass("org/openziti/tunnel/Tunnel");
if (c == nullptr) return JNI_ERR;

// Register your class' native methods.
static const JNINativeMethod methods[] = {
{"tlsuvVersion", "()Ljava/lang/String;", reinterpret_cast<void*>(tlsuvVersion)},
{"zitiSdkVersion", "()Ljava/lang/String;", (void*)(zitiSdkVersion)},
{"zitiTunnelVersion", "()Ljava/lang/String;", (void*)(zitiTunnelVersion)},
};
int rc = env->RegisterNatives(c, methods, sizeof(methods)/sizeof(JNINativeMethod));
if (rc != JNI_OK) return rc;

return JNI_VERSION_1_6;
}

jstring JNICALL tlsuvVersion(JNIEnv *env, jobject) {
return env->NewStringUTF(tlsuv_version());
}

jstring JNICALL zitiSdkVersion(JNIEnv *env, jobject) {
return env->NewStringUTF(ziti_get_version()->version);
}

jstring JNICALL zitiTunnelVersion(JNIEnv *env, jobject) {
return env->NewStringUTF(ziti_tunneler_version());
}


Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.openziti.tunnel

class NativeLib {
class Tunnel {

/**
* A native method that is implemented by the 'tunnel' native library,
* which is packaged with this application.
*/
external fun stringFromJNI(): String
external fun tlsuvVersion(): String
external fun zitiSdkVersion(): String
external fun zitiTunnelVersion(): String

companion object {
// Used to load the 'tunnel' library on application startup.
Expand Down
File renamed without changes.
Loading

0 comments on commit c3cd141

Please sign in to comment.