httpconnect.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "github.com/fatedier/frp/pkg/util/util"
  23. "github.com/fatedier/frp/pkg/util/vhost"
  24. gnet "github.com/fatedier/golib/net"
  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. return util.OkResponse().Write(c)
  59. }
  60. func (muxer *HTTPConnectTCPMuxer) getHostFromHTTPConnect(c net.Conn) (net.Conn, map[string]string, error) {
  61. reqInfoMap := make(map[string]string, 0)
  62. sc, rd := gnet.NewSharedConn(c)
  63. host, httpUser, err := muxer.readHTTPConnectRequest(rd)
  64. if err != nil {
  65. return nil, reqInfoMap, err
  66. }
  67. reqInfoMap["Host"] = host
  68. reqInfoMap["Scheme"] = "tcp"
  69. reqInfoMap["HTTPUser"] = httpUser
  70. var outConn net.Conn = c
  71. if muxer.passthrough {
  72. outConn = sc
  73. if muxer.authRequired && httpUser == "" {
  74. util.ProxyUnauthorizedResponse().Write(c)
  75. outConn = c
  76. }
  77. }
  78. return outConn, reqInfoMap, nil
  79. }