Skip to content
Open
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
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cypherstack.tor_ffi_plugin">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
12 changes: 11 additions & 1 deletion cargokit/build_tool/lib/src/android_environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,17 @@ class AndroidEnvironment {
if (rustFlags.isNotEmpty) {
rustFlags = '$rustFlags\x1f';
}
rustFlags = '$rustFlags-L\x1f$workaroundDir';
rustFlags = '$rustFlags-L\x1f$workaroundDir\x1f';

const pageSizeArgs = [
"-C",
"link-arg=-Wl,--hash-style=both",
"-C",
"link-arg=-Wl,-z,max-page-size=16384"
];
final pageSizeArgsString = pageSizeArgs.join("\x1f");

rustFlags = '$rustFlags$pageSizeArgsString';
return rustFlags;
}
}
20 changes: 13 additions & 7 deletions lib/tor_ffi_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'dart:math';

import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -174,9 +173,16 @@ class Tor {

/// Stop the proxy.
stop() async {
// Return early if already stopped.
if (_proxyPtr == nullptr) {
return;
}

final lib = TorFfiPluginBindings(_lib);
lib.tor_proxy_stop(_proxyPtr);
_proxyPtr = nullptr;
_bootstrapped = false;
_status = TorStatus.off;
}

setClientDormant(bool dormant) async {
Expand All @@ -192,16 +198,16 @@ class Tor {
Pointer<Void> _proxyPtr = nullptr;

Future<int?> _getRandomUnusedPort({List<int> excluded = const []}) async {
var random = Random.secure();
int potentialPort = 0;
int port = 0;

retry:
while (potentialPort <= 0 || excluded.contains(potentialPort)) {
potentialPort = random.nextInt(65535);
while (port == 0 || excluded.contains(port)) {
try {
var socket = await ServerSocket.bind("0.0.0.0", potentialPort);
// Bind to port 0 to let the OS assign a free port.
var socket = await ServerSocket.bind("0.0.0.0", 0);
port = socket.port;
socket.close();
return potentialPort;
return port;
} catch (_) {
continue retry;
}
Expand Down
26 changes: 18 additions & 8 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ pub unsafe extern "C" fn tor_start(
.state_dir(CfgPath::new(state_dir.to_owned()))
.cache_dir(CfgPath::new(cache_dir.to_owned()));
cfg_builder.address_filter().allow_onion_addrs(true);
cfg_builder
.preemptive_circuits()
.disable_at_threshold(1)
.min_exit_circs_for_port(1)
.initial_predicted_ports()
.clear();

let cfg = unwrap_or_return!(cfg_builder.build(), err_ret);

Expand All @@ -81,21 +87,25 @@ pub unsafe extern "C" fn tor_start(

#[no_mangle]
pub unsafe extern "C" fn tor_client_bootstrap(client: *mut c_void) -> bool {
let client = {
assert!(!client.is_null());
Box::from_raw(client as *mut TorClient<TokioNativeTlsRuntime>)
};
// Return false if client is null (not started).
if client.is_null() {
return false;
}

let client = Box::from_raw(client as *mut TorClient<TokioNativeTlsRuntime>);

unwrap_or_return!(client.runtime().block_on(client.bootstrap()), false);
true
}

#[no_mangle]
pub unsafe extern "C" fn tor_client_set_dormant(client: *mut c_void, soft_mode: bool) {
let client = {
assert!(!client.is_null());
Box::from_raw(client as *mut TorClient<TokioNativeTlsRuntime>)
};
// Return early if client is null (not started).
if client.is_null() {
return;
}

let client = Box::from_raw(client as *mut TorClient<TokioNativeTlsRuntime>);

let dormant_mode = if soft_mode {
DormantMode::Soft
Expand Down