From e5a318c14ccbfe902d00d8833590a21d2809ed5a Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Tue, 16 Jul 2019 00:39:24 -0400 Subject: [PATCH] Implement equivalent of test.sh in pure rust in udp-test.rs --- README.md | 3 +- src/bin/udp-test.rs | 56 +++++++++++++++++++++++++++++++++++--- src/bin/wireguard-proxy.rs | 2 +- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e78be01..4f9b57a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ Proxy wireguard UDP packets over TCP/TLS Testing: `udp-test` is a utility to send a UDP packet and then receive a UDP packet and ensure they are the same, this verifies packets sent through proxy/proxyd are unmolested -`test.sh` runs udp-test against itself and then through proxyd/proxy +`test.sh` runs udp-test against itself and then through proxyd/proxy +`udp-test -s` runs udp-test against itself through proxyd/proxy Testing with GNU netcat: diff --git a/src/bin/udp-test.rs b/src/bin/udp-test.rs index 7d4d48a..73403dd 100644 --- a/src/bin/udp-test.rs +++ b/src/bin/udp-test.rs @@ -1,7 +1,8 @@ use std::net::UdpSocket; use std::time::Duration; -use std::env; +use std::process::{exit, Command}; +use std::{env, thread}; use wireguard_proxy::Args; const PONG: [u8; 246] = [ @@ -69,16 +70,63 @@ impl Server { fn main() { let raw_args = env::args().collect(); let args = Args::new(&raw_args); - if args.get_str(1, "").contains("-h") { + let first_arg = args.get_str(1, "127.0.0.1:51821"); + if first_arg.contains("-h") { println!( - "usage: {} [-h] [udp_host, 127.0.0.1:51821] [udp_target, 127.0.0.1:51821] [socket_timeout, 1]", + "usage: {} [-h] [-s run a self test through proxy/proxyd] [udp_host, 127.0.0.1:51821] [udp_target, 127.0.0.1:51821] [socket_timeout, 1]", args.get_str(0, "udp-test") ); return; + } else if first_arg.contains("-s") { + // here is the hard work, we need to spawn proxyd and proxy from the same dir as udp-test... + let host = "127.0.0.1:51822"; + let tcp_host = "127.0.0.1:5555"; + let sleep = Duration::from_secs(5); + + let udp_test = args.get_str(0, "udp-test"); + let proxyd = udp_test.clone().replace("udp-test", "wireguard-proxyd"); + let proxy = udp_test.clone().replace("udp-test", "wireguard-proxy"); + + println!("executing: {} '{}' '{}'", proxyd, tcp_host, host); + let mut proxyd = Command::new(proxyd) + .arg(tcp_host) + .arg(host) + .spawn() + .expect("wireguard-proxyd failed to launch"); + println!("waiting: {:?} for wireguard-proxyd to come up.....", sleep); + thread::sleep(sleep); + + println!("executing: {}", proxy); + let mut proxy = Command::new(proxy) + .spawn() + .expect("wireguard-proxy failed to launch"); + println!("waiting: {:?} for wireguard-proxy to come up.....", sleep); + thread::sleep(sleep); + + println!("executing: {} '{}'", udp_test, host); + let mut udp_test = Command::new(udp_test) + .arg(host) + .spawn() + .expect("udp-test failed to launch"); + println!("waiting: {:?} for udp-test to come up.....", sleep); + thread::sleep(sleep); + + // ignore all these, what could we do anyway? + proxy.kill().ok(); + proxyd.kill().ok(); + udp_test.kill().ok(); + + exit( + udp_test + .wait() + .expect("could not get udp-test exit code") + .code() + .expect("could not get udp-test exit code"), + ); } let server = Server::new( - args.get_str(1, "127.0.0.1:51821").to_owned(), + first_arg.to_owned(), args.get_str(2, "127.0.0.1:51821").to_owned(), args.get(3, 1), ); diff --git a/src/bin/wireguard-proxy.rs b/src/bin/wireguard-proxy.rs index ac9f5fc..81e4627 100644 --- a/src/bin/wireguard-proxy.rs +++ b/src/bin/wireguard-proxy.rs @@ -63,7 +63,7 @@ fn main() { 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), + args.get(4, 0), ); println!(