diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 23ad8ff7..e45eeee1 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -1,9 +1,8 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. +# SPDX-FileCopyrightText: 2022 Foundation Devices Inc. +# +# SPDX-License-Identifier: GPL-3.0-or-later -name: Dart +name: Check on: push: @@ -18,25 +17,33 @@ jobs: steps: - uses: actions/checkout@v3 - # Note: This workflow uses the latest stable version of the Dart SDK. - # You can specify other versions if desired, see documentation here: - # https://github.com/dart-lang/setup-dart/blob/main/README.md - # - uses: dart-lang/setup-dart@v1 - - uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd603 + - name: REUSE Compliance Check + uses: fsfe/reuse-action@v1 + + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: rustfmt, clippy + + - uses: subosito/flutter-action@v2 + with: + channel: 'stable' + flutter-version: '3.16.3' - name: Install dependencies - run: dart pub get + run: flutter pub get + + - name: Verify Dart formatting + run: dart format --output=none --set-exit-if-changed . + + - name: Verify Rust formatting + run: cargo fmt --manifest-path rust/Cargo.toml --all -- --check - # Uncomment this step to verify the use of 'dart format' on each commit. - # - name: Verify formatting - # run: dart format --output=none --set-exit-if-changed . + - name: Run Rust tests + run: cargo test --manifest-path rust/Cargo.toml - # Consider passing '--fatal-infos' for slightly stricter analysis. - - name: Analyze project source - run: dart analyze + - name: Analyze Dart project source + run: flutter analyze - # Your project will need to have tests in test/ and a dependency on - # package:test for this step to succeed. Note that Flutter projects will - # want to change this to 'flutter test'. - - name: Run tests - run: dart test +# - name: Run Dart tests +# run: flutter test diff --git a/.gitignore b/.gitignore index 9943fb54..a5837dd1 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ migrate_working_dir/ build/ rust/target/.rustc_info.json +rust/target/debug \ No newline at end of file diff --git a/.reuse/dep5 b/.reuse/dep5 index 98c261ab..bcef0f8d 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -6,3 +6,11 @@ Source: https://github.com/Foundation-Devices/tor Files: cargokit/* Copyright: 2022 Matej Knopp License: MIT + +Files: lib/generated* +Copyright: 2024 Foundation Devices Inc. +License: GPL-3.0-or-later + +Files: rust/target/* +Copyright: 2024 Foundation Devices Inc. +License: GPL-3.0-or-later \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml index a5744c1c..39e43d69 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,4 +1,9 @@ +# SPDX-FileCopyrightText: 2022 Foundation Devices Inc. +# +# SPDX-License-Identifier: GPL-3.0-or-later include: package:flutter_lints/flutter.yaml -# Additional information about this file can be found at -# https://dart.dev/guides/language/analysis-options +analyzer: + exclude: + - 'lib/generated*' + - 'cargokit' \ No newline at end of file diff --git a/analysis_options.yaml.license b/analysis_options.yaml.license deleted file mode 100644 index 679f6bb2..00000000 --- a/analysis_options.yaml.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2023 Foundation Devices Inc. - -SPDX-License-Identifier: GPL-3.0-or-later diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml index 61b6c4de..890e2a61 100644 --- a/example/analysis_options.yaml +++ b/example/analysis_options.yaml @@ -1,29 +1,5 @@ -# This file configures the analyzer, which statically analyzes Dart code to -# check for errors, warnings, and lints. -# -# The issues identified by the analyzer are surfaced in the UI of Dart-enabled -# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be -# invoked from the command line by running `flutter analyze`. - -# The following line activates a set of recommended lints for Flutter apps, -# packages, and plugins designed to encourage good coding practices. include: package:flutter_lints/flutter.yaml linter: - # The lint rules applied to this project can be customized in the - # section below to disable rules from the `package:flutter_lints/flutter.yaml` - # included above or to enable additional rules. A list of all available lints - # and their documentation is published at - # https://dart-lang.github.io/linter/lints/index.html. - # - # Instead of disabling a lint rule for the entire project in the - # section below, it can also be suppressed for a single line of code - # or a specific dart file by using the `// ignore: name_of_lint` and - # `// ignore_for_file: name_of_lint` syntax on the line or in the file - # producing the lint. rules: - # avoid_print: false # Uncomment to disable the `avoid_print` rule - # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule - -# Additional information about this file can be found at -# https://dart.dev/guides/language/analysis-options + avoid_print: false \ No newline at end of file diff --git a/justfile b/justfile new file mode 100644 index 00000000..c2111eea --- /dev/null +++ b/justfile @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2022 Foundation Devices Inc. +# +# SPDX-License-Identifier: GPL-3.0-or-later + +generate: + dart run ffigen + +format: + cargo fmt --manifest-path rust/Cargo.toml && \ + dart format . && \ + flutter analyze diff --git a/lib/tor_bindings_generated.dart b/lib/generated_bindings.dart similarity index 77% rename from lib/tor_bindings_generated.dart rename to lib/generated_bindings.dart index c5be1b53..6da76580 100644 --- a/lib/tor_bindings_generated.dart +++ b/lib/generated_bindings.dart @@ -18,17 +18,7 @@ class NativeLibrary { lookup) : _lookup = lookup; - ffi.Pointer tor_last_error_message() { - return _tor_last_error_message(); - } - - late final _tor_last_error_messagePtr = - _lookup Function()>>( - 'tor_last_error_message'); - late final _tor_last_error_message = - _tor_last_error_messagePtr.asFunction Function()>(); - - ffi.Pointer tor_start( + ffi.Pointer tor_start( int socks_port, ffi.Pointer state_dir, ffi.Pointer cache_dir, @@ -42,14 +32,14 @@ class NativeLibrary { late final _tor_startPtr = _lookup< ffi.NativeFunction< - ffi.Pointer Function(ffi.Uint16, ffi.Pointer, + ffi.Pointer Function(ffi.Uint16, ffi.Pointer, ffi.Pointer)>>('tor_start'); late final _tor_start = _tor_startPtr.asFunction< - ffi.Pointer Function( + ffi.Pointer Function( int, ffi.Pointer, ffi.Pointer)>(); bool tor_bootstrap( - ffi.Pointer client, + ffi.Pointer client, ) { return _tor_bootstrap( client, @@ -57,10 +47,10 @@ class NativeLibrary { } late final _tor_bootstrapPtr = - _lookup)>>( + _lookup)>>( 'tor_bootstrap'); late final _tor_bootstrap = - _tor_bootstrapPtr.asFunction)>(); + _tor_bootstrapPtr.asFunction)>(); void tor_hello() { return _tor_hello(); @@ -69,8 +59,46 @@ class NativeLibrary { late final _tor_helloPtr = _lookup>('tor_hello'); late final _tor_hello = _tor_helloPtr.asFunction(); + + ffi.Pointer tor_last_error_message() { + return _tor_last_error_message(); + } + + late final _tor_last_error_messagePtr = + _lookup Function()>>( + 'tor_last_error_message'); + late final _tor_last_error_message = + _tor_last_error_messagePtr.asFunction Function()>(); + + int tor_get_nofile_limit() { + return _tor_get_nofile_limit(); + } + + late final _tor_get_nofile_limitPtr = + _lookup>( + 'tor_get_nofile_limit'); + late final _tor_get_nofile_limit = + _tor_get_nofile_limitPtr.asFunction(); + + int tor_set_nofile_limit( + int limit, + ) { + return _tor_set_nofile_limit( + limit, + ); + } + + late final _tor_set_nofile_limitPtr = + _lookup>( + 'tor_set_nofile_limit'); + late final _tor_set_nofile_limit = + _tor_set_nofile_limitPtr.asFunction(); } +const int true1 = 1; + +const int false1 = 0; + const int INT8_MIN = -128; const int INT16_MIN = -32768; @@ -173,72 +201,6 @@ const int WINT_MIN = 0; const int WINT_MAX = 4294967295; -const int INT8_WIDTH = 8; - -const int UINT8_WIDTH = 8; - -const int INT16_WIDTH = 16; - -const int UINT16_WIDTH = 16; - -const int INT32_WIDTH = 32; - -const int UINT32_WIDTH = 32; - -const int INT64_WIDTH = 64; - -const int UINT64_WIDTH = 64; - -const int INT_LEAST8_WIDTH = 8; - -const int UINT_LEAST8_WIDTH = 8; - -const int INT_LEAST16_WIDTH = 16; - -const int UINT_LEAST16_WIDTH = 16; - -const int INT_LEAST32_WIDTH = 32; - -const int UINT_LEAST32_WIDTH = 32; - -const int INT_LEAST64_WIDTH = 64; - -const int UINT_LEAST64_WIDTH = 64; - -const int INT_FAST8_WIDTH = 8; - -const int UINT_FAST8_WIDTH = 8; - -const int INT_FAST16_WIDTH = 64; - -const int UINT_FAST16_WIDTH = 64; - -const int INT_FAST32_WIDTH = 64; - -const int UINT_FAST32_WIDTH = 64; - -const int INT_FAST64_WIDTH = 64; - -const int UINT_FAST64_WIDTH = 64; - -const int INTPTR_WIDTH = 64; - -const int UINTPTR_WIDTH = 64; - -const int INTMAX_WIDTH = 64; - -const int UINTMAX_WIDTH = 64; - -const int PTRDIFF_WIDTH = 64; - -const int SIG_ATOMIC_WIDTH = 32; - -const int SIZE_WIDTH = 64; - -const int WCHAR_WIDTH = 32; - -const int WINT_WIDTH = 32; - const int NULL = 0; const int WNOHANG = 1; diff --git a/lib/socks_socket.dart b/lib/socks_socket.dart index db41eea5..cde9b2f6 100644 --- a/lib/socks_socket.dart +++ b/lib/socks_socket.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:flutter/foundation.dart'; + /// A SOCKS5 socket. /// /// This class is a wrapper around the Socket class that implements the @@ -68,11 +70,11 @@ class SOCKSSocket { /// A StreamController that listens to the _socksSocket and broadcasts. final StreamController> _responseController = - StreamController.broadcast(); + StreamController.broadcast(); /// A StreamController that listens to the _secureSocksSocket and broadcasts. final StreamController> _secureResponseController = - StreamController.broadcast(); + StreamController.broadcast(); /// Getter for the StreamController that listens to the _socksSocket and /// broadcasts, or the _secureSocksSocket and broadcasts if SSL is enabled. @@ -106,8 +108,8 @@ class SOCKSSocket { /// A Future that resolves to a SOCKSSocket instance. static Future create( {required String proxyHost, - required int proxyPort, - bool sslEnabled = false}) async { + required int proxyPort, + bool sslEnabled = false}) async { // Create a SOCKS socket instance. var instance = SOCKSSocket._(proxyHost, proxyPort, sslEnabled); @@ -121,8 +123,8 @@ class SOCKSSocket { /// Constructor. SOCKSSocket( {required this.proxyHost, - required this.proxyPort, - required this.sslEnabled}) { + required this.proxyPort, + required this.sslEnabled}) { _init(); } @@ -141,7 +143,7 @@ class SOCKSSocket { // Listen to the socket. _subscription = _socksSocket.listen( - (data) { + (data) { // Add the data to the response controller. _responseController.add(data); }, @@ -226,7 +228,7 @@ class SOCKSSocket { // Listen to the secure socket. _subscription = _secureSocksSocket.listen( - (data) { + (data) { // Add the data to the response controller. _secureResponseController.add(data); }, @@ -289,24 +291,24 @@ class SOCKSSocket { } StreamSubscription> listen( - void Function(List data)? onData, { - Function? onError, - void Function()? onDone, - bool? cancelOnError, - }) { + void Function(List data)? onData, { + Function? onError, + void Function()? onDone, + bool? cancelOnError, + }) { return sslEnabled ? _secureResponseController.stream.listen( - onData, - onError: onError, - onDone: onDone, - cancelOnError: cancelOnError, - ) + onData, + onError: onError, + onDone: onDone, + cancelOnError: cancelOnError, + ) : _responseController.stream.listen( - onData, - onError: onError, - onDone: onDone, - cancelOnError: cancelOnError, - ); + onData, + onError: onError, + onDone: onDone, + cancelOnError: cancelOnError, + ); } /// Sends the server.features command to the proxy server. @@ -327,16 +329,20 @@ class SOCKSSocket { // Wait for the response from the proxy server. var responseData = await _responseController.stream.first; - print("responseData: ${utf8.decode(responseData)}"); + if (kDebugMode) { + print("responseData: ${utf8.decode(responseData)}"); + } } else { // Send the command to the proxy server. _secureSocksSocket.writeln(command); // Wait for the response from the proxy server. var responseData = await _secureResponseController.stream.first; - print("secure responseData: ${utf8.decode(responseData)}"); + if (kDebugMode) { + print("secure responseData: ${utf8.decode(responseData)}"); + } } return; } -} \ No newline at end of file +} diff --git a/lib/tor.dart b/lib/tor.dart index 66c7d963..49d91e6e 100644 --- a/lib/tor.dart +++ b/lib/tor.dart @@ -11,7 +11,7 @@ import 'dart:math'; import 'package:ffi/ffi.dart'; import 'package:flutter/foundation.dart'; import 'package:path_provider/path_provider.dart'; -import 'package:tor/tor_bindings_generated.dart'; +import 'package:tor/generated_bindings.dart'; DynamicLibrary load(name) { if (Platform.isAndroid || Platform.isLinux) { @@ -36,10 +36,10 @@ class NotSupportedPlatform implements Exception { } class Tor { - static const String _libName = "tor"; + static const String libName = "tor"; static late DynamicLibrary _lib; - Pointer _clientPtr = nullptr; + Pointer _clientPtr = nullptr; /// Flag to indicate that Tor proxy has started. Traffic is routed through it only if it is also [enabled]. bool get started => _started; @@ -102,7 +102,7 @@ class Tor { /// Private constructor for the Tor class. Tor._internal() { - _lib = load(_libName); + _lib = load(libName); if (kDebugMode) { print("Instance of Tor created!"); @@ -164,7 +164,7 @@ class Tor { // Start the Tor service in an isolate. int ptr = await Isolate.run(() async { // Load the Tor library. - var lib = NativeLibrary(load(_libName)); + var lib = NativeLibrary(load(libName)); // Start the Tor service. final ptr = lib.tor_start( diff --git a/lib/tor_bindings_generated.dart.license b/lib/tor_bindings_generated.dart.license deleted file mode 100644 index 679f6bb2..00000000 --- a/lib/tor_bindings_generated.dart.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2023 Foundation Devices Inc. - -SPDX-License-Identifier: GPL-3.0-or-later diff --git a/lib/util.dart b/lib/util.dart new file mode 100644 index 00000000..61156152 --- /dev/null +++ b/lib/util.dart @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2024 Foundation Devices Inc. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +import 'package:tor/generated_bindings.dart'; +import 'package:tor/tor.dart'; + +int getNofileLimit() { + return NativeLibrary(load(Tor.libName)).tor_get_nofile_limit(); +} + +int setNofileLimit(int limit) { + return NativeLibrary(load(Tor.libName)).tor_set_nofile_limit(limit); +} diff --git a/pubspec.yaml b/pubspec.yaml index 069232ef..bd5e11a2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -20,6 +20,35 @@ dev_dependencies: sdk: flutter flutter_lints: ^2.0.0 +ffigen: + output: 'lib/generated_bindings.dart' + llvm-path: + - '/usr/lib/llvm-14/lib/libclang.so.1' + functions: + include: + - tor_.* + structs: + exclude: + - .* + unions: + exclude: + - .* + enums: + exclude: + - _.* + unnamed-enums: + exclude: + - _.* + macros: + exclude: + - _.* + globals: + exclude: + - _.* + headers: + entry-points: + - 'rust/target/tor.h' + # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9f21dfc2..2184d129 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -173,9 +173,9 @@ checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" [[package]] name = "arti" -version = "1.1.11" +version = "1.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb77374bf6207226a53c54ce8a93bbb0263b21c94197cdfd257abc40e0b26531" +checksum = "e59242d43ed23ebcae0209dd513eeb59c60cd275a577f58df8bd00cdede84085" dependencies = [ "anyhow", "arti-client", @@ -215,9 +215,9 @@ dependencies = [ [[package]] name = "arti-client" -version = "0.12.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cae92510bdf360ae31cb92cbd2b7c86c74a5f9c9c3afbb8eed674184192c607f" +checksum = "c6191481e328147b72d295d7c6f60c86d5775e2f1ff3ef96ede8ef8779c7383a" dependencies = [ "cfg-if", "derive_builder_fork_arti", @@ -856,7 +856,17 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5283ac2881753c76c0892406705553f0d9ab30649f81e18964d3408f4501edb8" dependencies = [ - "derive-adhoc-macros", + "derive-adhoc-macros 0.7.3", + "heck", +] + +[[package]] +name = "derive-adhoc" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acd539187faa4741d0b5cee4bfe866b655cbe3cf497ecbe60db60883108e19fb" +dependencies = [ + "derive-adhoc-macros 0.8.1", "heck", ] @@ -877,6 +887,23 @@ dependencies = [ "void", ] +[[package]] +name = "derive-adhoc-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0f27b6d3c8bc4e69b0e1091cfc38e3bb9e7b6d3dfc3703a1dd5c75d0a281e2a" +dependencies = [ + "heck", + "itertools 0.12.0", + "proc-macro-crate", + "proc-macro2", + "quote", + "sha3", + "strum", + "syn 2.0.29", + "void", +] + [[package]] name = "derive_builder_core_fork_arti" version = "0.11.2" @@ -1220,9 +1247,9 @@ dependencies = [ [[package]] name = "fs-mistrust" -version = "0.7.5" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d56cf38399669a80f0bc5fa9289985301cb947db12ff9471d58a87029dde0bad" +checksum = "fd786a25f4e21d10cd61513afaa3cf393ee0734aa61fa11da41f8307ef130f87" dependencies = [ "derive_builder_fork_arti", "dirs", @@ -1616,6 +1643,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "inventory" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8573b2b1fb643a372c73b23f4da5f888677feef3305146d68a539250a9bccc7" + [[package]] name = "ipnet" version = "2.8.0" @@ -2296,7 +2329,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit", + "toml_edit 0.19.14", ] [[package]] @@ -2314,7 +2347,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6955c41fd7e4283bdf6ff3e7218b7e3f8ef24c4236b31d22be050f4cfd5e2a2c" dependencies = [ - "derive-adhoc", + "derive-adhoc 0.7.3", "libc", "paste", "thiserror", @@ -2438,12 +2471,6 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" -[[package]] -name = "retain_mut" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4389f1d5789befaf6029ebd9f7dac4af7f7e3d61b69d4f30e2ac02b57e7712b0" - [[package]] name = "retry-error" version = "0.5.0" @@ -2705,9 +2732,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.3" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96426c9936fd7a0124915f9185ea1d20aa9445cc9821142f0a73bc9207a2e186" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" dependencies = [ "serde", ] @@ -3167,21 +3194,21 @@ dependencies = [ [[package]] name = "toml" -version = "0.7.6" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" +checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit", + "toml_edit 0.21.0", ] [[package]] name = "toml_datetime" -version = "0.6.3" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" dependencies = [ "serde", ] @@ -3191,6 +3218,17 @@ name = "toml_edit" version = "0.19.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a" +dependencies = [ + "indexmap 2.0.0", + "toml_datetime", + "winnow", +] + +[[package]] +name = "toml_edit" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" dependencies = [ "indexmap 2.0.0", "serde", @@ -3208,6 +3246,7 @@ dependencies = [ "cbindgen", "lazy_static", "log", + "rlimit", "tokio", "tor-config", "tor-rtcompat", @@ -3243,9 +3282,9 @@ dependencies = [ [[package]] name = "tor-bytes" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46270ae4dba2a1a0632bcc2d0441de0f967ec480b52b5ee920c7d92c2de03e6d" +checksum = "2794756f4d016bf64aa5fb29f0e97b87fbc80d659bf526097eaeb18e0f7e7a17" dependencies = [ "bytes", "digest", @@ -3260,9 +3299,9 @@ dependencies = [ [[package]] name = "tor-cell" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95e03cdb2f056684cb2bb55e4f334464817e691a52a31961acdd5dc502cf5a36" +checksum = "31fba8b3c695fa5255e4b625428f65b51e0df5a6d3fbe8dd5b592dc215cdc00a" dependencies = [ "bitflags 2.4.0", "bytes", @@ -3284,14 +3323,13 @@ dependencies = [ [[package]] name = "tor-cert" -version = "0.9.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8b7e01e5dd4b990e1269432239a8d4820a525ddb212ffaeea84855ce4fb674c" +checksum = "cc2a614c848474d002e7ab19d4cb8f85adac7d17f50d5ad2d091f078d52fe1b2" dependencies = [ "caret", "derive_more", "digest", - "signature 1.6.4", "thiserror", "tor-bytes", "tor-checkable", @@ -3300,9 +3338,9 @@ dependencies = [ [[package]] name = "tor-chanmgr" -version = "0.12.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d239e009a2fdf805abf180f6d3ef4adf84059098c0218a5207ce788a3b6cbab0" +checksum = "bcf34109047240681c6f8372d801e1e0290218a02687b242d50e0f8acec86022" dependencies = [ "async-trait", "derive_builder_fork_arti", @@ -3332,9 +3370,9 @@ dependencies = [ [[package]] name = "tor-checkable" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2621b68f18b04fdc09b24c825aceae9f839121c324edd606897055c0b8ba8c86" +checksum = "df7ccb6cdfeafa34fd1052043dc4d083080e4a17d493ce083d25c5d2cd0bf1b3" dependencies = [ "humantime", "signature 1.6.4", @@ -3344,9 +3382,9 @@ dependencies = [ [[package]] name = "tor-circmgr" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ad71c509608a5d020aa27a93f0fdc0e6e244a1d1f550760c863cdbe2342bd2" +checksum = "af11c0a68294323c98e4818697d65e34ac2d581c86e2ab5bd34ff37aac88b2e1" dependencies = [ "amplify", "async-trait", @@ -3379,6 +3417,7 @@ dependencies = [ "tor-netdoc", "tor-persist", "tor-proto", + "tor-protover", "tor-rtcompat", "tracing", "void", @@ -3387,12 +3426,12 @@ dependencies = [ [[package]] name = "tor-config" -version = "0.9.7" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7269acd2a6216641ee7448bffade1f8270236a2cbecf37f7f9e2261fa284b46b" +checksum = "cc84217b53ee8f13986e9d5c634e12ba9bdf996b4da87d974ef43f5ac5c3eeef" dependencies = [ "config", - "derive-adhoc", + "derive-adhoc 0.8.1", "derive_builder_fork_arti", "directories", "educe", @@ -3408,7 +3447,7 @@ dependencies = [ "shellexpand", "strum", "thiserror", - "toml 0.7.6", + "toml 0.8.8", "tor-basic-utils", "tor-error", "tracing", @@ -3417,9 +3456,9 @@ dependencies = [ [[package]] name = "tor-consdiff" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54a6bee9535b09f433cdf0a2315f39448e19feadf76ac6cf752cd7e928be21c" +checksum = "4e3cbb6e44c8c697198f3da16b9f587b8a40367db91f91c6be3faaa2710e3224" dependencies = [ "digest", "hex", @@ -3429,9 +3468,9 @@ dependencies = [ [[package]] name = "tor-dirclient" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00746b93f0f7731522dac4731970389d23dee3daec4a2f519d70417bcd2a5240" +checksum = "dcdfdc725b134a5467b8c7e0e80c6f895ca5b661d69601975ccb54b5c81bcc68" dependencies = [ "async-compression", "base64ct", @@ -3457,9 +3496,9 @@ dependencies = [ [[package]] name = "tor-dirmgr" -version = "0.13.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f58bbd0934d23a0f901050d840571213ce708e7600a2b347dbe9ef86e023029d" +checksum = "05bb131decf0e57996d0fe0154908e3187dcc84efabbbfec7c06666bb53a592b" dependencies = [ "async-trait", "base64ct", @@ -3501,6 +3540,7 @@ dependencies = [ "tor-llcrypto", "tor-netdir", "tor-netdoc", + "tor-persist", "tor-proto", "tor-rtcompat", "tracing", @@ -3508,9 +3548,9 @@ dependencies = [ [[package]] name = "tor-error" -version = "0.5.5" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc4dec82bb91affde760c803e84efceaaca18c26e99fc7e5255038605546e106" +checksum = "a606e92ce352155bb8c4865a018a75a95cc31ca6a5a04da4b25cec8edad40350" dependencies = [ "backtrace", "derive_more", @@ -3526,9 +3566,9 @@ dependencies = [ [[package]] name = "tor-guardmgr" -version = "0.12.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec3b5f8c87dff7045ad7377e5ce9d0334a966afbf6ba7d2ece502fdbe641d1be" +checksum = "616fcf3c81635b7ccbedb1c7bcfb448277bd38f1a54cbe31e6aaaa5758c10bd3" dependencies = [ "base64ct", "derive_builder_fork_arti", @@ -3543,7 +3583,6 @@ dependencies = [ "pin-project", "postage", "rand", - "retain_mut", "safelog", "serde", "strum", @@ -3566,9 +3605,9 @@ dependencies = [ [[package]] name = "tor-hsclient" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae622c438f8253eb82098f5b8057d695defd8acb8dffa3555dca542c97baf4f2" +checksum = "54b76b0413b84b528236210e613174cb2cdc67e0802b4c2fec46b038e848fbfe" dependencies = [ "async-trait", "derive_more", @@ -3606,9 +3645,9 @@ dependencies = [ [[package]] name = "tor-hscrypto" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fdff90517c38a7704bbf10d7bea9fccdfce60d35b5434250e1ca43660b977f1" +checksum = "79cd27b15ca9d2aab1fa953dc678e4141eb811429073b93c0de38c64e4114cbe" dependencies = [ "data-encoding", "derive_more", @@ -3631,19 +3670,22 @@ dependencies = [ [[package]] name = "tor-keymgr" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44b637303ad2702401d36d2c95541bc4780383c0aa35901b86c4538a7441e332" +checksum = "361666ae79d3361d35a16d9de3cd89ac42b6b56f609cc12d336f4461476e667f" dependencies = [ "arrayvec", - "derive-adhoc", + "derive-adhoc 0.8.1", "derive_builder_fork_arti", "derive_more", "downcast-rs", "dyn-clone", "fs-mistrust", "glob-match", + "humantime", + "inventory", "itertools 0.12.0", + "paste", "rand", "serde", "ssh-key", @@ -3658,9 +3700,9 @@ dependencies = [ [[package]] name = "tor-linkspec" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa8a24909c90ac8292bd4cc98686bea66ef93ecc278d2dcfb5052827dee130c8" +checksum = "b59c2d2d44e20a18757ab48ffc500cb3a04ed42173f43ba02b255fa31b998e06" dependencies = [ "base64ct", "by_address", @@ -3685,9 +3727,9 @@ dependencies = [ [[package]] name = "tor-llcrypto" -version = "0.6.0" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b84fe0188173166240065213a65695b008161b23d72bd21a90a537178d1cc6" +checksum = "76e990473a7d00c3dc0b4b9815d8438c8c4a0830da31f0b03579863d8ed8b214" dependencies = [ "aes", "base64ct", @@ -3716,9 +3758,9 @@ dependencies = [ [[package]] name = "tor-netdir" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db4f25e45f47b8f13d1b72ed2f2634f0595538abae123f0c168c8592f36bea9a" +checksum = "0a6ecf882e652c69bb74e635adcfccf0f807669d3eed8129f32de43cc28beebb" dependencies = [ "bitflags 2.4.0", "derive_builder_fork_arti", @@ -3752,9 +3794,9 @@ dependencies = [ [[package]] name = "tor-netdoc" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c8fdb79eb0d45e6fe2490a36352913cc8087edafd521df90cedc085c2c851fe" +checksum = "0dfbe2691ae6de0b3d461e5c2ea9577f2786843fd9d0d90f18fbaac9e5cfc9a1" dependencies = [ "amplify", "base64ct", @@ -3795,13 +3837,14 @@ dependencies = [ [[package]] name = "tor-persist" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d4daf63b4d111aa0d2699e5bcfd9193123d4e68ef326c4cf09b905598685225" +checksum = "722256a6c75775bf1656ee88a4a3765f569eaf0a4c0aa112bf0c9956ef5f1c6a" dependencies = [ "derive_more", "fs-mistrust", "fslock", + "paste", "sanitize-filename", "serde", "serde_json", @@ -3812,9 +3855,9 @@ dependencies = [ [[package]] name = "tor-proto" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a86cacde60835efec682d3ba38284f7318850f196c1f215e401a0d4bd1bca3f" +checksum = "fa328ca7c510779ab86b7903411e34fe356d663d271bfa29b9a6e8325ca2de17" dependencies = [ "asynchronous-codec", "bitvec", @@ -3860,9 +3903,9 @@ dependencies = [ [[package]] name = "tor-protover" -version = "0.5.3" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98ad70864f04edb3a57d7a1c7c8e8e1bfe9d1fbf138699711162ba1a44a03094" +checksum = "203fe1e2a4de305d285f8cf4edfe1d392e89c1e13cf69835dad43a0120a6ea83" dependencies = [ "caret", "thiserror", @@ -3870,9 +3913,9 @@ dependencies = [ [[package]] name = "tor-ptmgr" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31393134cd502de80310275466e53cf66a8f64cd23eaf8e5d4130cefef349102" +checksum = "e246e0d1020fe611a48dcfae93e43972762c031f5c88f69bd4695e635383a744" dependencies = [ "async-trait", "derive_builder_fork_arti", @@ -3892,9 +3935,9 @@ dependencies = [ [[package]] name = "tor-rtcompat" -version = "0.9.6" +version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3631fa5457470b52acbc4581dd0bbbb2a459b024157f24ad30185130bea15db" +checksum = "be71f1a409ed38b64f11d4506407898daae2fab486a9cfc87d0d56fa783b22da" dependencies = [ "async-native-tls", "async-trait", @@ -3910,14 +3953,14 @@ dependencies = [ [[package]] name = "tor-rtmock" -version = "0.11.1" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e54eab8e1436e94bc4777a865205809cf0fe8feddca56e44f754b087908583b" +checksum = "c6b192b2c5815de093eac9aae95d49fbf6574083464da44d530416d1ac6d0378" dependencies = [ "amplify", "async-trait", "backtrace", - "derive-adhoc", + "derive-adhoc 0.8.1", "derive_more", "educe", "futures", @@ -3938,9 +3981,9 @@ dependencies = [ [[package]] name = "tor-socksproto" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c98be3d44a66b2441e7fb55757a8795d73c523cec76cd4417dd762f80cec7ba" +checksum = "d9e8dc59c1cb3ee436cac022f5a3952db0b4b7a98cf7f9d2833abbd0fd5fb846" dependencies = [ "caret", "subtle", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index aaca3c9b..aa914f1a 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -6,7 +6,7 @@ name = "tor" version = "0.1.0" authors = ["Igor Cota "] -edition = "2018" +edition = "2021" [lib] crate-type = ["cdylib", "staticlib"] @@ -14,12 +14,13 @@ crate-type = ["cdylib", "staticlib"] [dependencies] lazy_static = "1.4" tokio = { version = "1", features = ["full"] } -arti-client = { version = "0.12.0", features = ["static"] } -arti = { version = "1.1.11", features = ["experimental-api", "static"] } -tor-rtcompat = { version = "0.9.6", features = ["static"] } -tor-config = "0.9.7" +arti-client = { version = "0.13.0", features = ["static"] } +arti = { version = "1.1.12", features = ["experimental-api", "static"] } +tor-rtcompat = { version = "0.9.7", features = ["static"] } +tor-config = "0.10.0" log = "0.4.20" #android_log-sys = "0.3.1" +rlimit = "0.10.1" [build-dependencies] cbindgen = "= 0.24.3" diff --git a/rust/build.rs b/rust/build.rs index 8b3d6010..7939de07 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2022 Foundation Devices Inc. +// SPDX-FileCopyrightText: 2023 Foundation Devices Inc. // // SPDX-License-Identifier: GPL-3.0-or-later diff --git a/rust/build.rs.license b/rust/build.rs.license deleted file mode 100644 index 679f6bb2..00000000 --- a/rust/build.rs.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2023 Foundation Devices Inc. - -SPDX-License-Identifier: GPL-3.0-or-later diff --git a/rust/src/error.rs b/rust/src/error.rs index 3a66f5b0..7bf5ba0c 100644 --- a/rust/src/error.rs +++ b/rust/src/error.rs @@ -1,15 +1,40 @@ -// SPDX-FileCopyrightText: 2022 Foundation Devices Inc. +// SPDX-FileCopyrightText: 2023 Foundation Devices Inc. // // SPDX-License-Identifier: GPL-3.0-or-later use log::{error, warn}; use std::cell::RefCell; use std::error::Error; +use std::ffi::{c_char, CString}; +//pub(crate) use crate::unwrap_or_return; thread_local! { static LAST_ERROR: RefCell>> = RefCell::new(None); } +#[no_mangle] +pub unsafe extern "C" fn tor_last_error_message() -> *const c_char { + let last_error = match crate::error::take_last_error() { + Some(err) => err, + None => return CString::new("").unwrap().into_raw(), + }; + + let error_message = last_error.to_string(); + CString::new(error_message).unwrap().into_raw() +} + +macro_rules! unwrap_or_return { + ($a:expr,$b:expr) => { + match $a { + Ok(x) => x, + Err(e) => { + update_last_error(e); + return $b; + } + } + }; +} + /// Update the most recent error, clearing whatever may have been there before. pub fn update_last_error(err: E) { error!("Setting LAST_ERROR: {}", err); diff --git a/rust/src/error.rs.license b/rust/src/error.rs.license deleted file mode 100644 index 679f6bb2..00000000 --- a/rust/src/error.rs.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2023 Foundation Devices Inc. - -SPDX-License-Identifier: GPL-3.0-or-later diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 5b97172b..017a0a62 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,54 +1,37 @@ -// SPDX-FileCopyrightText: 2022 Foundation Devices Inc. +// SPDX-FileCopyrightText: 2023 Foundation Devices Inc. // // SPDX-License-Identifier: GPL-3.0-or-later -//use android_log_sys::__android_log_write; +use crate::error::update_last_error; use arti::socks; use arti_client::config::CfgPath; use arti_client::{TorClient, TorClientConfig}; use lazy_static::lazy_static; -use std::ffi::{c_char, CStr, CString}; +use std::ffi::{c_char, c_void, CStr}; use std::{io, ptr}; use tokio::runtime::{Builder, Runtime}; +use tor_config::Listen; use tor_rtcompat::tokio::TokioNativeTlsRuntime; use tor_rtcompat::BlockOn; -use tor_config::Listen; +pub use crate::error::tor_last_error_message; +pub use crate::util::tor_get_nofile_limit; +pub use crate::util::tor_set_nofile_limit; + +#[macro_use] mod error; +mod util; lazy_static! { static ref RUNTIME: io::Result = Builder::new_multi_thread().enable_all().build(); } -#[no_mangle] -pub unsafe extern "C" fn tor_last_error_message() -> *const c_char { - let last_error = match error::take_last_error() { - Some(err) => err, - None => return CString::new("").unwrap().into_raw(), - }; - - let error_message = last_error.to_string(); - CString::new(error_message).unwrap().into_raw() -} - -macro_rules! unwrap_or_return { - ($a:expr,$b:expr) => { - match $a { - Ok(x) => x, - Err(e) => { - error::update_last_error(e); - return $b; - } - } - }; -} - #[no_mangle] pub unsafe extern "C" fn tor_start( socks_port: u16, state_dir: *const c_char, cache_dir: *const c_char, -) -> *mut TorClient { +) -> *mut c_void { let err_ret = ptr::null_mut(); let state_dir = unwrap_or_return!(CStr::from_ptr(state_dir).to_str(), err_ret); @@ -89,14 +72,14 @@ pub unsafe extern "C" fn tor_start( Box::leak(handle_box); let client_box = Box::new(client); - Box::into_raw(client_box) + Box::into_raw(client_box) as *mut c_void } #[no_mangle] -pub unsafe extern "C" fn tor_bootstrap(client: *mut TorClient) -> bool { +pub unsafe extern "C" fn tor_bootstrap(client: *mut c_void) -> bool { let client = { assert!(!client.is_null()); - &mut *client + Box::from_raw(client as *mut TorClient) }; unwrap_or_return!(client.runtime().block_on(client.bootstrap()), false); diff --git a/rust/src/lib.rs.license b/rust/src/lib.rs.license deleted file mode 100644 index 679f6bb2..00000000 --- a/rust/src/lib.rs.license +++ /dev/null @@ -1,3 +0,0 @@ -SPDX-FileCopyrightText: 2023 Foundation Devices Inc. - -SPDX-License-Identifier: GPL-3.0-or-later diff --git a/rust/src/util.rs b/rust/src/util.rs new file mode 100644 index 00000000..c2f73a70 --- /dev/null +++ b/rust/src/util.rs @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2024 Foundation Devices Inc. +// +// SPDX-License-Identifier: GPL-3.0-or-later +use crate::error::update_last_error; + +#[no_mangle] +pub unsafe extern "C" fn tor_get_nofile_limit() -> u64 { + let nofile_limit = unwrap_or_return!(rlimit::getrlimit(rlimit::Resource::NOFILE), 0); + nofile_limit.0 +} + +#[no_mangle] +pub unsafe extern "C" fn tor_set_nofile_limit(limit: u64) -> u64 { + unwrap_or_return!(rlimit::increase_nofile_limit(limit), 0) +} diff --git a/rust/target/tor.h b/rust/target/tor.h new file mode 100644 index 00000000..3804ada7 --- /dev/null +++ b/rust/target/tor.h @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +void *tor_start(uint16_t socks_port, const char *state_dir, const char *cache_dir); + +bool tor_bootstrap(void *client); + +void tor_hello(void); + +const char *tor_last_error_message(void); + +uint64_t tor_get_nofile_limit(void); + +uint64_t tor_set_nofile_limit(uint64_t limit);