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"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
|
|
|
"github.com/fatedier/frp/pkg/util/vhost"
|
2022-05-26 23:57:30 +08:00
|
|
|
gnet "github.com/fatedier/golib/net"
|
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
|
|
|
|
|
|
|
passthrough bool
|
|
|
|
authRequired bool // Not supported until we really need this.
|
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) {
|
|
|
|
ret := &HTTPConnectTCPMuxer{passthrough: passthrough, authRequired: false}
|
|
|
|
mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, nil, ret.sendConnectResponse, nil, timeout)
|
|
|
|
ret.Muxer = mux
|
|
|
|
return ret, err
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host string, httpUser 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 != "" {
|
|
|
|
httpUser, _, _ = util.ParseBasicAuth(proxyAuth)
|
|
|
|
}
|
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
|
|
|
|
}
|
2020-03-05 21:47:49 +08:00
|
|
|
return util.OkResponse().Write(c)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
host, httpUser, 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
|
|
|
|
|
|
|
|
var outConn net.Conn = c
|
|
|
|
if muxer.passthrough {
|
|
|
|
outConn = sc
|
|
|
|
if muxer.authRequired && httpUser == "" {
|
|
|
|
util.ProxyUnauthorizedResponse().Write(c)
|
|
|
|
outConn = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outConn, reqInfoMap, nil
|
2020-03-05 21:47:49 +08:00
|
|
|
}
|