Remove 'udp_target' argument from wireguard-proxy, set it dynamically from first UDP packet recieved

This commit is contained in:
Travis Burtrum 2019-12-15 13:50:19 -05:00
parent a9f7daf823
commit 152bb991d4
3 changed files with 27 additions and 17 deletions

View File

@ -70,10 +70,10 @@ impl Server {
fn main() { fn main() {
let raw_args = env::args().collect(); let raw_args = env::args().collect();
let args = Args::new(&raw_args); let args = Args::new(&raw_args);
let mut first_arg = args.get_str(1, "127.0.0.1:51821"); let mut first_arg = args.get_str(1, "127.0.0.1:51820");
if first_arg.contains("-h") { if first_arg.contains("-h") {
println!( println!(
"usage: {} [-h] [-s run a self test through proxy/proxyd] [-is run a self test through proxy/proxyd without spawning other processes] [udp_host, 127.0.0.1:51821] [udp_target, 127.0.0.1:51821] [socket_timeout, 10]", "usage: {} [-h] [-s run a self test through proxy/proxyd] [-is run a self test through proxy/proxyd without spawning other processes] [udp_host, 127.0.0.1:51820] [udp_target, 127.0.0.1:51820] [socket_timeout, 10]",
args.get_str(0, "udp-test") args.get_str(0, "udp-test")
); );
return; return;
@ -148,16 +148,14 @@ fn main() {
thread::sleep(sleep); thread::sleep(sleep);
let proxy_client = ProxyClient::new( let proxy_client = ProxyClient::new(
"127.0.0.1:51821".to_owned(),
"127.0.0.1:51820".to_owned(), "127.0.0.1:51820".to_owned(),
tcp_host.to_owned().to_owned(), tcp_host.to_owned().to_owned(),
15, 15,
); );
println!( println!(
"udp_host: {}, udp_target: {}, tcp_target: {}, socket_timeout: {:?}", "udp_host: {}, tcp_target: {}, socket_timeout: {:?}",
proxy_client.udp_host, proxy_client.udp_host,
proxy_client.udp_target,
proxy_client.tcp_target, proxy_client.tcp_target,
proxy_client.socket_timeout, proxy_client.socket_timeout,
); );
@ -172,7 +170,7 @@ fn main() {
let server = Server::new( let server = Server::new(
first_arg.to_owned(), first_arg.to_owned(),
args.get_str(2, "127.0.0.1:51821").to_owned(), args.get_str(2, "127.0.0.1:51820").to_owned(),
args.get(3, 10), args.get(3, 10),
); );

View File

@ -6,23 +6,21 @@ fn main() {
let args = Args::new(&raw_args); let args = Args::new(&raw_args);
if args.get_str(1, "").contains("-h") { if args.get_str(1, "").contains("-h") {
println!( println!(
"usage: {} [-h] [udp_host, 127.0.0.1:51821] [udp_target, 127.0.0.1:51820] [tcp_target, 127.0.0.1:5555] [socket_timeout, 0]", "usage: {} [-h] [udp_host, 127.0.0.1:51820] [tcp_target, 127.0.0.1:5555] [socket_timeout, 0]",
args.get_str(0, "wireguard-proxy") args.get_str(0, "wireguard-proxy")
); );
return; return;
} }
let proxy_client = ProxyClient::new( let proxy_client = ProxyClient::new(
args.get_str(1, "127.0.0.1:51821").to_owned(), args.get_str(1, "127.0.0.1:51820").to_owned(),
args.get_str(2, "127.0.0.1:51820").to_owned(), args.get_str(2, "127.0.0.1:5555").to_owned(),
args.get_str(3, "127.0.0.1:5555").to_owned(), args.get(3, 0),
args.get(4, 0),
); );
println!( println!(
"udp_host: {}, udp_target: {}, tcp_target: {}, socket_timeout: {:?}", "udp_host: {}, tcp_target: {}, socket_timeout: {:?}",
proxy_client.udp_host, proxy_client.udp_host,
proxy_client.udp_target,
proxy_client.tcp_target, proxy_client.tcp_target,
proxy_client.socket_timeout, proxy_client.socket_timeout,
); );

View File

@ -52,8 +52,21 @@ impl TcpUdpPipe {
)) ))
} }
pub fn udp_to_tcp_connect_socket(&mut self) -> std::io::Result<usize> {
let (len, src_addr) = self.udp_socket.recv_from(&mut self.buf[2..])?;
println!("first packet from {}, connecting to that", src_addr);
self.udp_socket.connect(src_addr)?;
self.send_udp(len)
}
pub fn udp_to_tcp(&mut self) -> std::io::Result<usize> { pub fn udp_to_tcp(&mut self) -> std::io::Result<usize> {
let len = self.udp_socket.recv(&mut self.buf[2..])?; let len = self.udp_socket.recv(&mut self.buf[2..])?;
self.send_udp(len)
}
fn send_udp(&mut self, len: usize) -> std::io::Result<usize> {
println!("udp got len: {}", len); println!("udp got len: {}", len);
self.buf[0] = ((len >> 8) & 0xFF) as u8; self.buf[0] = ((len >> 8) & 0xFF) as u8;
@ -80,16 +93,14 @@ impl TcpUdpPipe {
pub struct ProxyClient { pub struct ProxyClient {
pub udp_host: String, pub udp_host: String,
pub udp_target: String,
pub tcp_target: String, pub tcp_target: String,
pub socket_timeout: Option<Duration>, pub socket_timeout: Option<Duration>,
} }
impl ProxyClient { impl ProxyClient {
pub fn new(udp_host: String, udp_target: String, tcp_target: String, secs: u64) -> ProxyClient { pub fn new(udp_host: String, tcp_target: String, secs: u64) -> ProxyClient {
ProxyClient { ProxyClient {
udp_host, udp_host,
udp_target,
tcp_target, tcp_target,
socket_timeout: match secs { socket_timeout: match secs {
0 => None, 0 => None,
@ -105,9 +116,12 @@ impl ProxyClient {
let udp_socket = UdpSocket::bind(&self.udp_host)?; let udp_socket = UdpSocket::bind(&self.udp_host)?;
udp_socket.set_read_timeout(self.socket_timeout)?; udp_socket.set_read_timeout(self.socket_timeout)?;
//udp_socket.connect(&self.udp_target)?; // this isn't strictly needed... just filters who we can receive from
let mut udp_pipe = TcpUdpPipe::new(tcp_stream, udp_socket); let mut udp_pipe = TcpUdpPipe::new(tcp_stream, udp_socket);
// we want to wait for first udp packet from client first, to set the target to respond to
udp_pipe.udp_to_tcp_connect_socket()?;
let mut udp_pipe_clone = udp_pipe.try_clone()?; let mut udp_pipe_clone = udp_pipe.try_clone()?;
thread::spawn(move || loop { thread::spawn(move || loop {
udp_pipe_clone udp_pipe_clone