| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | //go:build !frps |
| 16 |
| 17 | package client |
| 18 |
| 19 | import ( |
| 20 | "bufio" |
| 21 | "context" |
| 22 | "encoding/base64" |
| 23 | "io" |
| 24 | "net" |
| 25 | "net/http" |
| 26 | "strings" |
| 27 | "time" |
| 28 |
| 29 | libio "github.com/fatedier/golib/io" |
| 30 | libnet "github.com/fatedier/golib/net" |
| 31 |
| 32 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 33 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 34 | "github.com/fatedier/frp/pkg/util/util" |
| 35 | ) |
| 36 |
| 37 | func init() { |
| 38 | Register(v1.PluginHTTPProxy, NewHTTPProxyPlugin) |
| 39 | } |
| 40 |
| 41 | type HTTPProxy struct { |
| 42 | opts *v1.HTTPProxyPluginOptions |
| 43 |
| 44 | l *Listener |
| 45 | s *http.Server |
| 46 | } |
| 47 |
| 48 | const httpProxyReadHeaderTimeout = 60 * time.Second |
| 49 |
| 50 | func NewHTTPProxyPlugin(_ PluginContext, options v1.ClientPluginOptions) (Plugin, error) { |
| 51 | opts := options.(*v1.HTTPProxyPluginOptions) |
| 52 | listener := NewProxyListener() |
| 53 |
| 54 | hp := &HTTPProxy{ |
| 55 | l: listener, |
| 56 | opts: opts, |
| 57 | } |
| 58 |
| 59 | hp.s = &http.Server{ |
| 60 | Handler: hp, |
| 61 | ReadHeaderTimeout: httpProxyReadHeaderTimeout, |
| 62 | } |
| 63 |
| 64 | go func() { |
| 65 | _ = hp.s.Serve(listener) |
| 66 | }() |
| 67 | return hp, nil |
| 68 | } |
| 69 |
| 70 | func (hp *HTTPProxy) Name() string { |
| 71 | return v1.PluginHTTPProxy |
| 72 | } |
| 73 |
| 74 | func (hp *HTTPProxy) Handle(_ context.Context, connInfo *ConnectionInfo) { |
| 75 | wrapConn := netpkg.WrapReadWriteCloserToConn(connInfo.Conn, connInfo.UnderlyingConn) |
| 76 |
| 77 | sc, rd := libnet.NewSharedConn(wrapConn) |
| 78 | firstBytes := make([]byte, len(http.MethodConnect)) |
| 79 | _ = wrapConn.SetReadDeadline(time.Now().Add(httpProxyReadHeaderTimeout)) |
| 80 | _, err := io.ReadFull(rd, firstBytes) |
| 81 | if err != nil { |
| 82 | _ = wrapConn.SetReadDeadline(time.Time{}) |
| 83 | wrapConn.Close() |
| 84 | return |
| 85 | } |
| 86 |
| 87 | if strings.EqualFold(string(firstBytes), http.MethodConnect) { |
| 88 | bufRd := bufio.NewReader(sc) |
| 89 | request, err := http.ReadRequest(bufRd) |
| 90 | _ = wrapConn.SetReadDeadline(time.Time{}) |
| 91 | if err != nil { |
| 92 | wrapConn.Close() |
| 93 | return |
| 94 | } |
| 95 | hp.handleConnectReq(request, libio.WrapReadWriteCloser(bufRd, wrapConn, wrapConn.Close)) |
| 96 | return |
| 97 | } |
| 98 |
| 99 | _ = wrapConn.SetReadDeadline(time.Time{}) |
| 100 | _ = hp.l.PutConn(sc) |
| 101 | } |
| 102 |
| 103 | func (hp *HTTPProxy) Close() error { |
| 104 | hp.s.Close() |
| 105 | hp.l.Close() |
| 106 | return nil |
| 107 | } |
| 108 |
| 109 | func (hp *HTTPProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { |
| 110 | if ok := hp.Auth(req); !ok { |
| 111 | rw.Header().Set("Proxy-Authenticate", "Basic") |
| 112 | rw.WriteHeader(http.StatusProxyAuthRequired) |
| 113 | return |
| 114 | } |
| 115 |
| 116 | hp.HTTPHandler(rw, req) |
| 117 | } |
| 118 |
| 119 | func (hp *HTTPProxy) HTTPHandler(rw http.ResponseWriter, req *http.Request) { |
| 120 | removeProxyHeaders(req) |
| 121 |
| 122 | resp, err := http.DefaultTransport.RoundTrip(req) |
| 123 | if err != nil { |
| 124 | http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 125 | return |
| 126 | } |
| 127 | defer resp.Body.Close() |
| 128 |
| 129 | copyHeaders(rw.Header(), resp.Header) |
| 130 | rw.WriteHeader(resp.StatusCode) |
| 131 |
| 132 | _, err = io.Copy(rw, resp.Body) |
| 133 | if err != nil && err != io.EOF { |
| 134 | return |
| 135 | } |
| 136 | } |
| 137 |
| 138 | func (hp *HTTPProxy) Auth(req *http.Request) bool { |
| 139 | if hp.opts.HTTPUser == "" && hp.opts.HTTPPassword == "" { |
| 140 | return true |
| 141 | } |
| 142 |
| 143 | s := strings.SplitN(req.Header.Get("Proxy-Authorization"), " ", 2) |
| 144 | if len(s) != 2 { |
| 145 | return false |
| 146 | } |
| 147 |
| 148 | b, err := base64.StdEncoding.DecodeString(s[1]) |
| 149 | if err != nil { |
| 150 | return false |
| 151 | } |
| 152 |
| 153 | pair := strings.SplitN(string(b), ":", 2) |
| 154 | if len(pair) != 2 { |
| 155 | return false |
| 156 | } |
| 157 |
| 158 | if !util.ConstantTimeEqString(pair[0], hp.opts.HTTPUser) || |
| 159 | !util.ConstantTimeEqString(pair[1], hp.opts.HTTPPassword) { |
| 160 | time.Sleep(200 * time.Millisecond) |
| 161 | return false |
| 162 | } |
| 163 | return true |
| 164 | } |
| 165 |
| 166 | func (hp *HTTPProxy) handleConnectReq(req *http.Request, rwc io.ReadWriteCloser) { |
| 167 | defer rwc.Close() |
| 168 | if ok := hp.Auth(req); !ok { |
| 169 | res := getBadResponse() |
| 170 | _ = res.Write(rwc) |
| 171 | if res.Body != nil { |
| 172 | res.Body.Close() |
| 173 | } |
| 174 | return |
| 175 | } |
| 176 |
| 177 | remote, err := net.Dial("tcp", req.URL.Host) |
| 178 | if err != nil { |
| 179 | res := &http.Response{ |
| 180 | StatusCode: 400, |
| 181 | Proto: "HTTP/1.1", |
| 182 | ProtoMajor: 1, |
| 183 | ProtoMinor: 1, |
| 184 | } |
| 185 | _ = res.Write(rwc) |
| 186 | return |
| 187 | } |
| 188 | _, _ = rwc.Write([]byte("HTTP/1.1 200 OK\r\n\r\n")) |
| 189 |
| 190 | libio.Join(remote, rwc) |
| 191 | } |
| 192 |
| 193 | func copyHeaders(dst, src http.Header) { |
| 194 | for key, values := range src { |
| 195 | for _, value := range values { |
| 196 | dst.Add(key, value) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 |
| 201 | func removeProxyHeaders(req *http.Request) { |
| 202 | req.RequestURI = "" |
| 203 | req.Header.Del("Proxy-Connection") |
| 204 | req.Header.Del("Connection") |
| 205 | req.Header.Del("Proxy-Authenticate") |
| 206 | req.Header.Del("Proxy-Authorization") |
| 207 | req.Header.Del("TE") |
| 208 | req.Header.Del("Trailers") |
| 209 | req.Header.Del("Transfer-Encoding") |
| 210 | req.Header.Del("Upgrade") |
| 211 | } |
| 212 |
| 213 | func getBadResponse() *http.Response { |
| 214 | header := make(map[string][]string) |
| 215 | header["Proxy-Authenticate"] = []string{"Basic"} |
| 216 | header["Connection"] = []string{"close"} |
| 217 | res := &http.Response{ |
| 218 | Status: "407 Not authorized", |
| 219 | StatusCode: 407, |
| 220 | Proto: "HTTP/1.1", |
| 221 | ProtoMajor: 1, |
| 222 | ProtoMinor: 1, |
| 223 | Header: header, |
| 224 | } |
| 225 | return res |
| 226 | } |