提交记录修改internal/managedproxy/fetch.go+59 −27看文件
修改internal/managedproxy/fetch_test.go+74 −4看文件
| @@ -3,6 +3,7 @@ package managedproxy | ||
| 3 | 3 | import ( |
| 4 | 4 | "bytes" |
| 5 | 5 | "context" |
| 6 | 新增: "crypto/tls" | |
| 6 | 7 | "errors" |
| 7 | 8 | "io" |
| 8 | 9 | "net" |
| @@ -14,35 +15,73 @@ import ( | ||
| 14 | 15 | "time" |
| 15 | 16 | |
| 16 | 17 | "github.com/miekg/dns" |
| 18 | 新增: "github.com/sylvinus/agent-vm/internal/hostproxy" | |
| 17 | 19 | ) |
| 18 | 20 | |
| 19 | 删除:// BootstrapClient is for explicit infrastructure downloads only. It ignores | |
| 20 | 删除:// environment proxies and resolves public hosts over IP-addressed DoH URLs. | |
| 21 | 新增:// BootstrapClient serves only explicitly host-routed subscription requests. | |
| 21 | 22 | func BootstrapClient() *http.Client { |
| 22 | 删除: tr := &http.Transport{Proxy: nil, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | |
| 23 | 删除: host, port, err := net.SplitHostPort(addr) | |
| 24 | 删除: if err != nil { | |
| 25 | 删除: return nil, err | |
| 26 | 删除: } | |
| 27 | 删除: ip, err := netip.ParseAddr(host) | |
| 28 | 删除: if err != nil { | |
| 29 | 删除: ip, err = resolveInfrastructure(ctx, host) | |
| 30 | 删除: if err != nil { | |
| 31 | 删除: return nil, err | |
| 32 | 删除: } | |
| 33 | 删除: } | |
| 34 | 删除: if !publicIP(ip) { | |
| 35 | 删除: return nil, errors.New("订阅服务器必须使用公网地址") | |
| 36 | 删除: } | |
| 37 | 删除: return (&net.Dialer{Timeout: 10 * time.Second}).DialContext(ctx, network, net.JoinHostPort(ip.String(), port)) | |
| 38 | 删除: }, IdleConnTimeout: 10 * time.Second} | |
| 39 | 删除: return &http.Client{Transport: tr, Timeout: 30 * time.Second, CheckRedirect: func(r *http.Request, via []*http.Request) error { | |
| 23 | 新增: return &http.Client{Transport: subscriptionHostTransport{hostproxy.NewTransport(), BootstrapDNSURLs()}, Timeout: 30 * time.Second, CheckRedirect: func(r *http.Request, via []*http.Request) error { | |
| 40 | 24 | if len(via) >= 3 || r.URL.Scheme != "https" || r.URL.User != nil { |
| 41 | 25 | return errors.New("订阅重定向被拒绝") |
| 42 | 26 | } |
| 43 | 27 | return nil |
| 44 | 28 | }} |
| 45 | 29 | } |
| 30 | 新增: | |
| 31 | 新增:type subscriptionHostTransport struct { | |
| 32 | 新增: host *http.Transport | |
| 33 | 新增: resolvers []string | |
| 34 | 新增:} | |
| 35 | 新增: | |
| 36 | 新增:func (t subscriptionHostTransport) RoundTrip(req *http.Request) (*http.Response, error) { | |
| 37 | 新增: // Resolve routing for the original hostname (including on redirects). | |
| 38 | 新增: // Pin both DoH and the subscription transfer to the selected route. | |
| 39 | 新增: routing := req.Clone(req.Context()) | |
| 40 | 新增: routing.URL.Path, routing.URL.RawPath, routing.URL.RawQuery = "", "", "" | |
| 41 | 新增: route, err := t.host.Proxy(routing) | |
| 42 | 新增: if err != nil { | |
| 43 | 新增: return nil, err | |
| 44 | 新增: } | |
| 45 | 新增: tr := t.host.Clone() | |
| 46 | 新增: tr.Proxy = http.ProxyURL(route) | |
| 47 | 新增: tr.DisableKeepAlives = true | |
| 48 | 新增: host := req.URL.Hostname() | |
| 49 | 新增: ip, err := netip.ParseAddr(host) | |
| 50 | 新增: if err != nil { | |
| 51 | 新增: dnsClient := &http.Client{Transport: tr, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("DNS redirect refused") }} | |
| 52 | 新增: ip, err = resolveInfrastructureWith(req.Context(), host, dnsClient, t.resolvers) | |
| 53 | 新增: if err != nil { | |
| 54 | 新增: return nil, err | |
| 55 | 新增: } | |
| 56 | 新增: } | |
| 57 | 新增: if !publicIP(ip) { | |
| 58 | 新增: return nil, errors.New("订阅服务器必须使用公网地址") | |
| 59 | 新增: } | |
| 60 | 新增: | |
| 61 | 新增: // CONNECT (or SOCKS) must address the validated IP, while HTTP Host and | |
| 62 | 新增: // TLS SNI/certificate checks still use the subscription's original name. | |
| 63 | 新增: // DoH requests may still be finishing, so configure a separate transport. | |
| 64 | 新增: target := tr.Clone() | |
| 65 | 新增: if target.TLSClientConfig == nil { | |
| 66 | 新增: target.TLSClientConfig = &tls.Config{} | |
| 67 | 新增: } | |
| 68 | 新增: if route != nil && route.Scheme == "https" { | |
| 69 | 新增: // The outer TLS handshake authenticates the proxy, independently of | |
| 70 | 新增: // the inner TLS session established through its CONNECT tunnel. | |
| 71 | 新增: outer := target.TLSClientConfig.Clone() | |
| 72 | 新增: outer.ServerName, outer.NextProtos = route.Hostname(), []string{"http/1.1"} | |
| 73 | 新增: target.DialTLSContext = (&tls.Dialer{Config: outer, NetDialer: &net.Dialer{Timeout: 10 * time.Second}}).DialContext | |
| 74 | 新增: } | |
| 75 | 新增: target.TLSClientConfig.ServerName = host | |
| 76 | 新增: pinned := req.Clone(req.Context()) | |
| 77 | 新增: pinned.Host = req.URL.Host | |
| 78 | 新增: pinned.URL.Host = ip.String() | |
| 79 | 新增: if port := req.URL.Port(); port != "" { | |
| 80 | 新增: pinned.URL.Host = net.JoinHostPort(ip.String(), port) | |
| 81 | 新增: } | |
| 82 | 新增: return target.RoundTrip(pinned) | |
| 83 | 新增:} | |
| 84 | 新增: | |
| 46 | 85 | func usageSummary(raw string) string { |
| 47 | 86 | parts := []string{} |
| 48 | 87 | for _, field := range strings.Split(raw, ";") { |
| @@ -69,13 +108,6 @@ func BootstrapDNSURLs() []string { | ||
| 69 | 108 | return []string{"https://1.1.1.1/dns-query", "https://223.5.5.5/dns-query"} |
| 70 | 109 | } |
| 71 | 110 | |
| 72 | 删除:func resolveInfrastructure(ctx context.Context, host string) (netip.Addr, error) { | |
| 73 | 删除: tr := &http.Transport{Proxy: nil} | |
| 74 | 删除: defer tr.CloseIdleConnections() | |
| 75 | 删除: c := &http.Client{Transport: tr, Timeout: 8 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("DNS redirect refused") }} | |
| 76 | 删除: return resolveInfrastructureWith(ctx, host, c, BootstrapDNSURLs()) | |
| 77 | 删除:} | |
| 78 | 删除: | |
| 79 | 111 | func resolveInfrastructureWith(ctx context.Context, host string, client *http.Client, servers []string) (netip.Addr, error) { |
| 80 | 112 | ctx, cancel := context.WithTimeout(ctx, 8*time.Second) |
| 81 | 113 | defer cancel() |
| @@ -7,9 +7,11 @@ import ( | ||
| 7 | 7 | "net" |
| 8 | 8 | "net/http" |
| 9 | 9 | "net/http/httptest" |
| 10 | 新增: "net/url" | |
| 10 | 11 | "os" |
| 11 | 12 | "reflect" |
| 12 | 13 | "strings" |
| 14 | 新增: "sync/atomic" | |
| 13 | 15 | "testing" |
| 14 | 16 | "time" |
| 15 | 17 | |
| @@ -163,7 +165,10 @@ func TestInfrastructureDNSLiveIntegration(t *testing.T) { | ||
| 163 | 165 | if os.Getenv("CAGE_TEST_BOOTSTRAP") != "1" { |
| 164 | 166 | t.Skip("explicit host-network DNS test required") |
| 165 | 167 | } |
| 166 | 删除: ip, err := resolveInfrastructure(context.Background(), "example.com") | |
| 168 | 新增: tr := &http.Transport{Proxy: nil} | |
| 169 | 新增: defer tr.CloseIdleConnections() | |
| 170 | 新增: client := &http.Client{Transport: tr, Timeout: 8 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("redirect refused") }} | |
| 171 | 新增: ip, err := resolveInfrastructureWith(context.Background(), "example.com", client, BootstrapDNSURLs()) | |
| 167 | 172 | if err != nil || !publicIP(ip) { |
| 168 | 173 | t.Fatal("redundant encrypted DNS lookup failed", err) |
| 169 | 174 | } |
| @@ -171,9 +176,6 @@ func TestInfrastructureDNSLiveIntegration(t *testing.T) { | ||
| 171 | 176 | // on this network; successful combined resolution is the required result. |
| 172 | 177 | for _, endpoint := range BootstrapDNSURLs() { |
| 173 | 178 | t.Run(endpoint, func(t *testing.T) { |
| 174 | 删除: tr := &http.Transport{Proxy: nil} | |
| 175 | 删除: defer tr.CloseIdleConnections() | |
| 176 | 删除: client := &http.Client{Transport: tr, Timeout: 8 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("redirect refused") }} | |
| 177 | 179 | ip, err := resolveInfrastructureWith(context.Background(), "example.com", client, []string{endpoint}) |
| 178 | 180 | if err != nil || !publicIP(ip) { |
| 179 | 181 | t.Log("DNS endpoint unavailable on this network", err) |
| @@ -183,3 +185,71 @@ func TestInfrastructureDNSLiveIntegration(t *testing.T) { | ||
| 183 | 185 | }) |
| 184 | 186 | } |
| 185 | 187 | } |
| 188 | 新增: | |
| 189 | 新增:func TestHostSubscriptionUsesChosenProxyAndPinnedPublicIP(t *testing.T) { | |
| 190 | 新增: resolver := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { writeDNSAnswer(t, w, r, nil) })) | |
| 191 | 新增: defer resolver.Close() | |
| 192 | 新增: origin := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 193 | 新增: if r.Host != "example.com" || r.TLS.ServerName != "example.com" { | |
| 194 | 新增: t.Error("subscription authority or TLS name changed") | |
| 195 | 新增: } | |
| 196 | 新增: io.WriteString(w, "proxies: []") | |
| 197 | 新增: })) | |
| 198 | 新增: defer origin.Close() | |
| 199 | 新增: for _, encrypted := range []bool{false, true} { | |
| 200 | 新增: var dnsCalls, sourceCalls atomic.Int32 | |
| 201 | 新增: proxy := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 202 | 新增: if r.Method != "CONNECT" { | |
| 203 | 新增: http.Error(w, "unexpected proxy method", 400) | |
| 204 | 新增: return | |
| 205 | 新增: } | |
| 206 | 新增: var upstream string | |
| 207 | 新增: switch r.Host { | |
| 208 | 新增: case resolver.Listener.Addr().String(): | |
| 209 | 新增: upstream = r.Host | |
| 210 | 新增: dnsCalls.Add(1) | |
| 211 | 新增: case "8.8.8.8:443": | |
| 212 | 新增: upstream = origin.Listener.Addr().String() | |
| 213 | 新增: sourceCalls.Add(1) | |
| 214 | 新增: default: | |
| 215 | 新增: t.Errorf("unvalidated proxy destination %s", r.Host) | |
| 216 | 新增: http.Error(w, "bad target", 400) | |
| 217 | 新增: return | |
| 218 | 新增: } | |
| 219 | 新增: conn, err := net.Dial("tcp", upstream) | |
| 220 | 新增: if err != nil { | |
| 221 | 新增: t.Error(err) | |
| 222 | 新增: return | |
| 223 | 新增: } | |
| 224 | 新增: defer conn.Close() | |
| 225 | 新增: downstream, buf, err := w.(http.Hijacker).Hijack() | |
| 226 | 新增: if err != nil { | |
| 227 | 新增: t.Error(err) | |
| 228 | 新增: return | |
| 229 | 新增: } | |
| 230 | 新增: defer downstream.Close() | |
| 231 | 新增: buf.WriteString("HTTP/1.1 200 Connection Established\r\n\r\n") | |
| 232 | 新增: buf.Flush() | |
| 233 | 新增: go io.Copy(conn, buf) | |
| 234 | 新增: io.Copy(downstream, conn) | |
| 235 | 新增: })) | |
| 236 | 新增: if encrypted { | |
| 237 | 新增: proxy.StartTLS() | |
| 238 | 新增: } else { | |
| 239 | 新增: proxy.Start() | |
| 240 | 新增: } | |
| 241 | 新增: defer proxy.Close() | |
| 242 | 新增: addr, _ := url.Parse(proxy.URL) | |
| 243 | 新增: host := origin.Client().Transport.(*http.Transport).Clone() | |
| 244 | 新增: host.ForceAttemptHTTP2 = true | |
| 245 | 新增: host.Proxy = http.ProxyURL(addr) | |
| 246 | 新增: client := &http.Client{Transport: subscriptionHostTransport{host, []string{resolver.URL}}} | |
| 247 | 新增: data, _, err := Fetch(t.Context(), client, "https://example.com/test-token") | |
| 248 | 新增: if err != nil || string(data) != "proxies: []" || dnsCalls.Load() != 1 || sourceCalls.Load() != 1 { | |
| 249 | 新增: t.Fatalf("encrypted=%v data=%q err=%v dns=%d source=%d", encrypted, data, err, dnsCalls.Load(), sourceCalls.Load()) | |
| 250 | 新增: } | |
| 251 | 新增: if _, _, err = Fetch(t.Context(), client, "https://127.0.0.1/private"); err == nil { | |
| 252 | 新增: t.Fatal("private destination accepted") | |
| 253 | 新增: } | |
| 254 | 新增: } | |
| 255 | 新增:} | |

