http.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if domain == "" {
  46. continue
  47. }
  48. routeConfig.Domain = domain
  49. for _, location := range locations {
  50. routeConfig.Location = location
  51. err = pxy.rc.HttpReverseProxy.Register(routeConfig)
  52. if err != nil {
  53. return
  54. }
  55. tmpDomain := routeConfig.Domain
  56. tmpLocation := routeConfig.Location
  57. addrs = append(addrs, util.CanonicalAddr(tmpDomain, int(g.GlbServerCfg.VhostHttpPort)))
  58. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  59. pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  60. })
  61. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  62. }
  63. }
  64. if pxy.cfg.SubDomain != "" {
  65. routeConfig.Domain = pxy.cfg.SubDomain + "." + g.GlbServerCfg.SubDomainHost
  66. for _, location := range locations {
  67. routeConfig.Location = location
  68. err = pxy.rc.HttpReverseProxy.Register(routeConfig)
  69. if err != nil {
  70. return
  71. }
  72. tmpDomain := routeConfig.Domain
  73. tmpLocation := routeConfig.Location
  74. addrs = append(addrs, util.CanonicalAddr(tmpDomain, g.GlbServerCfg.VhostHttpPort))
  75. pxy.closeFuncs = append(pxy.closeFuncs, func() {
  76. pxy.rc.HttpReverseProxy.UnRegister(tmpDomain, tmpLocation)
  77. })
  78. pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
  79. }
  80. }
  81. remoteAddr = strings.Join(addrs, ",")
  82. return
  83. }
  84. func (pxy *HttpProxy) GetConf() config.ProxyConf {
  85. return pxy.cfg
  86. }
  87. func (pxy *HttpProxy) GetRealConn() (workConn frpNet.Conn, err error) {
  88. tmpConn, errRet := pxy.GetWorkConnFromPool(nil, nil)
  89. if errRet != nil {
  90. err = errRet
  91. return
  92. }
  93. var rwc io.ReadWriteCloser = tmpConn
  94. if pxy.cfg.UseEncryption {
  95. rwc, err = frpIo.WithEncryption(rwc, []byte(g.GlbServerCfg.Token))
  96. if err != nil {
  97. pxy.Error("create encryption stream error: %v", err)
  98. return
  99. }
  100. }
  101. if pxy.cfg.UseCompression {
  102. rwc = frpIo.WithCompression(rwc)
  103. }
  104. workConn = frpNet.WrapReadWriteCloserToConn(rwc, tmpConn)
  105. workConn = frpNet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
  106. pxy.statsCollector.Mark(stats.TypeOpenConnection, &stats.OpenConnectionPayload{ProxyName: pxy.GetName()})
  107. return
  108. }
  109. func (pxy *HttpProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
  110. name := pxy.GetName()
  111. pxy.statsCollector.Mark(stats.TypeCloseProxy, &stats.CloseConnectionPayload{ProxyName: name})
  112. pxy.statsCollector.Mark(stats.TypeAddTrafficIn, &stats.AddTrafficInPayload{
  113. ProxyName: name,
  114. TrafficBytes: totalWrite,
  115. })
  116. pxy.statsCollector.Mark(stats.TypeAddTrafficOut, &stats.AddTrafficOutPayload{
  117. ProxyName: name,
  118. TrafficBytes: totalRead,
  119. })
  120. }
  121. func (pxy *HttpProxy) Close() {
  122. pxy.BaseProxy.Close()
  123. for _, closeFn := range pxy.closeFuncs {
  124. closeFn()
  125. }
  126. }