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

New Tor engine Android test #2268

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01'
implementation files("../../node_modules/react-native-tor/android/libs/sifir_android.aar")
// gif
implementation 'com.facebook.fresco:fresco:3.1.3'
implementation 'com.facebook.fresco:animated-gif:3.1.3'
Expand All @@ -214,10 +213,14 @@ dependencies {

// implementation 'info.guardianproject:tor-android:0.4.7.8'
// implementation 'info.guardianproject:jtorctl:0.4.5.7'
implementation "io.matthewnelson.kotlin-components:kmp-tor:4.8.10-0-1.4.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"

implementation('dev.doubledot.doki:library:0.0.1@aar') {
transitive = true
}

}

configurations {
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/java/com/zeus/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MainApplication : Application(), ReactApplication {
add(LndMobileToolsPackage())
add(LndMobileScheduledSyncPackage())
add(LncPackage())
// add(new ZeusTorPackage());
add(TorMobilePackage());
}

override fun getJSMainModuleName(): String = "index"
Expand Down
138 changes: 138 additions & 0 deletions android/app/src/main/java/com/zeus/TorMobile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package app.zeusln.zeus;
Copy link
Author

Choose a reason for hiding this comment

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


import android.util.Log;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import io.bluewallet.bluewallet.tor.TorKmpManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class TorMobile extends ReactContextBaseJavaModule {
private final String TAG = "TorMobile";
private TorKmpManager torKmpManager;
private ReactApplicationContext context;
public TorMobile(ReactApplicationContext reactContext) {
context = reactContext;
}

@Override
public String getName() {
return "TorMobile";
}

@ReactMethod
public void sendRequest(String action, String url, String headers, String body, final Promise promise) throws JSONException {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS) // Set connection timeout
.readTimeout(30, TimeUnit.SECONDS) // Set read timeout
.proxy(torKmpManager.getProxy()).build();

Request.Builder requestBuilder = new Request.Builder().url(url);

JSONObject headersObject = new JSONObject(headers);
headersObject.keys().forEachRemaining(key -> {
String value = headersObject.optString(key);
requestBuilder.addHeader(key, value);
});

if (Objects.equals(action, "DELETE")) {
requestBuilder.delete();
} else if (Objects.equals(action, "POST")) {
RequestBody requestBody = RequestBody.create(body, MediaType.get("application/json; charset=utf-8"));
requestBuilder.post(requestBody);
} else {
requestBuilder.get();
}

Request request = requestBuilder.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.d(TAG, e.toString());
}

@Override
public void onResponse(Call call, Response response) throws IOException {
String body = response.body() != null ? response.body().string() : "{}";
JSONObject headersJson = new JSONObject();
response.headers().names().forEach(name -> {
try {
headersJson.put(name, response.header(name));
} catch (JSONException e) {
throw new RuntimeException(e);
}
});
promise.resolve("{\"json\":" + body + ", \"headers\": " + headersJson +"}");
}
});
}

@ReactMethod(isBlockingSynchronousMethod = true)
public String getTorStatus() {
return torKmpManager.getTorState().getState().name();
}

@ReactMethod
public void stop(Promise promise) {
try {
torKmpManager.getTorOperationManager().stopQuietly();
promise.resolve(true);
} catch (Exception e) {
promise.reject("ERROR_CODE", e);
}
}

@ReactMethod
public void start(Promise promise) {
try {
torKmpManager = new TorKmpManager(context.getCurrentActivity().getApplication());
torKmpManager.getTorOperationManager().startQuietly();
promise.resolve(true);
} catch (Exception e) {
promise.reject("ERROR_CODE", e);
}
}

@ReactMethod
public void restart(Promise promise) {
try {
torKmpManager = new TorKmpManager(context.getCurrentActivity().getApplication());
torKmpManager.getTorOperationManager().restartQuietly();
promise.resolve(true);
} catch (Exception e) {
promise.reject("ERROR_CODE", e);
}
}

@ReactMethod
public void newIdentity(Promise promise) {
try {
torKmpManager.newIdentity(context.getCurrentActivity().getApplication());
promise.resolve(true);
} catch (Exception e) {
promise.reject("ERROR_CODE", e);
}
}
}
22 changes: 22 additions & 0 deletions android/app/src/main/java/com/zeus/TorMobilePackage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package app.zeusln.zeus;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TorMobilePackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.asList(new TorMobile(reactContext));
}
}
8 changes: 8 additions & 0 deletions android/app/src/main/java/com/zeus/tor/EnumTorState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.bluewallet.bluewallet.tor

enum class EnumTorState {
STARTING,
ON,
STOPPING,
OFF
}
Loading
Loading