Commit c4c37227 authored by trinity-1686a's avatar trinity-1686a 💜
Browse files

update to recent Arti and remove unnecessary fslock patch

parent 41a3442e
......@@ -6,28 +6,22 @@ edition = "2018"
[dependencies]
# use fixed commits because Arti does not have a stable Rust API yet
arti-tor-client = { git = "https://gitlab.torproject.org/tpo/core/arti/", rev="4d0cd5e86ff9f0b216d9b00c3b293e23bb13e871" }
tor-circmgr = { git = "https://gitlab.torproject.org/tpo/core/arti/", rev="4d0cd5e86ff9f0b216d9b00c3b293e23bb13e871" }
tor-dirmgr = { git = "https://gitlab.torproject.org/tpo/core/arti/", rev="4d0cd5e86ff9f0b216d9b00c3b293e23bb13e871", features=["static"]}
tor-rtcompat = { git = "https://gitlab.torproject.org/tpo/core/arti/", rev="4d0cd5e86ff9f0b216d9b00c3b293e23bb13e871", features=["static"]}
arti-client = { version = "=0.0.3", features=["static"]}
tor-rtcompat = "=0.0.3"
# the java native interface
jni = { version = "0.19", default-features = false }
# dependancies to make logs available with logcat
tracing-subscriber = "0.2.20"
tracing-android = "0.1.3"
tracing-subscriber = "0.3.3"
tracing-android = "0.1.6"
# dependancy to help implementing lockf, for android < 7.0
libc = "0.2.101"
libc = "0.2.112"
# helpers
anyhow = "1.0.43"
futures = "0.3.17"
[patch.crates-io]
# used to compile on 32bit Android
fslock = { git="https://gitlab.torproject.org/trinity-1686a/fslock", rev="9150650d9619f933bea4f34259002ab6628d2570" }
anyhow = "1.0.52"
futures = "0.3.19"
[lib]
crate-type = ["dylib"]
use std::sync::Once;
use anyhow::Result;
use futures::{AsyncReadExt, AsyncWriteExt};
use std::path::Path;
use arti_tor_client::TorClient;
use tor_circmgr::CircMgrConfigBuilder;
use tor_dirmgr::DirMgrConfig;
use arti_client::{config::TorClientConfigBuilder, TorClient};
use tor_rtcompat::SpawnBlocking;
use tracing_subscriber::fmt::Subscriber;
use tracing_subscriber::prelude::*;
fn run_arti(to: &str, cache: &str) -> Result<String> {
// note: setting up tracing should be done only once. App will crash if this is called twice.
Subscriber::new()
.with(tracing_android::layer("rust.ndk.arti")?)
.init();
let runtime = tor_rtcompat::create_runtime()?;
let dircfg = DirMgrConfig::builder()
.cache_path(Path::new(&format!("{}/arti-cache", cache)))
.build()?;
let circcfg = CircMgrConfigBuilder::default().build()?;
let rt_copy = runtime.clone();
let config = TorClientConfigBuilder::from_directories(
format!("{}/arti-data", cache),
format!("{}/arti-cache", cache),
)
.build()?;
rt_copy.block_on(async {
let client = TorClient::bootstrap(
runtime,
format!("{}/arti-data", cache).into(),
dircfg,
circcfg,
)
.await?;
let mut stream = client.connect(to, 80, None).await?;
let client = TorClient::bootstrap(runtime, config).await?;
let mut stream = client.connect((to, 80)).await?;
stream.write_all(b"GET / HTTP/1.1\r\nHost: ").await?;
stream.write_all(to.as_bytes()).await?;
stream.write_all(b"\r\nConnection: close\r\n\r\n").await?;
......@@ -46,13 +35,14 @@ fn run_arti(to: &str, cache: &str) -> Result<String> {
}
/// Expose the JNI interface for android below
#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {
use super::*;
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;
use super::*;
/// Create a static method myMethod on class net.example.MyClass
#[no_mangle]
......@@ -62,6 +52,10 @@ pub mod android {
target: JString,
cache_dir: JString,
) -> jstring {
// if logger initialization failed, there isn't much we can do, not even log it.
// it shouldn't stop Arti from functionning however!
let _ = init_logger();
let result = match run_arti(
&env.get_string(target)
.expect("target is invalid")
......@@ -80,53 +74,59 @@ pub mod android {
output.into_inner()
}
}
/// Android 5.0 to 6.0 misses this function, which prevent Arti from running. This is a translation
/// to Rust of Musl implementation. If you don't plan to support anything below Android 7.0, you
/// should probably not copy this code.
/// It might be possible to support Android 4.4 and below with the same trick applied to more
/// functions (at least create_epoll1), but this might not be worth the effort.
#[no_mangle]
pub unsafe extern "C" fn lockf(
fd: libc::c_int,
cmd: libc::c_int,
len: libc::off_t
) -> libc::c_int {
use libc::*;
let mut l = flock {
l_type: F_WRLCK as i16,
l_whence: SEEK_CUR as i16,
l_len: len,
l_pid: 0,
l_start: 0,
};
match cmd {
F_TEST => {
l.l_type = F_RDLCK as i16;
if fcntl(fd, F_GETLK, &l) < 0 {
return -1;
static LOGGER: Once = Once::new();
fn init_logger() -> Result<()> {
if LOGGER.is_completed() {
let layer = tracing_android::layer("rust.ndk.arti")?;
LOGGER.call_once(|| Subscriber::new().with(layer).init());
}
Ok(())
}
/// Android 5.0 to 6.0 misses this function, which prevent Arti from running. This is a translation
/// to Rust of Musl implementation. If you don't plan to support anything below Android 7.0, you
/// should probably not copy this code.
/// It might be possible to support Android 4.4 and below with the same trick applied to more
/// functions (at least create_epoll1), but this might not be worth the effort.
#[cfg(target_os = "android")]
#[no_mangle]
pub unsafe extern "C" fn lockf(
fd: libc::c_int,
cmd: libc::c_int,
len: libc::off_t,
) -> libc::c_int {
use libc::*;
let mut l = flock {
l_type: F_WRLCK as i16,
l_whence: SEEK_CUR as i16,
l_len: len,
l_pid: 0,
l_start: 0,
};
match cmd {
F_TEST => {
l.l_type = F_RDLCK as i16;
if fcntl(fd, F_GETLK, &l) < 0 {
return -1;
}
if l.l_type == F_UNLCK as i16 || l.l_pid == getpid() {
return 0;
}
*__errno() = EACCES;
-1
}
if l.l_type == F_UNLCK as i16 || l.l_pid == getpid() {
return 0;
F_ULOCK => {
l.l_type = F_UNLCK as i16;
fcntl(fd, F_SETLK, &l)
}
F_TLOCK => fcntl(fd, F_SETLK, &l),
F_LOCK => fcntl(fd, F_SETLKW, &l),
_ => {
*__errno() = EINVAL;
-1
}
*__errno() = EACCES;
-1
}
F_ULOCK => {
l.l_type = F_UNLCK as i16;
fcntl(fd, F_SETLK, &l)
}
F_TLOCK => {
fcntl(fd, F_SETLK, &l)
}
F_LOCK => {
fcntl(fd, F_SETLKW, &l)
}
_ => {
*__errno() = EINVAL;
-1
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment