Implement client, tweaks

This commit is contained in:
Travis Burtrum 2019-07-13 01:00:06 -04:00
parent df013c7543
commit b28a68edb1
4 changed files with 106 additions and 6 deletions

View File

@ -5,7 +5,7 @@ Server-side daemon to proxy multiple TCP connections to wireguard, client-side i
Testing with GNU netcat: Testing with GNU netcat:
- `nc -vulp 51820` listen on udp like wireguard would - `nc -vulp 51820` listen on udp like wireguard would
- `nc -u 127.0.0.1 51820` connect directly to local udp wireguard port to send/receive data - `nc -u -p 51821 127.0.0.1 51820` connect directly to local udp wireguard port to send data to 51820 from port 51821
- `nc -vlp 5555` listen on tcp like wireguard-proxy would - `nc -vlp 5555` listen on tcp like wireguard-proxy would
- `nc 127.0.0.1 5555` connect directly to local tcp wireguard-proxy port to send/recieve data - `nc 127.0.0.1 5555` connect directly to local tcp wireguard-proxy port to send/recieve data
- so to test through wireguard-proxy run first and last command while it's running, type in both places - so to test through wireguard-proxy run first and last command while it's running, type in both places

100
src/bin/wireguard-proxy.rs Normal file
View File

@ -0,0 +1,100 @@
use std::io::{Read, Write};
use std::net::{TcpStream, UdpSocket};
use std::time::Duration;
use std::env;
use std::thread;
use wireguard_proxy::Args;
struct Server {
udp_host: String,
udp_target: String,
tcp_target: String,
socket_timeout: Option<Duration>,
}
impl Server {
fn new(udp_host: String, udp_target: String, tcp_target: String, secs: u64) -> Server {
Server {
udp_host,
udp_target,
tcp_target,
socket_timeout: match secs {
0 => None,
x => Some(Duration::from_secs(x)),
},
}
}
fn start(&self) -> std::io::Result<usize> {
let mut tcp_stream = TcpStream::connect(&self.tcp_target)?;
tcp_stream.set_read_timeout(self.socket_timeout)?;
let udp_socket = UdpSocket::bind(&self.udp_host)?;
udp_socket.set_read_timeout(self.socket_timeout)?;
udp_socket.connect(&self.udp_target)?;
let udp_socket_clone = udp_socket.try_clone().expect("clone udp_socket failed");
let mut tcp_stream_clone = tcp_stream.try_clone().expect("clone tcp_stream failed");
thread::spawn(move || {
let mut buf = [0u8; 2048];
loop {
match udp_socket_clone.recv(&mut buf) {
Ok(len) => {
println!("udp got len: {}", len);
tcp_stream_clone
.write(&buf[..len])
.expect("cannot write to tcp_clone");
}
Err(e) => {
println!("recv function failed: {:?}", e);
break;
}
}
}
});
let mut buf = [0u8; 2048];
loop {
match tcp_stream.read(&mut buf) {
Ok(len) => {
println!("tcp got len: {}", len);
udp_socket.send(&buf[..len])?;
}
Err(e) => {
println!("Unable to read stream: {}", e);
break;
}
}
}
Ok(0)
}
}
fn main() {
let raw_args = env::args().collect();
let args = Args::new(&raw_args);
if args.get_str(1, "").contains("-h") {
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]",
args.get_str(0, "wireguard-proxy")
);
return;
}
let server = Server::new(
args.get_str(1, "127.0.0.1:51821").to_owned(),
args.get_str(2, "127.0.0.1:51820").to_owned(),
args.get_str(3, "127.0.0.1:5555").to_owned(),
args.get(3, 0),
);
println!(
"udp_host: {}, udp_target: {}, tcp_target: {}, socket_timeout: {:?}",
server.udp_host, server.udp_target, server.tcp_target, server.socket_timeout,
);
server.start().expect("error running server");
}

View File

@ -51,7 +51,7 @@ impl Server {
} }
}; };
udp_socket.set_read_timeout(self.socket_timeout)?; udp_socket.set_read_timeout(self.socket_timeout)?;
udp_socket.connect(&self.udp_target)?; //udp_socket.connect(&self.udp_target)?;
let udp_socket_clone = udp_socket.try_clone().expect("clone udp_socket failed"); let udp_socket_clone = udp_socket.try_clone().expect("clone udp_socket failed");
let mut tcp_stream_clone = tcp_stream.try_clone().expect("clone tcp_stream failed"); let mut tcp_stream_clone = tcp_stream.try_clone().expect("clone tcp_stream failed");
@ -78,7 +78,9 @@ impl Server {
match tcp_stream.read(&mut buf) { match tcp_stream.read(&mut buf) {
Ok(len) => { Ok(len) => {
println!("tcp got len: {}", len); println!("tcp got len: {}", len);
udp_socket.send(&buf[..len])?; //udp_socket.send(&buf[..len])?;
let sent = udp_socket.send_to(&buf[..len], &self.udp_target)?;
println!("udp sent len: {}", sent);
} }
Err(e) => { Err(e) => {
println!("Unable to read stream: {}", e); println!("Unable to read stream: {}", e);
@ -96,7 +98,7 @@ 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] [host, 127.0.0.1:5555] [udp_target, 127.0.0.1:51820] [udp_bind_host_range, 127.0.0.1:30000-40000] [socket_timeout, 0]", "usage: {} [-h] [tcp_host, 127.0.0.1:5555] [udp_target, 127.0.0.1:51820] [udp_bind_host_range, 127.0.0.1:30000-40000] [socket_timeout, 0]",
args.get_str(0, "wireguard-proxyd") args.get_str(0, "wireguard-proxyd")
); );
return; return;

View File

@ -24,5 +24,3 @@ impl<'a> Args<'a> {
} }
} }
} }