346 lines
16 KiB
Nix
346 lines
16 KiB
Nix
# proxy-suite (github:FUFSoB/proxy-suite-flake): zapret DPI bypass, the
|
|
# Telegram WebSocket proxy, the GUI, and an AmneziaWG split tunnel for AI
|
|
# services. Both secrets are optional agenix files (commands in
|
|
# secrets/README.md):
|
|
# secrets/tg-ws-proxy-secret.age the MTProto secret (32 hex digits)
|
|
# secrets/amneziawg-ai.age the whole AmneziaWG client .conf (AWG 1.x
|
|
# to 3.x)
|
|
# proxy-suite reads them as root when its units start, so root ownership is
|
|
# enough. A missing file turns its feature off, and a warning says so.
|
|
#
|
|
# Split tunnel: the AmneziaWG profile `ai` is an interface outbound without
|
|
# routes (asOutbound = "interface"), so the config's AllowedIPs and DNS never
|
|
# touch the host's routes or resolver. The global mode is TProxy: nftables
|
|
# hands this host's own TCP and UDP connections to public addresses (its DNS
|
|
# lookups included) to sing-box; loopback, private and LAN ranges, replies and
|
|
# proxy-suite's own traffic stay out. The Claude/Anthropic and ChatGPT/OpenAI
|
|
# domains (and the anthropic/openai geosites, plus Anthropic's published
|
|
# addresses) go through outbound "ai", zapret2's fallback lists (below) through
|
|
# "proxy", which is `ai` too, and the rest goes direct, where zapret handles
|
|
# it. The tunnelled names are looked up by the remote resolver (1.1.1.1)
|
|
# through the tunnel, and their HTTPS (type 65) records are stripped so that a
|
|
# future ECH rollout cannot let browsers bypass the tunnel through the outer
|
|
# SNI.
|
|
#
|
|
# Why TProxy and not TUN: only proxy-suite-socks, which serves TProxy, loads
|
|
# zapret2's fallback lists: zapret-cutoff (networks that cut TLS off at about
|
|
# 16 KB: Cloudflare, AWS, Hetzner...) and zapret-unfixable (addresses no
|
|
# strategy fixed: CDN77, G-Core, OVH, Fastly, Akamai...). Under TUN they went
|
|
# direct and timed out. TProxy also leaves the proxy's own marked traffic
|
|
# alone (the `ai` tunnel's packets, the cutoff probe); TUN takes it in, so with
|
|
# the lists there the tunnel's own packets could be routed into the tunnel.
|
|
# What TProxy costs:
|
|
# - Only this host's own connections are taken (dockerd's included), not
|
|
# forwarded ones: rootful containers (the Gitea runner's jobs) and VMs go out
|
|
# direct, through zapret but without the split or the fallback lists.
|
|
# - The fallback networks have no direct fallback: while `ai` is down they do
|
|
# not load at all (cache.nixos.org, behind Fastly, among them).
|
|
#
|
|
# Patched upstream: the TProxy output chain took zapret's own desync packets
|
|
# (fakes and split segments, which carry zapret's mark) back into the proxy, so
|
|
# every connection zapret desyncs stalled. The patch and its description are in
|
|
# patches/proxy-suite/. The module is built from the locked source with them:
|
|
# upstream's flake.nix calls the same factory with the same inputs for
|
|
# nixosModules.default. Evaluation builds the patched tree first (import from
|
|
# derivation), and `nix flake check --no-build` is read-only: it stops with
|
|
# "...-proxy-suite-flake-patched.drv is not valid" whenever the store lacks the
|
|
# tree's .drv (a fresh clone or machine, a `nix flake update` that moves nixpkgs
|
|
# or proxy-suite, an edited patch, a garbage collection). That error means: run
|
|
# the toplevel eval first, which builds the tree, then the check. Drop a patch
|
|
# once upstream has the fix: one that no longer applies fails evaluation.
|
|
#
|
|
# Full tunnel: the same config also backs the global profile `vpn`, a normal
|
|
# VPN that routes all traffic (AllowedIPs 0.0.0.0/0, ::/0), unlike `ai`. It is
|
|
# not started at boot. proxy-suite has no kernel module here, so it runs in
|
|
# userspace (amneziawg-go), as `ai` does:
|
|
# proxy-ctl awg on vpn (or: systemctl start proxy-suite-awg-vpn)
|
|
# proxy-ctl awg off (or: systemctl stop proxy-suite-awg-vpn)
|
|
# Starting it stops TProxy and zapret (proxy-suite runs one global mode at a
|
|
# time) and, declared below, the `ai` interface, since both hold the same
|
|
# private key and peer: a peer accepts one endpoint, so two live interfaces
|
|
# would fight over it. Stopping it returns to split mode by itself: a hook
|
|
# below starts TProxy (which pulls in `ai`) and zapret; a `restart` keeps the
|
|
# full tunnel, and so does a shutdown. A `nixos-rebuild switch` ends the full
|
|
# tunnel too: it starts multi-user.target, whose TProxy, zapret and `ai` each
|
|
# conflict with it. Run `proxy-ctl awg on vpn` again afterwards. A second peer
|
|
# (its own client config and secret) would only keep `ai` up beside the full
|
|
# tunnel, and only once the unconditional conflicts and after on `ai` below are
|
|
# dropped; TProxy and zapret stop either way. Interfaces are awg-ai and awg-vpn,
|
|
# the full tunnel takes the default route with its fwmark rules, and its DNS
|
|
# goes through resolvconf; `ai` has no routes and no DNS, so the two never
|
|
# share routes or a resolver.
|
|
#
|
|
# The AmneziaVPN desktop client must not be connected with the same peer at
|
|
# the same time either, for the same reason.
|
|
{
|
|
config,
|
|
inputs,
|
|
lib,
|
|
...
|
|
}:
|
|
let
|
|
inherit (config.meta) secretsDir;
|
|
tgSecret = secretsDir + "/tg-ws-proxy-secret.age";
|
|
hasTgSecret = builtins.pathExists tgSecret;
|
|
awgSecret = secretsDir + "/amneziawg-ai.age";
|
|
hasAwgSecret = builtins.pathExists awgSecret;
|
|
|
|
# The locked proxy-suite source with this repository's patches, built for the
|
|
# flake's first system (modules/flake/parts.nix; the host's is the same): an
|
|
# `imports` entry cannot come from the NixOS module's own pkgs.
|
|
proxySuiteSource = inputs.nixpkgs.legacyPackages.${lib.head config.systems}.applyPatches {
|
|
name = "proxy-suite-flake-patched";
|
|
src = inputs.proxy-suite;
|
|
patches = [ ../../patches/proxy-suite/0001-tproxy-let-zapret-desync-packets-past.patch ];
|
|
};
|
|
# What upstream's flake.nix exports as nixosModules.default, from that tree.
|
|
# Upstream's core.nix takes exactly these four inputs, no `...`: a new required
|
|
# one fails evaluation, a new defaulted one would be skipped without a word. On
|
|
# every `nix flake update proxy-suite` compare this call with the one in
|
|
# upstream's flake.nix.
|
|
proxySuiteModule =
|
|
(import "${proxySuiteSource}/modules/proxy-suite" {
|
|
inherit (inputs.proxy-suite.inputs)
|
|
zapret
|
|
nixpkgs
|
|
nfqws2-keenetic
|
|
z2k
|
|
;
|
|
}).nixos;
|
|
|
|
# Sent through the tunnel, subdomains included. The geosite lists
|
|
# (sing-geosite) add the services' other hosts: uploads, CDNs, voice.
|
|
aiDomains = [
|
|
"anthropic.com"
|
|
"claude.ai"
|
|
"claude.com"
|
|
"openai.com"
|
|
"chatgpt.com"
|
|
"oaistatic.com"
|
|
"oaiusercontent.com"
|
|
"oaiusercontent.net"
|
|
"rule34.world"
|
|
"modhub.giants-software.com"
|
|
"giants-software.com"
|
|
"farming-simulator.com"
|
|
"modhub.us"
|
|
];
|
|
aiGeosites = [
|
|
"anthropic"
|
|
"openai"
|
|
];
|
|
# Whether the proxy handles IPv6. This uplink has none (see proxy.ipv6).
|
|
proxyIpv6 = false;
|
|
# Anthropic's inbound addresses, from the "IP addresses" page of its API docs
|
|
# (platform.claude.com/docs/en/api/ip-addresses; "will not change without
|
|
# notice"). The Claude hosts looked up so far resolve into the IPv4 range.
|
|
# They back the domain rules for clients that skip the lookup or the SNI,
|
|
# such as DoH.
|
|
anthropicIps4 = [ "160.79.104.0/23" ];
|
|
anthropicIps6 = [ "2607:6bc0::/48" ];
|
|
in
|
|
{
|
|
flake.modules.nixos.proxy-suite =
|
|
{ config, pkgs, ... }:
|
|
let
|
|
cfg = config.services.proxy-suite;
|
|
systemctl = "${config.systemd.package}/bin/systemctl";
|
|
|
|
# ExecStopPost of the full tunnel: back to split mode. Skipped at
|
|
# shutdown, and on a `restart` (proxy-ctl awg restart, systemctl restart),
|
|
# which stops the unit only to start it again: its job is still listed
|
|
# as "restart" while the stop hooks run. Both units conflict with the
|
|
# full tunnel, so `proxy-ctl proxy tproxy on` reaches the same state
|
|
# through the same stop, and starting them again is a no-op. zapret is
|
|
# started here rather than wanted by TProxy, so that `proxy-ctl zapret
|
|
# off` holds until the next full tunnel stop, boot or switch (a switch
|
|
# starts zapret and TProxy again).
|
|
# --no-block: the start would otherwise wait for this very stop.
|
|
splitRestore = pkgs.writeShellScript "proxy-suite-split-restore" ''
|
|
[ "$(${systemctl} is-system-running 2>/dev/null)" = stopping ] && exit 0
|
|
if ${systemctl} list-jobs --no-legend --plain 2>/dev/null \
|
|
| ${pkgs.gawk}/bin/awk '$2 == "proxy-suite-awg-vpn.service" && $3 == "restart" { f = 1 } END { exit !f }'; then
|
|
exit 0
|
|
fi
|
|
exec ${systemctl} --no-block start proxy-suite-tproxy.service proxy-suite-zapret.service
|
|
'';
|
|
in
|
|
{
|
|
imports = [ proxySuiteModule ];
|
|
|
|
# The patched tree stays in the system closure, so the garbage collector
|
|
# leaves it and evaluation works without building it or reaching
|
|
# cache.nixos.org (which is out of reach while `ai` is down).
|
|
system.extraDependencies = [ proxySuiteSource ];
|
|
|
|
age.secrets =
|
|
lib.optionalAttrs hasTgSecret {
|
|
tg-ws-proxy-secret = {
|
|
file = tgSecret;
|
|
mode = "0400";
|
|
};
|
|
}
|
|
// lib.optionalAttrs hasAwgSecret {
|
|
amneziawg-ai = {
|
|
file = awgSecret;
|
|
mode = "0400";
|
|
};
|
|
};
|
|
|
|
services.proxy-suite = {
|
|
enable = true;
|
|
|
|
# Without the AmneziaWG config nothing needs routing: no local proxy.
|
|
proxy = lib.mkIf hasAwgSecret {
|
|
enable = true;
|
|
# TProxy, not TUN (see the header). tun.enable stays at its default,
|
|
# false.
|
|
tproxy.enable = true;
|
|
autostart = "tproxy";
|
|
# This uplink has no IPv6: TProxy answers it with an unreachable
|
|
# route and DNS answers carry no AAAA, so apps fall back to IPv4
|
|
# instead of hanging.
|
|
ipv6 = proxyIpv6;
|
|
dns.strategy = "ipv4_only";
|
|
# HTTPS (type 65) records can carry ECH keys and hints that let a
|
|
# browser skip the SNI sing-box routes on. An empty NOERROR answer
|
|
# makes it fall back to the plain TLS handshake.
|
|
dns.singBox.rules = [
|
|
{
|
|
query_type = [ "HTTPS" ];
|
|
domain_suffix = aiDomains;
|
|
action = "predefined";
|
|
rcode = "NOERROR";
|
|
}
|
|
{
|
|
query_type = [ "HTTPS" ];
|
|
rule_set = map (s: "geosite-${s}") aiGeosites;
|
|
action = "predefined";
|
|
rcode = "NOERROR";
|
|
}
|
|
];
|
|
routing = {
|
|
default = "direct";
|
|
rules = [
|
|
{
|
|
domains = aiDomains;
|
|
geosites = aiGeosites;
|
|
ips = anthropicIps4 ++ lib.optionals proxyIpv6 anthropicIps6;
|
|
outbound = "ai";
|
|
}
|
|
];
|
|
};
|
|
};
|
|
|
|
amneziaWg = lib.mkIf hasAwgSecret {
|
|
enable = true;
|
|
# Userspace amneziawg-go only: no out-of-tree module to rebuild for
|
|
# every kernel. AWG 3 RandomTrailers with ranged H1-H3 needs
|
|
# userspace anyway.
|
|
kernelModulePackage = null;
|
|
# Only the declared profile: none added with proxy-ctl, no wireproxy.
|
|
runtime.enable = false;
|
|
profiles.ai = {
|
|
configFile = config.age.secrets.amneziawg-ai.path;
|
|
asOutbound = "interface";
|
|
};
|
|
# Full tunnel on the same config: a global profile (no asOutbound),
|
|
# never autostarted. Started with `proxy-ctl awg on vpn`.
|
|
profiles.vpn.configFile = config.age.secrets.amneziawg-ai.path;
|
|
};
|
|
|
|
# Its proxychains and TUN routes need the local proxy, which needs the
|
|
# AmneziaWG config.
|
|
perAppRouting = lib.mkIf hasAwgSecret {
|
|
enable = true;
|
|
createDefaultProfiles = true;
|
|
proxychains.enable = true;
|
|
tun.enable = true;
|
|
# No tproxy.enable: the per-app TProxy backend refuses to start under
|
|
# the global TProxy, and so does per-app zapret (it stays inactive
|
|
# while the global TProxy runs).
|
|
zapret.enable = true;
|
|
};
|
|
|
|
zapret = {
|
|
enable = true;
|
|
engine = "zapret2";
|
|
zapret2.strategySource = "z2k";
|
|
zapret-discord-youtube.configName = "general(ALT)";
|
|
};
|
|
|
|
tgWsProxy = {
|
|
enable = hasTgSecret;
|
|
secretFile = if hasTgSecret then config.age.secrets.tg-ws-proxy-secret.path else null;
|
|
bypassTransparentProxy = true;
|
|
};
|
|
|
|
gui.enable = true;
|
|
};
|
|
|
|
# The `ai` watchdog checks the tunnel with `curl --interface awg-ai`,
|
|
# unmarked, so TProxy marked it for the proxy and its misrouted guard
|
|
# dropped it: three misses in a row, and the watchdog reset the peer
|
|
# every five minutes. An unmarked packet already routed into the `ai`
|
|
# interface gets the proxy's own mark here, before TProxy's output chain
|
|
# (priority mangle), which lets that mark past. Drop the table once
|
|
# upstream marks the watchdog's probe.
|
|
networking.nftables.tables = lib.mkIf hasAwgSecret {
|
|
proxy-suite-ai-probe = {
|
|
family = "inet";
|
|
content = ''
|
|
chain output {
|
|
type route hook output priority mangle - 5; policy accept;
|
|
oifname "${cfg.amneziaWg.profiles.ai.interfaceName}" meta mark 0 meta mark set ${toString cfg.proxy.tproxy.proxyMark}
|
|
}
|
|
'';
|
|
};
|
|
};
|
|
|
|
systemd.services = lib.optionalAttrs hasAwgSecret {
|
|
# Split mode needs the `ai` interface up with TProxy. Nothing else
|
|
# starts it again after the full tunnel stopped it, and an `ai` that
|
|
# is down makes every tunnelled name fail ("no such network
|
|
# interface"). Wants=, not Requires=: a failed `ai` must not keep
|
|
# TProxy (and with it the rest of the traffic) down. After=: TProxy
|
|
# takes traffic once `ai` has had its handshake; on a dead tunnel that
|
|
# delays TProxy by the handshake wait (about 20 seconds), no longer.
|
|
proxy-suite-tproxy = {
|
|
wants = [ "proxy-suite-awg-ai.service" ];
|
|
after = [ "proxy-suite-awg-ai.service" ];
|
|
};
|
|
|
|
proxy-suite-awg-vpn = {
|
|
# proxy-suite stops the other global modes (TProxy, zapret) when a
|
|
# global profile starts, but not an "interface" outbound like `ai`.
|
|
# Both hold the same key and peer, so `ai` goes down first (After=
|
|
# makes the stop finish before `vpn` starts).
|
|
conflicts = [ "proxy-suite-awg-ai.service" ];
|
|
after = [ "proxy-suite-awg-ai.service" ];
|
|
# Not stopped and started again by a switch that changed it: the
|
|
# switch ends the full tunnel anyway (see the header), and a restart
|
|
# on top of that would bounce between the two modes.
|
|
restartIfChanged = false;
|
|
# Stopping the full tunnel (proxy-ctl awg off), or a start of it
|
|
# that failed, returns to split mode. After the unit's own stop
|
|
# commands (upstream's ExecStopPost, which clears the proxy bypass
|
|
# rules); the "-" ignores a failure, which must not mark the stop as
|
|
# failed.
|
|
serviceConfig.ExecStopPost = lib.mkAfter [ "-${splitRestore}" ];
|
|
};
|
|
};
|
|
|
|
warnings =
|
|
lib.optional (!hasTgSecret) ''
|
|
The Telegram WebSocket proxy (services.proxy-suite.tgWsProxy) is off:
|
|
secrets/tg-ws-proxy-secret.age is missing. Create it with the command in
|
|
secrets/README.md (32 random hex digits) and `git add` it.
|
|
''
|
|
++ lib.optional (!hasAwgSecret) ''
|
|
The AmneziaWG split tunnel for Claude and ChatGPT (services.proxy-suite.amneziaWg,
|
|
the global TProxy and perAppRouting) is off: secrets/amneziawg-ai.age is missing.
|
|
Encrypt the client .conf with the command in secrets/README.md and `git add` it.
|
|
'';
|
|
};
|
|
}
|