Skip to content

Commit

Permalink
not sure all the generics are helping
Browse files Browse the repository at this point in the history
format

use gsoc2023/pt-proxy from arti and get it to compile

building - yay
  • Loading branch information
jmwample committed Mar 7, 2024
1 parent 2e6e8e9 commit ff103ec
Show file tree
Hide file tree
Showing 28 changed files with 750 additions and 460 deletions.
62 changes: 28 additions & 34 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ jobs:
- name: Lint
run: cargo clippy --workspace --all-targets --verbose --all-features

bench:
name: Check that benchmarks compile
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- name: Build u32 bench
env:
RUSTFLAGS: '--cfg curve25519_dalek_bits="32"'
run: cargo build --benches
- name: Build u64 bench
env:
RUSTFLAGS: '--cfg curve25519_dalek_bits="64"'
run: cargo build --benches
- name: Build default (host native) bench
run: cargo build --benches
# bench:
# name: Check that benchmarks compile
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - uses: dtolnay/rust-toolchain@stable
# - name: Build u32 bench
# env:
# RUSTFLAGS: '--cfg curve25519_dalek_bits="32"'
# run: cargo build --benches
# - name: Build u64 bench
# env:
# RUSTFLAGS: '--cfg curve25519_dalek_bits="64"'
# run: cargo build --benches
# - name: Build default (host native) bench
# run: cargo build --benches

test-stable:
name: Test 32/64 bit stable
Expand All @@ -69,7 +69,11 @@ jobs:
include:
# 32-bit target
- target: i686-unknown-linux-gnu
deps: sudo apt update && sudo apt install gcc-multilib
deps: >
sudo dpkg --add-architecture i386;
sudo touch /etc/apt/sources.list.d/i386-cross-compile-sources.list;
echo "deb [arch=i386] http://ports.ubuntu.com/ focal universe\ndeb [arch=i386] http://ports.ubuntu.com/ focal-updates universe\n" | sudo tee -a /etc/apt/sources.list.d/i386-cross-compile-sources.list;
sudo apt update && sudo apt install gcc-multilib libsqlite3-dev:i386
# 64-bit target
- target: x86_64-unknown-linux-gnu
Expand All @@ -78,15 +82,9 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
- run: rustup target add ${{ matrix.target }}
- run: ${{ matrix.deps }}
- run: cargo test --target ${{ matrix.target }} --workspace --all-targets --no-default-features
- run: cargo test --target ${{ matrix.target }} --workspace --all-targets
- run: cargo test --target ${{ matrix.target }} --workspace --all-targets --all-features
# - name: Test
# run: cargo test --verbose --workspace --all-features
# env:
# CARGO_INCREMENTAL: '0'
# RUSTFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
# RUSTDOCFLAGS: '-Zprofile -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort -Zpanic_abort_tests'
# - run: cargo test --target ${{ matrix.target }} --workspace --all-targets
# - run: cargo test --target ${{ matrix.target }} --workspace --all-targets --no-default-features

test-nightly:
name: Test Nightly
Expand Down Expand Up @@ -114,22 +112,18 @@ jobs:
uses: dtolnay/rust-toolchain@nightly

# Build ptrs library
- name: Build library
- name: Build library and bins
run: cargo build

