udpgw feature

This commit is contained in:
ssrlive 2024-10-21 14:59:26 +08:00
parent 7c4b1d7504
commit 442e752cba
3 changed files with 29 additions and 6 deletions

View file

@ -13,6 +13,10 @@ rust-version = "1.80"
[lib] [lib]
crate-type = ["staticlib", "cdylib", "lib"] crate-type = ["staticlib", "cdylib", "lib"]
[features]
default = ["udpgw"]
udpgw = []
[dependencies] [dependencies]
async-trait = "0.1" async-trait = "0.1"
base64 = { version = "0.22" } base64 = { version = "0.22" }
@ -68,6 +72,7 @@ path = "src/bin/main.rs"
[[bin]] [[bin]]
name = "udpgw-server" name = "udpgw-server"
path = "src/bin/udpgw_server.rs" path = "src/bin/udpgw_server.rs"
required-features = ["udpgw"]
[profile.release] [profile.release]
strip = "symbols" strip = "symbols"

View file

@ -113,10 +113,12 @@ pub struct Args {
pub max_sessions: usize, pub max_sessions: usize,
/// UDP gateway server address, similar to badvpn-udpgw /// UDP gateway server address, similar to badvpn-udpgw
#[cfg(feature = "udpgw")]
#[arg(long, value_name = "IP:PORT")] #[arg(long, value_name = "IP:PORT")]
pub udpgw_server: Option<SocketAddr>, pub udpgw_server: Option<SocketAddr>,
/// Max udpgw connections /// Max udpgw connections
#[cfg(feature = "udpgw")]
#[arg(long, value_name = "number")] #[arg(long, value_name = "number")]
pub udpgw_max_connections: Option<u16>, pub udpgw_max_connections: Option<u16>,
} }
@ -152,8 +154,6 @@ impl Default for Args {
admin_command: Vec::new(), admin_command: Vec::new(),
ipv6_enabled: false, ipv6_enabled: false,
setup, setup,
udpgw_server: None,
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![],
@ -164,6 +164,10 @@ impl Default for Args {
daemonize: false, daemonize: false,
exit_on_fatal_error: false, exit_on_fatal_error: false,
max_sessions: 200, max_sessions: 200,
#[cfg(feature = "udpgw")]
udpgw_server: None,
#[cfg(feature = "udpgw")]
udpgw_max_connections: None,
} }
} }
} }
@ -191,11 +195,18 @@ impl Args {
self self
} }
pub fn udpgw(&mut self, udpgw: SocketAddr) -> &mut Self { #[cfg(feature = "udpgw")]
pub fn udpgw_server(&mut self, udpgw: SocketAddr) -> &mut Self {
self.udpgw_server = Some(udpgw); self.udpgw_server = Some(udpgw);
self self
} }
#[cfg(feature = "udpgw")]
pub fn udpgw_max_connections(&mut self, udpgw_max_connections: u16) -> &mut Self {
self.udpgw_max_connections = Some(udpgw_max_connections);
self
}
#[cfg(unix)] #[cfg(unix)]
pub fn tun_fd(&mut self, tun_fd: Option<i32>) -> &mut Self { pub fn tun_fd(&mut self, tun_fd: Option<i32>) -> &mut Self {
self.tun_fd = tun_fd; self.tun_fd = tun_fd;

View file

@ -1,19 +1,22 @@
#[cfg(feature = "udpgw")]
use crate::udpgw::UdpGwClient;
use crate::{ use crate::{
directions::{IncomingDataEvent, IncomingDirection, OutgoingDirection}, directions::{IncomingDataEvent, IncomingDirection, OutgoingDirection},
http::HttpManager, http::HttpManager,
no_proxy::NoProxyManager, no_proxy::NoProxyManager,
session_info::{IpProtocol, SessionInfo}, session_info::{IpProtocol, SessionInfo},
udpgw::UdpGwClient,
virtual_dns::VirtualDns, virtual_dns::VirtualDns,
}; };
use ipstack::stream::{IpStackStream, IpStackTcpStream, IpStackUdpStream}; use ipstack::stream::{IpStackStream, IpStackTcpStream, IpStackUdpStream};
use proxy_handler::{ProxyHandler, ProxyHandlerManager}; use proxy_handler::{ProxyHandler, ProxyHandlerManager};
use socks::SocksProxyManager; use socks::SocksProxyManager;
pub use socks5_impl::protocol::UserKey; pub use socks5_impl::protocol::UserKey;
#[cfg(feature = "udpgw")]
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::{ use std::{
collections::VecDeque, collections::VecDeque,
io::ErrorKind, io::ErrorKind,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}, net::{IpAddr, SocketAddr},
sync::Arc, sync::Arc,
}; };
use tokio::{ use tokio::{
@ -24,6 +27,7 @@ use tokio::{
pub use tokio_util::sync::CancellationToken; pub use tokio_util::sync::CancellationToken;
use tproxy_config::is_private_ip; use tproxy_config::is_private_ip;
use udp_stream::UdpStream; use udp_stream::UdpStream;
#[cfg(feature = "udpgw")]
use udpgw::{UdpGwClientStream, UdpGwResponse, UDPGW_KEEPALIVE_TIME}; use udpgw::{UdpGwClientStream, UdpGwResponse, UDPGW_KEEPALIVE_TIME};
pub use { pub use {
@ -61,6 +65,7 @@ mod session_info;
pub mod socket_transfer; pub mod socket_transfer;
mod socks; mod socks;
mod traffic_status; mod traffic_status;
#[cfg(feature = "udpgw")]
pub mod udpgw; pub mod udpgw;
mod virtual_dns; mod virtual_dns;
#[doc(hidden)] #[doc(hidden)]
@ -234,6 +239,7 @@ where
let mut ip_stack = ipstack::IpStack::new(ipstack_config, device); let mut ip_stack = ipstack::IpStack::new(ipstack_config, device);
#[cfg(feature = "udpgw")]
let udpgw_client = match args.udpgw_server { let udpgw_client = match args.udpgw_server {
None => None, None => None,
Some(addr) => { Some(addr) => {
@ -290,7 +296,6 @@ where
if let Err(err) = handle_tcp_session(tcp, proxy_handler, socket_queue).await { if let Err(err) = handle_tcp_session(tcp, proxy_handler, socket_queue).await {
log::error!("{} error \"{}\"", info, err); log::error!("{} error \"{}\"", info, err);
} }
log::trace!("Session count {}", TASK_COUNT.fetch_sub(1, Relaxed) - 1); log::trace!("Session count {}", TASK_COUNT.fetch_sub(1, Relaxed) - 1);
}); });
} }
@ -341,6 +346,7 @@ where
} else { } else {
None None
}; };
#[cfg(feature = "udpgw")]
if let Some(udpgw) = udpgw_client.clone() { if let Some(udpgw) = udpgw_client.clone() {
let tcp_src = match udp.peer_addr() { let tcp_src = match udp.peer_addr() {
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)),
@ -477,6 +483,7 @@ async fn handle_tcp_session(
Ok(()) Ok(())
} }
#[cfg(feature = "udpgw")]
async fn handle_udp_gateway_session( async fn handle_udp_gateway_session(
mut udp_stack: IpStackUdpStream, mut udp_stack: IpStackUdpStream,
udpgw_client: Arc<UdpGwClient>, udpgw_client: Arc<UdpGwClient>,