overtls/
lib.rs

1pub(crate) mod android;
2pub(crate) mod api;
3pub(crate) mod client;
4pub(crate) mod cmdopt;
5pub(crate) mod config;
6pub(crate) mod dns;
7pub(crate) mod dump_logger;
8pub(crate) mod error;
9pub(crate) mod server;
10pub(crate) mod tcp_stream;
11pub(crate) mod tls;
12pub(crate) mod traffic_audit;
13pub(crate) mod traffic_status;
14pub(crate) mod udprelay;
15pub(crate) mod webapi;
16pub(crate) mod weirduri;
17pub mod win_svc;
18
19pub use api::{over_tls_client_run, over_tls_client_run_with_ssr_url, over_tls_client_stop, overtls_free_string, overtls_generate_url};
20use bytes::BytesMut;
21pub use client::run_client;
22pub use cmdopt::{ArgVerbosity, CmdOpt, Role};
23pub use config::{Client as ClientConfig, Config, ManageClients, Server as ServerConfig, TunnelPath};
24pub use dump_logger::overtls_set_log_callback;
25pub use error::{BoxError, Error, Result};
26pub use server::run_server;
27use socks5_impl::protocol::{Address, StreamOperation};
28pub use tokio_util::sync::CancellationToken;
29pub use traffic_status::{TrafficStatus, overtls_set_traffic_status_callback};
30
31#[cfg(target_os = "windows")]
32pub(crate) const STREAM_BUFFER_SIZE: usize = 1024 * 32;
33#[cfg(not(target_os = "windows"))]
34pub(crate) const STREAM_BUFFER_SIZE: usize = 1024 * 32 * 3;
35
36pub(crate) fn addess_to_b64str(addr: &Address, url_safe: bool) -> String {
37    let mut buf = BytesMut::with_capacity(1024);
38    addr.write_to_buf(&mut buf);
39    if url_safe {
40        base64easy::encode(&buf, base64easy::EngineKind::UrlSafeNoPad)
41    } else {
42        base64easy::encode(&buf, base64easy::EngineKind::StandardNoPad)
43    }
44}
45
46pub(crate) fn b64str_to_address(s: &str, url_safe: bool) -> Result<Address> {
47    let buf = if url_safe {
48        let result = base64easy::decode(s, base64easy::EngineKind::UrlSafeNoPad);
49        if result.is_err() {
50            base64easy::decode(s, base64easy::EngineKind::UrlSafe)?
51        } else {
52            result?
53        }
54    } else {
55        let result = base64easy::decode(s, base64easy::EngineKind::StandardNoPad);
56        if result.is_err() {
57            // backward compatibility for SSRoT
58            base64easy::decode(s, base64easy::EngineKind::Standard)?
59        } else {
60            result?
61        }
62    };
63    Address::try_from(&buf[..]).map_err(|e| e.into())
64}
65
66#[doc(hidden)]
67pub async fn async_main(config: Config, allow_shutdown: bool, shutdown_token: CancellationToken) -> Result<()> {
68    let ctrlc_fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
69    let mut ctrlc_handle = None;
70    if allow_shutdown {
71        let shutdown_token_clone = shutdown_token.clone();
72        let ctrlc_fired_clone = ctrlc_fired.clone();
73        let handle = ctrlc2::AsyncCtrlC::new(move || {
74            log::info!("Ctrl-C received, exiting...");
75            ctrlc_fired_clone.store(true, std::sync::atomic::Ordering::SeqCst);
76            shutdown_token_clone.cancel();
77            true
78        })?;
79        ctrlc_handle = Some(handle);
80    }
81
82    let main_body = async {
83        if config.is_server {
84            if config.exist_server() {
85                run_server(&config, shutdown_token).await?;
86            } else {
87                return Err(Error::from("Config is not a server config"));
88            }
89        } else if config.exist_client() {
90            let callback = |addr| {
91                log::trace!("Listening on {addr}");
92            };
93            run_client(&config, shutdown_token, Some(callback)).await?;
94        } else {
95            return Err("Config is not a client config".into());
96        }
97
98        if ctrlc_fired.load(std::sync::atomic::Ordering::SeqCst) {
99            let Some(handle) = ctrlc_handle else {
100                return Ok(());
101            };
102            log::info!("Waiting for Ctrl-C handler to finish...");
103            handle.await.map_err(|e| e.to_string())?;
104        }
105        Ok(())
106    };
107
108    if let Err(e) = main_body.await {
109        log::error!("main_body error: \"{e}\"");
110    }
111
112    Ok(())
113}