diff --git a/README.md b/README.md index 4fd1024..9191c33 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Declarative proxy stack for NixOS, on either side of the connection: as a client > [!NOTE] > This project is developed with AI assistance: much of the code and documentation was written with AI tools, then reviewed and tested on real-world setups. -Bundles [sing-box](https://github.com/SagerNet/sing-box), [XRay](https://github.com/XTLS/Xray-core), [AmneziaWG](https://github.com/amnezia-vpn/amneziawg-go), [zapret-discord-youtube](https://github.com/kartavkun/zapret-discord-youtube), [zapret2](https://github.com/bol-van/zapret2) and [tg-ws-proxy](https://github.com/Flowseal/tg-ws-proxy); see their repositories for their own licenses and documentation. +Bundles [sing-box](https://github.com/SagerNet/sing-box), [XRay](https://github.com/XTLS/Xray-core), [AmneziaWG](https://github.com/amnezia-vpn/amneziawg-go), [zapret-discord-youtube](https://github.com/kartavkun/zapret-discord-youtube), [zapret2](https://github.com/bol-van/zapret2), [tg-ws-proxy](https://github.com/Flowseal/tg-ws-proxy) and [whitelist-bypass](https://github.com/kulikov0/whitelist-bypass); see their repositories for their own licenses and documentation. Inspired by [Throne](https://github.com/throneproj/Throne) (formerly NekoRay), [3x-ui](https://github.com/MHSanaei/3x-ui) and other similar projects. @@ -27,6 +27,7 @@ Inspired by [Throne](https://github.com/throneproj/Throne) (formerly NekoRay), [ - Telegram MTProto WebSocket proxy and SSH SOCKS5 tunnel - Cloudflare WARP from a `wgcf` profile, as an outbound or an AWG VPN profile - Tor as an outbound, with `.onion` names routed to it automatically, bridges (obfs4, webtunnel, meek, snowflake), and an onion service in front of the server inbounds +- Mobile internet whitelists: a tunnel through the media servers of video calls (WB Stream, Telemost, DION, Bitrix), with the creator on a server and the joiner as an outbound, Nix-on-Droid included - Server inbounds (XRay protocols, hysteria2, and multi-user AmneziaWG) with share links, QR codes, per-user subscriptions and traffic stats - Proxy Suite GUI (a desktop app with a tray icon), the `proxy-ctl` CLI, and the `proxy-tui` terminal UI @@ -112,6 +113,14 @@ services.proxy-suite = { secretFile = "/run/secrets/tg-ws-proxy-secret"; }; + # Through mobile internet whitelists: the call's media server relays, whatever the IP. + # The server runs a creator per device; its log prints the call link for the joiner. + whitelistBypass = { + enable = true; + joiners.wl = { platform = "wbstream"; linkFile = "/run/secrets/wb-link"; }; # an outbound + # creators.phone = { platform = "wbstream"; cookiesFile = "/run/secrets/wb-cookies.json"; upstream = "proxy"; }; + }; + gui.enable = true; }; ``` @@ -193,6 +202,9 @@ Secrets and changes need root, or the userControl group. tor [status|on|off] Tor, behind the tor outbound and the onion service tor newnym new circuits for new connections tg [status|on|off] Telegram WebSocket proxy + wl [list] whitelist-bypass creators and joiners + wl link [--qr] the call link its joiner takes + wl on|off|toggle|restart [name] one creator or joiner, or all of them apps [list] per-app routing profiles apps run -- [args] run a command through a profile diff --git a/docs/options/index.md b/docs/options/index.md index 3a33574..8d2eb51 100644 --- a/docs/options/index.md +++ b/docs/options/index.md @@ -19,6 +19,7 @@ Update the module option docs there instead of editing these files by hand. - [tui](./tui.md) - [userControl](./userControl.md) - [warp](./warp.md) +- [whitelistBypass](./whitelistBypass.md) - [zapret](./zapret.md) ## Complete default config @@ -304,6 +305,12 @@ services.proxy-suite = { endpoint = null; generatorUrl = null; }; + whitelistBypass = { + creators = { }; + enable = false; + joiners = { }; + package = pkgs.whitelist-bypass; + }; zapret = { cidrExemption = { cidrs = [ ]; diff --git a/docs/options/whitelistBypass.md b/docs/options/whitelistBypass.md new file mode 100644 index 0000000..c5ba414 --- /dev/null +++ b/docs/options/whitelistBypass.md @@ -0,0 +1,220 @@ +# services.proxy-suite.whitelistBypass + +Part of the [proxy-suite options reference](./index.md). + +## Options + +- whitelistBypass + - [enable](#services-proxy-suite-whitelistbypass-enable) + - [package](#services-proxy-suite-whitelistbypass-package) + - [creators](#services-proxy-suite-whitelistbypass-creators) + - `` + - [cookiesFile](#services-proxy-suite-whitelistbypass-creators-name-cookiesfile) + - [linkFile](#services-proxy-suite-whitelistbypass-creators-name-linkfile) + - [platform](#services-proxy-suite-whitelistbypass-creators-name-platform) + - [resources](#services-proxy-suite-whitelistbypass-creators-name-resources) + - [upstream](#services-proxy-suite-whitelistbypass-creators-name-upstream) + - [joiners](#services-proxy-suite-whitelistbypass-joiners) + - `` + - [linkFile](#services-proxy-suite-whitelistbypass-joiners-name-linkfile) + - [platform](#services-proxy-suite-whitelistbypass-joiners-name-platform) + + +## services\.proxy-suite\.whitelistBypass\.enable + +Whether to enable tunnels through the media servers of video calls, which mobile internet whitelists let +through ([whitelist-bypass](https://github\.com/kulikov0/whitelist-bypass))\. A creator on a +free host serves exactly one joiner on a censored one\. + +*Type:* +boolean + +*Default:* + +```nix +false +``` + +*Example:* + +```nix +true +``` + + +## services\.proxy-suite\.whitelistBypass\.package + +whitelist-bypass package with the headless creators and joiners\. + +*Type:* +package + +*Default:* +proxy-suite’s ` whitelist-bypass ` (` pkgs/whitelist-bypass.nix `) + + +## services\.proxy-suite\.whitelistBypass\.creators + +Creators, one per joiner device\. The link each uses is in its log\. + +*Type:* +attribute set of (submodule) + +*Default:* + +```nix +{ } +``` + +*Example:* + +```nix +{ + phone = { + cookiesFile = "/run/secrets/whitelist-bypass-cookies-wbstream.json"; + platform = "wbstream"; + upstream = "proxy"; + }; +} +``` + + +## services\.proxy-suite\.whitelistBypass\.creators\.\\.cookiesFile + +Runtime path to the platform’s cookies, as the upstream desktop Creator exports them\. +Copied into the state directory on first start only: DION and Bitrix rotate their +refresh token into that copy, and a stale one would kill the session\. Delete +` /whitelist-bypass/.cookies.json ` to take a new export\. + +*Type:* +string + +*Example:* + +```nix +"/run/secrets/whitelist-bypass-cookies-wbstream.json" +``` + + +## services\.proxy-suite\.whitelistBypass\.creators\.\\.linkFile + +Runtime path to the call to rejoin\. Null rejoins the last call written to +` /whitelist-bypass/.link `, and creates one on first start: the link +stays the same across restarts, so the joiner needs it only once\. + +*Type:* +null or string + +*Default:* + +```nix +null +``` + +*Example:* + +```nix +"/run/secrets/whitelist-bypass-link" +``` + + +## services\.proxy-suite\.whitelistBypass\.creators\.\\.platform + +Call platform\. Every one needs an account: “vk” only serves the upstream Android app, +the rest a joiner of this module too\. + +*Type:* +one of “wbstream”, “telemost”, “dion”, “bitrix”, “vk” + +*Example:* + +```nix +"wbstream" +``` + + +## services\.proxy-suite\.whitelistBypass\.creators\.\\.resources + +Buffer sizes and Go memory limit: 64, 128 or 256 MB\. + +*Type:* +one of “moderate”, “default”, “unlimited” + +*Default:* + +```nix +"moderate" +``` + + +## services\.proxy-suite\.whitelistBypass\.creators\.\\.upstream + +Where the joiner’s traffic leaves\. + + - “direct”: from this host, past TUN and TProxy (it runs as proxy-suite-daemon)\. + - “proxy”: through the local proxy listener (proxy\.listener, with its auth), and so + its outbounds and routing\. + +*Type:* +one of “direct”, “proxy” + +*Default:* + +```nix +"direct" +``` + + +## services\.proxy-suite\.whitelistBypass\.joiners + +Joiners, each an outbound tagged with its name: a loopback SOCKS5 listener that tunnels +through the call\. Needs proxy\.enable\. + +*Type:* +attribute set of (submodule) + +*Default:* + +```nix +{ } +``` + +*Example:* + +```nix +{ + wl = { + linkFile = "/run/secrets/whitelist-bypass-link"; + platform = "wbstream"; + }; +} +``` + + +## services\.proxy-suite\.whitelistBypass\.joiners\.\\.linkFile + +Runtime path to the call link the creator printed (or wrote to its state directory): +a WB Stream room id, a Telemost link, a DION event slug or a Bitrix conference link\. + +*Type:* +string + +*Example:* + +```nix +"/run/secrets/whitelist-bypass-link" +``` + + +## services\.proxy-suite\.whitelistBypass\.joiners\.\\.platform + +Call platform the creator on the other end uses\. VK has no Linux joiner\. + +*Type:* +one of “wbstream”, “telemost”, “dion”, “bitrix” + +*Example:* + +```nix +"wbstream" +``` diff --git a/flake.nix b/flake.nix index 9cb4e7d..f2ddb6a 100644 --- a/flake.nix +++ b/flake.nix @@ -85,6 +85,7 @@ mkProxyCtl mkTgWsProxy tg-ws-proxy + whitelist-bypass wireproxy-awg zapret2 ; @@ -136,6 +137,7 @@ amneziawg-tools amneziawg-go tg-ws-proxy + whitelist-bypass wireproxy-awg zapret2 ; diff --git a/modules/proxy-suite/core.nix b/modules/proxy-suite/core.nix index 78dae66..45ffe75 100644 --- a/modules/proxy-suite/core.nix +++ b/modules/proxy-suite/core.nix @@ -47,6 +47,7 @@ in xray = import ../../pkgs/xray.nix { pkgs = upstream; }; # AWG 3.1 userspace; its kernel module has to match the system's kernel instead. amneziaWg = import ../../pkgs/amneziawg.nix { pkgs = upstream; }; + whitelist-bypass = import ../../pkgs/whitelist-bypass.nix { pkgs = upstream; }; inherit (upstream) sing-box; }; } @@ -138,6 +139,17 @@ in } )) + (lib.mkIf cfg.whitelistBypass.enable ( + import ./whitelist-bypass.nix { + inherit + lib + pkgs + cfg + derived + ; + } + )) + (lib.mkIf derived.ruleSetsEnabled ( import ./rulesets.nix { inherit diff --git a/modules/proxy-suite/derived.nix b/modules/proxy-suite/derived.nix index f6892fc..594ab12 100644 --- a/modules/proxy-suite/derived.nix +++ b/modules/proxy-suite/derived.nix @@ -117,6 +117,19 @@ let torOutboundEnabled = torCfg.enable && torCfg.asOutbound; torRouteOnion = torOutboundEnabled && torCfg.routeOnion && proxyEnabled; + # Joiners in name order, each an outbound on a loopback SOCKS listener of its own. + whitelistBypassCfg = cfg.whitelistBypass; + whitelistBypassJoiners = lib.optionals whitelistBypassCfg.enable ( + lib.imap0 ( + i: tag: + whitelistBypassCfg.joiners.${tag} + // { + inherit tag; + port = 18700 + i; + } + ) (builtins.attrNames whitelistBypassCfg.joiners) + ); + # Two ports per "singBox" or "userspace" AmneziaWG outbound, above the autoProxy prober's own # listeners (proxy.autoProxy.probeBasePort, 18540 by default, one per exit): # both bind loopback, so an overlap leaves whichever unit starts second dead. @@ -290,6 +303,7 @@ let ++ lib.optional sshProxyOutboundEnabled sshProxyOutboundTag ++ lib.optional warpOutboundEnabled warpOutboundTag ++ lib.optional torOutboundEnabled torOutboundTag + ++ map (j: j.tag) whitelistBypassJoiners ++ map (ob: ob.tag) awgOutbounds; subscriptionTags = map (sub: sub.tag) proxyCfg.subscriptions; @@ -301,6 +315,7 @@ let || sshProxyOutboundEnabled || warpOutboundEnabled || torOutboundEnabled + || whitelistBypassJoiners != [ ] || awgOutbounds != [ ]; collapseNamedOutbounds = selectionMode == "first"; # Always on with sing-box: `proxy-ctl proxy outbounds test` needs it in every selection mode. @@ -509,6 +524,8 @@ in torOutboundTag torOutboundEnabled torRouteOnion + whitelistBypassCfg + whitelistBypassJoiners torOnionEnabled torOnionInbounds proxyInboundOnionCapable diff --git a/modules/proxy-suite/options/default.nix b/modules/proxy-suite/options/default.nix index 405a6ea..92b51c4 100644 --- a/modules/proxy-suite/options/default.nix +++ b/modules/proxy-suite/options/default.nix @@ -17,6 +17,7 @@ ./warp.nix ./tor.nix ./tg-ws-proxy.nix + ./whitelist-bypass.nix ./geodata.nix ./gui.nix ./tui.nix diff --git a/modules/proxy-suite/options/whitelist-bypass.nix b/modules/proxy-suite/options/whitelist-bypass.nix new file mode 100644 index 0000000..9aa0923 --- /dev/null +++ b/modules/proxy-suite/options/whitelist-bypass.nix @@ -0,0 +1,137 @@ +{ lib, proxySuiteUpstream, ... }: + +let + inherit (lib) mkEnableOption mkOption types; + + joinerType = types.submodule { + options = { + platform = mkOption { + type = types.enum [ + "wbstream" + "telemost" + "dion" + "bitrix" + ]; + description = "Call platform the creator on the other end uses. VK has no Linux joiner."; + example = "wbstream"; + }; + + linkFile = mkOption { + type = types.str; + description = '' + Runtime path to the call link the creator printed (or wrote to its state directory): + a WB Stream room id, a Telemost link, a DION event slug or a Bitrix conference link. + ''; + example = "/run/secrets/whitelist-bypass-link"; + }; + }; + }; + + creatorType = types.submodule { + options = { + platform = mkOption { + type = types.enum [ + "wbstream" + "telemost" + "dion" + "bitrix" + "vk" + ]; + description = '' + Call platform. Every one needs an account: "vk" only serves the upstream Android app, + the rest a joiner of this module too. + ''; + example = "wbstream"; + }; + + cookiesFile = mkOption { + type = types.str; + description = '' + Runtime path to the platform's cookies, as the upstream desktop Creator exports them. + Copied into the state directory on first start only: DION and Bitrix rotate their + refresh token into that copy, and a stale one would kill the session. Delete + `/whitelist-bypass/.cookies.json` to take a new export. + ''; + example = "/run/secrets/whitelist-bypass-cookies-wbstream.json"; + }; + + linkFile = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + Runtime path to the call to rejoin. Null rejoins the last call written to + `/whitelist-bypass/.link`, and creates one on first start: the link + stays the same across restarts, so the joiner needs it only once. + ''; + example = "/run/secrets/whitelist-bypass-link"; + }; + + upstream = mkOption { + type = types.enum [ + "direct" + "proxy" + ]; + default = "direct"; + description = '' + Where the joiner's traffic leaves. + - "direct": from this host, past TUN and TProxy (it runs as proxy-suite-daemon). + - "proxy": through the local proxy listener (proxy.listener, with its auth), and so + its outbounds and routing. + ''; + }; + + resources = mkOption { + type = types.enum [ + "moderate" + "default" + "unlimited" + ]; + default = "moderate"; + description = "Buffer sizes and Go memory limit: 64, 128 or 256 MB."; + }; + }; + }; +in +{ + options.services.proxy-suite.whitelistBypass = { + enable = mkEnableOption '' + tunnels through the media servers of video calls, which mobile internet whitelists let + through ([whitelist-bypass](https://github.com/kulikov0/whitelist-bypass)). A creator on a + free host serves exactly one joiner on a censored one''; + + package = mkOption { + type = types.package; + default = proxySuiteUpstream.whitelist-bypass; + defaultText = lib.literalMD "proxy-suite's `whitelist-bypass` (`pkgs/whitelist-bypass.nix`)"; + description = "whitelist-bypass package with the headless creators and joiners."; + }; + + joiners = mkOption { + type = types.attrsOf joinerType; + default = { }; + description = '' + Joiners, each an outbound tagged with its name: a loopback SOCKS5 listener that tunnels + through the call. Needs proxy.enable. + ''; + example = { + wl = { + platform = "wbstream"; + linkFile = "/run/secrets/whitelist-bypass-link"; + }; + }; + }; + + creators = mkOption { + type = types.attrsOf creatorType; + default = { }; + description = "Creators, one per joiner device. The link each uses is in its log."; + example = { + phone = { + platform = "wbstream"; + cookiesFile = "/run/secrets/whitelist-bypass-cookies-wbstream.json"; + upstream = "proxy"; + }; + }; + }; + }; +} diff --git a/modules/proxy-suite/service-assertions.nix b/modules/proxy-suite/service-assertions.nix index 5df884e..4f39f3a 100644 --- a/modules/proxy-suite/service-assertions.nix +++ b/modules/proxy-suite/service-assertions.nix @@ -78,6 +78,7 @@ let ob.directPort ]) derived.awgTunnelOutbounds ++ lib.optional derived.torOutboundEnabled derived.torCfg.socksPort + ++ map (j: j.port) derived.whitelistBypassJoiners ++ lib.optionals derived.proxyInboundsEnabled ( map (listener: listener.internalPort) derived.proxyInboundsAwg ); @@ -889,6 +890,21 @@ let torOnionVirtualPorts = map ( ib: if ib.listener.sharePort != null then ib.listener.sharePort else ib.listener.port ) derived.torOnionInbounds; + whitelistBypassCfg = cfg.whitelistBypass; + whitelistBypassAssertions = [ + (requireEnabled ( + derived.whitelistBypassJoiners != [ ] + ) proxyEnabled "proxy-suite: whitelistBypass.joiners requires proxy.enable = true") + (requireEnabled + ( + whitelistBypassCfg.enable + && lib.any (c: c.upstream == "proxy") (builtins.attrValues whitelistBypassCfg.creators) + ) + proxyEnabled + ''proxy-suite: whitelistBypass.creators..upstream = "proxy" requires proxy.enable = true'' + ) + ]; + torSnowflake = lib.any (line: lib.hasPrefix "snowflake " line) torCfg.bridges.lines; torAssertions = [ (mkAssertion ( @@ -959,6 +975,7 @@ in rootlessAssertions ++ featureAssertions ++ torAssertions +++ whitelistBypassAssertions ++ perAppRoutingAssertions ++ localProxyAuthAssertions ++ secretAssertions diff --git a/modules/proxy-suite/service/control.nix b/modules/proxy-suite/service/control.nix index 3937471..ab13f8b 100644 --- a/modules/proxy-suite/service/control.nix +++ b/modules/proxy-suite/service/control.nix @@ -72,6 +72,24 @@ in ROUTE_MODE_STATE_FILE = routeModeStateFile; DEFAULT_ROUTE_MODE = if proxyCfg.routing.default == "proxy" then "blacklist" else "whitelist"; AWG_PROFILES_FILE = toString amneziaWgProfileNamesFile; + WL_FILE = toString ( + pkgs.writeText "proxy-suite-control" ( + builtins.toJSON ( + lib.optionals cfg.whitelistBypass.enable ( + lib.mapAttrsToList (name: c: { + inherit name; + inherit (c) platform; + role = "creator"; + }) cfg.whitelistBypass.creators + ++ lib.mapAttrsToList (name: j: { + inherit name; + inherit (j) platform; + role = "joiner"; + }) cfg.whitelistBypass.joiners + ) + ) + ) + ); INBOUNDS_ENABLED = flag proxyInboundsEnabled; INBOUNDS_LINKS_FILE = proxyInboundsLinksFile; INBOUNDS_STATS_FILE = constants.inboundStatsFile; diff --git a/modules/proxy-suite/service/script-blocks/outbounds.nix b/modules/proxy-suite/service/script-blocks/outbounds.nix index ee694fd..767521e 100644 --- a/modules/proxy-suite/service/script-blocks/outbounds.nix +++ b/modules/proxy-suite/service/script-blocks/outbounds.nix @@ -6,6 +6,7 @@ sshProxyCfg, warpCfg, torCfg, + whitelistBypassJoiners, awgOutbounds, constants, pureXrayEnabled, @@ -588,6 +589,9 @@ let torBlock = lib.optionalString torOutboundEnabled ( mkTunnelOutboundBlock "proxy-suite-tor" "tor" "tor" torCfg.socksPort ); + whitelistBypassBlocks = lib.concatMapStrings ( + j: mkTunnelOutboundBlock "proxy-suite-wb-joiner-${j.tag}" "whitelist-bypass" j.tag j.port + ) whitelistBypassJoiners; awgBlocks = lib.concatMapStrings (mkAwgOutboundBlock routingMark) awgOutbounds; wrapperBlock = @@ -666,6 +670,7 @@ let + sshProxyBlock + warpBlock + torBlock + + whitelistBypassBlocks + awgBlocks + detourBlock + requireOutboundsBlock diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 88cbbad..46ecd45 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -125,6 +125,7 @@ let sshProxyCfg warpCfg torCfg + whitelistBypassJoiners awgOutbounds constants pureXrayEnabled diff --git a/modules/proxy-suite/service/units.nix b/modules/proxy-suite/service/units.nix index 9f43e52..fbd2cc3 100644 --- a/modules/proxy-suite/service/units.nix +++ b/modules/proxy-suite/service/units.nix @@ -20,6 +20,7 @@ let sshProxyUnitEnabled torOutboundEnabled torOnionEnabled + whitelistBypassJoiners proxyInboundsEnabled proxyInboundsNeedLocalProxy scripts @@ -88,7 +89,8 @@ let "network-online.target" ] ++ lib.optional (sshProxyOutboundEnabled && sshProxyUnitEnabled) "proxy-suite-ssh-proxy.service" - ++ lib.optional torOutboundEnabled "proxy-suite-tor.service"; + ++ lib.optional torOutboundEnabled "proxy-suite-tor.service" + ++ map (j: "proxy-suite-wb-joiner-${j.tag}.service") whitelistBypassJoiners; wantedBy = [ "multi-user.target" ]; execStart = scripts.startSocks; runtimeDirectory = serviceNames.socks; diff --git a/modules/proxy-suite/whitelist-bypass.nix b/modules/proxy-suite/whitelist-bypass.nix new file mode 100644 index 0000000..bb01499 --- /dev/null +++ b/modules/proxy-suite/whitelist-bypass.nix @@ -0,0 +1,108 @@ +# whitelist-bypass: joiners, the loopback SOCKS listeners behind their outbounds, and +# creators, the free ends of the calls they tunnel through. +{ + lib, + pkgs, + cfg, + derived, +}: + +let + w = derived.whitelistBypassCfg; + inherit (derived.constants) unprivilegedServiceConfig; + inherit (derived.localProxy) auth; + + # Every flag that takes the call, joiner and creator alike. + linkFlag = { + wbstream = "--room"; + dion = "--room"; + bitrix = "--room"; + telemost = "--tm-link"; + vk = "--vk-link"; + }; + joinerLinkFlag = linkFlag // { + bitrix = "--link"; + }; + + passwordSource = + if auth.passwordFile != null then + auth.passwordFile + else if auth.password != null then + pkgs.writeText "proxy-suite-wb-creator" auth.password + else + null; + + # Restart=always must not trip the start limit while the network or the platform is down. + mkUnit = description: serviceConfig: { + inherit description; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; + startLimitIntervalSec = 0; + serviceConfig = + unprivilegedServiceConfig [ ] + // { + Restart = "always"; + RestartSec = 5; + } + // serviceConfig; + }; + + mkJoiner = + j: + lib.nameValuePair "proxy-suite-wb-joiner-${j.tag}" ( + mkUnit "proxy-suite - whitelist-bypass joiner ${j.tag}" { + ExecStart = pkgs.writeShellScript "proxy-suite-wb-joiner-${j.tag}" '' + set -euo pipefail + link=$(tr -d '[:space:]' < "$CREDENTIALS_DIRECTORY/link") + exec ${w.package}/bin/headless-${j.platform}-joiner \ + ${joinerLinkFlag.${j.platform}} "$link" --socks-port ${toString j.port} + ''; + LoadCredential = [ "link:${j.linkFile}" ]; + } + ); + + mkCreator = + name: c: + let + viaProxy = c.upstream == "proxy"; + withProxyAuth = viaProxy && derived.localProxy.authEnabled; + unit = mkUnit "proxy-suite - whitelist-bypass creator ${name}" { + ExecStart = pkgs.writeShellScript "proxy-suite-wb-creator-${name}" '' + set -euo pipefail + cookies="$STATE_DIRECTORY/${name}.cookies.json" + links="$STATE_DIRECTORY/${name}.link" + [ -e "$cookies" ] || install -m 600 "$CREDENTIALS_DIRECTORY/cookies" "$cookies" + args=(--cookies "$cookies" --write-file "$links" --resources ${c.resources}) + ${ + if c.linkFile != null then + ''link=$(tr -d '[:space:]' < "$CREDENTIALS_DIRECTORY/link")'' + else + ''link=$(tail -n 1 "$links" 2>/dev/null || true)'' + } + [ -z "$link" ] || args+=(${linkFlag.${c.platform}} "$link") + ${lib.optionalString viaProxy '' + args+=(--upstream-socks ${derived.localProxy.hostPart}:${toString cfg.proxy.listener.port}) + ''}${lib.optionalString withProxyAuth '' + args+=(--upstream-user ${lib.escapeShellArg auth.username} --upstream-pass "$(< "$CREDENTIALS_DIRECTORY/proxy-password")") + ''} + exec ${w.package}/bin/headless-${c.platform}-creator "''${args[@]}" + ''; + StateDirectory = "proxy-suite/whitelist-bypass"; + StateDirectoryMode = "0700"; + LoadCredential = [ + "cookies:${c.cookiesFile}" + ] + ++ lib.optional (c.linkFile != null) "link:${c.linkFile}" + ++ lib.optional withProxyAuth "proxy-password:${passwordSource}"; + }; + in + lib.nameValuePair "proxy-suite-wb-creator-${name}" ( + unit // lib.optionalAttrs viaProxy { after = unit.after ++ [ "proxy-suite-socks.service" ]; } + ); +in +{ + services.proxy-suite.internal.services = lib.listToAttrs ( + map mkJoiner derived.whitelistBypassJoiners ++ lib.mapAttrsToList mkCreator w.creators + ); +} diff --git a/nix/checks/hosts.nix b/nix/checks/hosts.nix index 9a291d4..137ee8d 100644 --- a/nix/checks/hosts.nix +++ b/nix/checks/hosts.nix @@ -130,6 +130,18 @@ let tls.keyFile = "/k"; }; }; + whitelistBypass = { + enable = true; + joiners.wl = { + platform = "wbstream"; + linkFile = "/run/secrets/wb-link"; + }; + creators.phone = { + platform = "dion"; + cookiesFile = "/run/secrets/wb-cookies"; + upstream = "proxy"; + }; + }; }; }; @@ -213,6 +225,8 @@ in assert lib.hasInfix "proxy-suitectl ensure" nodCfg.environment.etc.profile.text; assert forced nodCfg.build.activationAfter.proxySuite; assert nodCfg.services.proxy-suite.internal.services.proxy-suite-tor.enable; + assert nodCfg.services.proxy-suite.internal.services.proxy-suite-wb-joiner-wl.enable; + assert nodCfg.services.proxy-suite.internal.services.proxy-suite-wb-creator-phone.enable; # Android bans apps from netlink's route groups, which stock sing-box subscribes to. assert nodCfg.services.proxy-suite.proxy.singBox.package.passthru.rootlessNetlink or false; assert !(hmCfg.services.proxy-suite.proxy.singBox.package.passthru.rootlessNetlink or false); diff --git a/nix/checks/repo.nix b/nix/checks/repo.nix index 8fe56d5..a80c679 100644 --- a/nix/checks/repo.nix +++ b/nix/checks/repo.nix @@ -753,6 +753,7 @@ in sshProxyCfg = null; warpCfg = null; torCfg = null; + whitelistBypassJoiners = [ ]; awgOutbounds = null; constants = null; pureXrayEnabled = null; diff --git a/nix/checks/xray-backend-features/fixtures.nix b/nix/checks/xray-backend-features/fixtures.nix index 8eec604..4c58b25 100644 --- a/nix/checks/xray-backend-features/fixtures.nix +++ b/nix/checks/xray-backend-features/fixtures.nix @@ -20,6 +20,7 @@ let sshProxyCfg = null; warpCfg = null; torCfg = null; + whitelistBypassJoiners = [ ]; awgOutbounds = null; constants = null; hybridEnabled = null; diff --git a/pkgs/default.nix b/pkgs/default.nix index 87aa78d..b15dbb2 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -3,6 +3,7 @@ let amneziaWg = import ./amneziawg.nix { inherit pkgs; }; mkTgWsProxy = _args: import ./tg-ws-proxy.nix { inherit pkgs; }; zapret2 = import ./zapret2.nix { inherit pkgs; }; + whitelist-bypass = import ./whitelist-bypass.nix { inherit pkgs; }; mkProxySuiteSupervisor = import ./proxy-suite-supervisor.nix { lib = pkgs.lib; inherit pkgs; @@ -22,6 +23,7 @@ in mkProxyCtl mkProxySuiteSupervisor zapret2 + whitelist-bypass ; tg-ws-proxy = mkTgWsProxy { }; } diff --git a/pkgs/proxy-ctl/proxy_ctl.py b/pkgs/proxy-ctl/proxy_ctl.py index bc607da..6dd1393 100644 --- a/pkgs/proxy-ctl/proxy_ctl.py +++ b/pkgs/proxy-ctl/proxy_ctl.py @@ -115,6 +115,9 @@ Secrets and changes need root, or the userControl group. tor [status|on|off] Tor, behind the tor outbound and the onion service tor newnym new circuits for new connections tg [status|on|off] Telegram WebSocket proxy + wl [list] whitelist-bypass creators and joiners + wl link [--qr] the call link its joiner takes + wl on|off|toggle|restart [name] one creator or joiner, or all of them apps [list] per-app routing profiles apps run -- [args] run a command through a profile @@ -491,6 +494,7 @@ COMPLETE = { "warp": "WARP tunnel behind the warp outbound", "tor": "Tor, behind the tor outbound and the onion service", "tg": "Telegram WebSocket proxy", + "wl": "whitelist-bypass creators and joiners", "apps": "per-app routing profiles", "inbounds": "server inbounds", "help": "usage", @@ -618,6 +622,18 @@ COMPLETE = { "killswitch": {"words": TOGGLE}, "tor": {"words": {**TOGGLE, "newnym": "new circuits for new connections"}}, "tg": {"words": TOGGLE}, + "wl": { + "words": { + "list": "creators and joiners and their state", + "link": "the call link a creator's joiner takes", + "on": "start one, or all", + "off": "stop one, or all", + "toggle": "start it if stopped, stop it if running", + "restart": "restart one, or all", + } + }, + "wl link": {"args": lambda: _names(w["name"] for w in _wl() if w["role"] == "creator"), "flags": {"--qr": "print a QR code"}}, + **{f"wl {verb}": {"args": lambda: {w["name"]: w["role"] for w in _wl()}} for verb in ("on", "off", "toggle", "restart")}, "apps": {"words": {"list": "per-app routing profiles", "run": "run a command through a profile"}}, "apps run": {"args": lambda: {_s(p["name"]): _s(p.get("route") or "") for p in read_json(env("PER_APP_ROUTING_PROFILES_FILE"))}}, "inbounds": { @@ -732,7 +748,7 @@ def _snapshot_units(): profiles = _awg_profiles() # WARP as an AmneziaWG outbound has no profile to toggle, but it is a unit that can fail (_warp_unit). warp = [] if "warp" in profiles else [_awg_service("warp")] - return [*ALL_SERVICES, SUBSCRIPTION_UPDATE, *map(_awg_service, profiles), *warp] + return [*ALL_SERVICES, SUBSCRIPTION_UPDATE, *map(_awg_service, profiles), *warp, *map(_wl_unit, _wl())] def _status_snapshot(states=None): @@ -2840,6 +2856,56 @@ def cmd_awg(verb="list", *args): usage("awg [list] | on | off [profile] | toggle [profile] | restart [profile]") +# --- wl ----------------------------------------------------------------------- + + +def _wl(): + """whitelist-bypass creators and joiners: [{name, role, platform}].""" + return read_json_or(env("WL_FILE"), []) + + +def _wl_unit(w): + return f"proxy-suite-wb-{w['role']}-{w['name']}" + + +def _wl_link(name): + """The call a creator made and keeps rejoining: the last line it wrote.""" + path = os.path.join(state_dir(), "whitelist-bypass", f"{name}.link") + try: + links = [line.strip() for line in lines(read_text(path)) if line.strip()] + except FileNotFoundError: + links = [] + except PermissionError: + die(f"Cannot read {path} - re-run with sudo.") + if not links: + die(f"{name} has no call yet: {journal_hint(_wl_unit({'role': 'creator', 'name': name}))}") + return links[-1] + + +def cmd_wl(verb="list", *args): + entries = _wl() + named = [w for w in entries if w["name"] == args[0]] if args else entries + if args and not named: + die(f"Unknown whitelist-bypass creator or joiner: {args[0]}") + if verb in ("list", "status"): + if not entries: + print("No whitelist-bypass creators or joiners configured.") + return + print(f" {'NAME':<16} {'ROLE':<8} {'PLATFORM':<10} STATUS") + for w in entries: + print(f" {w['name']:<16} {w['role']:<8} {w['platform']:<10} {svc_state(_wl_unit(w)) or 'unknown'}") + elif verb == "link": + if not args or named[0]["role"] != "creator": + usage("wl link [--qr]") + _emit(_wl_link(args[0]), "--qr" in args) + elif verb in ("on", "off", "toggle", "restart"): + for w in named: + action = {"on": "start", "off": "stop", "restart": "restart"}[_flip(_wl_unit(w), verb)] + must(action, _wl_unit(w)) + else: + usage("wl [list] | link [--qr] | on|off|toggle|restart [name]") + + # --- apps --------------------------------------------------------------------- SLICE_ROUTES = { @@ -3288,6 +3354,7 @@ COMMANDS = { "warp": lambda *args: _toggle(_warp_unit(), "warp", *args), "tor": cmd_tor, "tg": lambda *args: _toggle("proxy-suite-tg-ws-proxy", "tg", *args), + "wl": cmd_wl, "apps": cmd_apps, "inbounds": cmd_inbounds, "where": cmd_where, diff --git a/pkgs/proxy-ctl/test_proxy_ctl.py b/pkgs/proxy-ctl/test_proxy_ctl.py index 687165a..92c1571 100644 --- a/pkgs/proxy-ctl/test_proxy_ctl.py +++ b/pkgs/proxy-ctl/test_proxy_ctl.py @@ -387,6 +387,25 @@ class ServiceManagerTest(EnvTest): with self.assertRaises(SystemExit), contextlib.redirect_stderr(io.StringIO()): ctl.cmd_awg("toggle") + def test_wl(self): + seen = self.calls() + active = {"proxy-suite-wb-joiner-wl"} + self.patch("svc_active", lambda unit: unit in active) + os.environ["WL_FILE"] = self.write("wl.json", [{"name": "phone", "role": "creator", "platform": "dion"}, {"name": "wl", "role": "joiner", "platform": "dion"}]) + os.environ["STATE_DIR"] = self.dir + ctl.cmd_wl("toggle") + ctl.cmd_wl("restart", "wl") + # Toggle flips each one on its own. + self.assertEqual( + [argv[1:3] for argv in seen], + [["start", "proxy-suite-wb-creator-phone"], ["stop", "proxy-suite-wb-joiner-wl"], ["restart", "proxy-suite-wb-joiner-wl"]], + ) + self.assertNotEqual(run(ctl.cmd_wl, "link", "phone")[0], 0) # no call yet + self.write("whitelist-bypass/phone.link", "dion://old\ndion://new\n") + self.assertEqual(ok(ctl.cmd_wl, "link", "phone"), "dion://new\n") + self.assertNotEqual(run(ctl.cmd_wl, "link", "wl")[0], 0) # a joiner has no link to give + self.assertEqual(set(ctl._complete_tree("wl", "link")), {"phone", "--qr"}) + def test_toggle_completes(self): for path in ("ssh", "zapret", "proxy", "proxy tun", "tor"): self.assertLessEqual({"toggle", "restart"}, set(ctl._complete_tree(*path.split()))) diff --git a/pkgs/whitelist-bypass.nix b/pkgs/whitelist-bypass.nix new file mode 100644 index 0000000..dc5b4b7 --- /dev/null +++ b/pkgs/whitelist-bypass.nix @@ -0,0 +1,49 @@ +# whitelist-bypass's headless ends: a tunnel through the media servers of video calls +# (WB Stream, Telemost, DION, Bitrix, VK), which stay reachable under mobile whitelists. +# Creators sit on the free side, joiners expose a SOCKS5 listener on the censored one. +{ pkgs }: + +pkgs.buildGo126Module (finalAttrs: { + pname = "whitelist-bypass"; + version = "0.4.4"; + + src = pkgs.fetchFromGitHub { + owner = "kulikov0"; + repo = "whitelist-bypass"; + rev = "v${finalAttrs.version}"; + hash = "sha256-dYSadPA3X/f61XjpU+DTiLTPkKb3RC18/Z3x/YAOcC0="; + }; + vendorHash = "sha256-zKQzcfYdjKfpsyVgTTRdT0AFGqZQMtStpdVyCrojltE="; + + subPackages = [ + "headless/vk" + "headless/telemost" + "headless/wbstream" + "headless/dion" + "headless/bitrix" + "headless/telemost-joiner" + "headless/wbstream-joiner" + "headless/dion-joiner" + "headless/bitrix-joiner" + ]; + ldflags = [ + "-s" + "-w" + ]; + + # Upstream's names (build-headless.sh): each directory builds one binary. + postInstall = '' + for p in vk telemost wbstream dion bitrix; do + mv "$out/bin/$p" "$out/bin/headless-$p-creator" + done + for p in telemost wbstream dion bitrix; do + mv "$out/bin/$p-joiner" "$out/bin/headless-$p-joiner" + done + ''; + + meta = { + description = "Tunnel through video call media servers to get past mobile internet whitelists"; + homepage = "https://github.com/kulikov0/whitelist-bypass"; + license = pkgs.lib.licenses.mit; + }; +}) diff --git a/scripts/update-pins.sh b/scripts/update-pins.sh index bf0c020..bc5f1bf 100755 --- a/scripts/update-pins.sh +++ b/scripts/update-pins.sh @@ -16,6 +16,7 @@ PINS=( "pkgs/tg-ws-proxy.nix|Flowseal|tg-ws-proxy|rev|tg-ws-proxy" "pkgs/zapret2.nix|bol-van|zapret2|version|zapret2" "pkgs/xray.nix|XTLS|Xray-core|version|xray" + "pkgs/whitelist-bypass.nix|kulikov0|whitelist-bypass|version|whitelist-bypass" ) FAKE_HASH="sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="