提交记录修改internal/cage/gate_probe.go+16 −2看文件
修改internal/cage/gate_probe_test.go+19 −5看文件
| @@ -32,10 +32,24 @@ func ProbeExitIP(ctx context.Context, cfg Config) (string, error) { | ||
| 32 | 32 | func probeExitIP(ctx context.Context, e *Egress) (string, error) { |
| 33 | 33 | transport := &http.Transport{Proxy: nil, DialContext: e.DialContext, DisableKeepAlives: true} |
| 34 | 34 | defer transport.CloseIdleConnections() |
| 35 | 删除: client := &http.Client{Transport: transport, Timeout: 12 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("probe redirect refused") }} | |
| 35 | 新增: client := &http.Client{Transport: transport, Timeout: 5 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("probe redirect refused") }} | |
| 36 | 36 | // The proxy resolves this name remotely. An IPv4-only service measures the |
| 37 | 37 | // same address family as the guest policy even when the proxy is dual-stack. |
| 38 | 删除: req, _ := http.NewRequestWithContext(ctx, "GET", "https://ipv4.icanhazip.com/", nil) | |
| 38 | 新增: for _, endpoint := range []string{"https://ipv4.icanhazip.com/", "https://api.ipify.org/"} { | |
| 39 | 新增: ip, err := queryExitIP(ctx, client, endpoint) | |
| 40 | 新增: if err == nil { | |
| 41 | 新增: return ip, nil | |
| 42 | 新增: } | |
| 43 | 新增: diagnostics.Log("warn", "exit-probe", fmt.Sprintf("exit query %s failed: %v", endpoint, err)) | |
| 44 | 新增: if ctx.Err() != nil { | |
| 45 | 新增: break | |
| 46 | 新增: } | |
| 47 | 新增: } | |
| 48 | 新增: return "", errors.New("通过所选代理查询出口 IP 失败。请检查节点与网络,稍后重新检测;未能确认出口时,外网保持关闭。") | |
| 49 | 新增:} | |
| 50 | 新增: | |
| 51 | 新增:func queryExitIP(ctx context.Context, client *http.Client, endpoint string) (string, error) { | |
| 52 | 新增: req, _ := http.NewRequestWithContext(ctx, "GET", endpoint, nil) | |
| 39 | 53 | req.Header.Set("User-Agent", "Cage-network-check/1") |
| 40 | 54 | resp, err := client.Do(req) |
| 41 | 55 | if err != nil { |
| @@ -112,6 +112,8 @@ func TestExitProbeUsesIPv4ServiceThroughProxy(t *testing.T) { | ||
| 112 | 112 | status int |
| 113 | 113 | }{ |
| 114 | 114 | {"public-ipv4", "8.8.8.8\n", "8.8.8.8", 200}, |
| 115 | 新增: {"fallback-after-unavailable", "8.8.8.8\n", "8.8.8.8", 503}, | |
| 116 | 新增: {"fallback-after-invalid", "8.8.8.8\n", "8.8.8.8", 200}, | |
| 115 | 117 | {"whitespace", " \r\n1.1.1.1\r\n", "1.1.1.1", 200}, |
| 116 | 118 | {"ipv6", "2606:4700:4700::1111\n", "", 200}, |
| 117 | 119 | {"mapped-ipv4", "::ffff:8.8.8.8", "", 200}, |
| @@ -131,12 +133,20 @@ func TestExitProbeUsesIPv4ServiceThroughProxy(t *testing.T) { | ||
| 131 | 133 | var requests atomic.Int32 |
| 132 | 134 | upstream := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { |
| 133 | 135 | requests.Add(1) |
| 136 | 新增: if (request.Host != "ipv4.icanhazip.com" && request.Host != "api.ipify.org") || request.URL.RequestURI() != "/" || request.Method != "GET" || request.Header.Get("User-Agent") != "Cage-network-check/1" || request.Header.Get("Cookie") != "" || request.Header.Get("Authorization") != "" || request.Header.Get("Proxy-Authorization") != "" { | |
| 137 | 新增: t.Error("probe request changed destination or forwarded guest/proxy data") | |
| 138 | 新增: } | |
| 134 | 139 | if request.Host == "api.ipify.org" { |
| 140 | 新增: if strings.HasPrefix(tc.name, "fallback-") { | |
| 141 | 新增: _, _ = io.WriteString(w, tc.body) | |
| 142 | 新增: return | |
| 143 | 新增: } | |
| 135 | 144 | _, _ = io.WriteString(w, `{"ip":"2606:4700:4700::1111"}`) |
| 136 | 145 | return |
| 137 | 146 | } |
| 138 | 删除: if request.Host != "ipv4.icanhazip.com" || request.URL.RequestURI() != "/" || request.Method != "GET" || request.Header.Get("User-Agent") != "Cage-network-check/1" || request.Header.Get("Cookie") != "" || request.Header.Get("Authorization") != "" || request.Header.Get("Proxy-Authorization") != "" { | |
| 139 | 删除: t.Error("probe request changed destination or forwarded guest/proxy data") | |
| 147 | 新增: if tc.name == "fallback-after-invalid" { | |
| 148 | 新增: _, _ = io.WriteString(w, "192.168.1.1") | |
| 149 | 新增: return | |
| 140 | 150 | } |
| 141 | 151 | w.Header().Set("Content-Type", "text/plain") |
| 142 | 152 | if tc.name == "redirect" { |
| @@ -157,8 +167,12 @@ func TestExitProbeUsesIPv4ServiceThroughProxy(t *testing.T) { | ||
| 157 | 167 | if output, err := command.CombinedOutput(); err != nil { |
| 158 | 168 | t.Fatalf("real probe entry failed: %v\n%s", err, output) |
| 159 | 169 | } |
| 160 | 删除: if connects.Load() != 2 || requests.Load() != 2 { | |
| 161 | 删除: t.Fatalf("host and guest probes must each use the configured proxy once: connects=%d requests=%d", connects.Load(), requests.Load()) | |
| 170 | 新增: wantRequests := int32(2) | |
| 171 | 新增: if tc.want == "" || strings.HasPrefix(tc.name, "fallback-") { | |
| 172 | 新增: wantRequests = 4 | |
| 173 | 新增: } | |
| 174 | 新增: if connects.Load() != wantRequests || requests.Load() != wantRequests { | |
| 175 | 新增: t.Fatalf("host and guest probes must use only the configured proxy: connects=%d requests=%d", connects.Load(), requests.Load()) | |
| 162 | 176 | } |
| 163 | 177 | }) |
| 164 | 178 | } |
| @@ -176,7 +190,7 @@ func TestExitProbeProxyRejectionDoesNotFallbackOrLeakResponse(t *testing.T) { | ||
| 176 | 190 | cfg := DefaultConfig() |
| 177 | 191 | cfg.ProxyURL = proxy.URL |
| 178 | 192 | ip, err := ProbeExitIP(context.Background(), cfg) |
| 179 | 删除: if err == nil || ip != "" || requests.Load() != 1 || strings.Contains(err.Error(), "private-proxy-response") { | |
| 193 | 新增: if err == nil || ip != "" || requests.Load() != 2 || strings.Contains(err.Error(), "private-proxy-response") { | |
| 180 | 194 | t.Fatalf("proxy rejection escaped the configured route or leaked its response: IP=%q error=%v requests=%d", ip, err, requests.Load()) |
| 181 | 195 | } |
| 182 | 196 | } |

