httpconnect.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. gnet "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. passthrough bool
  29. authRequired bool // Not supported until we really need this.
  30. }
  31. func NewHTTPConnectTCPMuxer(listener net.Listener, passthrough bool, timeout time.Duration) (*HTTPConnectTCPMuxer, error) {
  32. ret := &HTTPConnectTCPMuxer{passthrough: passthrough, authRequired: false}
  33. mux, err := vhost.NewMuxer(listener, ret.getHostFromHTTPConnect, nil, ret.sendConnectResponse, nil, timeout)
  34. ret.Muxer = mux
  35. return ret, err
  36. }
  37. func (muxer *HTTPConnectTCPMuxer) readHTTPConnectRequest(rd io.Reader) (host string, httpUser string, err error) {
  38. bufioReader := bufio.NewReader(rd)
  39. req, err := http.ReadRequest(bufioReader)
  40. if err != nil {
  41. return
  42. }
  43. if req.Method != "CONNECT" {
  44. err = fmt.Errorf("connections to tcp vhost must be of method CONNECT")
  45. return
  46. }
  47. host, _ = util.CanonicalHost(req.Host)
  48. proxyAuth := req.Header.Get("Proxy-Authorization")
  49. if proxyAuth != "" {
  50. httpUser, _, _ = util.ParseBasicAuth(proxyAuth)
  51. }
  52. return
  53. }
  54. func (muxer *HTTPConnectTCPMuxer) sendConnectResponse(c net.Conn, reqInfo map[string]string) error {
  55. if muxer.passthrough {
  56. return nil
  57. }
  58. res := util.OkResponse()
  59. if res.Body != nil {
  60. defer res.Body.Close()
  61. }
  62. return res.Write(c)
  63. }
  64. func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
  65. reqInfoMap := make(map[string]string, 0)
  66. sc, rd := gnet.NewSharedConn(c)
  67. host, httpUser, err := muxer.readHTTPConnectRequest(rd)
  68. if err != nil {
  69. return nil, reqInfoMap, err
  70. }
  71. reqInfoMap["Host"] = host
  72. reqInfoMap["Scheme"] = "tcp"
  73. reqInfoMap["HTTPUser"] = httpUser
  74. outConn := c
  75. if muxer.passthrough {
  76. outConn = sc
  77. if muxer.authRequired && httpUser == "" {
  78. resp := util.ProxyUnauthorizedResponse()
  79. if resp.Body != nil {
  80. defer resp.Body.Close()
  81. }
  82. _ = resp.Write(c)
  83. outConn = c
  84. }
  85. }
  86. return outConn, reqInfoMap, nil
  87. }