http.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  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 proxy
  15. import (
  16. "io"
  17. "net"
  18. "strings"
  19. "github.com/fatedier/frp/g"
  20. "github.com/fatedier/frp/models/config"
  21. "github.com/fatedier/frp/server/stats"
  22. frpNet "github.com/fatedier/frp/utils/net"
  23. "github.com/fatedier/frp/utils/util"
  24. "github.com/fatedier/frp/utils/vhost"
  25. frpIo "github.com/fatedier/golib/io"
  26. )
  27. type HttpProxy struct {
  28. *BaseProxy
  29. cfg *config.HttpProxyConf
  30. closeFuncs []func()
  31. }
  32. func (pxy *HttpProxy) Run() (remoteAddr string, err error) {
  33. routeConfig := vhost.VhostRouteConfig{
  34. RewriteHost: pxy.cfg.HostHeaderRewrite,
  35. Headers: pxy.cfg.Headers,
  36. Username: pxy.cfg.HttpUser,
  37. Password: pxy.cfg.HttpPwd,
  38. CreateConnFn: pxy.GetRealConn,
  39. }
  40. locations := pxy.cfg.Locations
  41. if len(locations) == 0 {
  42. locations = []string{""}
  43. }
  44. addrs := make([]string, 0)
  45. for _, domain := range pxy.cfg.CustomDomains {
  46. if domain == "" {
  47. continue
  48. }
  49. routeConfig.Domain = domain
  50. for _, location := range locations {
  51. routeConfig.Location = location
  52. err = pxy.rc.HttpReverseProxy.Register(routeConfig)
  53. if err != nil {
  54. return
  55. }
  56. tmpDomain := routeConfig.Domain
  57. tmpLocation := routeConfig.Location
  58. addrs = append(addrs, util.CanonicalAddr(tmpDomain, int(g.GlbServerCfg.VhostHttpPort)))
  59. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  60. pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  61. })
  62. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  63. }
  64. }
  65. if pxy.cfg.SubDomain != "" {
  66. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  67. for _, location := range locations {
  68. routeConfig.Location = location
  69. err = pxy.rc.HttpReverseProxy.Register(routeConfig)
  70. if err != nil {
  71. return
  72. }
  73. tmpDomain := routeConfig.Domain
  74. tmpLocation := routeConfig.Location
  75. addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort))
  76. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  77. pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  78. })
  79. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  80. }
  81. }
  82. remoteAddr = strings.Join(addrs, ",")
  83. return
  84. }
  85. func (pxy *HttpProxy) GetConf() config.ProxyConf {
  86. return pxy.cfg
  87. }
  88. func (pxy *HttpProxy) GetRealConn(remoteAddr string) (workConn frpNet.Conn, err error) {
  89. rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr)
  90. if errRet != nil {
  91. pxy.Warn("resolve TCP addr [%s] error: %v", remoteAddr, errRet)
  92. // we do not return error here since remoteAddr is not necessary for proxies without proxy protocol enabled
  93. }
  94. tmpConn, errRet := pxy.GetWorkConnFromPool(rAddr, nil)
  95. if errRet != nil {
  96. err = errRet
  97. return
  98. }
  99. var rwc io.ReadWriteCloser = tmpConn
  100. if pxy.cfg.UseEncryption {
  101. rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token))
  102. if err != nil {
  103. pxy.Error("create encryption stream error: %v", err)
  104. return
  105. }
  106. }
  107. if pxy.cfg.UseCompression {
  108. rwc = frpIo.WithCompression(rwc)
  109. }
  110. workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
  111. workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
  112. pxy.statsCollector.Mark(stats.TypeOpenConnection, &stats.OpenConnectionPayload{ProxyName: pxy.GetName()})
  113. return
  114. }
  115. func (pxy *HttpProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
  116. name := pxy.GetName()
  117. pxy.statsCollector.Mark(stats.TypeCloseProxy, &stats.CloseConnectionPayload{ProxyName: name})
  118. pxy.statsCollector.Mark(stats.TypeAddTrafficIn, &stats.AddTrafficInPayload{
  119. ProxyName: name,
  120. TrafficBytes: totalWrite,
  121. })
  122. pxy.statsCollector.Mark(stats.TypeAddTrafficOut, &stats.AddTrafficOutPayload{
  123. ProxyName: name,
  124. TrafficBytes: totalRead,
  125. })
  126. }
  127. func (pxy *HttpProxy) Close() {
  128. pxy.BaseProxy.Close()
  129. for _, closeFn := range pxy.closeFuncs {
  130. closeFn()
  131. }
  132. }