feat: helper functions; docs: more complex examples

This commit is contained in:
fufsob committed 2026-09-25 11:00:55 +00:00
1 parent 63d9dffcae
commit 878983147c
20 files changed
+1046 -19

No files matched your search

+174
View File
@@ -0,0 +1,174 @@
# Example: several exits, picked per site
One host with four ways out: direct (with zapret2 for DPI), a VPS, WARP and an SSH server.
Each site goes to whichever works best for it, and autoProxy covers the sites nobody
listed. Everything runs in global TUN mode, so every app on the host follows these rules.
```nix
{ ... }:
let
# Geosite "youtube" can miss the video and API hosts.
youtubeDomains = [
"googlevideo.com"
"gvt1.com"
"ytimg.com"
"ggpht.com"
"youtubei.googleapis.com"
];
# Telegram apps dial their data centres by IP, so names alone do not catch them
# (https://core.telegram.org/resources/cidr.txt).
telegramIps = [
"91.105.192.0/23"
"91.108.4.0/22"
"91.108.8.0/22"
"91.108.12.0/22"
"91.108.16.0/22"
"91.108.20.0/22"
"91.108.56.0/22"
"149.154.160.0/20"
"185.76.151.0/24"
"2001:67c:4e8::/48"
"2001:b28:f23c::/48"
"2001:b28:f23d::/48"
"2001:b28:f23f::/48"
"2a0a:f280::/32"
];
in
{
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ];
tun.enable = true;
autostart = "tun";
routing = {
# Unlisted sites leave directly, where zapret2 unblocks what DPI blocks.
default = "direct";
# Russian sites are routed by the last rule below instead.
directRu = false;
# Checked in order; the first match wins.
rules = [
# Sites that reject both this host's and the VPS's networks.
{
outbound = "ssh-proxy";
geosites = [ "pixiv" "twitter" ];
}
# Video throttled per connection by destination IP, which zapret cannot fix.
# One exit for the whole session: video URLs are bound to the IP that asked.
{
outbound = "warp";
domains = youtubeDomains;
geosites = [ "youtube" ];
}
# A CDN that stalls through WARP but runs at full speed directly.
{
outbound = "direct";
domains = [ "video-cdn.example" ];
}
# Russian services that refuse the VPS's foreign IP.
{
outbound = "warp";
geosites = [ "category-ru" ];
geoips = [ "ru" ];
}
];
# Through the selected outbound ("primary").
proxy = {
geosites = [ "openai" "anthropic" "telegram" "netflix" ];
ips = telegramIps;
# WARP registers through the proxy: its API is blocked directly.
domains = [ "api.cloudflareclient.com" ];
};
};
# For blocked sites no list names yet. YouTube is pinned to WARP above, so it
# is kept out of the probes.
autoProxy = {
enable = true;
exclude = [ "googlevideo.com" "gvt1.com" ];
};
};
warp = {
enable = true;
asOutbound = "singBox";
};
sshProxy = {
enable = true;
asOutbound = true;
server = {
user = "me";
host = "203.0.113.20";
port = 2222;
};
identityFile = "/run/secrets/proxy-ssh-key";
hostKeyFile = "/run/secrets/proxy-ssh-known-hosts";
};
# Tor through the proxy, since this network blocks Tor.
tor = {
enable = true;
asOutbound = true;
upstream = "proxy";
};
zapret = {
enable = true;
engine = "zapret2";
};
};
# autoProxy probes in bursts (seconds of CPU, a few hundred MB). On a busy host, keep
# it from competing with real work.
systemd.services.proxy-suite-autoproxy.serviceConfig = {
CPUWeight = 10;
IOWeight = 10;
Nice = 19;
MemoryHigh = "200M";
};
}
```
## Checking the result
```sh
proxy-ctl where youtube.com # -> warp, by the rule for youtube
proxy-ctl where chatgpt.com # -> proxy -> primary, by geosite openai
proxy-ctl proxy auto # what autoProxy routed, and through which exit
proxy-ctl zapret auto # what zapret2 learned
```
## With relay clients
On a [relay server](./relay-server.md) with `inbounds.routing.via = "proxy"`, clients follow
these rules too, with two exceptions. Russian sites are blocked for them (`blockRu`), and
sites on zapret's list go straight out, so this host's zapret handles them (`zapretDirect`).
Sites listed in `inbounds.routing.proxy` skip both exceptions and go to the rules above:
```nix no-check
services.proxy-suite.inbounds.routing = {
via = "proxy";
blockRu = false; # clients may reach Russian sites, through WARP by the rule above
proxy = {
domains = youtubeDomains; # on zapret's list, but WARP works better here
geosites = [ "youtube" "category-ru" ];
geoips = [ "ru" ];
};
};
```
The same list applies to listeners with their own `via`, including `"direct"` ones.
## Notes
- Rules take effect in order. Put narrow ones (a single CDN) before broad ones (a whole
country).
- Rules can name any outbound tag: yours, `warp`, `ssh-proxy`, `tor`, or an AmneziaWG
outbound.
- `.onion` names go to Tor without a rule.
+91
View File
@@ -0,0 +1,91 @@
# Example: a private VPN for your own devices
An AmneziaWG server for your laptop and phone. Unlike the proxy listeners, it also reaches
the server itself and its private services, like a home VPN. Internet traffic still follows
the server's routing (`inbounds.routing.via`), so the devices get the same exits and zapret
as the server.
A small DNS server on the tunnel address answers for private names, such as a mail server
that is only reachable over the VPN.
```nix
{ ... }:
let
interface = "awg0";
serverAddress = "10.77.77.1";
privateDomains = [ "mail.example.com" ];
in
{
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ];
};
inbounds = {
enable = true;
routing.via = "proxy";
listeners.vpn = {
type = "amneziawg";
port = 51820;
# Fixed addresses, so firewall rules and DNS can name each device. Keys are
# generated on first start.
users = [
{ name = "laptop"; address = "10.77.77.2"; }
{ name = "phone"; address = "10.77.77.3"; }
];
amneziaWg = {
# Devices also reach this host, its LAN and each other, directly.
mode = "lan";
interfaceName = interface;
subnet = "10.77.77.0/24";
subnet6 = "fd77:77:77::/64";
mtu = 1400;
dns = [ serverAddress ];
# Optional: a fixed server key, so client configs survive a reinstall.
privateKeyFile = "/run/secrets/awg-server-key";
};
};
};
};
# Private names for VPN clients; everything else is forwarded to 1.1.1.1.
services.dnsmasq = {
enable = true;
resolveLocalQueries = false;
settings = {
bind-dynamic = true;
interface = interface;
listen-address = serverAddress;
no-resolv = true;
server = [ "1.1.1.1" ];
address = map (domain: "/${domain}/${serverAddress}") privateDomains;
};
};
# The interface comes and goes with the listener.
systemd.services.dnsmasq = {
after = [ "proxy-suite-inbounds-awg.service" ];
wants = [ "proxy-suite-inbounds-awg.service" ];
};
}
```
## Adding a device
```sh
proxy-ctl inbounds link vpn phone --config --qr # scan in the AmneziaWG app
proxy-ctl inbounds link vpn laptop --config > laptop.conf
proxy-ctl inbounds link vpn phone # a vpn:// link instead
```
On a laptop that also runs proxy-suite, the `.conf` becomes a profile: see
[Use an AmneziaWG config](../amneziawg.md).
## Notes
- `mode = "lan"` turns on IP forwarding. The default, `"proxy"`, gives devices the
internet only, like the other listeners.
- Only TCP and UDP reach the internet; ping and other protocols stop at the server.
- A device that keeps its own private key can be added with `users.*.publicKey` instead;
it gets no generated config.
+200
View File
@@ -0,0 +1,200 @@
# Example: a relay server behind nginx
A VPS that already runs nginx for websites on port 443 also serves proxy clients: family
and friends, each with their own UUID and subscription. To a censor, the proxy traffic looks
like the site's own HTTPS. Clients get several transports, so each network can use
whichever one gets through.
| Listener | Reached as | Why |
|---|---|---|
| `xhttp` | `https://vpn.example.com/media` through nginx | Looks like ordinary HTTPS to the site |
| `ws` | `/media-ws` through nginx | For clients without XHTTP |
| `xhttp-stream` | `/media-stream` through nginx, over gRPC | HTTP/2 end to end, for stream-one mode |
| `xhttp-h3` | UDP 443, directly | HTTP/3 next to nginx, which holds TCP 443 |
| `reality` | TCP 2053, directly | REALITY with Vision, no HTTP layer to stall |
Client traffic exits through this host's own proxy (`routing.via = "proxy"`): here a single
upstream server, `primary`. With more exits, the server's routing decides where each site
goes; [Several exits, picked per site](./multi-exit.md) shows that.
```nix
{ config, lib, ... }:
let
domain = "vpn.example.com";
certDir = config.security.acme.certs.${domain}.directory;
users = map (name: { inherit name; uuidFile = "/run/secrets/proxy-uuid-${name}"; }) [
"alice"
"bob"
"carol"
];
# The listeners terminate TLS themselves, so share links honestly say security=tls.
tls = {
enable = true;
certificateFile = "${certDir}/fullchain.pem";
keyFile = "${certDir}/key.pem";
serverName = domain;
};
# nginx on 443 hands a path to a loopback listener.
fronted = port: path: extra: {
type = "vless";
inherit port users;
address = "127.0.0.1";
sharePort = 443; # share links name nginx's port, not the listener's
transport = extra // {
inherit path;
# nginx sets this header, so XRay sees each client's real address (for
# `proxy-ctl inbounds online`). A client cannot forge it: nginx overwrites it.
trustedXForwardedFor = [ "X-Real-IP" ];
};
inherit tls;
};
location = port: extra: {
proxyPass = "https://127.0.0.1:${toString port}";
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_ssl_server_name on;
proxy_ssl_name ${domain};
# Tunnels idle along with their users; the 60s default would cut them.
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
''
+ extra;
};
in
{
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
outbounds = [ { tag = "primary"; urlFile = "/run/secrets/proxy-primary-url"; } ];
};
inbounds = {
enable = true;
serverAddress = domain;
routing.via = "proxy";
listeners = {
# nginx speaks HTTP/1.1 to the listener, which only packet-up mode crosses.
xhttp = fronted 10003 "/media" {
type = "xhttp";
mode = "packet-up";
};
ws = fronted 10002 "/media-ws" { type = "ws"; };
# stream-one needs HTTP/2 all the way: nginx's grpc_pass provides it.
xhttp-stream = lib.recursiveUpdate (fronted 10004 "/media-stream" {
type = "xhttp";
mode = "stream-one";
}) { tls.alpn = [ "h2" ]; };
# HTTP/3 takes UDP 443 only, beside nginx on TCP 443. No trustedXForwardedFor:
# nothing is in front, so it already sees the real client.
xhttp-h3 = {
type = "vless";
port = 443;
inherit users;
transport = { type = "xhttp"; path = "/media-h3"; };
tls = tls // { alpn = [ "h3" ]; };
};
reality = {
type = "vless";
port = 2053;
inherit users;
flow = "xtls-rprx-vision";
reality = {
enable = true;
# Probes without the key land on a real TLS 1.3 + HTTP/2 site.
dest = "www.samsung.com:443";
serverNames = [ "www.samsung.com" ];
privateKeyFile = "/run/secrets/proxy-reality-key";
publicKey = "jNXH…";
shortIds = [ "0123abcd" ];
};
};
};
subscriptions = {
enable = true;
baseUrl = "https://${domain}/sub";
};
};
userControl.enable = true; # passwordless proxy-ctl for the admin, see below
};
users.users.admin.extraGroups = [ "proxy-suite" ];
security.acme = {
acceptTerms = true;
defaults.email = "admin@example.com";
# A renewed certificate restarts the listeners.
certs.${domain}.reloadServices = [ "proxy-suite-inbounds.service" ];
};
systemd.services.proxy-suite-inbounds = {
wants = [ "acme-${domain}.service" ];
after = [ "acme-${domain}.service" ];
};
services.nginx = {
enable = true;
virtualHosts.${domain} = {
forceSSL = true;
enableACME = true;
locations = {
# A prefix match: XHTTP appends /<session>/<seq>.
"/media" = location 10003 ''
proxy_http_version 1.1;
proxy_request_buffering off;
proxy_buffering off;
client_max_body_size 0;
'';
"= /media-ws" = location 10002 "" // { proxyWebsockets = true; };
"/media-stream".extraConfig = ''
grpc_pass grpcs://127.0.0.1:10004;
grpc_set_header X-Forwarded-For $remote_addr;
grpc_set_header X-Real-IP $remote_addr;
grpc_ssl_server_name on;
grpc_ssl_name ${domain};
grpc_read_timeout 3600s;
grpc_send_timeout 3600s;
client_max_body_size 0;
'';
# Subscriptions. Some clients show profile-title and follow profile-update-interval.
"/sub/".extraConfig = ''
alias /run/proxy-suite-inbounds/subscriptions/;
default_type text/plain;
autoindex off;
add_header profile-title "My VPN" always;
add_header profile-update-interval "12" always;
'';
};
};
};
}
```
## Handing out access
```sh
proxy-ctl inbounds sub alice --qr # alice's subscription: every listener, kept current
proxy-ctl inbounds link reality alice --qr
proxy-ctl inbounds online # who is connected, by their real address
proxy-ctl inbounds stats 7 --by user # traffic over the last week
```
To add a user, add a name and a UUID file, and rebuild. Existing links stay valid.
## Notes
- `sharePort = 443` puts nginx's port into share links, while the listener stays on
loopback, closed in the firewall.
- `trustedXForwardedFor` is safe only behind a proxy that overwrites the header, as nginx
does here. On a listener open to the world, clients could set it themselves.
- The certificate belongs to nginx. If the listeners cannot read it, they use a copy, and
`reloadServices` restarts them with the renewed one.
+90
View File
@@ -0,0 +1,90 @@
# Example: a workstation using your own server
The desktop side of the [relay server](./relay-server.md) and the
[private VPN](./private-vpn.md). Most apps go direct, with zapret for DPI. Blocked sites go
through the server. Some programs always use the proxy, and the private VPN is one
command away.
```nix
{ config, pkgs, ... }:
let
proxySuite = config.lib.proxy-suite;
in
{
services.proxy-suite = {
enable = true;
proxy = {
enable = true;
# The server's subscription: every listener, each a separate outbound.
subscriptions = [ { tag = "home"; urlFile = "/run/secrets/proxy-home-sub"; } ];
# The fastest listener that gets through the current network.
selection = "urltest";
urlTest.url = "https://telegram.org"; # blocked here, so a pass means it works
routing.default = "direct";
autoProxy.enable = true;
tun.enable = true; # when everything should go through: proxy-ctl proxy tun on
};
zapret = {
enable = true;
engine = "zapret2";
};
# The private VPN: `proxy-ctl awg on home`.
amneziaWg = {
enable = true;
profiles.home.configFile = "/run/secrets/home-vpn.conf";
};
killSwitch.enable = true;
perAppRouting = {
enable = true;
createDefaultProfiles = true;
proxychains.enable = true;
tun.enable = true;
};
gui.enable = true;
userControl.enable = true;
};
users.users.me.extraGroups = [ "proxy-suite" ];
environment.systemPackages = [
# CLIs that only work through the proxy, whatever the routing says.
(proxySuite.wrapEnv { protocol = "all"; } pkgs.codex)
(proxySuite.wrapEnv { } pkgs.claude-code)
# A browser for blocked sites, with its UDP (QUIC) too.
(proxySuite.wrapPerApp { profile = "tun"; } pkgs.chromium)
];
# Nix downloads through the proxy.
systemd.services.nix-daemon.environment = proxySuite.envFor "http";
}
```
## Day to day
```sh
proxy-ctl status # what runs, and which outbound is picked
proxy-ctl proxy outbounds test # which listeners get through right now
proxy-ctl where some-site.com # direct, zapret or proxy, and why
proxy-ctl proxy tun on # everything through the server for a while
proxy-ctl awg on home # or through the private VPN (stops TUN)
```
The tray app shows the same, and switches with a click.
## Notes
- `wrapEnv` sets the proxy variables for one program only, so the rest of the system keeps
its direct routing. `protocol = "all"` sets both kinds, for a program whose choice you
do not know.
- The kill switch guards whichever global tunnel is on (TUN or the VPN), and does nothing
while both are off.
- Per-app `tun` profiles refuse to start while global TUN is on. Everything already goes
through the proxy then.
- See [Wrap programs with the proxy](../wrap-apps.md) for the helpers, and
[Keep secrets out of the Nix store](../secrets.md) for the files under `/run/secrets`.