| // |
| 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 | package tcpmux |
| 16 |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "net" |
| 22 | "net/http" |
| 23 | "time" |
| 24 |
| 25 | libnet "github.com/fatedier/golib/net" |
| 26 |
| 27 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 28 | "github.com/fatedier/frp/pkg/util/vhost" |
| 29 | ) |
| 30 |
| 31 | type HTTPConnectTCPMuxer struct { |
| 32 | *vhost.Muxer |
| 33 |
| 34 | // If passthrough is set to true, the CONNECT request will be forwarded to the backend service. |
| 35 | // Otherwise, it will return an OK response to the client and forward the remaining content to the backend service. |
| 36 | passthrough bool |
| 37 | } |
| 38 |
| 39 | func NewHTTPConnectTCPMuxer(listener net.Listener, passthrough bool, timeout time.Duration) (*HTTPConnectTCPMuxer, error) { |
| 40 | ret := &HTTPConnectTCPMuxer{passthrough: passthrough} |
| 41 | mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, timeout) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | mux.SetCheckAuthFunc(ret.auth). |
| 46 | SetSuccessHookFunc(ret.sendConnectResponse). |
| 47 | SetFailHookFunc(vhostFailed) |
| 48 | ret.Muxer = mux |
| 49 | return ret, nil |
| 50 | } |
| 51 |
| 52 | func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host, httpUser, httpPwd string, err error) { |
| 53 | bufioReader := bufio.NewReader(rd) |
| 54 |
| 55 | req, err := http.ReadRequest(bufioReader) |
| 56 | if err != nil { |
| 57 | return |
| 58 | } |
| 59 |
| 60 | if req.Method != "CONNECT" { |
| 61 | err = fmt.Errorf("connections to tcp vhost must be of method CONNECT") |
| 62 | return |
| 63 | } |
| 64 |
| 65 | host, _ = httppkg.CanonicalHost(req.Host) |
| 66 | proxyAuth := req.Header.Get("Proxy-Authorization") |
| 67 | if proxyAuth != "" { |
| 68 | httpUser, httpPwd, _ = httppkg.ParseBasicAuth(proxyAuth) |
| 69 | } |
| 70 | return |
| 71 | } |
| 72 |
| 73 | func (muxer *HTTPConnectTCPMuxer) sendConnectResponse(c net.Conn, _ map[string]string) error { |
| 74 | if muxer.passthrough { |
| 75 | return nil |
| 76 | } |
| 77 | res := httppkg.OkResponse() |
| 78 | if res.Body != nil { |
| 79 | defer res.Body.Close() |
| 80 | } |
| 81 | return res.Write(c) |
| 82 | } |
| 83 |
| 84 | func (muxer *HTTPConnectTCPMuxer) auth(c net.Conn, username, password string, reqInfo map[string]string) (bool, error) { |
| 85 | reqUsername := reqInfo["HTTPUser"] |
| 86 | reqPassword := reqInfo["HTTPPwd"] |
| 87 | if username == reqUsername && password == reqPassword { |
| 88 | return true, nil |
| 89 | } |
| 90 |
| 91 | resp := httppkg.ProxyUnauthorizedResponse() |
| 92 | if resp.Body != nil { |
| 93 | defer resp.Body.Close() |
| 94 | } |
| 95 | _ = resp.Write(c) |
| 96 | return false, nil |
| 97 | } |
| 98 |
| 99 | func vhostFailed(c net.Conn) { |
| 100 | res := vhost.NotFoundResponse() |
| 101 | if res.Body != nil { |
| 102 | defer res.Body.Close() |
| 103 | } |
| 104 | _ = res.Write(c) |
| 105 | _ = c.Close() |
| 106 | } |
| 107 |
| 108 | func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) { |
| 109 | reqInfoMap := make(map[string]string, 0) |
| 110 | sc, rd := libnet.NewSharedConn(c) |
| 111 |
| 112 | host, httpUser, httpPwd, err := muxer.readHTTPConnectRequest(rd) |
| 113 | if err != nil { |
| 114 | return nil, reqInfoMap, err |
| 115 | } |
| 116 |
| 117 | reqInfoMap["Host"] = host |
| 118 | reqInfoMap["Scheme"] = "tcp" |
| 119 | reqInfoMap["HTTPUser"] = httpUser |
| 120 | reqInfoMap["HTTPPwd"] = httpPwd |
| 121 |
| 122 | outConn := c |
| 123 | if muxer.passthrough { |
| 124 | outConn = sc |
| 125 | } |
| 126 | return outConn, reqInfoMap, nil |
| 127 | } |