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:
faumarayandClaude Opus 5.5 committed 2026-10-27 12:32:43 +03:00
1 parent 62adadc207
commit 2c4cdee7d3
15 files changed
+980 -10

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
//! AmneziaWG outbounds and WARP devices of the "userspace" kind: wireproxy-awg, a SOCKS
//! listener on loopback in front of each tunnel, with the hop login, as upstream's
//! wg-tunnel.nix runs it. The "singBox" kind needs only the profile's `.conf`, which the
//! renderer makes a WireGuard endpoint of (AmneziaWG's obfuscation dropped, with a warning).
//! AmneziaWG as a system-wide VPN needs its driver, which is not here yet.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use ps_core::config::{AwgOutboundKind, Config, WarpOutboundKind};
use ps_core::derived::{self, ExtraKind};
use crate::paths::{Paths, write_atomic};
use crate::units::UnitSpec;
pub const UNIT_PREFIX: &str = "proxy-suite-awg-";
fn read(f: &str) -> Result<String, String> {
std::fs::read_to_string(f).map_err(|e| format!("{f}: {e}"))
}
/// Each "singBox" AmneziaWG outbound's `.conf`, by profile, and why any could not be read.
pub fn sing_box_profiles(cfg: &Config) -> (BTreeMap<String, String>, Vec<String>) {
let mut out = BTreeMap::new();
let mut errors = Vec::new();
for (name, kind, _) in derived::awg_outbounds(cfg) {
if kind != AwgOutboundKind::SingBox {
continue;
}
match ps_core::awg::prepare(&cfg.amnezia_wg.profiles[&name], &read) {
Ok(conf) => {
out.insert(name, conf);
}
Err(e) => errors.push(format!("AmneziaWG profile '{name}': {e}")),
}
}
(out, errors)
}
/// The wireproxy units: one per "userspace" AmneziaWG outbound and WARP device, with what
/// could not be made of the rest. `warp` holds the WARP devices' profiles.
pub fn units(cfg: &Config, paths: &Paths, program: &Path, login: (&str, &str), warp: &BTreeMap<String, String>) -> (Vec<UnitSpec>, Vec<String>) {
let mut tunnels: Vec<(String, Result<String, String>, u16)> = Vec::new();
for extra in derived::extra_outbounds(cfg) {
match extra.kind {
ExtraKind::Awg { profile, kind: AwgOutboundKind::Userspace, port } => {
let conf = ps_core::awg::prepare(&cfg.amnezia_wg.profiles[&profile], &read).map_err(|e| format!("AmneziaWG profile '{profile}': {e}"));
tunnels.push((profile, conf, port));
}
ExtraKind::Warp { device } if device.kind == WarpOutboundKind::Userspace => {
// A device registered (or given) only: until then the outbound waits.
if let Some(profile) = warp.get(&device.tag) {
let conf = ps_core::awg::validate(profile, false).map(|()| profile.clone()).map_err(|e| format!("WARP device '{}': {e}", device.tag));
tunnels.push((device.tag.clone(), conf, device.port));
}
}
_ => {}
}
}
let mut units = Vec::new();
let mut errors = Vec::new();
for (name, conf, port) in tunnels {
let made = conf.and_then(|c| ps_core::awg::as_wireproxy(&c, &format!("127.0.0.1:{port}"), Some(login))).and_then(|text| {
write(paths, &name, &text).map_err(|e| format!("'{name}': {e:#}"))
});
match made {
Ok((file, text)) => units.push(UnitSpec {
name: format!("{UNIT_PREFIX}{name}"),
program: program.to_path_buf(),
args: vec!["-c".into(), file.display().to_string()],
env: Vec::new(),
cwd: None,
generation: text,
}),
Err(e) => errors.push(e),
}
}
(units, errors)
}
/// The configuration, below the protected state root: it holds the tunnel's private key.
fn write(paths: &Paths, name: &str, text: &str) -> Result<(PathBuf, String)> {
let dir = paths.run().join("awg");
std::fs::create_dir_all(&dir)?;
let file = dir.join(format!("{name}.conf"));
write_atomic(&file, text.as_bytes()).with_context(|| format!("writing {}", file.display()))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600))?;
}
Ok((file, text.to_owned()))
}
+1
View File
@@ -4,6 +4,7 @@
//! it); `--console` runs it in the foreground, which is also how it runs off Windows.
mod api;
mod awg;
mod clash;
mod components;
mod fetch;
+48 -2
View File
@@ -93,6 +93,12 @@ impl Service {
{
direct.push(p.to_string_lossy().into_owned());
}
// wireproxy's tunnels, like the sidecar's, would loop back into the TUN.
if derived_userspace(cfg)
&& let Some(p) = self.comps.find("wireproxy-awg", "wireproxy")
{
direct.push(p.to_string_lossy().into_owned());
}
// Tor's own connections, unless it is to go through the proxy (upstream = "proxy").
if cfg.tor.enable
&& cfg.tor.upstream == ps_core::config::Upstream::Direct
@@ -112,10 +118,10 @@ impl Service {
groups_watched: false,
sidecar_secret: self.sidecar_secret.clone(),
warp_profiles: crate::warp::profiles(cfg, paths),
awg_profiles: crate::awg::sing_box_profiles(cfg).0,
ssh_identity_path: cfg.ssh_proxy.identity_file.as_ref().map(|_| crate::ssh::identity_path(paths).to_string_lossy().into_owned()),
ssh_host_keys: if cfg.ssh_proxy.enable { crate::ssh::host_keys(cfg, |f| std::fs::read_to_string(f).ok()) } else { Vec::new() },
windows: cfg!(windows),
..Default::default()
}
}
@@ -153,8 +159,40 @@ impl Service {
if let Err(e) = &tor {
self.sup.note(crate::tor::UNIT, format!("proxy-suite: Tor: {e:#}"));
}
let awg = self.apply_awg().await;
if let Err(e) = &awg {
self.sup.note(SOCKS_UNIT, format!("proxy-suite: AmneziaWG: {e:#}"));
}
let proxy = self.apply_proxy().await;
zapret.and(tg).and(ssh).and(tor).and(proxy)
zapret.and(tg).and(ssh).and(tor).and(awg).and(proxy)
}
/// The wireproxy tunnels behind "userspace" AmneziaWG outbounds and WARP devices, while
/// the proxy they serve runs; the ones no longer wanted stopped.
async fn apply_awg(&mut self) -> Result<()> {
let cfg = self.cfg().clone();
let userspace = derived_userspace(&cfg);
let (units, errors) = if self.proxy_wanted() && userspace {
let program = self.comps.program("wireproxy-awg", "wireproxy")?;
let login = (self.store.secrets.hop_user.clone(), self.store.secrets.hop_password.clone());
let warp = crate::warp::profiles(&cfg, self.paths());
crate::awg::units(&cfg, self.paths(), &program, (&login.0, &login.1), &warp)
} else {
(Vec::new(), Vec::new())
};
for e in errors {
self.sup.note(SOCKS_UNIT, format!("proxy-suite: {e}; leaving it out"));
}
let wanted: Vec<String> = units.iter().map(|u| u.name.clone()).collect();
for name in self.sup.names() {
if name.starts_with(crate::awg::UNIT_PREFIX) && !wanted.contains(&name) {
self.sup.stop(&name).await;
}
}
for unit in units {
self.sup.ensure(unit);
}
Ok(())
}
async fn apply_tor(&mut self) -> Result<()> {
@@ -506,6 +544,14 @@ async fn decompile(sing_box: &PathBuf, srs: &std::path::Path) -> Result<Value> {
Ok(v)
}
/// Whether any outbound runs in wireproxy: a "userspace" AmneziaWG profile or WARP device.
fn derived_userspace(cfg: &Config) -> bool {
ps_core::derived::extra_outbounds(cfg).iter().any(|e| {
matches!(&e.kind, ps_core::derived::ExtraKind::Awg { kind: ps_core::config::AwgOutboundKind::Userspace, .. })
|| matches!(&e.kind, ps_core::derived::ExtraKind::Warp { device } if device.kind == ps_core::config::WarpOutboundKind::Userspace)
})
}
/// WARP devices with no profile, registered (outside the lock: it is the network), and the
/// proxy applied again with them.
pub async fn register_warp(shared: &Shared) {