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 2 commits
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
30 changes: 30 additions & 0 deletions docs/tor_mobile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Guide on Android Tor

Tor runs within its own thread, spawned and managed by [TorHandler](https://github.com/TryQuiet/quiet/blob/8924a5573ed11980ceeadb2f8dd1ce45169f03ee/packages/mobile/android/app/src/main/java/com/quietmobile/Backend/TorHandler.java). TorHandler is based on the Tor-Android library from the Guardian Project, specifically the [TorService](https://github.com/guardianproject/tor-android/blob/master/tor-android-binary/src/main/java/org/torproject/jni/TorService.java).

We ship the binary [libtor.so](https://github.com/TryQuiet/quiet/blob/8924a5573ed11980ceeadb2f8dd1ce45169f03ee/packages/mobile/android/app/src/main/libs/arm64-v8a/libtor.so), which exposes certain native APIs through JNI. These are used to configure and run Tor. These JNI methods are directed to the official Guardian Project's library, forcing us to use our [wrapper](https://github.com/TryQuiet/quiet/blob/8924a5573ed11980ceeadb2f8dd1ce45169f03ee/packages/mobile/android/app/src/main/cpp/tor-wrapper.cpp) around it. It bridges C-level global namespace accessible libtor.so methods and exposes them through Quiet's app scope JNI interface, enabling its use without modifying the Tor source code itself.

## Building the Tor Binary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we currently using the most recent release of Tor?

Are there any other notes it would be good to include for someone updating Tor in the future, about how to proceed?


The Tor binary is designed to be a ready-made product and does not contain the SONAME property. This absence makes linking it to any other shared object, like our tor-wrapper, challenging. While building Tor, it is essential to remember to pass an extra linker flag to set the SONAME property for the compilation product. The entry point for building the Tor binary is [tor-droid-make.sh](https://github.com/guardianproject/tor-android/blob/master/tor-droid-make.sh), with a crucial step being `build_external_dependencies()`. It is advisable to modify the script and skip the `./gradlew` command build assembling, as it is not essential for acquiring the Tor binary. The script is important as it sets flags to enable the Android API in Tor. The prerequisites for building are listed [here](https://raw.githubusercontent.com/guardianproject/tor-android/master/BUILD). The output Tor binary will be located in `external/tor/lib/{ABI}/`.

### Build Command
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to automate building of Tor directly from their repository on each release for a given Tor version?

If so, how hard would this be?

```bash
LDFLAGS='-Wl,-soname,libtor.so' ./tor-droid-make.sh externals -a arm64-v8a
```

### Verify Build
To check for the presence of the SONAME property, run:
```bash
readelf -d libtor.so
```

To verify the API presence, run:
```bash
nm -D libtor.so | grep "Java_org_torproject_jni_TorService_createTorConfiguration"
```

## Post Mortem

#### Missing Tor Binary ([Issue #2328](https://github.com/TryQuiet/quiet/issues/2328))
The issue was caused by the removal of the deprecated Gradle flag `enableUncompressedNativeLibs`, which resulted in the deletion of the binary from the location to which we were pointing. We had to alter the method we use the binary, as now described in the previous sections of this document.
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