fix: multiple fixes

This commit is contained in:
FUFSoB committed 2026-09-17 18:15:55 +05:00
1 parent ffc7816289
commit 97332c1774
23 files changed
+940 -96

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
# One `proxy-ctl proxy auto forget|clear` request, applied under the prober's lock.
# $op "forget": domain $d goes back to direct, and every verdict and strike about it
# goes too, so the next time it is dialled it is probed from scratch.
# $op "clear": the same for every domain. What each exit is (ip, asn), the backlog
# and the egress address stay: they are about the exits and the clients, not a verdict.
def rebad:
(.strikes // {}) as $s
| . + {bad: (($s | length) >= 2),
badBy: ($s | to_entries | sort_by(.value.at) | map("\(.key) \(.value.why)"))};
if $op == "clear" then
.domains = {}
| .hosts = {}
| del(.slowWant, .slowSkip)
| .exits = ((.exits // {}) | map_values(.strikes = {} | rebad))
elif $op == "forget" then
.domains = ((.domains // {}) | del(.[$d]))
| .hosts = ((.hosts // {}) | with_entries(select(.value.domain != $d)))
| .backlog = ((.backlog // {}) | with_entries(select(.value.domain != $d)))
| if .slowWant then .slowWant |= del(.[$d]) else . end
| if .slowSkip then .slowSkip |= del(.[$d]) else . end
| .exits = ((.exits // {}) | map_values(if .strikes then (.strikes |= del(.[$d]) | rebad) else . end))
else . end
+53 -11
View File
@@ -78,7 +78,7 @@ let
set -euo pipefail
export PATH=${bin}
# --requests-only: just the `proxy-ctl proxy auto learn` requests.
# --requests-only: just the `proxy-ctl proxy auto learn|forget|clear` requests.
mode=''${1:-full}
# Overridable to rehearse a run against a scratch copy; the units never set them.
@@ -87,13 +87,10 @@ let
index=$PROBE_EXITS_FILE
state="$state_dir/state.json"
requests="$state_dir/requests"
edits="$state_dir/edits"
now=$(date +%s)
ttl=$(( ${toString apCfg.ttlDays} * 86400 ))
if [ ! -r "$index" ]; then
echo "no probe listeners - is proxy-suite-socks running with autoProxy on?"
exit 0
fi
install -d -m ${stateDirMode} "$state_dir"
# Timer runs and learn requests take turns on the state.
@@ -102,12 +99,14 @@ let
# Leftovers of a run that died partway; taken requests go back in line.
rm -f "$state_dir/verdicts.json" "$state_dir/proxied-domains.txt" "$state".?????? "$state_dir/samples.taking"
if [ -e "$requests.taking" ]; then
cat "$requests.taking" >> "$requests"
rm -f "$requests.taking"
# Made here by root: the group appends to it too.
chmod 0660 "$requests"
fi
for queue in "$requests" "$edits"; do
if [ -e "$queue.taking" ]; then
cat "$queue.taking" >> "$queue"
rm -f "$queue.taking"
# Made here by root: the group appends to it too.
chmod 0660 "$queue"
fi
done
if [ -e "$state" ] && ! jq -e 'type == "object"' "$state" > /dev/null 2>&1; then
echo "$state is unreadable; set aside as $state.broken, starting fresh" >&2
@@ -129,6 +128,49 @@ let
mv -f "$tmp" "$state"
}
# --- 0. forget what `proxy-ctl proxy auto forget|relearn|clear` asked to ---
# Before anything else, and even with the proxy stopped: its start renders the
# rule-sets from the state again.
edited=""
if [ -s "$edits" ]; then
mv -f "$edits" "$edits.taking"
while read -r op dom; do
case "$op" in
forget)
[[ "$dom" =~ ^[a-zA-Z0-9._-]+\.[a-zA-Z]{2,}$ ]] || continue
update --arg op forget --arg d "$dom" -f ${jqFile ./autoproxy-edit.jq}
echo "forgot $dom"
;;
clear)
update --arg op clear --arg d "" -f ${jqFile ./autoproxy-edit.jq}
echo "forgot everything learned"
;;
*) continue ;;
esac
edited=1
done < "$edits.taking"
rm -f "$edits.taking"
fi
if [ ! -r "$index" ]; then
echo "no probe listeners - is proxy-suite-socks running with autoProxy on?"
exit 0
fi
# Routes through an outbound disabled since (`proxy-ctl proxy outbounds disable`)
# are forgotten like the above, so their domains get probed again without it.
inventory="$(dirname "$index")/outbounds.json"
if [ -r "$inventory" ]; then
while IFS=$'\t' read -r dom exit; do
echo "forgot $dom: its exit $exit is disabled"
update --arg op forget --arg d "$dom" -f ${jqFile ./autoproxy-edit.jq}
edited=1
done < <(jq -r --slurpfile inv "$inventory" '($inv[0].disabled // []) as $off
| (.domains // {}) | to_entries[] | select(.value.exit as $e | $off | index([$e]))
| "\(.key)\t\(.value.exit)"' "$state")
fi
[ -z "$edited" ] || ${render} "$index" "$state"
# $1 JSON array of exit tags, $2 registrable domain, $3 wall:<page>|refused|slow.
strike() {
update --argjson tags "$1" --arg d "$2" --arg why "$3" --argjson now "$now" --argjson ttl "$ttl" \
@@ -5,6 +5,7 @@
config,
lib,
pkgs,
proxySuiteUpstream,
...
}:
@@ -84,6 +85,10 @@ in
firewallPackage = null;
resolvconfPackage = null;
};
# An app may not join netlink's route groups: without this sing-box fails to start.
services.proxy-suite.proxy.singBox.package = lib.mkDefault (
import ../../../pkgs/sing-box-rootless-netlink.nix { inherit (proxySuiteUpstream) sing-box; }
);
}
(lib.mkIf cfg.enable {
+2 -2
View File
@@ -32,9 +32,9 @@ in
- "services": turn the proxy-suite units on and off (proxy, tun, tproxy, zapret, ssh, warp, tg, awg, inbounds), and `restart`.
- "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, and update subscriptions.
- "outbounds": add and remove runtime outbounds and subscriptions, disable and enable outbounds, and update subscriptions.
- "secrets": read share links, subscription URLs and the running configs.
- "autoProxy": read what autoProxy learned, and `proxy auto learn`.
- "autoProxy": read what autoProxy learned, and `proxy auto learn|forget|relearn|clear`.
- "zapret": edit zapret2's learned hosts, and `zapret cutoff probe`.
- "stats": `inbounds stats`.
'';
@@ -7,6 +7,7 @@
routeModeStateFile,
pinnedOutboundFile,
outboundInventoryFile,
runtimeOutboundsDir,
subscriptionCacheDir,
subscriptionCacheHelpersBlock,
mkSubscriptionFetchBlock,
@@ -95,6 +96,16 @@ let
echo "proxy-suite: unknown outbound '$tag'" >&2
exit 1
fi
# Disabled means nothing picks it on its own, a pin included.
case "$tag" in
*/* | . | ..) ;;
*)
if [ -e "${runtimeOutboundsDir}/$tag.disabled" ]; then
echo "proxy-suite: outbound '$tag' is disabled; enable it first: proxy-ctl proxy outbounds enable $tag" >&2
exit 1
fi
;;
esac
mkdir -p "$(dirname "${pinnedOutboundFile}")"
printf '%s\n' "$tag" > "${pinnedOutboundFile}"
# Live switch when the running config exposes a selector; the persisted pin
@@ -147,6 +158,21 @@ let
esac
done
# A pin on a disabled outbound goes with it, so enabling the outbound later does not
# bring the pin back. Here rather than through the unpin unit: disabling is the
# outbounds scope's, pinning the routing scope's.
if [ -r "${pinnedOutboundFile}" ]; then
PINNED="$(tr -d '\r\n[:space:]' < "${pinnedOutboundFile}" 2>/dev/null || true)"
case "$PINNED" in
"" | */* | . | ..) ;;
*)
if [ -e "${runtimeOutboundsDir}/$PINNED.disabled" ]; then
rm -f "${pinnedOutboundFile}"
fi
;;
esac
fi
${restartActiveConfigConsumersBlock}
'';
in
@@ -4,7 +4,6 @@
selectionMode,
proxyInboundsGuardPrivate,
userDnsRules ? [ ],
selectionExclude ? [ ],
}:
if pureXrayEnabled then
@@ -72,14 +71,15 @@ if pureXrayEnabled then
| if $route_enabled then
.routing.rules = (xray_preserved_rules + $route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)])
else . end
${lib.optionalString (selectionExclude != [ ]) ''
# The balancer's prefix would take in proxy.selectionExclude too: name the rest instead.
# ponytail: selectors are prefixes still, so "de" also takes in an excluded "de-hop";
# rename one of them if that matters.
| ($xray_selectable | map("proxy-suite-ob-" + .)) as $candidates
| if .routing.balancers then .routing.balancers |= map(.selector = $candidates) else . end
| if .observatory then .observatory.subjectSelector = $candidates else . end
''}
# The balancer's prefix would take in proxy.selectionExclude and disabled outbounds
# too: name the rest instead. Disabling is a runtime decision, so always.
# ponytail: selectors are prefixes still, so "de" also takes in an excluded "de-hop";
# rename one of them if that matters.
| if $ARGS.named.xray_selectable == null then . else
($ARGS.named.xray_selectable | map("proxy-suite-ob-" + .)) as $candidates
| if .routing.balancers then .routing.balancers |= map(.selector = $candidates) else . end
| if .observatory then .observatory.subjectSelector = $candidates else . end
end
| if $xray_single_proxy_tag == "" then
.
else
@@ -496,6 +496,20 @@ let
# entry that went away, say - is dropped rather than left to break the config.
pinBlock = ''
OUTBOUND_TAGS_JSON=$(${jq} -c '[.[].tag]' <<< "$OUTBOUNDS_JSON")
# `proxy-ctl proxy outbounds disable` leaves <tag>.disabled next to the runtime entries:
# the outbound stays, so rules and chains naming it still work, but nothing picks it on
# its own - selection, autoProxy and a pin. Markers for tags that are gone are ignored.
DISABLED_TAGS_JSON='[]'
if [ -d "${runtimeOutboundsDir}" ]; then
for DISABLED_MARKER in "${runtimeOutboundsDir}"/*.disabled; do
[ -e "$DISABLED_MARKER" ] || continue
DISABLED_TAG="''${DISABLED_MARKER##*/}"
DISABLED_TAGS_JSON=$(${jq} -c --arg t "''${DISABLED_TAG%.disabled}" '. + [$t]' <<< "$DISABLED_TAGS_JSON")
done
fi
DISABLED_TAGS_JSON=$(${jq} -c --argjson tags "$OUTBOUND_TAGS_JSON" 'map(select(. as $t | $tags | index([$t]))) | unique' <<< "$DISABLED_TAGS_JSON")
PINNED_OUTBOUND=""
if [ -r "${pinnedOutboundFile}" ]; then
PINNED_OUTBOUND="$(tr -d '\r\n[:space:]' < "${pinnedOutboundFile}" 2>/dev/null || true)"
@@ -505,21 +519,27 @@ let
echo "proxy-suite: warning: pinned outbound '$PINNED_OUTBOUND' is not available; picking automatically" >&2
PINNED_OUTBOUND=""
fi
if [ -n "$PINNED_OUTBOUND" ] \
&& ${jq} -e --arg t "$PINNED_OUTBOUND" 'index($t) != null' <<< "$DISABLED_TAGS_JSON" >/dev/null; then
echo "proxy-suite: warning: pinned outbound '$PINNED_OUTBOUND' is disabled; picking automatically" >&2
PINNED_OUTBOUND=""
fi
'';
# What selection may pick on its own: all but proxy.selectionExclude. A pin and a
# selector switched by hand still reach the excluded ones.
# 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.
selectableBlock = ''
SELECTABLE_TAGS_JSON=$(${jq} -c --argjson ex ${lib.escapeShellArg (builtins.toJSON proxyCfg.selectionExclude)} \
'map(select(. as $t | $ex | index([$t]) | not))' <<< "$OUTBOUND_TAGS_JSON")
--argjson disabled "$DISABLED_TAGS_JSON" \
'map(select(. as $t | ($ex + $disabled) | 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, so there is nothing to select; pin one, or exclude fewer" >&2
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
fi
'';
# proxy-ctl reads this unprivileged: tags, where each came from, the pin, what each one
# chains through, and what selection leaves alone.
# chains through, what selection leaves alone, and what was disabled.
inventoryBlock = ''
${jq} -n \
--argjson tags "$OUTBOUND_TAGS_JSON" \
@@ -529,10 +549,11 @@ let
--argjson obs "$OUTBOUNDS_JSON" \
--argjson xobs "${if hybridEnabled then "$XRAY_OUTBOUNDS_JSON" else "[]"}" \
--argjson selectable "$SELECTABLE_TAGS_JSON" \
--argjson disabled "$DISABLED_TAGS_JSON" \
'{tags: $tags, sources: $sources, pinned: $pinned, selection: $selection,
detours: ([($xobs + $obs)[] | (.detour // .streamSettings.sockopt.dialerProxy?) as $h
| select($h != null) | {key: .tag, value: $h}] | from_entries),
excluded: ($tags - $selectable)}' \
excluded: ($tags - $selectable), disabled: $disabled}' \
> "$RUNTIME_DIR/outbounds.json"
chmod 644 "$RUNTIME_DIR/outbounds.json"
'';
@@ -640,4 +661,6 @@ let
in
{
inherit mkOutboundScript rawOutboundJson;
# For the checks: which outbounds a pin and selection may take.
selectionBlocks = pinBlock + selectableBlock + inventoryBlock;
}
+1 -1
View File
@@ -154,7 +154,6 @@ let
proxyInboundsGuardPrivate
;
userDnsRules = proxyCfg.dns.singBox.rules;
inherit (proxyCfg) selectionExclude;
};
backendJqFilterFile = pkgs.writeText "proxy-suite-core" backendJqFilter;
@@ -234,6 +233,7 @@ let
routeModeStateFile
pinnedOutboundFile
outboundInventoryFile
runtimeOutboundsDir
subscriptionCacheDir
subscriptionCacheHelpersBlock
mkSubscriptionFetchBlock
@@ -205,12 +205,14 @@ let
[ -s "$AUTOPROXY_DIR/state.json" ] || echo '{"domains":{},"hosts":{},"exits":{},"backlog":{}}' > "$AUTOPROXY_DIR/state.json"
# direct is always exit 0; state is keyed by tag, so shifting indices are
# harmless.
# harmless. Disabled outbounds are no exit: the prober drops what was learned
# through them.
PROBE_EXITS_JSON=$(${jq} -c \
--argjson max ${toString proxyCfg.autoProxy.maxExits} \
--argjson base ${toString proxyCfg.autoProxy.probeBasePort} \
--argjson disabled "''${DISABLED_TAGS_JSON:-[]}" \
--arg dir "$AUTOPROXY_DIR" '
(["direct"] + map(select(. != "direct")))[0:$max]
(["direct"] + map(select(. != "direct" and (. as $t | $disabled | index([$t]) | not))))[0:$max]
| to_entries
| map({i: .key, tag: .value, port: ($base + .key),
rule_set: ("autoproxy-" + (.key | tostring)),