|
| 1 | +package testingproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/apex/log" |
| 11 | + "github.com/ooni/netem" |
| 12 | + "github.com/ooni/probe-cli/v3/internal/netxlite" |
| 13 | + "github.com/ooni/probe-cli/v3/internal/runtimex" |
| 14 | + "github.com/ooni/probe-cli/v3/internal/testingsocks5" |
| 15 | + "github.com/ooni/probe-cli/v3/internal/testingx" |
| 16 | +) |
| 17 | + |
| 18 | +// WithNetemSOCKSProxyAndURL returns a [TestCase] where: |
| 19 | +// |
| 20 | +// - we fetch a URL; |
| 21 | +// |
| 22 | +// - using the github.com/ooni.netem; |
| 23 | +// |
| 24 | +// - and a SOCKS5 proxy. |
| 25 | +// |
| 26 | +// Because this [TestCase] uses netem, it also runs in -short mode. |
| 27 | +func WithNetemSOCKSProxyAndURL(URL string) TestCase { |
| 28 | + return &netemTestCaseWithSOCKS{ |
| 29 | + TargetURL: URL, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type netemTestCaseWithSOCKS struct { |
| 34 | + TargetURL string |
| 35 | +} |
| 36 | + |
| 37 | +var _ TestCase = &netemTestCaseWithSOCKS{} |
| 38 | + |
| 39 | +// Name implements TestCase. |
| 40 | +func (tc *netemTestCaseWithSOCKS) Name() string { |
| 41 | + return fmt.Sprintf("fetching %s using netem and a SOCKS5 proxy", tc.TargetURL) |
| 42 | +} |
| 43 | + |
| 44 | +// Run implements TestCase. |
| 45 | +func (tc *netemTestCaseWithSOCKS) Run(t *testing.T) { |
| 46 | + topology := runtimex.Try1(netem.NewStarTopology(log.Log)) |
| 47 | + defer topology.Close() |
| 48 | + |
| 49 | + const ( |
| 50 | + wwwIPAddr = "93.184.216.34" |
| 51 | + proxyIPAddr = "10.0.0.1" |
| 52 | + clientIPAddr = "10.0.0.2" |
| 53 | + ) |
| 54 | + |
| 55 | + // create: |
| 56 | + // |
| 57 | + // - a www stack modeling www.example.com |
| 58 | + // |
| 59 | + // - a proxy stack |
| 60 | + // |
| 61 | + // - a client stack |
| 62 | + // |
| 63 | + // Note that www.example.com's IP address is also the resolver used by everyone |
| 64 | + wwwStack := runtimex.Try1(topology.AddHost(wwwIPAddr, wwwIPAddr, &netem.LinkConfig{})) |
| 65 | + proxyStack := runtimex.Try1(topology.AddHost(proxyIPAddr, wwwIPAddr, &netem.LinkConfig{})) |
| 66 | + clientStack := runtimex.Try1(topology.AddHost(clientIPAddr, wwwIPAddr, &netem.LinkConfig{})) |
| 67 | + |
| 68 | + // configure the wwwStack as the DNS resolver with proper configuration |
| 69 | + dnsConfig := netem.NewDNSConfig() |
| 70 | + dnsConfig.AddRecord("www.example.com.", "", wwwIPAddr) |
| 71 | + dnsServer := runtimex.Try1(netem.NewDNSServer(log.Log, wwwStack, wwwIPAddr, dnsConfig)) |
| 72 | + defer dnsServer.Close() |
| 73 | + |
| 74 | + // configure the wwwStack to respond to HTTP requests on port 80 |
| 75 | + wwwServer80 := testingx.MustNewHTTPServerEx( |
| 76 | + &net.TCPAddr{IP: net.ParseIP(wwwIPAddr), Port: 80}, |
| 77 | + wwwStack, |
| 78 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | + w.Write([]byte("Bonsoir, Elliot!\r\n")) |
| 80 | + }), |
| 81 | + ) |
| 82 | + defer wwwServer80.Close() |
| 83 | + |
| 84 | + // configure the wwwStack to respond to HTTPS requests on port 443 |
| 85 | + wwwServer443 := testingx.MustNewHTTPServerTLSEx( |
| 86 | + &net.TCPAddr{IP: net.ParseIP(wwwIPAddr), Port: 443}, |
| 87 | + wwwStack, |
| 88 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 89 | + w.Write([]byte("Bonsoir, Elliot!\r\n")) |
| 90 | + }), |
| 91 | + wwwStack, |
| 92 | + ) |
| 93 | + defer wwwServer443.Close() |
| 94 | + |
| 95 | + // configure the proxyStack to implement the SOCKS proxy on port 9050 |
| 96 | + proxyServer := testingsocks5.MustNewServer( |
| 97 | + log.Log, |
| 98 | + &netxlite.Netx{ |
| 99 | + Underlying: &netxlite.NetemUnderlyingNetworkAdapter{UNet: proxyStack}}, |
| 100 | + &net.TCPAddr{IP: net.ParseIP(proxyIPAddr), Port: 9050}, |
| 101 | + ) |
| 102 | + defer proxyServer.Close() |
| 103 | + |
| 104 | + // create the netx instance for the client |
| 105 | + netx := &netxlite.Netx{Underlying: &netxlite.NetemUnderlyingNetworkAdapter{UNet: clientStack}} |
| 106 | + |
| 107 | + // create an HTTP client configured to use the given proxy |
| 108 | + // |
| 109 | + // note how we use a dialer that asserts that we're using the proxy IP address |
| 110 | + // rather than the host address, so we're sure that we're using the proxy |
| 111 | + dialer := &dialerWithAssertions{ |
| 112 | + ExpectAddress: proxyIPAddr, |
| 113 | + Dialer: netx.NewDialerWithResolver(log.Log, netx.NewStdlibResolver(log.Log)), |
| 114 | + } |
| 115 | + tlsDialer := netxlite.NewTLSDialer(dialer, netx.NewTLSHandshakerStdlib(log.Log)) |
| 116 | + txp := netxlite.NewHTTPTransportWithOptions(log.Log, dialer, tlsDialer, |
| 117 | + netxlite.HTTPTransportOptionProxyURL(proxyServer.URL()), |
| 118 | + |
| 119 | + // TODO(https://github.com/ooni/probe/issues/2536) |
| 120 | + netxlite.HTTPTransportOptionTLSClientConfig(&tls.Config{ |
| 121 | + RootCAs: runtimex.Try1(clientStack.DefaultCertPool()), |
| 122 | + }), |
| 123 | + ) |
| 124 | + client := &http.Client{Transport: txp} |
| 125 | + defer client.CloseIdleConnections() |
| 126 | + |
| 127 | + // get the homepage and assert we're getting a succesful response |
| 128 | + httpCheckResponse(t, client, tc.TargetURL) |
| 129 | +} |
| 130 | + |
| 131 | +// Short implements TestCase. |
| 132 | +func (tc *netemTestCaseWithSOCKS) Short() bool { |
| 133 | + return true |
| 134 | +} |
0 commit comments