# Build ptrs proxy PoC
- name: Build library
run: cargo build --bin proxy --release

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Generate code coverage
run: cargo llvm-cov --all-features --workspace --codecov --output-path codecov.json
- name: Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.PTRS_CODECOV_TOKEN }}
uses: codecov/[email protected]
with:
fail_ci_if_error: true
files: codecov.json
token: ${{ secrets.CODECOV_TOKEN }} # required
verbose: true
fail_ci_if_error: true
slug: jmwample/o7
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ members = [

resolver = "2"


8 changes: 6 additions & 2 deletions crates/obfs4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ cipher = "0.4.4"
zeroize = "1.7.0"
thiserror = "1.0.56"

# [build-dependencies]
# cc = { version = "1.0.83", features = ["parallel"] }
## lyrebird bin only
fast-socks5 = "0.9.1"
tokio-stream = "0.1.14"
tor-linkspec = { version="0.11.1" }
tor-chanmgr = { version="0.14.1", features = ["pt-client"] }
tor-ptmgr = "0.11.0"

[dev-dependencies]
tracing-subscriber = "0.3.18"
Expand Down
75 changes: 25 additions & 50 deletions crates/obfs4/src/bin/fwd_proxy/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::handler::{EchoHandler, Handler, Socks5Handler};
use obfs::traits::Builder;

use std::{convert::TryFrom, default::Default, marker::PhantomData, net, str::FromStr, sync::Arc};
use std::{net, sync::Arc};

use anyhow::anyhow;
use clap::{Args, CommandFactory, Parser, Subcommand};
Expand All @@ -20,38 +19,38 @@ pub const DEFAULT_SERVER_ADDRESS: &str = "127.0.0.1:9001";
pub const DEFAULT_REMOTE_ADDRESS: &str = "127.0.0.1:9010";
pub const DEFAULT_LOG_LEVEL: Level = Level::INFO;

pub enum ProxyConfig<B> {
Entrance(EntranceConfig<B>),
Socks5Exit(ExitConfig<B, Socks5Handler>),
EchoExit(ExitConfig<B, EchoHandler>),
pub enum ProxyConfig {
Entrance(EntranceConfig),
Socks5Exit(ExitConfig),
EchoExit(ExitConfig),
}

impl<B: Builder + Default> ProxyConfig<B> {
impl ProxyConfig {
pub async fn run(
self,
close: CancellationToken,
wait: Sender<()>,
) -> Result<(), anyhow::Error> {
match self {
ProxyConfig::Entrance(config) => config.run(close, wait).await,
ProxyConfig::Socks5Exit(config) => Arc::new(config).run(close, wait).await,
ProxyConfig::EchoExit(config) => Arc::new(config).run(close, wait).await,
ProxyConfig::Socks5Exit(config) => {
Arc::new(config).run::<Socks5Handler>(close, wait).await
}
ProxyConfig::EchoExit(config) => Arc::new(config).run::<EchoHandler>(close, wait).await,
}
}
}

pub struct EntranceConfig<B> {
pt: String,
pub struct EntranceConfig {
pt_args: Vec<String>,

listen_address: net::SocketAddr,
remote_address: net::SocketAddr,

level: Level,
builder: B,
}

impl<B: Builder + Default> EntranceConfig<B> {
impl EntranceConfig {
pub async fn run(
self,
close: CancellationToken,
Expand Down Expand Up @@ -93,38 +92,29 @@ impl<B: Builder + Default> EntranceConfig<B> {
}
}

impl<B: Builder + Default> Default for EntranceConfig<B> {
impl Default for EntranceConfig {
fn default() -> Self {
Self {
pt: String::from("plain"),
pt_args: vec![],

listen_address: DEFAULT_LISTEN_ADDRESS.parse().unwrap(),
remote_address: DEFAULT_REMOTE_ADDRESS.parse().unwrap(),
level: DEFAULT_LOG_LEVEL,
builder: B::default(),
}
}
}

pub struct ExitConfig<B, H> {
pt: String,
#[allow(unused)] // TODO: parse args and things
pub struct ExitConfig {
pt_args: Vec<String>,
level: Level,

// handler: Handler,
listen_address: net::SocketAddr,

level: Level,
builder: B,
phantom_backend: PhantomData<H>,
}

impl<B: Builder + Default, H: Handler> ExitConfig<B, H> {
pub fn new() -> Self {
Self::default()
}

pub async fn run(
impl ExitConfig {
pub async fn run<H: Handler + Send + Sync + 'static>(
self: Arc<Self>,
close: CancellationToken,
_wait: Sender<()>,
Expand All @@ -151,11 +141,8 @@ impl<B: Builder + Default, H: Handler> ExitConfig<B, H> {
break
}
r = sessions.join_next() => {
match r {
Some(Err(e)) => {
warn!("handler error: \"{e}\", session closed");
}
_ => {}
if let Some(Err(e)) = r {
warn!("handler error: \"{e}\", session closed");
}
}
r = listener.accept() => {
Expand All @@ -181,7 +168,7 @@ impl<B: Builder + Default, H: Handler> ExitConfig<B, H> {
};

debug!("connection successfully opened ->{t_name}-[{socket_addr}]");
sessions.spawn(Self::H::handle(proxy_stream, close_c));
sessions.spawn(H::handle(proxy_stream, close_c));
}
}
}
Expand All @@ -195,25 +182,21 @@ impl<B: Builder + Default, H: Handler> ExitConfig<B, H> {
}
}

impl<B: Default, H> Default for ExitConfig<B, H> {
impl Default for ExitConfig {
fn default() -> Self {
Self {
pt: String::from("plain"),
pt_args: vec![],
listen_address: DEFAULT_SERVER_ADDRESS.parse().unwrap(),
level: DEFAULT_LOG_LEVEL,
// handler: Handlers::Echo,
builder: B::default(),
phantom_backend: PhantomData,
}
}
}

impl<B: Builder + Default> TryFrom<Cli> for ProxyConfig<B> {
impl TryFrom<&Cli> for ProxyConfig {
type Error = anyhow::Error;

fn try_from(cli: Cli) -> Result<Self, Self::Error> {
match cli.command {
fn try_from(cli: &Cli) -> Result<Self, Self::Error> {
match &cli.command {
Some(Commands::Server(args)) => {
match &*args.backend {
"socks5" => {} // ExitConfig::<B, Socks5Handler>::new(),
Expand All @@ -233,31 +216,24 @@ impl<B: Builder + Default> TryFrom<Cli> for ProxyConfig<B> {
trace!("{:?}", args);

// TODO: parse pt name and arguments.
let pt = "".to_string();
let pt_args = vec![];

let listen_address = args.listen_addr.parse()?;

match &*args.backend {
"socks5" => {
let config = ExitConfig {
pt,
pt_args,
listen_address,
level,
builder: B::default(),
phantom_backend: PhantomData,
};
Ok(ProxyConfig::Socks5Exit(config))
}
"echo" => {
let config = ExitConfig {
pt,
pt_args,
listen_address,
level,
builder: B::default(),
phantom_backend: PhantomData,
};
Ok(ProxyConfig::EchoExit(config))
}
Expand All @@ -280,7 +256,6 @@ impl<B: Builder + Default> TryFrom<Cli> for ProxyConfig<B> {
config.listen_address = args.listen_addr.parse()?;

// TODO: parse pt name and arguments.
config.pt = "".to_string();
config.pt_args = vec![];

Ok(ProxyConfig::Entrance(config))
Expand Down
14 changes: 6 additions & 8 deletions crates/obfs4/src/bin/fwd_proxy/handler.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#![allow(dead_code)]
use crate::socks5;
use futures::Future;
use obfs::{Error, Result};
use obfs::Result;

use std::str::FromStr;

use tokio::{
self,
io::{copy, split, AsyncRead, AsyncWrite},
};
use tokio::io::{copy, split, AsyncRead, AsyncWrite};
use tokio_util::sync::CancellationToken;
use tracing::trace;

pub trait Handler {
fn handle<RW>(stream: RW, close_c: CancellationToken) -> impl Future<Output = Result<()>>
fn handle<RW>(
stream: RW,
close_c: CancellationToken,
) -> impl Future<Output = Result<()>> + Send + Sync
where
RW: AsyncRead + AsyncWrite + Unpin + Send + Sync;
}
Expand Down
10 changes: 3 additions & 7 deletions crates/obfs4/src/bin/fwd_proxy/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#![feature(noop_waker)]
#![feature(trait_alias)]
#![feature(slice_flatten)]
#![feature(stdarch_x86_avx512)]

mod config;
mod handler;
mod socks5;

use config::{Cli, ProxyConfig};

use clap::Parser;
use tokio::{self, signal, sync::mpsc::channel};
use tokio::{signal, sync::mpsc::channel};
use tokio_util::sync::CancellationToken;

use tracing::{debug, error};
Expand All @@ -23,7 +18,8 @@ async fn main() -> std::result::Result<(), anyhow::Error> {
let shutdown_signal = CancellationToken::new();

let config = Cli::parse();
let proxy_runner: ProxyConfig<_> = config.try_into()?;
let proxy_runner = ProxyConfig::try_from(&config)?;
// let builder = Box::new(&config.pt) as Box<dyn Builder>;

tokio::select! {
// launch proxy runner based on the parsed config. If config parsing fails we fail and
Expand Down
Loading

0 comments on commit ff103ec

Please sign in to comment.