http.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. defer func() {
  45. if err != nil {
  46. pxy.Close()
  47. }
  48. }()
  49. addrs := make([]string, 0)
  50. for _, domain := range pxy.cfg.CustomDomains {
  51. if domain == "" {
  52. continue
  53. }
  54. routeConfig.Domain = domain
  55. for _, location := range locations {
  56. routeConfig.Location = location
  57. tmpDomain := routeConfig.Domain
  58. tmpLocation := routeConfig.Location
  59. // handle group
  60. if pxy.cfg.Group != "" {
  61. err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, routeConfig)
  62. if err != nil {
  63. return
  64. }
  65. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  66. pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.Group, tmpDomain, tmpLocation)
  67. })
  68. } else {
  69. // no group
  70. err = pxy.rc.HttpReverseProxy.Register(routeConfig)
  71. if err != nil {
  72. return
  73. }
  74. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  75. pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  76. })
  77. }
  78. addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, int(g.GlbServerCfg.VhostHttpPort)))
  79. pxy.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group)
  80. }
  81. }
  82. if pxy.cfg.SubDomain != "" {
  83. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  84. for _, location := range locations {
  85. routeConfig.Location = location
  86. tmpDomain := routeConfig.Domain
  87. tmpLocation := routeConfig.Location
  88. // handle group
  89. if pxy.cfg.Group != "" {
  90. err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, routeConfig)
  91. if err != nil {
  92. return
  93. }
  94. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  95. pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.Group, tmpDomain, tmpLocation)
  96. })
  97. } else {
  98. err = pxy.rc.HttpReverseProxy.Register(routeConfig)
  99. if err != nil {
  100. return
  101. }
  102. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  103. pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  104. })
  105. }
  106. addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort))
  107. pxy.Info("http proxy listen for host [%s] location [%s] group [%s]", routeConfig.Domain, routeConfig.Location, pxy.cfg.Group)
  108. }
  109. }
  110. remoteAddr = strings.Join(addrs, ",")
  111. return
  112. }
  113. func (pxy *HttpProxy) GetConf() config.ProxyConf {
  114. return pxy.cfg
  115. }
  116. func (pxy *HttpProxy) GetRealConn(remoteAddr string) (workConn frpNet.Conn, err error) {
  117. rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr)
  118. if errRet != nil {
  119. pxy.Warn("resolve TCP addr [%s] error: %v", remoteAddr, errRet)
  120. // we do not return error here since remoteAddr is not necessary for proxies without proxy protocol enabled
  121. }
  122. tmpConn, errRet := pxy.GetWorkConnFromPool(rAddr, nil)
  123. if errRet != nil {
  124. err = errRet
  125. return
  126. }
  127. var rwc io.ReadWriteCloser = tmpConn
  128. if pxy.cfg.UseEncryption {
  129. rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token))
  130. if err != nil {
  131. pxy.Error("create encryption stream error: %v", err)
  132. return
  133. }
  134. }
  135. if pxy.cfg.UseCompression {
  136. rwc = frpIo.WithCompression(rwc)
  137. }
  138. workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
  139. workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
  140. pxy.statsCollector.Mark(stats.TypeOpenConnection, &stats.OpenConnectionPayload{ProxyName: pxy.GetName()})
  141. return
  142. }
  143. func (pxy *HttpProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
  144. name := pxy.GetName()
  145. pxy.statsCollector.Mark(stats.TypeCloseProxy, &stats.CloseConnectionPayload{ProxyName: name})
  146. pxy.statsCollector.Mark(stats.TypeAddTrafficIn, &stats.AddTrafficInPayload{
  147. ProxyName: name,
  148. TrafficBytes: totalWrite,
  149. })
  150. pxy.statsCollector.Mark(stats.TypeAddTrafficOut, &stats.AddTrafficOutPayload{
  151. ProxyName: name,
  152. TrafficBytes: totalRead,
  153. })
  154. }
  155. func (pxy *HttpProxy) Close() {
  156. pxy.BaseProxy.Close()
  157. for _, closeFn := range pxy.closeFuncs {
  158. closeFn()
  159. }
  160. }