udpgw_server options

This commit is contained in:
ssrlive 2024-10-20 19:29:36 +08:00
parent d4e2fca83a
commit b2482ab411
5 changed files with 21 additions and 21 deletions

View file

@ -66,7 +66,7 @@ name = "tun2proxy-bin"
path = "src/bin/main.rs" path = "src/bin/main.rs"
[[bin]] [[bin]]
name = "udpgwserver" name = "udpgw-server"
path = "src/bin/udpgw_server.rs" path = "src/bin/udpgw_server.rs"
[profile.release] [profile.release]

View file

@ -70,14 +70,6 @@ pub struct Args {
#[arg(short, long, default_value = if cfg!(target_os = "linux") { "false" } else { "true" })] #[arg(short, long, default_value = if cfg!(target_os = "linux") { "false" } else { "true" })]
pub setup: bool, pub setup: bool,
/// UDP gateway address
#[arg(long, value_name = "IP:PORT")]
pub udpgw_bind_addr: Option<SocketAddr>,
/// Max udpgw connections
#[arg(long, value_name = "number", default_value = "100")]
pub max_udpgw_connections: u16,
/// DNS handling strategy /// DNS handling strategy
#[arg(short, long, value_name = "strategy", value_enum, default_value = "direct")] #[arg(short, long, value_name = "strategy", value_enum, default_value = "direct")]
pub dns: ArgDns, pub dns: ArgDns,
@ -119,6 +111,14 @@ pub struct Args {
/// Maximum number of sessions to be handled concurrently /// Maximum number of sessions to be handled concurrently
#[arg(long, value_name = "number", default_value = "200")] #[arg(long, value_name = "number", default_value = "200")]
pub max_sessions: usize, pub max_sessions: usize,
/// UDP gateway server address, similar to badvpn-udpgw
#[arg(long, value_name = "IP:PORT")]
pub udpgw_server: Option<SocketAddr>,
/// Max udpgw connections
#[arg(long, value_name = "number")]
pub udpgw_max_connections: Option<u16>,
} }
fn validate_tun(p: &str) -> Result<String> { fn validate_tun(p: &str) -> Result<String> {
@ -152,8 +152,8 @@ impl Default for Args {
admin_command: Vec::new(), admin_command: Vec::new(),
ipv6_enabled: false, ipv6_enabled: false,
setup, setup,
udpgw_bind_addr: None, udpgw_server: None,
max_udpgw_connections: 100, udpgw_max_connections: None,
dns: ArgDns::default(), dns: ArgDns::default(),
dns_addr: "8.8.8.8".parse().unwrap(), dns_addr: "8.8.8.8".parse().unwrap(),
bypass: vec![], bypass: vec![],
@ -192,7 +192,7 @@ impl Args {
} }
pub fn udpgw(&mut self, udpgw: SocketAddr) -> &mut Self { pub fn udpgw(&mut self, udpgw: SocketAddr) -> &mut Self {
self.udpgw_bind_addr = Some(udpgw); self.udpgw_server = Some(udpgw);
self self
} }

View file

@ -10,7 +10,7 @@ use tokio::net::TcpListener;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
pub use tun2proxy::udpgw::*; use tun2proxy::udpgw::*;
use tun2proxy::ArgVerbosity; use tun2proxy::ArgVerbosity;
use tun2proxy::Result; use tun2proxy::Result;
pub(crate) const CLIENT_DISCONNECT_TIMEOUT: tokio::time::Duration = std::time::Duration::from_secs(60); pub(crate) const CLIENT_DISCONNECT_TIMEOUT: tokio::time::Duration = std::time::Duration::from_secs(60);

View file

@ -234,13 +234,13 @@ where
let mut ip_stack = ipstack::IpStack::new(ipstack_config, device); let mut ip_stack = ipstack::IpStack::new(ipstack_config, device);
let udpgw_client = match args.udpgw_bind_addr { let udpgw_client = match args.udpgw_server {
None => None, None => None,
Some(addr) => { Some(addr) => {
log::info!("UDPGW enabled"); log::info!("UDPGW enabled");
let client = Arc::new(UdpGwClient::new( let client = Arc::new(UdpGwClient::new(
mtu, mtu,
args.max_udpgw_connections, args.udpgw_max_connections.unwrap_or(100),
UDPGW_KEEPALIVE_TIME, UDPGW_KEEPALIVE_TIME,
args.udp_timeout, args.udp_timeout,
addr, addr,
@ -346,7 +346,7 @@ where
SocketAddr::V4(_) => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)), SocketAddr::V4(_) => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)),
SocketAddr::V6(_) => SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 0, 0, 0)), SocketAddr::V6(_) => SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), 0, 0, 0)),
}; };
let tcpinfo = SessionInfo::new(tcp_src, udpgw.get_udpgw_bind_addr(), IpProtocol::Tcp); let tcpinfo = SessionInfo::new(tcp_src, udpgw.get_server_addr(), IpProtocol::Tcp);
let proxy_handler = mgr.new_proxy_handler(tcpinfo, None, false).await?; let proxy_handler = mgr.new_proxy_handler(tcpinfo, None, false).await?;
let socket_queue = socket_queue.clone(); let socket_queue = socket_queue.clone();
tokio::spawn(async move { tokio::spawn(async move {

View file

@ -212,13 +212,13 @@ pub(crate) struct UdpGwClient {
max_connections: u16, max_connections: u16,
udp_timeout: u64, udp_timeout: u64,
keepalive_time: Duration, keepalive_time: Duration,
udpgw_bind_addr: SocketAddr, server_addr: SocketAddr,
keepalive_packet: Vec<u8>, keepalive_packet: Vec<u8>,
server_connections: Mutex<VecDeque<UdpGwClientStream>>, server_connections: Mutex<VecDeque<UdpGwClientStream>>,
} }
impl UdpGwClient { impl UdpGwClient {
pub fn new(udp_mtu: u16, max_connections: u16, keepalive_time: Duration, udp_timeout: u64, udpgw_bind_addr: SocketAddr) -> Self { pub fn new(udp_mtu: u16, max_connections: u16, keepalive_time: Duration, udp_timeout: u64, server_addr: SocketAddr) -> Self {
let mut keepalive_packet = vec![]; let mut keepalive_packet = vec![];
keepalive_packet.extend_from_slice(&(std::mem::size_of::<UdpgwHeader>() as u16).to_le_bytes()); keepalive_packet.extend_from_slice(&(std::mem::size_of::<UdpgwHeader>() as u16).to_le_bytes());
keepalive_packet.extend_from_slice(&[UDPGW_FLAG_KEEPALIVE, 0, 0]); keepalive_packet.extend_from_slice(&[UDPGW_FLAG_KEEPALIVE, 0, 0]);
@ -227,7 +227,7 @@ impl UdpGwClient {
udp_mtu, udp_mtu,
max_connections, max_connections,
udp_timeout, udp_timeout,
udpgw_bind_addr, server_addr,
keepalive_time, keepalive_time,
keepalive_packet, keepalive_packet,
server_connections, server_connections,
@ -269,8 +269,8 @@ impl UdpGwClient {
} }
} }
pub(crate) fn get_udpgw_bind_addr(&self) -> SocketAddr { pub(crate) fn get_server_addr(&self) -> SocketAddr {
self.udpgw_bind_addr self.server_addr
} }
/// Heartbeat task asynchronous function to periodically check and maintain the active state of the server connection. /// Heartbeat task asynchronous function to periodically check and maintain the active state of the server connection.