2023-03-20 23:52:38 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
extern crate reqwest;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::io::BufRead;
|
2023-03-22 13:18:07 +01:00
|
|
|
use std::net::SocketAddr;
|
2023-03-20 23:52:38 +01:00
|
|
|
use std::process::Command;
|
|
|
|
use std::string::ToString;
|
2023-03-22 13:18:07 +01:00
|
|
|
|
|
|
|
use fork::Fork;
|
|
|
|
use nix::sys::signal;
|
|
|
|
use nix::unistd::Pid;
|
|
|
|
use serial_test::serial;
|
|
|
|
|
2023-03-23 13:03:01 +01:00
|
|
|
use tun2proxy::tun2proxy::Options;
|
2023-03-22 13:18:07 +01:00
|
|
|
use tun2proxy::{main_entry, Proxy, ProxyType};
|
2023-03-20 23:52:38 +01:00
|
|
|
|
|
|
|
static TUN_TEST_DEVICE: &str = "tun0";
|
|
|
|
static ALL_ROUTES: [&str; 4] = ["0.0.0.0/1", "128.0.0.0/1", "::/1", "8000::/1"];
|
|
|
|
|
2023-03-22 13:18:07 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2023-03-20 23:52:38 +01:00
|
|
|
struct Test {
|
2023-03-22 13:18:07 +01:00
|
|
|
proxy: Proxy,
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 13:18:07 +01:00
|
|
|
fn proxy_from_env(env_var: &str) -> Result<Proxy, String> {
|
|
|
|
let url =
|
|
|
|
env::var(env_var).map_err(|_| format!("{env_var} environment variable not found"))?;
|
|
|
|
Proxy::from_url(url.as_str()).map_err(|_| format!("{env_var} URL cannot be parsed"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_from_env(env_var: &str) -> Result<Test, String> {
|
|
|
|
let proxy = proxy_from_env(env_var)?;
|
|
|
|
Ok(Test { proxy })
|
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
fn tests() -> [Result<Test, String>; 3] {
|
|
|
|
[
|
|
|
|
test_from_env("SOCKS4_SERVER"),
|
|
|
|
test_from_env("SOCKS5_SERVER"),
|
|
|
|
test_from_env("HTTP_SERVER"),
|
|
|
|
]
|
2023-03-22 13:18:07 +01:00
|
|
|
}
|
2023-03-20 23:52:38 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[ctor::ctor]
|
|
|
|
fn init() {
|
2023-03-22 18:06:51 +01:00
|
|
|
dotenvy::dotenv().ok();
|
2023-03-20 23:52:38 +01:00
|
|
|
routes_setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[ctor::dtor]
|
|
|
|
fn cleanup() {
|
|
|
|
Command::new("ip")
|
|
|
|
.args(["link", "del", TUN_TEST_DEVICE])
|
|
|
|
.output()
|
|
|
|
.expect("failed to delete tun device");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn routes_setup() {
|
|
|
|
let mut all_servers: Vec<SocketAddr> = Vec::new();
|
|
|
|
|
2023-03-22 13:18:07 +01:00
|
|
|
for test in tests() {
|
|
|
|
if test.is_err() {
|
|
|
|
continue;
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
2023-03-22 13:18:07 +01:00
|
|
|
all_servers.push(test.unwrap().proxy.addr);
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Command::new("ip")
|
|
|
|
.args(["tuntap", "add", "name", TUN_TEST_DEVICE, "mode", "tun"])
|
|
|
|
.output()
|
|
|
|
.expect("failed to create tun device");
|
|
|
|
|
|
|
|
Command::new("ip")
|
|
|
|
.args(["link", "set", TUN_TEST_DEVICE, "up"])
|
|
|
|
.output()
|
|
|
|
.expect("failed to bring up tun device");
|
|
|
|
|
|
|
|
let routes = Command::new("ip")
|
|
|
|
.args(["route", "show"])
|
|
|
|
.output()
|
|
|
|
.expect("failed to get routing table");
|
|
|
|
|
|
|
|
// Equivalent of `ip route | grep '^default' | cut -d ' ' -f 2-`
|
|
|
|
let mut default_route_args = Vec::<String>::new();
|
|
|
|
for result in routes.stdout.lines() {
|
|
|
|
let line = result.unwrap();
|
|
|
|
let split = line.split_whitespace();
|
|
|
|
for (i, route_component) in split.enumerate() {
|
|
|
|
if i == 0 && route_component != "default" {
|
|
|
|
break;
|
|
|
|
} else if i == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
default_route_args.push(String::from(route_component));
|
|
|
|
}
|
2023-03-23 13:03:01 +01:00
|
|
|
if !default_route_args.is_empty() {
|
2023-03-20 23:52:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for server in all_servers {
|
|
|
|
let mut proxy_route = vec!["route".to_string(), "add".to_string()];
|
|
|
|
proxy_route.push(server.ip().to_string());
|
|
|
|
proxy_route.extend(default_route_args.clone());
|
|
|
|
Command::new("ip")
|
|
|
|
.args(proxy_route)
|
|
|
|
.output()
|
|
|
|
.expect("failed to get routing table");
|
|
|
|
}
|
|
|
|
|
|
|
|
for route in ALL_ROUTES {
|
|
|
|
Command::new("ip")
|
|
|
|
.args(["route", "add", route, "dev", TUN_TEST_DEVICE])
|
|
|
|
.output()
|
|
|
|
.expect("failed to add route");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:51:56 +01:00
|
|
|
fn request_ip_host_http() {
|
|
|
|
reqwest::blocking::get("http://1.1.1.1").expect("failed to issue HTTP request");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn request_example_https() {
|
|
|
|
reqwest::blocking::get("https://example.org").expect("failed to issue HTTPs request");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_test<F, T>(filter: F, test_function: T)
|
2023-03-20 23:52:38 +01:00
|
|
|
where
|
|
|
|
F: Fn(&Test) -> bool,
|
2023-03-23 14:51:56 +01:00
|
|
|
T: Fn(),
|
2023-03-20 23:52:38 +01:00
|
|
|
{
|
2023-03-22 13:18:07 +01:00
|
|
|
for potential_test in tests() {
|
|
|
|
match potential_test {
|
|
|
|
Ok(test) => {
|
2023-03-24 18:04:27 +01:00
|
|
|
if !filter(&test) {
|
2023-03-22 13:18:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match fork::fork() {
|
|
|
|
Ok(Fork::Parent(child)) => {
|
2023-03-23 14:51:56 +01:00
|
|
|
test_function();
|
2023-03-22 13:18:07 +01:00
|
|
|
signal::kill(Pid::from_raw(child), signal::SIGKILL)
|
|
|
|
.expect("failed to kill child");
|
|
|
|
nix::sys::wait::waitpid(Pid::from_raw(child), None)
|
|
|
|
.expect("failed to wait for child");
|
|
|
|
}
|
|
|
|
Ok(Fork::Child) => {
|
|
|
|
prctl::set_death_signal(signal::SIGKILL as isize).unwrap(); // 9 == SIGKILL
|
2023-03-24 09:27:31 +08:00
|
|
|
let _ = main_entry(
|
2023-03-23 18:11:08 +01:00
|
|
|
TUN_TEST_DEVICE,
|
|
|
|
test.proxy,
|
|
|
|
Options::new().with_virtual_dns(),
|
|
|
|
);
|
2023-03-22 13:18:07 +01:00
|
|
|
}
|
2023-03-23 13:03:01 +01:00
|
|
|
Err(_) => panic!(),
|
2023-03-22 13:18:07 +01:00
|
|
|
}
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
2023-03-22 13:18:07 +01:00
|
|
|
Err(_) => {
|
|
|
|
continue;
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:39:08 +01:00
|
|
|
fn require_var(var: &str) {
|
2023-03-23 13:03:01 +01:00
|
|
|
env::var(var).unwrap_or_else(|_| panic!("{}", "{var} environment variable required"));
|
2023-03-22 15:39:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
#[serial]
|
|
|
|
#[test_log::test]
|
|
|
|
fn test_socks4() {
|
|
|
|
require_var("SOCKS4_SERVER");
|
|
|
|
run_test(
|
|
|
|
|test| test.proxy.proxy_type == ProxyType::Socks4,
|
|
|
|
request_ip_host_http,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-21 00:06:15 +01:00
|
|
|
#[serial]
|
2023-03-23 18:11:08 +01:00
|
|
|
#[test_log::test]
|
2023-03-20 23:52:38 +01:00
|
|
|
fn test_socks5() {
|
2023-03-22 15:39:08 +01:00
|
|
|
require_var("SOCKS5_SERVER");
|
2023-03-23 14:51:56 +01:00
|
|
|
run_test(
|
|
|
|
|test| test.proxy.proxy_type == ProxyType::Socks5,
|
|
|
|
request_ip_host_http,
|
|
|
|
)
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 00:06:15 +01:00
|
|
|
#[serial]
|
2023-03-23 18:11:08 +01:00
|
|
|
#[test_log::test]
|
2023-03-20 23:52:38 +01:00
|
|
|
fn test_http() {
|
2023-03-22 15:39:08 +01:00
|
|
|
require_var("HTTP_SERVER");
|
2023-03-23 14:51:56 +01:00
|
|
|
run_test(
|
|
|
|
|test| test.proxy.proxy_type == ProxyType::Http,
|
|
|
|
request_ip_host_http,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
#[serial]
|
|
|
|
#[test_log::test]
|
|
|
|
fn test_socks4_dns() {
|
|
|
|
require_var("SOCKS4_SERVER");
|
|
|
|
run_test(
|
|
|
|
|test| test.proxy.proxy_type == ProxyType::Socks4,
|
|
|
|
request_example_https,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:51:56 +01:00
|
|
|
#[serial]
|
2023-03-23 18:11:08 +01:00
|
|
|
#[test_log::test]
|
2023-03-23 14:51:56 +01:00
|
|
|
fn test_socks5_dns() {
|
|
|
|
require_var("SOCKS5_SERVER");
|
|
|
|
run_test(
|
|
|
|
|test| test.proxy.proxy_type == ProxyType::Socks5,
|
|
|
|
request_example_https,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[serial]
|
2023-03-23 18:11:08 +01:00
|
|
|
#[test_log::test]
|
2023-03-23 14:51:56 +01:00
|
|
|
fn test_http_dns() {
|
|
|
|
require_var("HTTP_SERVER");
|
|
|
|
run_test(
|
|
|
|
|test| test.proxy.proxy_type == ProxyType::Http,
|
|
|
|
request_example_https,
|
|
|
|
)
|
2023-03-20 23:52:38 +01:00
|
|
|
}
|
|
|
|
}
|