|
| 1 | +//! Build the YAML config for `tun2socks` (xjasonlyu/tun2socks). |
| 2 | +//! |
| 3 | +//! tun2socks creates the TUN device named in `device` (the OS routing layer then |
| 4 | +//! addresses it and steers traffic in — unlike hev it does not self-address) and |
| 5 | +//! relays everything to the local SOCKS5 the proxy core exposes. Passing a config |
| 6 | +//! file instead of CLI flags keeps us independent of the binary's flag parser |
| 7 | +//! (v2.7.0 switched to pflag, which rejects the single-dash long flags older |
| 8 | +//! builds accepted) and lets the TUN tuning knobs reach it like they reach hev. |
| 9 | +//! The keys mirror upstream `engine.Key`; only this file knows that shape. |
| 10 | +
|
| 11 | +use serde::Serialize; |
| 12 | + |
| 13 | +use crate::tun::TunOptions; |
| 14 | + |
| 15 | +#[derive(Serialize)] |
| 16 | +#[serde(rename_all = "kebab-case")] |
| 17 | +struct Tun2socksConfig { |
| 18 | + device: String, |
| 19 | + proxy: String, |
| 20 | + mtu: u32, |
| 21 | + loglevel: String, |
| 22 | + /// Wire format is a Go `time.Duration` string ("60000ms"); a bare integer is |
| 23 | + /// rejected by the parser. |
| 24 | + udp_timeout: String, |
| 25 | + /// Per-connection netstack TCP buffer sizes, in bytes (upstream parses the |
| 26 | + /// string with an optional size suffix). |
| 27 | + tcp_send_buffer_size: String, |
| 28 | + tcp_receive_buffer_size: String, |
| 29 | + /// SO_MARK stamped on tun2socks' own upstream sockets so an `ip rule` keeps |
| 30 | + /// them out of the tunnel — load-bearing on Android (mirrors hev's |
| 31 | + /// `socks5.mark`), unused on desktop (the core binds to the uplink instead), |
| 32 | + /// so omitted when `None`. |
| 33 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 34 | + fwmark: Option<u32>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Render the tun2socks YAML for an interface `iface` (which tun2socks creates; |
| 38 | +/// the routing layer addresses it) bridging to `127.0.0.1:<socks_port>`. |
| 39 | +pub fn build_tun2socks_config( |
| 40 | + iface: &str, |
| 41 | + socks_port: u16, |
| 42 | + fwmark: Option<u32>, |
| 43 | + opts: &TunOptions, |
| 44 | +) -> String { |
| 45 | + let cfg = Tun2socksConfig { |
| 46 | + device: format!("tun://{iface}"), |
| 47 | + proxy: format!("socks5://127.0.0.1:{socks_port}"), |
| 48 | + mtu: opts.mtu, |
| 49 | + // The resolved level vocabulary (debug|info|warn|error) is a subset of |
| 50 | + // tun2socks' own (which adds silent), so it passes through unmapped. |
| 51 | + loglevel: opts.log_level.clone(), |
| 52 | + udp_timeout: format!("{}ms", opts.udp_rw_timeout_ms), |
| 53 | + tcp_send_buffer_size: opts.tcp_buffer_size.to_string(), |
| 54 | + tcp_receive_buffer_size: opts.tcp_buffer_size.to_string(), |
| 55 | + fwmark, |
| 56 | + }; |
| 57 | + // Infallible for this plain-scalar shape. |
| 58 | + yaml_serde::to_string(&cfg).expect("tun2socks config serializes") |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + use crate::state::AdvancedSettings; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn renders_expected_keys() { |
| 68 | + let opts = AdvancedSettings::default().tun_options(); |
| 69 | + let yaml = build_tun2socks_config("kt0", 10808, None, &opts); |
| 70 | + assert!(yaml.contains("device: tun://kt0")); |
| 71 | + assert!(yaml.contains("proxy: socks5://127.0.0.1:10808")); |
| 72 | + assert!(yaml.contains("mtu: 9000")); |
| 73 | + assert!(yaml.contains("loglevel: warn")); |
| 74 | + assert!(yaml.contains("udp-timeout: 60000ms")); |
| 75 | + assert!(yaml.contains("tcp-send-buffer-size: '65536'")); |
| 76 | + assert!(yaml.contains("tcp-receive-buffer-size: '65536'")); |
| 77 | + // No fwmark passed → no key at all. |
| 78 | + assert!(!yaml.contains("fwmark")); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn fwmark_emitted_when_set() { |
| 83 | + // Android passes a fwmark so tun2socks' own sockets escape the tunnel; |
| 84 | + // desktop passes None (see `build_tun2socks_config`). |
| 85 | + let opts = AdvancedSettings::default().tun_options(); |
| 86 | + let yaml = build_tun2socks_config("kt0", 10808, Some(255), &opts); |
| 87 | + assert!(yaml.contains("fwmark: 255")); |
| 88 | + } |
| 89 | +} |
0 commit comments