socks_hub/
lib.rs

1cfg_if::cfg_if! {
2    if #[cfg(feature = "acl")] {
3        mod acl;
4        pub use acl::AccessControl;
5    }
6}
7
8//
9// can't use cfg_if here, because it will cause cbindgen to can't generate ffi.h correctly.
10// see this issue: https://github.com/mozilla/cbindgen/issues/935
11//
12// cfg_if::cfg_if! {
13//     if #[cfg(feature = "sockshub")] {
14
15#[cfg(feature = "sockshub")]
16mod config;
17#[cfg(feature = "sockshub")]
18pub use config::{ArgVerbosity, Config, Credentials, ProxyType};
19
20#[cfg(feature = "sockshub")]
21mod tokiort;
22#[cfg(feature = "sockshub")]
23use tokiort::TokioIo;
24
25#[cfg(feature = "sockshub")]
26mod http2socks;
27#[cfg(feature = "sockshub")]
28mod socks2socks;
29
30#[cfg(feature = "sockshub")]
31mod api;
32#[cfg(feature = "sockshub")]
33mod dump_logger;
34#[cfg(feature = "sockshub")]
35mod ffi;
36
37#[cfg(feature = "sockshub")]
38pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
39#[cfg(feature = "sockshub")]
40pub type Result<T, E = BoxError> = std::result::Result<T, E>;
41
42#[cfg(feature = "sockshub")]
43pub async fn main_entry<F>(config: &Config, cancel_token: tokio_util::sync::CancellationToken, callback: Option<F>) -> Result<(), BoxError>
44where
45    F: FnOnce(std::net::SocketAddr) + Send + Sync + 'static,
46{
47    if config.remote_server.proxy_type != ProxyType::Socks5 {
48        return Err("remote server must be socks5".into());
49    }
50    match config.listen_proxy_role.proxy_type {
51        ProxyType::Http => http2socks::main_entry(config, cancel_token, callback).await,
52        ProxyType::Socks5 => socks2socks::main_entry(config, cancel_token, callback).await,
53    }
54}
55
56#[cfg(feature = "sockshub")]
57pub(crate) const CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
58
59#[cfg(feature = "sockshub")]
60pub(crate) async fn create_s5_connect<A: tokio::net::ToSocketAddrs>(
61    server: A,
62    dur: std::time::Duration,
63    dst: &socks5_impl::protocol::Address,
64    auth: Option<socks5_impl::protocol::UserKey>,
65) -> std::io::Result<tokio::io::BufStream<tokio::net::TcpStream>> {
66    let stream = tokio::time::timeout(dur, tokio::net::TcpStream::connect(server)).await??;
67    let mut stream = tokio::io::BufStream::new(stream);
68    socks5_impl::client::connect(&mut stream, dst, auth).await?;
69    Ok(stream)
70}
71
72#[cfg(feature = "sockshub")]
73pub(crate) fn std_io_error_other<E: Into<BoxError>>(err: E) -> std::io::Error {
74    std::io::Error::other(err)
75}
76
77//     }
78// }