MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / TestBuildDialerHTTPProxyCONNECT

Function TestBuildDialerHTTPProxyCONNECT

sdk/proxyutil/proxy_test.go:170–270  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

168}
169
170func TestBuildDialerHTTPProxyCONNECT(t *testing.T) {
171 t.Parallel()
172
173 listener, errListen := net.Listen("tcp", "127.0.0.1:0")
174 if errListen != nil {
175 t.Fatalf("net.Listen returned error: %v", errListen)
176 }
177 defer func() {
178 if errClose := listener.Close(); errClose != nil {
179 t.Errorf("listener.Close returned error: %v", errClose)
180 }
181 }()
182
183 done := make(chan error, 1)
184 go func() {
185 conn, errAccept := listener.Accept()
186 if errAccept != nil {
187 done <- errAccept
188 return
189 }
190 defer func() { _ = conn.Close() }()
191 if errDeadline := conn.SetDeadline(time.Now().Add(5 * time.Second)); errDeadline != nil {
192 done <- errDeadline
193 return
194 }
195
196 req, errRead := http.ReadRequest(bufio.NewReader(conn))
197 if errRead != nil {
198 done <- fmt.Errorf("read CONNECT request failed: %w", errRead)
199 return
200 }
201 if req.Method != http.MethodConnect {
202 done <- fmt.Errorf("method = %s, want CONNECT", req.Method)
203 return
204 }
205 if req.Host != "target.example.com:443" {
206 done <- fmt.Errorf("host = %s, want target.example.com:443", req.Host)
207 return
208 }
209 wantAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("user:pass"))
210 if gotAuth := req.Header.Get("Proxy-Authorization"); gotAuth != wantAuth {
211 done <- fmt.Errorf("Proxy-Authorization = %q, want %q", gotAuth, wantAuth)
212 return
213 }
214
215 if _, errWrite := io.WriteString(conn, "HTTP/1.1 200 Connection Established\r\n\r\nok"); errWrite != nil {
216 done <- fmt.Errorf("write CONNECT response failed: %w", errWrite)
217 return
218 }
219
220 buf := make([]byte, 4)
221 n, errReadTunnel := io.ReadFull(conn, buf)
222 if errReadTunnel != nil {
223 done <- fmt.Errorf("read tunneled payload failed after %d bytes: %w", n, errReadTunnel)
224 return
225 }
226 if string(buf) != "ping" {
227 done <- fmt.Errorf("tunneled payload = %q, want ping", string(buf))

Callers

nothing calls this directly

Calls 9

BuildDialerFunction · 0.85
AcceptMethod · 0.80
WriteStringMethod · 0.80
AddrMethod · 0.80
DialMethod · 0.80
CloseMethod · 0.65
GetMethod · 0.45
StringMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected