httpconnect.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2020 guylewin, guy@lewin.co.il
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. package tcpmux
  15. import (
  16. "bufio"
  17. "fmt"
  18. "io"
  19. "net"
  20. "net/http"
  21. "time"
  22. libnet "github.com/fatedier/golib/net"
  23. "github.com/fatedier/frp/pkg/util/util"
  24. "github.com/fatedier/frp/pkg/util/vhost"
  25. )
  26. type HTTPConnectTCPMuxer struct {
  27. *vhost.Muxer
  28. // If passthrough is set to true, the CONNECT request will be forwarded to the backend service.
  29. // Otherwise, it will return an OK response to the client and forward the remaining content to the backend service.
  30. passthrough bool
  31. }
  32. func NewHTTPConnectTCPMuxer(listener net.Listener, passthrough bool, timeout time.Duration) (*HTTPConnectTCPMuxer, error) {
  33. ret := &HTTPConnectTCPMuxer{passthrough: passthrough}
  34. mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, timeout)
  35. mux.SetCheckAuthFunc(ret.auth).
  36. SetSuccessHookFunc(ret.sendConnectResponse)
  37. ret.Muxer = mux
  38. return ret, err
  39. }
  40. func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host, httpUser, httpPwd string, err error) {
  41. bufioReader := bufio.NewReader(rd)
  42. req, err := http.ReadRequest(bufioReader)
  43. if err != nil {
  44. return
  45. }
  46. if req.Method != "CONNECT" {
  47. err = fmt.Errorf("connections to tcp vhost must be of method CONNECT")
  48. return
  49. }
  50. host, _ = util.CanonicalHost(req.Host)
  51. proxyAuth := req.Header.Get("Proxy-Authorization")
  52. if proxyAuth != "" {
  53. httpUser, httpPwd, _ = util.ParseBasicAuth(proxyAuth)
  54. }
  55. return
  56. }
  57. func (muxer *HTTPConnectTCPMuxer) sendConnectResponse(c net.Conn, reqInfo map[string]string) error {
  58. if muxer.passthrough {
  59. return nil
  60. }
  61. res := util.OkResponse()
  62. if res.Body != nil {
  63. defer res.Body.Close()
  64. }
  65. return res.Write(c)
  66. }
  67. func (muxer *HTTPConnectTCPMuxer) auth(c net.Conn, username, password string, reqInfo map[string]string) (bool, error) {
  68. reqUsername := reqInfo["HTTPUser"]
  69. reqPassword := reqInfo["HTTPPwd"]
  70. if username == reqUsername && password == reqPassword {
  71. return true, nil
  72. }
  73. resp := util.ProxyUnauthorizedResponse()
  74. if resp.Body != nil {
  75. defer resp.Body.Close()
  76. }
  77. _ = resp.Write(c)
  78. return false, nil
  79. }
  80. func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
  81. reqInfoMap := make(map[string]string, 0)
  82. sc, rd := libnet.NewSharedConn(c)
  83. host, httpUser, httpPwd, err := muxer.readHTTPConnectRequest(rd)
  84. if err != nil {
  85. return nil, reqInfoMap, err
  86. }
  87. reqInfoMap["Host"] = host
  88. reqInfoMap["Scheme"] = "tcp"
  89. reqInfoMap["HTTPUser"] = httpUser
  90. reqInfoMap["HTTPPwd"] = httpPwd
  91. outConn := c
  92. if muxer.passthrough {
  93. outConn = sc
  94. }
  95. return outConn, reqInfoMap, nil
  96. }