|
| 1 | +package wireproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net" |
| 11 | + "net/http" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +const proxyAuthHeaderKey = "Proxy-Authorization" |
| 16 | + |
| 17 | +type HTTPServer struct { |
| 18 | + config *HTTPConfig |
| 19 | + |
| 20 | + auth CredentialValidator |
| 21 | + dial func(network, address string) (net.Conn, error) |
| 22 | + |
| 23 | + authRequired bool |
| 24 | +} |
| 25 | + |
| 26 | +func (s *HTTPServer) authenticate(req *http.Request) (int, error) { |
| 27 | + if !s.authRequired { |
| 28 | + return 0, nil |
| 29 | + } |
| 30 | + |
| 31 | + auth := req.Header.Get(proxyAuthHeaderKey) |
| 32 | + if auth != "" { |
| 33 | + enc := strings.TrimPrefix(auth, "Basic ") |
| 34 | + str, err := base64.StdEncoding.DecodeString(enc) |
| 35 | + if err != nil { |
| 36 | + return http.StatusNotAcceptable, fmt.Errorf("decode username and password failed: %w", err) |
| 37 | + } |
| 38 | + pairs := bytes.SplitN(str, []byte(":"), 2) |
| 39 | + if len(pairs) != 2 { |
| 40 | + return http.StatusLengthRequired, fmt.Errorf("username and password format invalid") |
| 41 | + } |
| 42 | + if s.auth.Valid(string(pairs[0]), string(pairs[1])) { |
| 43 | + return 0, nil |
| 44 | + } |
| 45 | + return http.StatusUnauthorized, fmt.Errorf("username and password not matching") |
| 46 | + } |
| 47 | + |
| 48 | + return http.StatusProxyAuthRequired, fmt.Errorf(http.StatusText(http.StatusProxyAuthRequired)) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *HTTPServer) handleConn(req *http.Request, conn net.Conn) (peer net.Conn, err error) { |
| 52 | + addr := req.Host |
| 53 | + if !strings.Contains(addr, ":") { |
| 54 | + port := "443" |
| 55 | + addr = net.JoinHostPort(addr, port) |
| 56 | + } |
| 57 | + |
| 58 | + peer, err = s.dial("tcp", addr) |
| 59 | + if err != nil { |
| 60 | + return peer, fmt.Errorf("tun tcp dial failed: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + _, err = conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) |
| 64 | + if err != nil { |
| 65 | + peer.Close() |
| 66 | + peer = nil |
| 67 | + } |
| 68 | + |
| 69 | + return |
| 70 | +} |
| 71 | + |
| 72 | +func (s *HTTPServer) handle(req *http.Request) (peer net.Conn, err error) { |
| 73 | + addr := req.Host |
| 74 | + if !strings.Contains(addr, ":") { |
| 75 | + port := "80" |
| 76 | + addr = net.JoinHostPort(addr, port) |
| 77 | + } |
| 78 | + |
| 79 | + peer, err = s.dial("tcp", addr) |
| 80 | + if err != nil { |
| 81 | + return peer, fmt.Errorf("tun tcp dial failed: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + err = req.Write(peer) |
| 85 | + if err != nil { |
| 86 | + peer.Close() |
| 87 | + peer = nil |
| 88 | + return peer, fmt.Errorf("conn write failed: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + return |
| 92 | +} |
| 93 | + |
| 94 | +func (s *HTTPServer) serve(conn net.Conn) error { |
| 95 | + defer conn.Close() |
| 96 | + |
| 97 | + var rd io.Reader = bufio.NewReader(conn) |
| 98 | + req, err := http.ReadRequest(rd.(*bufio.Reader)) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("read request failed: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + code, err := s.authenticate(req) |
| 104 | + if err != nil { |
| 105 | + _ = responseWith(req, code).Write(conn) |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + var peer net.Conn |
| 110 | + switch req.Method { |
| 111 | + case http.MethodConnect: |
| 112 | + peer, err = s.handleConn(req, conn) |
| 113 | + case http.MethodGet: |
| 114 | + peer, err = s.handle(req) |
| 115 | + default: |
| 116 | + _ = responseWith(req, http.StatusMethodNotAllowed).Write(conn) |
| 117 | + return fmt.Errorf("unsupported protocol: %s", req.Method) |
| 118 | + } |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("dial proxy failed: %w", err) |
| 121 | + } |
| 122 | + if peer == nil { |
| 123 | + return fmt.Errorf("dial proxy failed: peer nil") |
| 124 | + } |
| 125 | + defer peer.Close() |
| 126 | + |
| 127 | + go func() { |
| 128 | + defer peer.Close() |
| 129 | + defer conn.Close() |
| 130 | + _, _ = io.Copy(conn, peer) |
| 131 | + }() |
| 132 | + _, err = io.Copy(peer, conn) |
| 133 | + |
| 134 | + return err |
| 135 | +} |
| 136 | + |
| 137 | +// ListenAndServe is used to create a listener and serve on it |
| 138 | +func (s *HTTPServer) ListenAndServe(network, addr string) error { |
| 139 | + server, err := net.Listen("tcp", s.config.BindAddress) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("listen tcp failed: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + for { |
| 145 | + conn, err := server.Accept() |
| 146 | + if err != nil { |
| 147 | + return fmt.Errorf("accept request failed: %w", err) |
| 148 | + } |
| 149 | + go func(conn net.Conn) { |
| 150 | + err = s.serve(conn) |
| 151 | + if err != nil { |
| 152 | + log.Println(err) |
| 153 | + } |
| 154 | + }(conn) |
| 155 | + } |
| 156 | +} |
0 commit comments