feat: better inbounds user management

This commit is contained in:
FUFSoB committed 2026-10-14 21:41:49 +05:00
1 parent 2f8414455d
commit 9f7ad91f39
47 files changed
+6767 -78

No files matched your search

+1
View File
@@ -9,6 +9,7 @@
./proxy-tun.nix
./proxy-tproxy.nix
./inbounds.nix
./inbounds-manager.nix
./per-app-routing.nix
./zapret.nix
./zapret2.nix
@@ -0,0 +1,215 @@
{ config, lib, ... }:
let
inherit (lib)
literalExpression
mkEnableOption
mkOption
types
;
cfg = config.services.proxy-suite.inbounds;
listenType = types.strMatching "([[][0-9A-Fa-f:.]+[]]|[0-9.]+|localhost):[0-9]{1,5}";
# scripts/inbound_manager.py's EVENTS.
events = [
"user.created"
"user.deleted"
"user.updated"
"user.enabled"
"user.disabled"
"user.reset"
"user.activated"
"user.traffic"
"user.expiring"
"user.ip-limit"
];
in
{
# Consumed by scripts/inbound_manager.py (manager.json, service/inbounds-manager.nix) and
# scripts/inbound_policy.py (the spec's "manager").
options.services.proxy-suite.inbounds.manager = {
enable = mkEnableOption "" // {
description = ''
The inbound manager, a daemon that keeps the users' policies (`inbounds.users.<name>.policy`,
and what it is told at runtime): who is switched off, whose time ran out, who used up
their traffic, how many addresses and how much speed each may have. It applies them to
the running inbounds without a restart, answers an HTTP+JSON API for proxy-ctl and bots,
serves subscriptions with the traffic and expiry clients show, and calls webhooks.
Users and listeners added through it need `inbounds.runtime`.
'';
};
interval = mkOption {
type = types.ints.between 5 3600;
default = 30;
description = "Seconds between two rounds of counting traffic and checking the policies.";
};
api = {
group = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
Group that may use the API socket, /run/proxy-suite-inbounds-manager/api.sock: all of
it, every user's secrets included. `null`: `userControl.group` when it has the
"inbounds" scope, root only otherwise.
'';
example = "vpn-admins";
};
listen = mkOption {
type = types.nullOr listenType;
default = null;
description = ''
Address and port the API also listens on over TCP, for a bot on another host. Plain
HTTP: put it on loopback behind a TLS proxy. Needs `tokensFile`; the firewall is not
opened for it.
'';
example = "127.0.0.1:9180";
};
tokensFile = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
File with the bearer tokens the TCP API takes, one per line, "name:token" or the token
alone. Read at start.
'';
example = "/run/secrets/inbounds-manager-tokens";
};
};
subscriptions = {
enable = mkOption {
type = types.bool;
default = cfg.shareLinks;
defaultText = literalExpression "config.services.proxy-suite.inbounds.shareLinks";
description = ''
Serve each user's subscription from /run/proxy-suite-inbounds-manager/sub.sock, with the
headers clients read their traffic, allowance and expiry from (subscription-userinfo),
a title and an update interval. The token is the last part of the path, the same
as the static files' (`inbounds.subscriptions`), so their URLs keep working:
```nix
services.nginx.virtualHosts."vpn.example.com".locations."/sub/".proxyPass =
"http://unix:/run/proxy-suite-inbounds-manager/sub.sock:";
```
'';
};
group = mkOption {
type = types.str;
default = cfg.subscriptions.group;
defaultText = literalExpression "config.services.proxy-suite.inbounds.subscriptions.group";
description = "Group of the web server that connects to the subscription socket.";
};
listen = mkOption {
type = types.nullOr listenType;
default = null;
description = "Address and port the subscriptions are also served on over TCP, for a web server that cannot reach the socket.";
example = "127.0.0.1:9181";
};
profileTitle = mkOption {
type = types.str;
default = "{user}";
description = "The profile's name in clients; {user} is the user's.";
example = "My VPN ({user})";
};
updateInterval = mkOption {
type = types.nullOr types.ints.positive;
default = 12;
description = "Hours between clients' updates of the subscription (profile-update-interval).";
};
supportUrl = mkOption {
type = types.nullOr types.str;
default = null;
description = "Where clients send users for help (support-url), such as a Telegram link.";
example = "https://t.me/my_vpn_support";
};
webPageUrl = mkOption {
type = types.nullOr types.str;
default = null;
description = "The service's page (profile-web-page-url).";
};
};
speedLimits = {
enable = mkEnableOption "" // {
description = ''
Speed limits (`policy.speedLimit`). XRay marks the sockets its exits dial for a limited
user with the user's slot, which proxy-suite's XRay build carries a patch for
(pkgs/patches/xray-user-level-mark.patch), and nftables polices each mark's
traffic both ways. AmneziaWG peers are policed by their tunnel address. Needs root.
'';
};
slots = mkOption {
type = types.ints.between 1 65535;
default = 512;
description = "How many users may have a speed limit at once: an XRay user level each.";
};
};
deleteExpiredAfter = mkOption {
type = types.nullOr (types.strMatching "[0-9]+[mhdw]");
default = null;
description = ''
Delete runtime users this long after their `expiresAt`, as `proxy-ctl inbounds users rm`
would: their secrets, policy and subscription go. A number with m, h, d or w. Users
declared in the config stay. `null`: never.
'';
example = "30d";
};
ipLimit.banTime = mkOption {
type = types.ints.positive;
default = 600;
description = "Seconds an address past a user's `ipLimit` is banned for.";
};
notify = {
trafficPercent = mkOption {
type = types.listOf (types.ints.between 1 100);
default = [
80
100
];
description = "Shares of the traffic allowance used that send a user.traffic event, once per period.";
};
expiryDays = mkOption {
type = types.listOf types.ints.positive;
default = [
3
1
];
description = "Days before the expiry that send a user.expiring event.";
};
};
webhooks = mkOption {
type = types.listOf (
types.submodule {
options = {
url = mkOption {
type = types.str;
description = "URL the events are POSTed to as JSON: {event, user, at, data}.";
};
events = mkOption {
type = types.listOf (types.enum events);
default = [ ];
description = "Events to send. Empty: all of them.";
};
secretFile = mkOption {
type = types.nullOr types.str;
default = null;
description = "File with a secret: each body is signed, X-Proxy-Suite-Signature: sha256=<HMAC-SHA256 hex>.";
};
};
}
);
default = [ ];
description = ''
Where to tell about users created, deleted, updated, switched off (with why) and back
on, their traffic reset, their term started on their first connection (user.activated), thresholds of their allowance, coming expiries and addresses
banned past their ipLimit. Retried a few times; a hook that stays down misses them.
'';
example = literalExpression ''
[ { url = "http://127.0.0.1:8088/proxy-suite"; secretFile = "/run/secrets/hook"; } ]
'';
};
};
}
+37
View File
@@ -106,6 +106,24 @@ in
description = "Public URL of the subscription directory, used by `proxy-ctl inbounds sub`.";
example = "https://vpn.example.com/sub";
};
keyFile = mkOption {
type = types.nullOr (types.either (types.enum [ false ]) types.str);
default = null;
description = ''
The key the subscription tokens are an HMAC under. Changing it changes every user's
URL.
- `null`: generated on first start and kept in the state directory
(inbounds/subscription-key), where `proxy-ctl inbounds export` takes it from.
- a file with a secret, such as `openssl rand -hex 32`: that one, read at start and
at each runtime change.
- `false`: no key. A token is a hash of the user's name and credentials alone, as
before there was a key: whoever learns one of a user's links can work out their
subscription URL, and from it every other link of theirs. For a server that gave
URLs out before.
'';
example = "/run/secrets/inbounds-subscription-key";
};
};
# Mirrors proxy.routing, for traffic arriving from outside instead of from this host.
@@ -132,6 +150,25 @@ in
addresses (loopback, and its public ones as its interfaces carry them at start) are
reachable only on the listener ports and `serverPorts`, whichever `via` a client takes.
'';
blockTorrent = bool true ''
Block BitTorrent: connections XRay's sniffing recognizes by the peers' handshake, and µTP.
Not trackers or DHT, nor what a client encrypts (MSE): it makes downloading slow, not
impossible. Hosters act on the copyright notices torrents bring. Listeners given as raw
XRay JSON need sniffing on for it.
'';
blockPorts = mkOption {
type = types.listOf (types.either types.port (types.strMatching "[0-9]+-[0-9]+"));
default = [ 25 ];
description = ''
Destination ports clients may not reach, TCP and UDP: single ports and "from-to"
ranges. Port 25 by default: mail sent straight to other servers, which spam goes
out by and hosters close. Mail clients' own ports (465, 587) stay open.
'';
example = [
25
"6881-6889"
];
};
zapretDirect = bool true "Send zapret hostlist sites direct, so this host's zapret unblocks them. Only for the default `via`. With a direct listener or the XRay backend, only by IP: XRay matches names by what a client puts in its handshake, whatever address it connects to.";
@@ -35,6 +35,86 @@ let
AmneziaWG tunnel IPv4 address, inside `amneziaWg.subnet`. `null`: the lowest free one,
kept for as long as the user exists.
'' "10.66.0.10";
# Read by scripts/inbound_policy.py, with inbounds.manager. What the manager is told at
# runtime (`proxy-ctl inbounds users set`, its API) overrides these field by field.
policy = {
enabled = mkOption {
type = types.bool;
default = true;
description = "Whether the user may connect. Needs `inbounds.manager`, as every `policy` field does.";
};
expiresAt = mkOption {
type = types.nullOr (
types.either types.ints.unsigned (
types.strMatching "[0-9]{4}-[0-9]{2}-[0-9]{2}([T ][0-9:.]+([+-][0-9:]+|Z)?)?"
)
);
default = null;
description = ''
When the user's access ends: a date (the end of that day, local time), an ISO time,
or a Unix time. Their connections are cut then, and their subscription still says
when it ended.
'';
example = "2026-12-31";
};
expiresAfter = mkOption {
type = types.nullOr (types.either types.ints.positive (types.strMatching "[0-9]+[mhdw]"));
default = null;
description = ''
A term that starts with the user's first connection, for access sold ahead: seconds,
or a number with m, h, d or w. Until they connect the user is on hold, and when
they do `expiresAt` is set to that much later. An `expiresAt` given wins.
'';
example = "30d";
};
trafficLimit = mkOption {
type = types.nullOr (
types.either types.ints.unsigned (types.strMatching "[0-9]+(\\.[0-9]+)?[KMGTP]?(i?B)?")
);
default = null;
description = ''
Traffic allowance, up and down together: bytes, or with K, M, G, T (binary units, as
clients show them). Past it the user is switched off until the period renews or the
traffic is reset.
'';
example = "50G";
};
trafficPeriod = mkOption {
type = types.nullOr (
types.enum [
"daily"
"weekly"
"monthly"
]
);
default = null;
description = ''
How often the allowance renews, at local midnight: daily, weekly, or monthly on the
day the count began. `null`: never; only a reset starts it over.
'';
};
ipLimit = mkOption {
type = types.nullOr types.ints.positive;
default = null;
description = ''
How many addresses may use the user at once. The ones past it that came last are
banned for `inbounds.manager.ipLimit.banTime` from the user's ports. Not on
AmneziaWG, whose peer has one address anyway, nor behind a PROXY-protocol front.
'';
example = 2;
};
speedLimit = mkOption {
type = types.nullOr (types.strMatching "[0-9]+(\\.[0-9]+)?[kKMG]?(/[0-9]+(\\.[0-9]+)?[kKMG]?)?");
default = null;
description = ''
Speed limit in bits per second, decimal (k, M, G): one rate for both ways, or
"down/up". Needs `inbounds.manager.speedLimits.enable`. On socks and http listeners
it does not apply.
'';
example = "20M/5M";
};
};
};
};