feat: tor support
This commit is contained in:
42 files changed
+1853
-69
No files matched your search
@@ -38,7 +38,12 @@ let
|
||||
localDetour ? null,
|
||||
useOutboundRoutingMark ? false,
|
||||
fakeIp ? false,
|
||||
# Inbounds whose clients look names up in sing-box: .onion ones get a fake address there.
|
||||
onionFakeIpInbounds ? [ ],
|
||||
}:
|
||||
let
|
||||
onionFakeIp = derived.torRouteOnion && onionFakeIpInbounds != [ ];
|
||||
in
|
||||
{
|
||||
servers = [
|
||||
(mkDnsServer "remote" proxyCfg.dns.remote "proxy")
|
||||
@@ -52,7 +57,7 @@ let
|
||||
}
|
||||
// lib.optionalAttrs useOutboundRoutingMark { routing_mark = globalTproxy.proxyMark; }
|
||||
) derived.awgInterfaceOutbounds
|
||||
++ lib.optional fakeIp {
|
||||
++ lib.optional (fakeIp || onionFakeIp) {
|
||||
tag = "fakeip";
|
||||
type = "fakeip";
|
||||
inet4_range = proxyCfg.dns.fakeIp.inet4Range;
|
||||
@@ -61,6 +66,26 @@ let
|
||||
# The route mode keeps the user's rules and fake IP, and may drop what sits between.
|
||||
rules =
|
||||
proxyCfg.dns.singBox.rules
|
||||
# No resolver knows .onion, and asking one leaks the name. A transparent client gets a
|
||||
# fake address that routes to Tor by name; everything else is told there is no such name.
|
||||
++ lib.optionals derived.torRouteOnion (
|
||||
lib.optional onionFakeIp {
|
||||
inbound = onionFakeIpInbounds;
|
||||
domain_suffix = [ "onion" ];
|
||||
query_type = [
|
||||
"A"
|
||||
"AAAA"
|
||||
];
|
||||
server = "fakeip";
|
||||
}
|
||||
++ [
|
||||
{
|
||||
domain_suffix = [ "onion" ];
|
||||
action = "predefined";
|
||||
rcode = "NXDOMAIN";
|
||||
}
|
||||
]
|
||||
)
|
||||
++ rules.singBoxDnsRules
|
||||
# Only what apps ask through the TUN: the XRay sidecar's and sing-box's own lookups
|
||||
# need real addresses.
|
||||
@@ -126,6 +151,9 @@ let
|
||||
dns = mkDnsConfig {
|
||||
localDetour = if forceLocalDnsViaProxy then "proxy" else null;
|
||||
inherit useOutboundRoutingMark fakeIp;
|
||||
onionFakeIpInbounds =
|
||||
lib.optional enableTun "tun-in"
|
||||
++ lib.optionals enableTProxy ([ "tproxy-in" ] ++ lib.optional proxyCfg.ipv6 "tproxy-in6");
|
||||
};
|
||||
|
||||
inbounds =
|
||||
|
||||
@@ -171,12 +171,30 @@ let
|
||||
tag = "dns-out";
|
||||
settings = {
|
||||
userLevel = 0;
|
||||
rules = [
|
||||
{
|
||||
action = "direct";
|
||||
qType = "2-27,29-65535";
|
||||
}
|
||||
];
|
||||
# No resolver knows .onion, and asking one leaks the name. The TUN's fake DNS
|
||||
# answers with an address that routes to Tor by name; anything else is told there
|
||||
# is no such name.
|
||||
rules =
|
||||
lib.optionals derived.torRouteOnion (
|
||||
lib.optional enableTunFakeDns {
|
||||
action = "hijack";
|
||||
qType = "1,28";
|
||||
domain = [ "domain:onion" ];
|
||||
}
|
||||
++ [
|
||||
{
|
||||
action = "return";
|
||||
rCode = 3;
|
||||
domain = [ "domain:onion" ];
|
||||
}
|
||||
]
|
||||
)
|
||||
++ [
|
||||
{
|
||||
action = "direct";
|
||||
qType = "2-27,29-65535";
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
|
||||
@@ -26,7 +26,7 @@ let
|
||||
|
||||
inboundRules = import ./rules/proxy-inbounds.nix {
|
||||
inherit lib;
|
||||
inherit (derived) proxyInboundsCfg proxyInbounds;
|
||||
inherit (derived) proxyInboundsCfg proxyInbounds proxyInboundsRouteOnion;
|
||||
inherit (rules) zapretDirectRules;
|
||||
};
|
||||
|
||||
@@ -40,6 +40,8 @@ let
|
||||
proxyInboundsSpec = {
|
||||
serverAddress = derived.proxyInboundsCfg.serverAddress;
|
||||
shareLinks = derived.proxyInboundsCfg.shareLinks;
|
||||
# Listeners the onion service carries, which get a second set of links to it.
|
||||
onionListeners = map (ib: ib.tag) derived.torOnionInbounds;
|
||||
listeners = map (
|
||||
ib:
|
||||
{
|
||||
|
||||
@@ -157,6 +157,17 @@ in
|
||||
}
|
||||
))
|
||||
|
||||
(lib.mkIf cfg.tor.enable (
|
||||
import ./tor.nix {
|
||||
inherit
|
||||
lib
|
||||
pkgs
|
||||
cfg
|
||||
derived
|
||||
;
|
||||
}
|
||||
))
|
||||
|
||||
(lib.mkIf (derived.proxyInboundsEnabled && derived.proxyInboundsAwg != [ ]) (
|
||||
import ./amnezia-wg-inbounds.nix {
|
||||
inherit
|
||||
|
||||
@@ -49,6 +49,14 @@ let
|
||||
warpOutboundTag = "warp";
|
||||
# The sing-box tunnel; "userspace" and "interface" come in through the AmneziaWG profile list.
|
||||
warpOutboundEnabled = warpCfg.enable && warpCfg.asOutbound == "singBox";
|
||||
torCfg = cfg.tor // {
|
||||
# tor's DataDirectory; the onion service keeps its keys and hostname under it.
|
||||
dataDir = "${stateDir}/tor";
|
||||
onionDir = "${stateDir}/tor/onion";
|
||||
};
|
||||
torOutboundTag = "tor";
|
||||
torOutboundEnabled = torCfg.enable && torCfg.asOutbound;
|
||||
torRouteOnion = torOutboundEnabled && torCfg.routeOnion && proxyEnabled;
|
||||
|
||||
# Two ports per "singBox" or "userspace" AmneziaWG outbound, above the autoProxy prober's own
|
||||
# listeners (proxy.autoProxy.probeBasePort, 18540 by default, one per exit):
|
||||
@@ -87,10 +95,17 @@ let
|
||||
via = if listener.via == null then proxyInboundsCfg.routing.via else listener.via;
|
||||
}) proxyInboundsCfg.listeners;
|
||||
|
||||
# .onion names from clients go to the local proxy, whose rule hands them to Tor, whatever
|
||||
# the listener's via. AmneziaWG listeners never pass XRay's routing.
|
||||
proxyInboundsRouteOnion =
|
||||
torRouteOnion
|
||||
&& lib.any (ib: ib.via != "block" && ib.listener.type != "amneziawg") proxyInbounds;
|
||||
|
||||
# Whether any listener or inbounds.routing.proxy exception relays through the local SOCKS
|
||||
# listener.
|
||||
proxyInboundsNeedLocalProxy =
|
||||
lib.any (ib: ib.via == "proxy") proxyInbounds
|
||||
|| proxyInboundsRouteOnion
|
||||
|| lib.any (field: proxyInboundsCfg.routing.proxy.${field} != [ ]) [
|
||||
"domains"
|
||||
"ips"
|
||||
@@ -170,6 +185,17 @@ let
|
||||
# h3-only xhttp listeners are UDP-only, leaving the TCP port to a web server.
|
||||
proxyInboundIsH3Only =
|
||||
l: l.type != null && l.transport.type == "xhttp" && l.tls.enable && l.tls.alpn == [ "h3" ];
|
||||
# What an onion service can carry: TCP to a listener with a known protocol.
|
||||
proxyInboundOnionCapable =
|
||||
ib: ib.listener.type != null && ib.listener.type != "amneziawg" && !proxyInboundIsH3Only ib.listener;
|
||||
torOnionEnabled = torCfg.enable && torCfg.onionService.enable && proxyInboundsEnabled;
|
||||
torOnionInbounds =
|
||||
if !torOnionEnabled then
|
||||
[ ]
|
||||
else if torCfg.onionService.listeners == null then
|
||||
builtins.filter proxyInboundOnionCapable proxyInbounds
|
||||
else
|
||||
builtins.filter (ib: builtins.elem ib.tag torCfg.onionService.listeners) proxyInbounds;
|
||||
proxyInboundFirewallPorts = lib.unique (
|
||||
map (ib: ib.listener.port) (
|
||||
builtins.filter (
|
||||
@@ -200,6 +226,7 @@ let
|
||||
outboundTags
|
||||
++ lib.optional sshProxyOutboundEnabled sshProxyOutboundTag
|
||||
++ lib.optional warpOutboundEnabled warpOutboundTag
|
||||
++ lib.optional torOutboundEnabled torOutboundTag
|
||||
++ map (ob: ob.tag) awgOutbounds;
|
||||
subscriptionTags = map (sub: sub.tag) proxyCfg.subscriptions;
|
||||
|
||||
@@ -210,6 +237,7 @@ let
|
||||
|| hasSubscriptions
|
||||
|| sshProxyOutboundEnabled
|
||||
|| warpOutboundEnabled
|
||||
|| torOutboundEnabled
|
||||
|| awgOutbounds != [ ];
|
||||
collapseNamedOutbounds = selectionMode == "first";
|
||||
# Always on with sing-box: `proxy-ctl proxy outbounds test` needs it in every selection mode.
|
||||
@@ -318,6 +346,10 @@ let
|
||||
# Loopback listener behind the selector `proxy-ctl proxy outbounds test` switches.
|
||||
outboundTestPort = 18537;
|
||||
inboundStatsFile = "${stateDir}/inbound-stats.json";
|
||||
# Written by proxy-suite-tor as it starts, before it reaches the network.
|
||||
torOnionHostnameFile = "${stateDir}/tor/onion/hostname";
|
||||
# proxy-ctl tor status|newnym.
|
||||
torControlSocket = "${runtimeDir}/proxy-suite-tor/control/socket";
|
||||
|
||||
# sing-box DNS server resolving through an "interface" AmneziaWG outbound.
|
||||
awgDnsServerTag = tag: "awg-dns-${tag}";
|
||||
@@ -401,6 +433,13 @@ in
|
||||
warpCfg
|
||||
warpOutboundTag
|
||||
warpOutboundEnabled
|
||||
torCfg
|
||||
torOutboundTag
|
||||
torOutboundEnabled
|
||||
torRouteOnion
|
||||
torOnionEnabled
|
||||
torOnionInbounds
|
||||
proxyInboundOnionCapable
|
||||
awgGlobalProfiles
|
||||
awgOutbounds
|
||||
awgInterfaceOutbounds
|
||||
@@ -409,6 +448,7 @@ in
|
||||
proxyInboundsEnabled
|
||||
proxyInbounds
|
||||
proxyInboundsAwg
|
||||
proxyInboundsRouteOnion
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundsResolveInSingBox
|
||||
proxyInboundsGuardPrivate
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
./amnezia-wg.nix
|
||||
./ssh-proxy.nix
|
||||
./warp.nix
|
||||
./tor.nix
|
||||
./tg-ws-proxy.nix
|
||||
./geodata.nix
|
||||
./gui.nix
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
{ lib, pkgs, ... }:
|
||||
|
||||
let
|
||||
inherit (lib)
|
||||
literalExpression
|
||||
mkEnableOption
|
||||
mkOption
|
||||
types
|
||||
;
|
||||
package =
|
||||
name: description:
|
||||
mkOption {
|
||||
type = types.package;
|
||||
default = pkgs.${name};
|
||||
defaultText = literalExpression "pkgs.${name}";
|
||||
inherit description;
|
||||
};
|
||||
in
|
||||
{
|
||||
options.services.proxy-suite.tor = {
|
||||
enable = mkEnableOption "the Tor daemon (proxy-suite-tor)";
|
||||
|
||||
package = package "tor" "Tor package.";
|
||||
|
||||
lyrebirdPackage = package "lyrebird" "Pluggable transport for obfs4, webtunnel and meek_lite bridges.";
|
||||
|
||||
snowflakePackage = package "snowflake" "Pluggable transport for snowflake bridges.";
|
||||
|
||||
asOutbound = mkOption {
|
||||
type = types.bool;
|
||||
default = false;
|
||||
description = ''
|
||||
Add Tor as an outbound tagged "tor": a SOCKS hop to proxy-suite-tor on 127.0.0.1:socksPort,
|
||||
which receives names unresolved. proxy.selection and autoProxy leave it alone unless it is the
|
||||
only outbound; route to it with proxy.routing.rules or name it as a detour. Requires proxy.enable.
|
||||
'';
|
||||
};
|
||||
|
||||
routeOnion = mkOption {
|
||||
type = types.bool;
|
||||
default = true;
|
||||
description = ''
|
||||
Send .onion names to the "tor" outbound in every route mode, and keep them away from DNS:
|
||||
in TUN and TProxy modes sing-box answers them with a fake address it maps back to the name,
|
||||
and XRay drops the lookup. Inbound clients' .onion names go the same way, through the local
|
||||
proxy, whatever their listener's via (except "block"). Applies with asOutbound.
|
||||
'';
|
||||
};
|
||||
|
||||
clientOnly = mkOption {
|
||||
type = types.bool;
|
||||
default = true;
|
||||
description = ''
|
||||
Keep Tor a client (ClientOnly 1): never a relay, exit, bridge or directory server, even when
|
||||
extraConfig sets an ORPort. The onion service works either way. Turn off only to run a relay
|
||||
through extraConfig.
|
||||
'';
|
||||
};
|
||||
|
||||
upstream = mkOption {
|
||||
type = types.enum [
|
||||
"direct"
|
||||
"proxy"
|
||||
];
|
||||
default = "direct";
|
||||
description = ''
|
||||
How Tor reaches its relays or bridges.
|
||||
- "direct": from the uplink, past TUN and TProxy (it runs as proxy-suite-daemon).
|
||||
- "proxy": through the local proxy listener (proxy.listener, with its auth), for a network
|
||||
that blocks Tor. obfs4, webtunnel and meek_lite bridges follow it; snowflake cannot.
|
||||
'';
|
||||
};
|
||||
|
||||
socksPort = mkOption {
|
||||
type = types.port;
|
||||
default = 18530;
|
||||
description = "Loopback SOCKS port of proxy-suite-tor, which the tor outbound dials.";
|
||||
};
|
||||
|
||||
bridges = {
|
||||
lines = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
description = ''
|
||||
Bridge lines, as https://bridges.torproject.org hands them out, without the leading
|
||||
"Bridge". Any given turns on UseBridges; obfs4, webtunnel, meek_lite and snowflake
|
||||
transports are run as needed.
|
||||
'';
|
||||
example = [
|
||||
"obfs4 192.0.2.1:443 0123456789ABCDEF0123456789ABCDEF01234567 cert=... iat-mode=0"
|
||||
];
|
||||
};
|
||||
|
||||
file = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
description = "Runtime path to more bridge lines, one per line; blank lines and # comments are skipped.";
|
||||
example = "/run/secrets/tor-bridges";
|
||||
};
|
||||
};
|
||||
|
||||
onionService = {
|
||||
enable = mkEnableOption "an onion service in front of inbounds.listeners";
|
||||
|
||||
listeners = mkOption {
|
||||
type = types.nullOr (types.listOf types.str);
|
||||
default = null;
|
||||
description = ''
|
||||
inbounds.listeners served at the onion address, each on its share port. Null takes every
|
||||
listener a TCP-only onion can carry: not amneziawg, h3-only xhttp or raw JSON.
|
||||
|
||||
Share links for them (`proxy-ctl inbounds link TAG --onion`, and subscriptions) dial the
|
||||
.onion address and keep the listener's TLS and REALITY names. Clients reach XRay from
|
||||
127.0.0.1, so `proxy-ctl inbounds online` does not list them.
|
||||
'';
|
||||
example = [ "vless-reality" ];
|
||||
};
|
||||
|
||||
secretKeyFile = mkOption {
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
description = ''
|
||||
Runtime path to an hs_ed25519_secret_key, to keep an onion address. Null lets Tor create
|
||||
one in /var/lib/proxy-suite/tor/onion.
|
||||
'';
|
||||
example = "/run/secrets/tor-hs_ed25519_secret_key";
|
||||
};
|
||||
};
|
||||
|
||||
extraConfig = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
description = "Lines appended to the generated torrc.";
|
||||
example = ''
|
||||
ExitNodes {de},{nl}
|
||||
StrictNodes 1
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -29,7 +29,7 @@ in
|
||||
default = [ ];
|
||||
description = ''
|
||||
What userControl.group may do; empty allows every scope.
|
||||
- "services": turn the proxy-suite units on and off (proxy, tun, tproxy, zapret, ssh, warp, tg, awg, inbounds), and `restart`.
|
||||
- "services": turn the proxy-suite units on and off (proxy, tun, tproxy, zapret, ssh, warp, tor, tg, awg, inbounds), `restart`, and `tor status|newnym` on Tor's control socket.
|
||||
- "perApp": the per-app backend units used by `proxy-ctl apps run`.
|
||||
- "routing": `proxy pin`, `proxy unpin` and `proxy mode`.
|
||||
- "outbounds": add and remove runtime outbounds and subscriptions, disable and enable outbounds, and update subscriptions.
|
||||
|
||||
@@ -60,6 +60,9 @@ let
|
||||
}
|
||||
) proxyCfg.outbounds;
|
||||
|
||||
# .onion names go to Tor in every route mode: nothing else can reach them.
|
||||
onionOutbound = if derived.torRouteOnion then resolveTag derived.torOutboundTag else null;
|
||||
|
||||
# All custom rules in priority order: per-outbound first, then explicit routing.rules.
|
||||
customRules =
|
||||
perOutboundRules ++ map (rule: rule // { outbound = resolveTag rule.outbound; }) r.rules;
|
||||
@@ -72,6 +75,7 @@ let
|
||||
tgWsProxyCfg
|
||||
customRules
|
||||
customRuleCategory
|
||||
onionOutbound
|
||||
;
|
||||
inherit (cfg) geodata;
|
||||
};
|
||||
@@ -91,6 +95,7 @@ let
|
||||
tgWsProxyCfg
|
||||
customRules
|
||||
customRuleCategory
|
||||
onionOutbound
|
||||
;
|
||||
inherit (derived) selectionMode;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
lib,
|
||||
proxyInboundsCfg,
|
||||
proxyInbounds,
|
||||
proxyInboundsRouteOnion,
|
||||
zapretDirectRules,
|
||||
}:
|
||||
|
||||
@@ -17,6 +18,18 @@ let
|
||||
# A blocked listener stays blocked: neither serverAddress nor the proxy exceptions open it.
|
||||
exceptionInboundTags = map (ib: ib.tag) (builtins.filter (ib: ib.via != "block") proxyInbounds);
|
||||
|
||||
# Only Tor reaches .onion: to the local proxy, whose own rule sends it there. First, so no
|
||||
# IP rule before it has XRay look the name up, which would leak it to a resolver.
|
||||
onionRule = lib.optional proxyInboundsRouteOnion {
|
||||
type = "field";
|
||||
ruleTag = "inbound-tor-onion";
|
||||
domain = [ "domain:onion" ];
|
||||
inboundTag = map (ib: ib.tag) (
|
||||
builtins.filter (ib: ib.via != "block" && ib.listener.type != "amneziawg") proxyInbounds
|
||||
);
|
||||
outboundTag = "proxy";
|
||||
};
|
||||
|
||||
blockPrivateRule = lib.optional proxyInboundsCfg.routing.blockPrivate {
|
||||
type = "field";
|
||||
ruleTag = "inbound-block-private";
|
||||
@@ -147,7 +160,8 @@ let
|
||||
# Explicit proxy exceptions beat blockRu: RU-geolocated ranges (Telegram's,
|
||||
# at times) would otherwise swallow them.
|
||||
xrayInboundRules =
|
||||
blockPrivateRule
|
||||
onionRule
|
||||
++ blockPrivateRule
|
||||
++ serverAddressRule
|
||||
++ proxyDomainRule
|
||||
++ proxyIpRule
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
tgWsProxyCfg,
|
||||
customRules,
|
||||
customRuleCategory,
|
||||
onionOutbound,
|
||||
geodata,
|
||||
}:
|
||||
|
||||
@@ -60,7 +61,11 @@ let
|
||||
action = "hijack-dns";
|
||||
}
|
||||
{ action = "sniff"; }
|
||||
];
|
||||
]
|
||||
++ lib.optional (onionOutbound != null) {
|
||||
domain_suffix = [ "onion" ];
|
||||
outbound = onionOutbound;
|
||||
};
|
||||
|
||||
customRouteRules = map (rule: {
|
||||
category = customRuleCategory rule.outbound;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
tgWsProxyCfg,
|
||||
customRules,
|
||||
customRuleCategory,
|
||||
onionOutbound,
|
||||
selectionMode,
|
||||
}:
|
||||
|
||||
@@ -116,8 +117,14 @@ let
|
||||
(xrayGeoIPRules "proxy-geoip" "proxy" r.proxy.geoips)
|
||||
];
|
||||
|
||||
# Kept by every route mode, like sing-box's common rules.
|
||||
xrayCommonRules = lib.optional (onionOutbound != null) (
|
||||
mkXrayRule "tor-onion" { domain = [ "domain:onion" ]; } onionOutbound
|
||||
);
|
||||
|
||||
xrayRoutingRules =
|
||||
lib.concatMap (item: item.entries) xrayCustomRouteRules
|
||||
xrayCommonRules
|
||||
++ lib.concatMap (item: item.entries) xrayCustomRouteRules
|
||||
++ xrayProxyPrimaryRules
|
||||
++ xrayDirectRules
|
||||
++ xraySafetyDirectRules
|
||||
@@ -125,7 +132,7 @@ let
|
||||
++ xrayBlockRules;
|
||||
|
||||
xrayRouteModeRules = {
|
||||
common = [ ];
|
||||
common = xrayCommonRules;
|
||||
custom = xrayCustomRouteRules;
|
||||
proxyPrimary = xrayProxyPrimaryRules;
|
||||
direct = xrayDirectRules;
|
||||
|
||||
@@ -72,6 +72,7 @@ let
|
||||
ob.tunnelPort
|
||||
ob.directPort
|
||||
]) derived.awgTunnelOutbounds
|
||||
++ lib.optional derived.torOutboundEnabled derived.torCfg.socksPort
|
||||
++ lib.optionals derived.proxyInboundsEnabled (
|
||||
map (listener: listener.internalPort) derived.proxyInboundsAwg
|
||||
);
|
||||
@@ -350,7 +351,7 @@ let
|
||||
|
||||
proxyInboundAssertions = [
|
||||
(mkAssertion (!proxyInboundsEnabled || derived.invalidInboundViaTargets == [ ])
|
||||
"proxy-suite: inbounds via targets, or the hops they chain through, are not defined in proxy.outbounds: ${lib.concatStringsSep ", " derived.invalidInboundViaTargets}. Subscription proxies, warp, ssh-proxy and AmneziaWG outbounds cannot be named here because the inbound service does not run them; use via = \"proxy\" to reach those."
|
||||
"proxy-suite: inbounds via targets, or the hops they chain through, are not defined in proxy.outbounds: ${lib.concatStringsSep ", " derived.invalidInboundViaTargets}. Subscription proxies, warp, tor, ssh-proxy and AmneziaWG outbounds cannot be named here because the inbound service does not run them; use via = \"proxy\" to reach those."
|
||||
)
|
||||
(mkAssertion (!proxyInboundsEnabled || singBoxOnlyPinnedTags == [ ])
|
||||
"proxy-suite: inbounds via targets a sing-box-only outbound: ${lib.concatStringsSep ", " singBoxOnlyPinnedTags}. The inbound service runs XRay, so a pinned outbound needs url, urlFile, or xrayJson."
|
||||
@@ -928,9 +929,74 @@ let
|
||||
message = "proxy-suite: tgWsProxy.fwmark must not use per-app-zapret internal desync mark bits";
|
||||
}
|
||||
];
|
||||
|
||||
torCfg = cfg.tor;
|
||||
torOnionListeners = if torCfg.onionService.listeners == null then [ ] else torCfg.onionService.listeners;
|
||||
torOnionUnfit = map (ib: ib.tag) (
|
||||
builtins.filter (ib: !derived.proxyInboundOnionCapable ib) derived.torOnionInbounds
|
||||
);
|
||||
torOnionVirtualPorts = map (
|
||||
ib: if ib.listener.sharePort != null then ib.listener.sharePort else ib.listener.port
|
||||
) derived.torOnionInbounds;
|
||||
torSnowflake = lib.any (line: lib.hasPrefix "snowflake " line) torCfg.bridges.lines;
|
||||
torAssertions = [
|
||||
(mkAssertion (!torCfg.enable || torCfg.asOutbound || torCfg.onionService.enable)
|
||||
"proxy-suite: tor.enable = true needs tor.asOutbound or tor.onionService.enable"
|
||||
)
|
||||
(requireEnabled (torCfg.enable && torCfg.asOutbound) proxyEnabled
|
||||
"proxy-suite: tor.asOutbound requires proxy.enable = true"
|
||||
)
|
||||
(requireEnabled (torCfg.enable && torCfg.upstream == "proxy") proxyEnabled
|
||||
''proxy-suite: tor.upstream = "proxy" requires proxy.enable = true''
|
||||
)
|
||||
(mkAssertion (!(torCfg.enable && torCfg.upstream == "proxy" && torSnowflake))
|
||||
''proxy-suite: tor.bridges.lines has a snowflake bridge, which cannot follow tor.upstream = "proxy"; use obfs4, webtunnel or meek_lite bridges, or upstream = "direct"''
|
||||
)
|
||||
(mkAssertion (!(derived.torOutboundEnabled && builtins.elem derived.torOutboundTag outboundTags))
|
||||
"proxy-suite: the outbound tag \"tor\" belongs to tor.asOutbound; rename the proxy.outbounds entry"
|
||||
)
|
||||
(requireEnabled (torCfg.enable && torCfg.onionService.enable) cfg.inbounds.enable
|
||||
"proxy-suite: tor.onionService.enable requires inbounds.enable = true"
|
||||
)
|
||||
(mkAssertion (
|
||||
!derived.torOnionEnabled
|
||||
|| builtins.all (tag: builtins.hasAttr tag cfg.inbounds.listeners) torOnionListeners
|
||||
) "proxy-suite: tor.onionService.listeners names listeners not in inbounds.listeners: ${
|
||||
lib.concatStringsSep ", " (
|
||||
builtins.filter (tag: !builtins.hasAttr tag cfg.inbounds.listeners) torOnionListeners
|
||||
)
|
||||
}")
|
||||
(mkAssertion (!derived.torOnionEnabled || derived.torOnionInbounds != [ ])
|
||||
"proxy-suite: tor.onionService has no listener to serve: amneziawg, h3-only xhttp and raw JSON listeners cannot go over an onion"
|
||||
)
|
||||
(mkAssertion (!derived.torOnionEnabled || torOnionUnfit == [ ])
|
||||
"proxy-suite: tor.onionService.listeners cannot serve ${lib.concatStringsSep ", " torOnionUnfit}: an onion carries TCP only, so not amneziawg or h3-only xhttp, and raw JSON listeners have no known protocol"
|
||||
)
|
||||
(uniqueValues derived.torOnionEnabled torOnionVirtualPorts
|
||||
"proxy-suite: tor.onionService listeners must each share a distinct port (sharePort, or port)"
|
||||
)
|
||||
(mkAssertion
|
||||
(
|
||||
!derived.torOutboundEnabled
|
||||
|| !(
|
||||
(proxyEnabled && proxyCfg.listener.port == torCfg.socksPort)
|
||||
|| (proxyInboundsEnabled && builtins.elem torCfg.socksPort derived.proxyInboundPorts)
|
||||
|| builtins.elem torCfg.socksPort (
|
||||
builtins.attrValues constants.xrayDnsBridgePorts
|
||||
++ [
|
||||
constants.inboundStatsApiPort
|
||||
constants.outboundTestPort
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
"proxy-suite: tor.socksPort ${toString torCfg.socksPort} collides with another proxy-suite listener"
|
||||
)
|
||||
];
|
||||
in
|
||||
rootlessAssertions
|
||||
++ featureAssertions
|
||||
++ torAssertions
|
||||
++ perAppRoutingAssertions
|
||||
++ localProxyAuthAssertions
|
||||
++ secretAssertions
|
||||
|
||||
@@ -50,11 +50,13 @@ let
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
warpCfg
|
||||
torCfg
|
||||
awgOutbounds
|
||||
proxyInboundsCfg
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsAwg
|
||||
proxyInboundsNeedLocalProxy
|
||||
torOnionEnabled
|
||||
proxyInboundsGuardPrivate
|
||||
proxyInboundViaOutbounds
|
||||
userControlAllows
|
||||
@@ -100,6 +102,7 @@ let
|
||||
proxyCfg
|
||||
sshProxyCfg
|
||||
warpCfg
|
||||
torCfg
|
||||
awgOutbounds
|
||||
xrayEnabled
|
||||
hybridEnabled
|
||||
@@ -135,6 +138,7 @@ let
|
||||
proxyInboundsAwg
|
||||
awgBin
|
||||
proxyInboundsNeedLocalProxy
|
||||
torOnionEnabled
|
||||
proxyInboundsGuardPrivate
|
||||
proxyInboundViaOutbounds
|
||||
userControlAllows
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
if proxyInboundsSubscriptionsBaseUrl == null then "" else proxyInboundsSubscriptionsBaseUrl;
|
||||
autoProxyEnabled = if proxyCfg.autoProxy.enable then "1" else "0";
|
||||
autoProxyStateDir = constants.autoProxyStateDir;
|
||||
torControlSocket = constants.torControlSocket;
|
||||
inherit (constants) stateDir runtimeDir serviceManager;
|
||||
privileged = if constants.privileged then "1" else "0";
|
||||
supervisorCtl = if constants.serviceManager == "supervisor" then constants.systemctl else "";
|
||||
|
||||
@@ -66,6 +66,8 @@ let
|
||||
perAppZapretEnabled
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
torOutboundEnabled
|
||||
torOnionEnabled
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
proxyInboundFirewallPorts
|
||||
@@ -108,6 +110,8 @@ let
|
||||
perAppZapretEnabled
|
||||
sshProxyOutboundEnabled
|
||||
sshProxyUnitEnabled
|
||||
torOutboundEnabled
|
||||
torOnionEnabled
|
||||
proxyInboundsEnabled
|
||||
proxyInboundsNeedLocalProxy
|
||||
scripts
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
proxyInboundsAwg,
|
||||
awgBin,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
torOnionEnabled,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlCfg,
|
||||
userControlAllows,
|
||||
@@ -53,6 +54,26 @@ let
|
||||
else
|
||||
''SERVER_ADDRESS=""'';
|
||||
|
||||
# The onion service's address, for its share links. Tor writes it as it starts; a Tor
|
||||
# that is not up in time costs only the onion links, until the next restart.
|
||||
onionAddressBlock =
|
||||
if torOnionEnabled && proxyInboundsCfg.shareLinks then
|
||||
''
|
||||
ONION_ADDRESS=""
|
||||
onion_hostname=${lib.escapeShellArg constants.torOnionHostnameFile}
|
||||
for _ in $(${pkgs.coreutils}/bin/seq 30); do
|
||||
[ -s "$onion_hostname" ] && break
|
||||
${pkgs.coreutils}/bin/sleep 1
|
||||
done
|
||||
if [ -s "$onion_hostname" ]; then
|
||||
ONION_ADDRESS=$(${pkgs.coreutils}/bin/tr -d '[:space:]' < "$onion_hostname")
|
||||
else
|
||||
echo "proxy-suite: proxy-suite-tor has not written $onion_hostname; no onion share links until a restart" >&2
|
||||
fi
|
||||
''
|
||||
else
|
||||
''ONION_ADDRESS=""'';
|
||||
|
||||
# Outbounds pinned with `via = "<tag>"`, rendered at start so urlFile contents stay
|
||||
# out of the store.
|
||||
mkViaOutboundBlock =
|
||||
@@ -131,10 +152,12 @@ let
|
||||
mkdir -p "$RUNTIME_DIR"
|
||||
|
||||
${serverAddressBlock}
|
||||
${onionAddressBlock}
|
||||
|
||||
RENDERED=$(PYTHONPATH="${parserScriptsPythonPath}" ${python3} ${buildInboundPy} \
|
||||
--spec ${proxyInboundsSpecFile} \
|
||||
--server-address "$SERVER_ADDRESS")
|
||||
--server-address "$SERVER_ADDRESS" \
|
||||
--onion-address "$ONION_ADDRESS")
|
||||
|
||||
INBOUNDS_JSON=$(${jq} -c '.inbounds' <<< "$RENDERED")
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ else
|
||||
| .dns.final = $dns_final
|
||||
| if $clear_dns_rules then
|
||||
.dns.rules = .dns.rules[:${toString (builtins.length userDnsRules)}]
|
||||
+ [.dns.rules[${toString (builtins.length userDnsRules)}:][] | select(.server? == "fakeip")]
|
||||
+ [.dns.rules[${toString (builtins.length userDnsRules)}:][] | select(.server? == "fakeip" or .domain_suffix? == ["onion"])]
|
||||
else . end
|
||||
else . end
|
||||
# autoProxy rules, after the route-mode replace so no mode drops them: pins first
|
||||
@@ -118,10 +118,13 @@ else
|
||||
# inbounds.routing.blockPrivate, enforced here because the listener passes names unresolved.
|
||||
# Just before the first direct rule (or last, for a direct final), so names that the
|
||||
# proxy rules above take stay unresolved and none reaches a direct dial unchecked. Local
|
||||
# mixed-in clients lose names that resolve private.
|
||||
# mixed-in clients lose names that resolve private. Rules for other inbounds do not count:
|
||||
# autoProxy's direct probe pin would put the guard first, resolving every name, .onion too.
|
||||
| [{inbound: ["mixed-in"], action: "resolve"},
|
||||
{inbound: ["mixed-in"], ip_is_private: true, action: "reject"}] as $guard
|
||||
| (.route.rules | map((.outbound? // "") == "direct") | index(true)) as $first_direct
|
||||
| (.route.rules
|
||||
| map((.outbound? // "") == "direct" and ((.inbound? // ["mixed-in"]) | index(["mixed-in"])) != null)
|
||||
| index(true)) as $first_direct
|
||||
| if $first_direct != null then
|
||||
.route.rules = .route.rules[:$first_direct] + $guard + .route.rules[$first_direct:]
|
||||
elif (.route.final // "direct") == "direct" then
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
proxyCfg,
|
||||
sshProxyCfg,
|
||||
warpCfg,
|
||||
torCfg,
|
||||
awgOutbounds,
|
||||
constants,
|
||||
pureXrayEnabled,
|
||||
@@ -27,6 +28,7 @@
|
||||
|
||||
let
|
||||
sshProxyTag = "ssh-proxy";
|
||||
torOutboundEnabled = torCfg != null && torCfg.enable && torCfg.asOutbound;
|
||||
|
||||
# sing-box domain strategies in XRay's names (UseIPv4v6 = prefer IPv4).
|
||||
xrayDomainStrategies = {
|
||||
@@ -202,7 +204,7 @@ let
|
||||
tag="''${f##*/}"
|
||||
tag="''${tag%.*}"
|
||||
case "$tag" in
|
||||
proxy | direct | block)
|
||||
proxy | direct | block${lib.optionalString torOutboundEnabled " | tor"})
|
||||
echo "proxy-suite: warning: ignoring runtime outbound '$tag': reserved name" >&2
|
||||
continue
|
||||
;;
|
||||
@@ -528,10 +530,11 @@ let
|
||||
|
||||
# What selection may pick on its own: all but proxy.selectionExclude and disabled
|
||||
# outbounds. A pin still reaches the excluded ones, and a selector switched by hand both.
|
||||
# Tor is only for what is routed to it, unless it is the one outbound there is.
|
||||
selectableBlock = ''
|
||||
SELECTABLE_TAGS_JSON=$(${jq} -c --argjson ex ${lib.escapeShellArg (builtins.toJSON proxyCfg.selectionExclude)} \
|
||||
--argjson disabled "$DISABLED_TAGS_JSON" \
|
||||
'map(select(. as $t | ($ex + $disabled) | index([$t]) | not))' <<< "$OUTBOUND_TAGS_JSON")
|
||||
'${lib.optionalString torOutboundEnabled "(if length > 1 then [\"tor\"] else [] end) as $tor | "}map(select(. as $t | ($ex + $disabled${lib.optionalString torOutboundEnabled " + $tor"}) | index([$t]) | not))' <<< "$OUTBOUND_TAGS_JSON")
|
||||
if [ -z "$PINNED_OUTBOUND" ] && [ "$(${jq} 'length' <<< "$SELECTABLE_TAGS_JSON")" -eq 0 ]; then
|
||||
echo "proxy-suite: every outbound is in proxy.selectionExclude or disabled, so there is nothing to select; pin one, enable one (proxy-ctl proxy outbounds enable), or exclude fewer" >&2
|
||||
exit 1
|
||||
@@ -573,6 +576,9 @@ let
|
||||
warpBlock = lib.optionalString (warpCfg.enable && warpCfg.asOutbound == "singBox") (
|
||||
mkTunnelOutboundBlock "proxy-suite-warp-tunnel" "warp" "warp" warpCfg.tunnelPort
|
||||
);
|
||||
torBlock = lib.optionalString torOutboundEnabled (
|
||||
mkTunnelOutboundBlock "proxy-suite-tor" "tor" "tor" torCfg.socksPort
|
||||
);
|
||||
awgBlocks = lib.concatMapStrings (mkAwgOutboundBlock routingMark) awgOutbounds;
|
||||
|
||||
wrapperBlock =
|
||||
@@ -639,7 +645,9 @@ let
|
||||
# Real exits for autoProxy, taken after the wrapper may have renamed one to
|
||||
# "proxy".
|
||||
exitTagsBlock = ''
|
||||
EXIT_TAGS_JSON=$(${jq} -c '[.[] | select(.type != "selector" and .type != "urltest") | .tag]' <<< "$OUTBOUNDS_JSON")
|
||||
EXIT_TAGS_JSON=$(${jq} -c '[.[] | select(.type != "selector" and .type != "urltest"${
|
||||
lib.optionalString torOutboundEnabled " and (.tag | ltrimstr(\"proxy-suite-ob-\")) != \"tor\""
|
||||
}) | .tag]' <<< "$OUTBOUNDS_JSON")
|
||||
'';
|
||||
in
|
||||
mkUrlOutboundHelpersBlock routingMark
|
||||
@@ -650,6 +658,7 @@ let
|
||||
+ runtimeOutboundsBlock
|
||||
+ sshProxyBlock
|
||||
+ warpBlock
|
||||
+ torBlock
|
||||
+ awgBlocks
|
||||
+ detourBlock
|
||||
+ requireOutboundsBlock
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
proxyCfg,
|
||||
sshProxyCfg,
|
||||
warpCfg,
|
||||
torCfg,
|
||||
awgOutbounds,
|
||||
xrayEnabled,
|
||||
hybridEnabled,
|
||||
@@ -32,6 +33,7 @@
|
||||
proxyInboundsAwg,
|
||||
awgBin,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
torOnionEnabled,
|
||||
proxyInboundsGuardPrivate,
|
||||
proxyInboundViaOutbounds,
|
||||
userControlAllows,
|
||||
@@ -115,6 +117,7 @@ let
|
||||
proxyCfg
|
||||
sshProxyCfg
|
||||
warpCfg
|
||||
torCfg
|
||||
awgOutbounds
|
||||
constants
|
||||
pureXrayEnabled
|
||||
@@ -202,6 +205,7 @@ let
|
||||
proxyInboundsAwg
|
||||
awgBin
|
||||
proxyInboundsNeedLocalProxy
|
||||
torOnionEnabled
|
||||
proxyInboundViaOutbounds
|
||||
userControlCfg
|
||||
userControlAllows
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
perAppZapretEnabled,
|
||||
sshProxyOutboundEnabled,
|
||||
sshProxyUnitEnabled,
|
||||
torOutboundEnabled,
|
||||
torOnionEnabled,
|
||||
proxyInboundsEnabled,
|
||||
proxyInboundsNeedLocalProxy,
|
||||
scripts,
|
||||
@@ -76,10 +78,12 @@ let
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service";
|
||||
# Not after Tor: it may reach its relays through this listener.
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service";
|
||||
++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service"
|
||||
++ lib.optional torOutboundEnabled "proxy-suite-tor.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startSocks;
|
||||
runtimeDirectory = serviceNames.socks;
|
||||
@@ -97,11 +101,14 @@ let
|
||||
]
|
||||
# After the client stack, but not `requires`: direct listeners keep serving
|
||||
# without it.
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service";
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service"
|
||||
# For the onion address in the share links.
|
||||
++ lib.optional torOnionEnabled "proxy-suite-tor.service";
|
||||
wants = [
|
||||
"network-online.target"
|
||||
]
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service";
|
||||
++ lib.optional proxyInboundsNeedLocalProxy "${serviceNames.socks}.service"
|
||||
++ lib.optional torOnionEnabled "proxy-suite-tor.service";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
execStart = scripts.startInbounds;
|
||||
runtimeDirectory = serviceNames.inbounds;
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
# The Tor daemon: the SOCKS listener behind the "tor" outbound, and the onion service in front
|
||||
# of the inbounds.
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
cfg,
|
||||
derived,
|
||||
}:
|
||||
|
||||
let
|
||||
t = derived.torCfg;
|
||||
inherit (derived.constants) unprivilegedServiceConfig;
|
||||
inherit (cfg.host) privileged;
|
||||
|
||||
listener = cfg.proxy.listener;
|
||||
auth = listener.auth;
|
||||
# Tor dials the local proxy by address: a wildcard listener is reached on loopback.
|
||||
proxyHost =
|
||||
if
|
||||
builtins.elem listener.address [
|
||||
"0.0.0.0"
|
||||
"::"
|
||||
]
|
||||
then
|
||||
"127.0.0.1"
|
||||
else
|
||||
listener.address;
|
||||
proxyHostPart = if lib.hasInfix ":" proxyHost then "[${proxyHost}]" else proxyHost;
|
||||
passwordSource =
|
||||
if auth.passwordFile != null then
|
||||
auth.passwordFile
|
||||
else if auth.password != null then
|
||||
pkgs.writeText "proxy-suite-tor" auth.password
|
||||
else
|
||||
null;
|
||||
viaProxy = t.upstream == "proxy";
|
||||
withProxyAuth = viaProxy && auth.username != null && passwordSource != null;
|
||||
bridgesEnabled = t.bridges.lines != [ ] || t.bridges.file != null;
|
||||
|
||||
# The control socket's directory: members of userControl.group may use it for
|
||||
# `proxy-ctl tor status|newnym`, so it takes that group (setgid) on a system install.
|
||||
controlGroup = privileged && derived.userControlAllows "services";
|
||||
|
||||
# An onion service port per listener: its share port, forwarded to where XRay listens.
|
||||
onionTarget =
|
||||
l:
|
||||
let
|
||||
addr =
|
||||
if
|
||||
builtins.elem l.address [
|
||||
"::"
|
||||
"0.0.0.0"
|
||||
"localhost"
|
||||
]
|
||||
then
|
||||
"127.0.0.1"
|
||||
else
|
||||
l.address;
|
||||
in
|
||||
"${if lib.hasInfix ":" addr then "[${addr}]" else addr}:${toString l.port}";
|
||||
onionPorts = map (ib: {
|
||||
virtual = if ib.listener.sharePort != null then ib.listener.sharePort else ib.listener.port;
|
||||
target = onionTarget ib.listener;
|
||||
}) derived.torOnionInbounds;
|
||||
|
||||
staticConfig = (
|
||||
lib.concatStringsSep "\n" (
|
||||
[
|
||||
"GeoIPFile ${t.package.geoip}/share/tor/geoip"
|
||||
"GeoIPv6File ${t.package.geoip}/share/tor/geoip6"
|
||||
"ClientUseIPv6 1"
|
||||
"AvoidDiskWrites 1"
|
||||
"Log notice stdout"
|
||||
# Names come in over SOCKS unresolved; nothing here resolves for the host.
|
||||
"AutomapHostsOnResolve 0"
|
||||
"DNSPort 0"
|
||||
"TransPort 0"
|
||||
"NATDPort 0"
|
||||
"CookieAuthentication 0"
|
||||
"ControlSocketsGroupWritable 1"
|
||||
]
|
||||
++ lib.optional t.clientOnly "ClientOnly 1"
|
||||
++ (
|
||||
if t.asOutbound then
|
||||
[ "SocksPort 127.0.0.1:${toString t.socksPort}" ]
|
||||
else
|
||||
[ "SocksPort 0" ]
|
||||
)
|
||||
++ lib.optionals bridgesEnabled [
|
||||
"UseBridges 1"
|
||||
"ClientTransportPlugin obfs4,webtunnel,meek_lite exec ${t.lyrebirdPackage}/bin/lyrebird"
|
||||
"ClientTransportPlugin snowflake exec ${t.snowflakePackage}/bin/client"
|
||||
]
|
||||
++ lib.optional viaProxy "Socks5Proxy ${proxyHostPart}:${toString listener.port}"
|
||||
++ map (line: "Bridge ${line}") t.bridges.lines
|
||||
)
|
||||
);
|
||||
|
||||
# Group-owned and setgid, so the socket tor creates in it is the group's.
|
||||
controlDirScript = pkgs.writeShellScript "proxy-suite-tor-control-dir" ''
|
||||
set -euo pipefail
|
||||
dir="$RUNTIME_DIRECTORY/control"
|
||||
${pkgs.coreutils}/bin/install -d -m 2750 -o ${derived.constants.serviceUser} \
|
||||
-g ${lib.escapeShellArg cfg.userControl.group} "$dir"
|
||||
'';
|
||||
|
||||
startScript = pkgs.writeShellScript "proxy-suite-tor" ''
|
||||
set -euo pipefail
|
||||
umask 0077
|
||||
|
||||
torrc="$RUNTIME_DIRECTORY/torrc"
|
||||
${pkgs.coreutils}/bin/mkdir -p -m 0700 "$RUNTIME_DIRECTORY/control"
|
||||
{
|
||||
cat <<'TORRC'
|
||||
${staticConfig}
|
||||
TORRC
|
||||
echo "DataDirectory $STATE_DIRECTORY"
|
||||
echo "ControlSocket unix:$RUNTIME_DIRECTORY/control/socket"
|
||||
${lib.optionalString withProxyAuth ''
|
||||
echo "Socks5ProxyUsername "${lib.escapeShellArg auth.username}
|
||||
printf 'Socks5ProxyPassword %s\n' "$(${pkgs.coreutils}/bin/head -n1 "$CREDENTIALS_DIRECTORY/proxy-password")"
|
||||
''}
|
||||
${lib.optionalString (t.bridges.file != null) ''
|
||||
${pkgs.gnused}/bin/sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e '/^$/d' -e '/^#/d' \
|
||||
-e 's/^\(Bridge[[:space:]]\+\)\?/Bridge /' "$CREDENTIALS_DIRECTORY/bridges"
|
||||
''}
|
||||
${lib.optionalString derived.torOnionEnabled ''
|
||||
echo "HiddenServiceDir $STATE_DIRECTORY/onion"
|
||||
${lib.concatMapStrings (p: ''
|
||||
echo ${lib.escapeShellArg "HiddenServicePort ${toString p.virtual} ${p.target}"}
|
||||
'') onionPorts}
|
||||
''}
|
||||
cat ${pkgs.writeText "proxy-suite-torrc-extra" t.extraConfig}
|
||||
} > "$torrc"
|
||||
|
||||
${lib.optionalString (derived.torOnionEnabled && t.onionService.secretKeyFile != null) ''
|
||||
# Tor wants the service directory private to it.
|
||||
${pkgs.coreutils}/bin/install -d -m 0700 "$STATE_DIRECTORY/onion"
|
||||
if ! ${pkgs.diffutils}/bin/cmp -s "$CREDENTIALS_DIRECTORY/onion-secret-key" "$STATE_DIRECTORY/onion/hs_ed25519_secret_key"; then
|
||||
# Another key: the old address and its public key must not be served with it.
|
||||
${pkgs.coreutils}/bin/rm -f "$STATE_DIRECTORY/onion/hs_ed25519_public_key" "$STATE_DIRECTORY/onion/hostname"
|
||||
${pkgs.coreutils}/bin/install -m 0600 "$CREDENTIALS_DIRECTORY/onion-secret-key" \
|
||||
"$STATE_DIRECTORY/onion/hs_ed25519_secret_key"
|
||||
fi
|
||||
''}
|
||||
|
||||
exec ${t.package}/bin/tor -f "$torrc"
|
||||
'';
|
||||
in
|
||||
{
|
||||
services.proxy-suite.internal.services.proxy-suite-tor = {
|
||||
description = "proxy-suite - Tor";
|
||||
after = [ "network-online.target" ] ++ lib.optional viaProxy "proxy-suite-socks.service";
|
||||
wants = [ "network-online.target" ];
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
# Restart=always must not trip the start limit while the network is down.
|
||||
startLimitIntervalSec = 0;
|
||||
serviceConfig =
|
||||
unprivilegedServiceConfig [ ]
|
||||
// {
|
||||
Type = "simple";
|
||||
ExecStart = startScript;
|
||||
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
||||
# The service user is excluded from TUN and TProxy capture, which is what keeps
|
||||
# tor's own connections out of the proxy.
|
||||
StateDirectory = "proxy-suite/tor";
|
||||
StateDirectoryMode = "0700";
|
||||
RuntimeDirectory = "proxy-suite-tor";
|
||||
RuntimeDirectoryMode = "0711";
|
||||
# Copies the service user can read, whoever owns the originals.
|
||||
LoadCredential =
|
||||
lib.optional withProxyAuth "proxy-password:${passwordSource}"
|
||||
++ lib.optional (t.bridges.file != null) "bridges:${t.bridges.file}"
|
||||
++ lib.optional (
|
||||
derived.torOnionEnabled && t.onionService.secretKeyFile != null
|
||||
) "onion-secret-key:${t.onionService.secretKeyFile}";
|
||||
Restart = "always";
|
||||
RestartSec = 5;
|
||||
LimitNOFILE = 65536;
|
||||
}
|
||||
// lib.optionalAttrs controlGroup {
|
||||
ExecStartPre = "+${controlDirScript}";
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user