2020-03-05 21:47:49 +08:00
|
|
|
// Copyright 2020 guylewin, guy@lewin.co.il
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tcpmux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
gnet "github.com/fatedier/golib/net"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
|
|
|
"github.com/fatedier/frp/pkg/util/vhost"
|
2020-03-05 21:47:49 +08:00
|
|
|
)
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type HTTPConnectTCPMuxer struct {
|
|
|
|
*vhost.Muxer
|
2022-05-26 23:57:30 +08:00
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
// If passthrough is set to true, the CONNECT request will be forwarded to the backend service.
|
|
|
|
// Otherwise, it will return an OK response to the client and forward the remaining content to the backend service.
|
|
|
|
passthrough bool
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func NewHTTPConnectTCPMuxer(listener net.Listener, passthrough bool, timeout time.Duration) (*HTTPConnectTCPMuxer, error) {
|
2023-03-07 19:53:32 +08:00
|
|
|
ret := &HTTPConnectTCPMuxer{passthrough: passthrough}
|
|
|
|
mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, timeout)
|
|
|
|
mux.SetCheckAuthFunc(ret.auth).
|
|
|
|
SetSuccessHookFunc(ret.sendConnectResponse)
|
2022-05-26 23:57:30 +08:00
|
|
|
ret.Muxer = mux
|
|
|
|
return ret, err
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host, httpUser, httpPwd string, err error) {
|
2020-03-05 21:47:49 +08:00
|
|
|
bufioReader := bufio.NewReader(rd)
|
|
|
|
|
|
|
|
req, err := http.ReadRequest(bufioReader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Method != "CONNECT" {
|
|
|
|
err = fmt.Errorf("connections to tcp vhost must be of method CONNECT")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-09 15:19:35 +08:00
|
|
|
host, _ = util.CanonicalHost(req.Host)
|
2022-05-26 23:57:30 +08:00
|
|
|
proxyAuth := req.Header.Get("Proxy-Authorization")
|
|
|
|
if proxyAuth != "" {
|
2023-03-07 19:53:32 +08:00
|
|
|
httpUser, httpPwd, _ = util.ParseBasicAuth(proxyAuth)
|
2022-05-26 23:57:30 +08:00
|
|
|
}
|
2020-03-05 21:47:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) sendConnectResponse(c net.Conn, reqInfo map[string]string) error {
|
|
|
|
if muxer.passthrough {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-29 01:02:53 +08:00
|
|
|
res := util.OkResponse()
|
|
|
|
if res.Body != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
|
|
|
return res.Write(c)
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) auth(c net.Conn, username, password string, reqInfo map[string]string) (bool, error) {
|
|
|
|
reqUsername := reqInfo["HTTPUser"]
|
|
|
|
reqPassword := reqInfo["HTTPPwd"]
|
|
|
|
if username == reqUsername && password == reqPassword {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := util.ProxyUnauthorizedResponse()
|
|
|
|
if resp.Body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
_ = resp.Write(c)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
|
2020-03-05 21:47:49 +08:00
|
|
|
reqInfoMap := make(map[string]string, 0)
|
2022-05-26 23:57:30 +08:00
|
|
|
sc, rd := gnet.NewSharedConn(c)
|
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
host, httpUser, httpPwd, err := muxer.readHTTPConnectRequest(rd)
|
2020-03-05 21:47:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, reqInfoMap, err
|
|
|
|
}
|
2022-05-26 23:57:30 +08:00
|
|
|
|
2020-03-05 21:47:49 +08:00
|
|
|
reqInfoMap["Host"] = host
|
|
|
|
reqInfoMap["Scheme"] = "tcp"
|
2022-05-26 23:57:30 +08:00
|
|
|
reqInfoMap["HTTPUser"] = httpUser
|
2023-03-07 19:53:32 +08:00
|
|
|
reqInfoMap["HTTPPwd"] = httpPwd
|
2022-05-26 23:57:30 +08:00
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
outConn := c
|
2022-05-26 23:57:30 +08:00
|
|
|
if muxer.passthrough {
|
|
|
|
outConn = sc
|
|
|
|
}
|
|
|
|
return outConn, reqInfoMap, nil
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|