2020-03-05 14:47:49 +01: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"
|
|
|
|
|
2023-05-29 08:10:34 +02:00
|
|
|
libnet "github.com/fatedier/golib/net"
|
2022-08-28 19:02:53 +02:00
|
|
|
|
2020-09-23 07:49:14 +02:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
|
|
|
"github.com/fatedier/frp/pkg/util/vhost"
|
2020-03-05 14:47:49 +01:00
|
|
|
)
|
|
|
|
|
2020-05-24 11:48:37 +02:00
|
|
|
type HTTPConnectTCPMuxer struct {
|
|
|
|
*vhost.Muxer
|
2022-05-26 17:57:30 +02:00
|
|
|
|
2023-03-07 12:53:32 +01: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 14:47:49 +01:00
|
|
|
}
|
|
|
|
|
2022-05-26 17:57:30 +02:00
|
|
|
func NewHTTPConnectTCPMuxer(listener net.Listener, passthrough bool, timeout time.Duration) (*HTTPConnectTCPMuxer, error) {
|
2023-03-07 12:53:32 +01:00
|
|
|
ret := &HTTPConnectTCPMuxer{passthrough: passthrough}
|
|
|
|
mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, timeout)
|
|
|
|
mux.SetCheckAuthFunc(ret.auth).
|
|
|
|
SetSuccessHookFunc(ret.sendConnectResponse)
|
2022-05-26 17:57:30 +02:00
|
|
|
ret.Muxer = mux
|
|
|
|
return ret, err
|
2020-03-05 14:47:49 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 12:53:32 +01:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host, httpUser, httpPwd string, err error) {
|
2020-03-05 14:47:49 +01: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 08:19:35 +01:00
|
|
|
host, _ = util.CanonicalHost(req.Host)
|
2022-05-26 17:57:30 +02:00
|
|
|
proxyAuth := req.Header.Get("Proxy-Authorization")
|
|
|
|
if proxyAuth != "" {
|
2023-03-07 12:53:32 +01:00
|
|
|
httpUser, httpPwd, _ = util.ParseBasicAuth(proxyAuth)
|
2022-05-26 17:57:30 +02:00
|
|
|
}
|
2020-03-05 14:47:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-21 04:30:46 +02:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) sendConnectResponse(c net.Conn, _ map[string]string) error {
|
2022-05-26 17:57:30 +02:00
|
|
|
if muxer.passthrough {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-28 19:02:53 +02:00
|
|
|
res := util.OkResponse()
|
|
|
|
if res.Body != nil {
|
|
|
|
defer res.Body.Close()
|
|
|
|
}
|
|
|
|
return res.Write(c)
|
2020-03-05 14:47:49 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 12:53:32 +01: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 17:57:30 +02:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
|
2020-03-05 14:47:49 +01:00
|
|
|
reqInfoMap := make(map[string]string, 0)
|
2023-05-29 08:10:34 +02:00
|
|
|
sc, rd := libnet.NewSharedConn(c)
|
2022-05-26 17:57:30 +02:00
|
|
|
|
2023-03-07 12:53:32 +01:00
|
|
|
host, httpUser, httpPwd, err := muxer.readHTTPConnectRequest(rd)
|
2020-03-05 14:47:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, reqInfoMap, err
|
|
|
|
}
|
2022-05-26 17:57:30 +02:00
|
|
|
|
2020-03-05 14:47:49 +01:00
|
|
|
reqInfoMap["Host"] = host
|
|
|
|
reqInfoMap["Scheme"] = "tcp"
|
2022-05-26 17:57:30 +02:00
|
|
|
reqInfoMap["HTTPUser"] = httpUser
|
2023-03-07 12:53:32 +01:00
|
|
|
reqInfoMap["HTTPPwd"] = httpPwd
|
2022-05-26 17:57:30 +02:00
|
|
|
|
2022-08-28 19:02:53 +02:00
|
|
|
outConn := c
|
2022-05-26 17:57:30 +02:00
|
|
|
if muxer.passthrough {
|
|
|
|
outConn = sc
|
|
|
|
}
|
|
|
|
return outConn, reqInfoMap, nil
|
2020-03-05 14:47:49 +01:00
|
|
|
}
|