http urls monitor.

proxy.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket
  5. import (
  6. "bufio"
  7. "encoding/base64"
  8. "errors"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. )
  14. type netDialerFunc func(network, addr string) (net.Conn, error)
  15. func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
  16. return fn(network, addr)
  17. }
  18. func init() {
  19. proxy_RegisterDialerType("http", func(proxyURL *url.URL, forwardDialer proxy_Dialer) (proxy_Dialer, error) {
  20. return &httpProxyDialer{proxyURL: proxyURL, fowardDial: forwardDialer.Dial}, nil
  21. })
  22. }
  23. type httpProxyDialer struct {
  24. proxyURL *url.URL
  25. fowardDial func(network, addr string) (net.Conn, error)
  26. }
  27. func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error) {
  28. hostPort, _ := hostPortNoPort(hpd.proxyURL)
  29. conn, err := hpd.fowardDial(network, hostPort)
  30. if err != nil {
  31. return nil, err
  32. }
  33. connectHeader := make(http.Header)
  34. if user := hpd.proxyURL.User; user != nil {
  35. proxyUser := user.Username()
  36. if proxyPassword, passwordSet := user.Password(); passwordSet {
  37. credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
  38. connectHeader.Set("Proxy-Authorization", "Basic "+credential)
  39. }
  40. }
  41. connectReq := &http.Request{
  42. Method: "CONNECT",
  43. URL: &url.URL{Opaque: addr},
  44. Host: addr,
  45. Header: connectHeader,
  46. }
  47. if err := connectReq.Write(conn); err != nil {
  48. conn.Close()
  49. return nil, err
  50. }
  51. // Read response. It's OK to use and discard buffered reader here becaue
  52. // the remote server does not speak until spoken to.
  53. br := bufio.NewReader(conn)
  54. resp, err := http.ReadResponse(br, connectReq)
  55. if err != nil {
  56. conn.Close()
  57. return nil, err
  58. }
  59. if resp.StatusCode != 200 {
  60. conn.Close()
  61. f := strings.SplitN(resp.Status, " ", 2)
  62. return nil, errors.New(f[1])
  63. }
  64. return conn, nil
  65. }