http.go 4.0 KB

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