#!/bin/bash
# ============================================================================
# srg-proxy — 通过 mgraftcp 强制代理单条命令
# ============================================================================
# 用法: srg-proxy <command> [args...]
#
# 适用于不识别 HTTP_PROXY 环境变量的程序（如某些编译型二进制）。
# 通过 LD_PRELOAD + FakeDNS 实现进程级透明代理。
# ============================================================================

# ── Port auto-detection ─────────────────────────────────────────────
_srg_detect_port() {
    local base_port="__SRG_PORT__"
    if [ -n "${SRG_PROXY_PORT:-}" ]; then
        echo "$SRG_PROXY_PORT"
        return 0
    fi
    if curl --connect-timeout 1 -x "http://127.0.0.1:$base_port" http://localhost -so /dev/null 2>/dev/null; then
        echo "$base_port"
        return 0
    fi
    for offset in $(seq 1 10); do
        local test_port=$((base_port + offset))
        if curl --connect-timeout 1 -x "http://127.0.0.1:$test_port" http://localhost -so /dev/null 2>/dev/null; then
            echo "⚠ 端口自动探测: 使用 $test_port (配置端口 $base_port 不可达)" >&2
            echo "$test_port"
            return 0
        fi
    done
    echo "⚠ 代理端口 $base_port-$((base_port + 10)) 均不可达，SSH 隧道可能未建立" >&2
    echo "$base_port"
    return 1
}

_SRG_PORT=$(_srg_detect_port)
_SRG_TYPE="${SRG_PROXY_TYPE:-__SRG_TYPE__}"
_SRG_ADDR="127.0.0.1:$_SRG_PORT"

MGRAFTCP="$HOME/bin/mgraftcp-fakedns"

if [ ! -x "$MGRAFTCP" ]; then
    echo "✗ mgraftcp-fakedns 未找到: $MGRAFTCP" >&2
    echo "  运行 srg setup 安装，或手动编译 graftcp" >&2
    exit 1
fi

if [ $# -eq 0 ]; then
    echo "用法: srg-proxy <command> [args...]"
    echo "示例: srg-proxy curl https://www.google.com"
    exit 1
fi

# Go 程序需要 cgo DNS 解析器 (LD_PRELOAD 必需)
export GODEBUG="${GODEBUG:+$GODEBUG,}netdns=cgo"

if [ "$_SRG_TYPE" = "socks5" ]; then
    exec "$MGRAFTCP" --socks5 "$_SRG_ADDR" "$@"
else
    exec "$MGRAFTCP" --http_proxy "$_SRG_ADDR" "$@"
fi

