http.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/pkg/config"
  20. frpNet "github.com/fatedier/frp/pkg/util/net"
  21. "github.com/fatedier/frp/pkg/util/util"
  22. "github.com/fatedier/frp/pkg/util/vhost"
  23. "github.com/fatedier/frp/server/metrics"
  24. frpIo "github.com/fatedier/golib/io"
  25. )
  26. type HTTPProxy struct {
  27. *BaseProxy
  28. cfg *config.HTTPProxyConf
  29. closeFuncs []func()
  30. }
  31. func (pxy *HTTPProxy) Run() (remoteAddr string, err error) {
  32. xl := pxy.xl
  33. routeConfig := vhost.RouteConfig{
  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(pxy.serverCfg.VhostHTTPPort)))
  79. xl.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 + "." + pxy.serverCfg.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, pxy.serverCfg.VhostHTTPPort))
  107. xl.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 net.Conn, err error) {
  117. xl := pxy.xl
  118. rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr)
  119. if errRet != nil {
  120. xl.Warn("resolve TCP addr [%s] error: %v", remoteAddr, errRet)
  121. // we do not return error here since remoteAddr is not necessary for proxies without proxy protocol enabled
  122. }
  123. tmpConn, errRet := pxy.GetWorkConnFromPool(rAddr, nil)
  124. if errRet != nil {
  125. err = errRet
  126. return
  127. }
  128. var rwc io.ReadWriteCloser = tmpConn
  129. if pxy.cfg.UseEncryption {
  130. rwc, err = frpIo.WithEncryption(rwc, []byte(pxy.serverCfg.Token))
  131. if err != nil {
  132. xl.Error("create encryption stream error: %v", err)
  133. return
  134. }
  135. }
  136. if pxy.cfg.UseCompression {
  137. rwc = frpIo.WithCompression(rwc)
  138. }
  139. workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
  140. workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
  141. metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConf().GetBaseInfo().ProxyType)
  142. return
  143. }
  144. func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
  145. name := pxy.GetName()
  146. proxyType := pxy.GetConf().GetBaseInfo().ProxyType
  147. metrics.Server.CloseConnection(name, proxyType)
  148. metrics.Server.AddTrafficIn(name, proxyType, totalWrite)
  149. metrics.Server.AddTrafficOut(name, proxyType, totalRead)
  150. }
  151. func (pxy *HTTPProxy) Close() {
  152. pxy.BaseProxy.Close()
  153. for _, closeFn := range pxy.closeFuncs {
  154. closeFn()
  155. }
  156. }