httpconnect.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. httppkg "github.com/fatedier/frp/pkg/util/http"
  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. SetFailHookFunc(vhostFailed)
  38. ret.Muxer = mux
  39. return ret, err
  40. }
  41. func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host, httpUser, httpPwd string, err error) {
  42. bufioReader := bufio.NewReader(rd)
  43. req, err := http.ReadRequest(bufioReader)
  44. if err != nil {
  45. return
  46. }
  47. if req.Method != "CONNECT" {
  48. err = fmt.Errorf("connections to tcp vhost must be of method CONNECT")
  49. return
  50. }
  51. host, _ = httppkg.CanonicalHost(req.Host)
  52. proxyAuth := req.Header.Get("Proxy-Authorization")
  53. if proxyAuth != "" {
  54. httpUser, httpPwd, _ = httppkg.ParseBasicAuth(proxyAuth)
  55. }
  56. return
  57. }
  58. func (muxer *HTTPConnectTCPMuxer) sendConnectResponse(c net.Conn, _ map[string]string) error {
  59. if muxer.passthrough {
  60. return nil
  61. }
  62. res := httppkg.OkResponse()
  63. if res.Body != nil {
  64. defer res.Body.Close()
  65. }
  66. return res.Write(c)
  67. }
  68. func (muxer *HTTPConnectTCPMuxer) auth(c net.Conn, username, password string, reqInfo map[string]string) (bool, error) {
  69. reqUsername := reqInfo["HTTPUser"]
  70. reqPassword := reqInfo["HTTPPwd"]
  71. if username == reqUsername && password == reqPassword {
  72. return true, nil
  73. }
  74. resp := httppkg.ProxyUnauthorizedResponse()
  75. if resp.Body != nil {
  76. defer resp.Body.Close()
  77. }
  78. _ = resp.Write(c)
  79. return false, nil
  80. }
  81. func vhostFailed(c net.Conn) {
  82. res := vhost.NotFoundResponse()
  83. if res.Body != nil {
  84. defer res.Body.Close()
  85. }
  86. _ = res.Write(c)
  87. _ = c.Close()
  88. }
  89. func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
  90. reqInfoMap := make(map[string]string, 0)
  91. sc, rd := libnet.NewSharedConn(c)
  92. host, httpUser, httpPwd, err := muxer.readHTTPConnectRequest(rd)
  93. if err != nil {
  94. return nil, reqInfoMap, err
  95. }
  96. reqInfoMap["Host"] = host
  97. reqInfoMap["Scheme"] = "tcp"
  98. reqInfoMap["HTTPUser"] = httpUser
  99. reqInfoMap["HTTPPwd"] = httpPwd
  100. outConn := c
  101. if muxer.passthrough {
  102. outConn = sc
  103. }
  104. return outConn, reqInfoMap, nil
  105. }