-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
KoalaSat
wants to merge
2
commits into
ZeusLN:master
Choose a base branch
from
KoalaSat:new-tor-engine-android-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package app.zeusln.zeus; | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file has been extracted from Robosats' app https://github.com/RoboSats/robosats/blob/main/mobile/android/app/src/main/java/com/robosats/modules/TorModule.java