windows: AmneziaWG outbounds, and WARP through wireproxy
- ps_core::awg: upstream's scripts/amneziawg_config.py, ported: a profile from a .conf, an AmneziaVPN vpn:// link (qCompress'd JSON or the compact .conf form, containers and DNS placeholders as upstream reads them) or declarative settings (with obfuscationFile's secrets); AWG 3's MTU default, the endpoint override, upstream's checks (no wg-quick hooks unless allowed, no control characters, one [Interface]); and the wireproxy configuration. Checked against links made by upstream's own encoder, output byte for byte. - wireproxy-awg for Windows, built from upstream's tag (pinned to its commit) with upstream's patches, by tools/build-wireproxy-awg.sh; in the release as a bundled component. Go joins the dev shell. - The service runs one per "userspace" AmneziaWG outbound and WARP device, on its loopback port with the hop login, its configuration below the protected state root; "singBox" profiles reach the renderer. wireproxy goes direct under the TUN. - End to end on Linux (an_amneziawg_tunnel): sing-box as the WireGuard server, the service's wireproxy as the client; a page through its listener and through the proxy's outbound, named inside the tunnel. - What is still to come and said: AmneziaWG as a system-wide VPN. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
62adadc207
commit
2c4cdee7d3
15 files changed
+980
-10
No files matched your search
@@ -295,3 +295,146 @@ fn an_ssh_tunnel() {
|
||||
assert_eq!(logins, 2, "{}", diagnose());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// GET http://<host>:<port>/ through a SOCKS5 proxy that wants a login (RFC 1929).
|
||||
fn socks_get_login(proxy: u16, login: (&str, &str), host: &str, port: u16) -> std::io::Result<String> {
|
||||
let mut s = TcpStream::connect(("127.0.0.1", proxy))?;
|
||||
s.set_read_timeout(Some(Duration::from_secs(10)))?;
|
||||
s.write_all(&[5, 1, 2])?;
|
||||
let mut buf = [0u8; 2];
|
||||
s.read_exact(&mut buf)?;
|
||||
let (u, p) = login;
|
||||
let mut auth = vec![1, u.len() as u8];
|
||||
auth.extend_from_slice(u.as_bytes());
|
||||
auth.push(p.len() as u8);
|
||||
auth.extend_from_slice(p.as_bytes());
|
||||
s.write_all(&auth)?;
|
||||
s.read_exact(&mut buf)?;
|
||||
if buf[1] != 0 {
|
||||
return Err(std::io::Error::other("SOCKS login refused"));
|
||||
}
|
||||
let ip: std::net::Ipv4Addr = host.parse().map_err(std::io::Error::other)?;
|
||||
let mut req = vec![5, 1, 0, 1];
|
||||
req.extend_from_slice(&ip.octets());
|
||||
req.extend_from_slice(&port.to_be_bytes());
|
||||
s.write_all(&req)?;
|
||||
let mut reply = [0u8; 10];
|
||||
s.read_exact(&mut reply)?;
|
||||
if reply[1] != 0 {
|
||||
return Err(std::io::Error::other(format!("SOCKS reply {}", reply[1])));
|
||||
}
|
||||
s.write_all(format!("GET / HTTP/1.0\r\nHost: {host}\r\n\r\n").as_bytes())?;
|
||||
let mut body = String::new();
|
||||
s.read_to_string(&mut body)?;
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
fn wg_keypair() -> (String, String) {
|
||||
let out = Command::new("sing-box").args(["generate", "wg-keypair"]).output().unwrap();
|
||||
let text = String::from_utf8_lossy(&out.stdout).into_owned();
|
||||
let field = |name: &str| text.lines().find_map(|l| l.strip_prefix(name)).unwrap_or_else(|| panic!("{text}")).trim().to_owned();
|
||||
(field("PrivateKey:"), field("PublicKey:"))
|
||||
}
|
||||
|
||||
/// AmneziaWG of the "userspace" kind, offline: sing-box as the WireGuard server on loopback
|
||||
/// (answering for web.test, and taking 10.66.0.1 to this machine), the service running
|
||||
/// wireproxy-awg as its client, a page through wireproxy's listener and through the proxy.
|
||||
#[test]
|
||||
fn an_amneziawg_tunnel() {
|
||||
if Command::new("sing-box").arg("version").output().is_err() || on_path("wireproxy").is_none() {
|
||||
eprintln!("sing-box or wireproxy (tools/build-wireproxy-awg.sh OUT linux) is not on PATH: skipping");
|
||||
return;
|
||||
}
|
||||
let dir = std::env::temp_dir().join(format!("ps-e2e-awg-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let (wg_port, web_port, proxy_port, clash_port, test_port) = (free_port(), free_port(), free_port(), free_port(), free_port());
|
||||
let web = TcpListener::bind(("127.0.0.1", web_port)).unwrap();
|
||||
std::thread::spawn(move || {
|
||||
for mut s in web.incoming().flatten() {
|
||||
let mut buf = [0u8; 1024];
|
||||
let _ = s.read(&mut buf);
|
||||
let _ = s.write_all(b"HTTP/1.0 200 OK\r\nContent-Length: 16\r\n\r\nhello-through-wg");
|
||||
}
|
||||
});
|
||||
|
||||
let (srv_priv, srv_pub) = wg_keypair();
|
||||
let (cli_priv, cli_pub) = wg_keypair();
|
||||
let server = serde_json::json!({
|
||||
"log": { "level": "warn" },
|
||||
// web.test is the server's own tunnel address, which it takes to this machine's
|
||||
// loopback (a tunnel's packet to 127.0.0.1 itself would be dropped as martian).
|
||||
"dns": { "servers": [{ "type": "hosts", "tag": "hosts", "predefined": { "web.test": ["10.66.0.1"] } }] },
|
||||
"endpoints": [{ "type": "wireguard", "tag": "wg-in", "address": ["10.66.0.1/32"], "private_key": srv_priv,
|
||||
"listen_port": wg_port, "peers": [{ "public_key": cli_pub, "allowed_ips": ["10.66.0.2/32"] }] }],
|
||||
"outbounds": [{ "type": "direct", "tag": "direct" }],
|
||||
"route": {
|
||||
"default_domain_resolver": "hosts",
|
||||
"rules": [
|
||||
{ "port": 53, "action": "hijack-dns" },
|
||||
{ "ip_cidr": ["10.66.0.1/32"], "action": "route", "outbound": "direct", "override_address": "127.0.0.1" },
|
||||
],
|
||||
"final": "direct",
|
||||
},
|
||||
});
|
||||
std::fs::write(dir.join("server.json"), server.to_string()).unwrap();
|
||||
let _server = Kill(Command::new("sing-box").args(["run", "-c"]).arg(dir.join("server.json")).stderr(std::fs::File::create(dir.join("server.log")).unwrap()).spawn().unwrap());
|
||||
|
||||
let profile = format!(
|
||||
"[Interface]\nPrivateKey = {cli_priv}\nAddress = 10.66.0.2/32\nDNS = 10.66.0.1\n\n[Peer]\nPublicKey = {srv_pub}\nAllowedIPs = 0.0.0.0/0\nEndpoint = 127.0.0.1:{wg_port}\nPersistentKeepalive = 25\n"
|
||||
);
|
||||
let state = dir.join("state");
|
||||
std::fs::create_dir_all(&state).unwrap();
|
||||
let config = serde_json::json!({
|
||||
"proxy": {
|
||||
"enable": true,
|
||||
"listener": { "port": proxy_port },
|
||||
"singBox": { "clashApiPort": clash_port, "testPort": test_port },
|
||||
"routing": { "directRu": false },
|
||||
},
|
||||
"amneziaWg": { "enable": true, "profiles": { "vpn": { "config": profile, "asOutbound": "userspace" } } },
|
||||
});
|
||||
std::fs::write(state.join("config.json"), config.to_string()).unwrap();
|
||||
let socket = dir.join("api.sock");
|
||||
let _service = Kill(
|
||||
Command::new(service_binary())
|
||||
.args(["--console", "--state-dir"])
|
||||
.arg(&state)
|
||||
.arg("--socket")
|
||||
.arg(&socket)
|
||||
.stderr(std::fs::File::create(dir.join("service.log")).unwrap())
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
wait_for("the service's API", Duration::from_secs(10), || ctl(&socket, &["status"]).0);
|
||||
let diagnose = || {
|
||||
format!(
|
||||
"status:\n{}\nlogs:\n{}\nserver:\n{}",
|
||||
ctl(&socket, &["status"]).1,
|
||||
ctl(&socket, &["logs", "-n", "40"]).1,
|
||||
std::fs::read_to_string(dir.join("server.log")).unwrap_or_default()
|
||||
)
|
||||
};
|
||||
|
||||
// wireproxy's own listener, with the login the service gave it.
|
||||
let conf = state.join("run").join("awg").join("vpn.conf");
|
||||
wait_for("wireproxy's configuration", Duration::from_secs(10), || conf.is_file());
|
||||
let text = std::fs::read_to_string(&conf).unwrap();
|
||||
let field = |name: &str| text.lines().find_map(|l| l.strip_prefix(name)).unwrap().trim().to_owned();
|
||||
let (user, password) = (field("Username ="), field("Password ="));
|
||||
let port: u16 = field("BindAddress =").rsplit(':').next().unwrap().parse().unwrap();
|
||||
let body = (0..40).find_map(|_| {
|
||||
std::thread::sleep(Duration::from_millis(250));
|
||||
socks_get_login(port, (&user, &password), "10.66.0.1", web_port).ok().filter(|b| b.ends_with("hello-through-wg"))
|
||||
});
|
||||
assert!(body.is_some(), "no page through wireproxy\n{}", diagnose());
|
||||
|
||||
// The proxy's outbound "vpn", a name resolved inside the tunnel.
|
||||
assert!(ctl(&socket, &["proxy", "pin", "vpn"]).0, "{}", diagnose());
|
||||
let body = (0..40).find_map(|_| {
|
||||
std::thread::sleep(Duration::from_millis(250));
|
||||
socks_get(proxy_port, "web.test", web_port).ok().filter(|b| b.ends_with("hello-through-wg"))
|
||||
});
|
||||
assert!(body.is_some(), "no page through the proxy's AmneziaWG outbound\n{}", diagnose());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user