(w http.ResponseWriter, r *http.Request)
| 448 | } |
| 449 | |
| 450 | func (h *httpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 451 | if r.Method != "CONNECT" { |
| 452 | if !h.allowHTTP { |
| 453 | http.Error(w, "http proxy not allowed", 500) |
| 454 | return |
| 455 | } |
| 456 | h.rp.ServeHTTP(w, r) |
| 457 | return |
| 458 | } |
| 459 | |
| 460 | if !h.allowConnect { |
| 461 | http.Error(w, "connect not allowed", 500) |
| 462 | return |
| 463 | } |
| 464 | |
| 465 | dst := r.RequestURI |
| 466 | c, err := h.dialAndRecord(context.Background(), "tcp", dst) |
| 467 | if err != nil { |
| 468 | http.Error(w, err.Error(), 500) |
| 469 | return |
| 470 | } |
| 471 | defer c.Close() |
| 472 | |
| 473 | cc, ccbuf, err := w.(http.Hijacker).Hijack() |
| 474 | if err != nil { |
| 475 | http.Error(w, err.Error(), 500) |
| 476 | return |
| 477 | } |
| 478 | defer cc.Close() |
| 479 | |
| 480 | io.WriteString(cc, "HTTP/1.1 200 OK\r\n\r\n") |
| 481 | |
| 482 | errc := make(chan error, 1) |
| 483 | go func() { |
| 484 | _, err := io.Copy(cc, c) |
| 485 | errc <- err |
| 486 | }() |
| 487 | go func() { |
| 488 | _, err := io.Copy(c, ccbuf) |
| 489 | errc <- err |
| 490 | }() |
| 491 | <-errc |
| 492 | } |
| 493 | |
| 494 | func (h *httpProxy) dialAndRecord(ctx context.Context, network, addr string) (net.Conn, error) { |
| 495 | var d net.Dialer |
no test coverage detected