Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ship Tor binary and use custom TorHandler #2366

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 42 additions & 23 deletions packages/mobile/android/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,45 @@ cmake_minimum_required(VERSION 3.4.1)
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
own-native-lib
node-wrapper

# Sets the library as a shared library.
SHARED
# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/cpp/own-native-lib.cpp
src/main/cpp/rn-bridge.cpp
)
# Provides a relative path to your source file(s).
src/main/cpp/node-wrapper.cpp
src/main/cpp/rn-bridge.cpp )

add_library(
tor-wrapper
SHARED
src/main/cpp/tor-wrapper.cpp )

include_directories(libnode/include/node/)
include_directories(libtor/include/tor/)
include_directories(src/main/cpp/)

add_library( libnode
SHARED
IMPORTED )
SHARED
IMPORTED )

add_library( libtor
SHARED
IMPORTED )

set_target_properties( # Specifies the target library.
libnode
libnode

# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION

# Provides the path to the library you want to import.
${CMAKE_SOURCE_DIR}/libnode/bin/${ANDROID_ABI}/libnode.so )
# Provides the path to the library you want to import.
${CMAKE_SOURCE_DIR}/libnode/bin/libnode.so )

set_target_properties(
libtor
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libtor/bin/libtor.so )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
Expand All @@ -44,21 +58,26 @@ set_target_properties( # Specifies the target library.
# completing its build.

find_library( # Sets the name of the path variable.
log-lib
log-lib

# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies the name of the NDK library that
# you want CMake to locate.
log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
own-native-lib
node-wrapper

libnode

libnode
# Links the target library to the log library
# included in the NDK.
${log-lib} )

# Links the target library to the log library
# included in the NDK.
${log-lib} )
target_link_libraries(
tor-wrapper
libtor
${log-lib} )
7 changes: 1 addition & 6 deletions packages/mobile/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,6 @@ android {
cppFlags ""
}
}
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
splits {
abi {
Expand Down Expand Up @@ -320,7 +315,7 @@ dependencies {

implementation group: 'commons-io', name: 'commons-io', version: '2.6'

implementation 'info.guardianproject:tor-android:0.4.5.7'
// implementation 'info.guardianproject:tor-android:0.4.5.7'
implementation 'info.guardianproject:jtorctl:0.4.5.7'

// Websockets connection
Expand Down
Binary file added packages/mobile/android/app/libtor/bin/libtor.so
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright (c) 2019, Matthew Finkel.
* Copyright (c) 2019, Hans-Christoph Steiner.
* Copyright (c) 2007-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
leblowl marked this conversation as resolved.
Show resolved Hide resolved

#ifndef ORG_TORPROJECT_JNI_TORSERVICE_H
#define ORG_TORPROJECT_JNI_TORSERVICE_H

#include <jni.h>

/*
* Class: org_torproject_jni_TorService
* Method: createTorConfiguration
* Signature: ()Z
*/
extern "C" JNIEXPORT jboolean JNICALL
Java_org_torproject_jni_TorService_createTorConfiguration
(JNIEnv *, jobject);

/*
* Class: org_torproject_jni_TorService
* Method: mainConfigurationSetCommandLine
* Signature: ([Ljava/lang/String;)Z
*/
extern "C" JNIEXPORT jboolean JNICALL
Java_org_torproject_jni_TorService_mainConfigurationSetCommandLine
(JNIEnv *, jobject, jobjectArray);

/*
* Class: org_torproject_jni_TorService
* Method: mainConfigurationSetupControlSocket
* Signature: ()Z
*/
extern "C" JNIEXPORT jboolean JNICALL
Java_org_torproject_jni_TorService_mainConfigurationSetupControlSocket
(JNIEnv *, jobject);

/*
* Class: org_torproject_jni_TorService
* Method: mainConfigurationFree
* Signature: ()V
*/
extern "C" JNIEXPORT void JNICALL
Java_org_torproject_jni_TorService_mainConfigurationFree
(JNIEnv *, jobject);

/*
* Class: org_torproject_jni_TorService
* Method: apiGetProviderVersion
* Signature: ()Ljava/lang/String;
*/
extern "C" JNIEXPORT jstring JNICALL
Java_org_torproject_jni_TorService_apiGetProviderVersion
(JNIEnv *, jobject);

/*
* Class: org_torproject_jni_TorService
* Method: runMain
* Signature: ()I
*/
extern "C" JNIEXPORT jint JNICALL
Java_org_torproject_jni_TorService_runMain
(JNIEnv *, jobject);

/*
* Class: org_torproject_jni_TorService
* Method: prepareFileDescriptor
*/
extern "C" JNIEXPORT jobject JNICALL
Java_org_torproject_jni_TorService_prepareFileDescriptor
(JNIEnv *env, jclass, jstring);

#endif /* !defined(ORG_TORPROJECT_JNI_TORSERVICE_H) */

54 changes: 54 additions & 0 deletions packages/mobile/android/app/src/main/cpp/tor-wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <jni.h>

#include "org_torproject_jni_TorService.h"

extern "C" JNIEXPORT void JNICALL
Java_com_quietmobile_Backend_TorHandler_mainConfigurationSetupControlSocket(
JNIEnv *env,
jobject thisObj)
{
Java_org_torproject_jni_TorService_mainConfigurationSetupControlSocket(env, thisObj);
}

extern "C" JNIEXPORT void JNICALL
Java_com_quietmobile_Backend_TorHandler_runMain(
JNIEnv *env,
jobject thisObj)
{
Java_org_torproject_jni_TorService_runMain(env, thisObj);
}

extern "C" JNIEXPORT void JNICALL
Java_com_quietmobile_Backend_TorHandler_createTorConfiguration(
JNIEnv *env,
jobject thisObj)
{
Java_org_torproject_jni_TorService_createTorConfiguration(env, thisObj);
}

extern "C" JNIEXPORT void JNICALL
Java_com_quietmobile_Backend_TorHandler_prepareFileDescriptor(
JNIEnv *env,
jclass thisClass,
jstring stringArgv)
{
Java_org_torproject_jni_TorService_prepareFileDescriptor(env, thisClass, stringArgv);
}

extern "C" JNIEXPORT void JNICALL
Java_com_quietmobile_Backend_TorHandler_mainConfigurationSetCommandLine(
JNIEnv *env,
jobject thisObj,
jobjectArray arrArgv)
{
Java_org_torproject_jni_TorService_mainConfigurationSetCommandLine(env, thisObj, arrArgv);
}

extern "C" JNIEXPORT void JNICALL
Java_com_quietmobile_Backend_TorHandler_mainConfigurationFree(
JNIEnv *env,
jobject thisObj)
{
Java_org_torproject_jni_TorService_mainConfigurationFree(env, thisObj);
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters
// Use dedicated class for composing and displaying notifications
private lateinit var notificationHandler: NotificationHandler

private lateinit var torHandler: TorHandler

companion object {
init {
System.loadLibrary("own-native-lib")
System.loadLibrary("node")
System.loadLibrary("node-wrapper")
}
}

Expand Down Expand Up @@ -97,10 +99,19 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters

withContext(Dispatchers.IO) {

// Get and store data port for usage in methods across the app
val dataPath = Utils.createDirectory(context)
val dataPort = Utils.getOpenPort(11000)

val socketIOSecret = Utils.generateRandomString(20)

launch {
torHandler = TorHandler(context)
torHandler.controlPort = Utils.getOpenPort(Utils.generateRandomInt())
torHandler.socksPort = Utils.getOpenPort(Utils.generateRandomInt())
torHandler.httpTunnelPort = Utils.getOpenPort(Utils.generateRandomInt())
torHandler.startTorThread()
}

// Init nodejs project
launch {
nodeProject.init()
Expand All @@ -125,21 +136,14 @@ class BackendWorker(private val context: Context, workerParams: WorkerParameters
startWebsocketConnection(dataPort, socketIOSecret)
}

val dataPath = Utils.createDirectory(context)

val appInfo = context.packageManager.getApplicationInfo(context.packageName, 0)
val torBinary = appInfo.nativeLibraryDir + "/libtor.so"

val platform = "mobile"

launch {
/*
* The point of this delay is to prevent startup race condition
* which occurs particularly often when running Detox tests
* https://github.com/TryQuiet/quiet/issues/2214
*/
delay(500)
startNodeProjectWithArguments("bundle.cjs --torBinary $torBinary --dataPath $dataPath --dataPort $dataPort --platform $platform --socketIOSecret $socketIOSecret")
startNodeProjectWithArguments("bundle.cjs --authCookie ${torHandler.authCookie} --controlPort ${torHandler.controlPort} --httpTunnelPort ${torHandler.httpTunnelPort} --dataPath $dataPath --dataPort $dataPort --socketIOSecret $socketIOSecret --platform mobile")
}
}

Expand Down
Loading
Loading