| // |
| 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 |
| 15 | package proxy |
| 16 |
| 17 | import ( |
| 18 | "io" |
| 19 | "net" |
| 20 | "reflect" |
| 21 | "strings" |
| 22 |
| 23 | libio "github.com/fatedier/golib/io" |
| 24 |
| 25 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 26 | "github.com/fatedier/frp/pkg/util/limit" |
| 27 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 28 | "github.com/fatedier/frp/pkg/util/util" |
| 29 | "github.com/fatedier/frp/pkg/util/vhost" |
| 30 | "github.com/fatedier/frp/server/metrics" |
| 31 | ) |
| 32 |
| 33 | func init() { |
| 34 | RegisterProxyFactory(reflect.TypeFor[*v1.HTTPProxyConfig](), NewHTTPProxy) |
| 35 | } |
| 36 |
| 37 | type HTTPProxy struct { |
| 38 | *BaseProxy |
| 39 | cfg *v1.HTTPProxyConfig |
| 40 |
| 41 | closeFuncs []func() |
| 42 | } |
| 43 |
| 44 | func NewHTTPProxy(baseProxy *BaseProxy) Proxy { |
| 45 | unwrapped, ok := baseProxy.GetConfigurer().(*v1.HTTPProxyConfig) |
| 46 | if !ok { |
| 47 | return nil |
| 48 | } |
| 49 | return &HTTPProxy{ |
| 50 | BaseProxy: baseProxy, |
| 51 | cfg: unwrapped, |
| 52 | } |
| 53 | } |
| 54 |
| 55 | func (pxy *HTTPProxy) Run() (remoteAddr string, err error) { |
| 56 | xl := pxy.xl |
| 57 | routeConfig := vhost.RouteConfig{ |
| 58 | RewriteHost: pxy.cfg.HostHeaderRewrite, |
| 59 | RouteByHTTPUser: pxy.cfg.RouteByHTTPUser, |
| 60 | Headers: pxy.cfg.RequestHeaders.Set, |
| 61 | ResponseHeaders: pxy.cfg.ResponseHeaders.Set, |
| 62 | Username: pxy.cfg.HTTPUser, |
| 63 | Password: pxy.cfg.HTTPPassword, |
| 64 | CreateConnFn: pxy.GetRealConn, |
| 65 | } |
| 66 |
| 67 | locations := pxy.cfg.Locations |
| 68 | if len(locations) == 0 { |
| 69 | locations = []string{""} |
| 70 | } |
| 71 |
| 72 | defer func() { |
| 73 | if err != nil { |
| 74 | pxy.Close() |
| 75 | } |
| 76 | }() |
| 77 |
| 78 | domains := pxy.buildDomains(pxy.cfg.CustomDomains, pxy.cfg.SubDomain) |
| 79 |
| 80 | addrs := make([]string, 0) |
| 81 | for _, domain := range domains { |
| 82 | routeConfig.Domain = domain |
| 83 | for _, location := range locations { |
| 84 | routeConfig.Location = location |
| 85 | tmpRouteConfig := routeConfig |
| 86 |
| 87 | // handle group |
| 88 | if pxy.cfg.LoadBalancer.Group != "" { |
| 89 | err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.LoadBalancer.Group, pxy.cfg.LoadBalancer.GroupKey, routeConfig) |
| 90 | if err != nil { |
| 91 | return |
| 92 | } |
| 93 | pxy.closeFuncs = append(pxy.closeFuncs, func() { |
| 94 | pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.LoadBalancer.Group, tmpRouteConfig) |
| 95 | }) |
| 96 | } else { |
| 97 | err = pxy.rc.HTTPReverseProxy.Register(routeConfig) |
| 98 | if err != nil { |
| 99 | return |
| 100 | } |
| 101 | pxy.closeFuncs = append(pxy.closeFuncs, func() { |
| 102 | pxy.rc.HTTPReverseProxy.UnRegister(tmpRouteConfig) |
| 103 | }) |
| 104 | } |
| 105 | addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, pxy.serverCfg.VhostHTTPPort)) |
| 106 | xl.Infof("http proxy listen for host [%s] location [%s] group [%s], routeByHTTPUser [%s]", |
| 107 | routeConfig.Domain, routeConfig.Location, pxy.cfg.LoadBalancer.Group, pxy.cfg.RouteByHTTPUser) |
| 108 | } |
| 109 | } |
| 110 | remoteAddr = strings.Join(addrs, ",") |
| 111 | return |
| 112 | } |
| 113 |
| 114 | func (pxy *HTTPProxy) GetRealConn(remoteAddr string) (workConn net.Conn, err error) { |
| 115 | xl := pxy.xl |
| 116 | rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr) |
| 117 | if errRet != nil { |
| 118 | xl.Warnf("resolve TCP addr [%s] error: %v", remoteAddr, errRet) |
| 119 | // we do not return error here since remoteAddr is not necessary for proxies without proxy protocol enabled |
| 120 | } |
| 121 |
| 122 | tmpConn, errRet := pxy.GetWorkConnFromPool(rAddr, nil) |
| 123 | if errRet != nil { |
| 124 | err = errRet |
| 125 | return |
| 126 | } |
| 127 |
| 128 | var rwc io.ReadWriteCloser = tmpConn |
| 129 | if pxy.cfg.Transport.UseEncryption { |
| 130 | rwc, err = libio.WithEncryption(rwc, pxy.encryptionKey) |
| 131 | if err != nil { |
| 132 | xl.Errorf("create encryption stream error: %v", err) |
| 133 | tmpConn.Close() |
| 134 | return |
| 135 | } |
| 136 | } |
| 137 | if pxy.cfg.Transport.UseCompression { |
| 138 | rwc = libio.WithCompression(rwc) |
| 139 | } |
| 140 |
| 141 | if pxy.GetLimiter() != nil { |
| 142 | rwc = libio.WrapReadWriteCloser(limit.NewReader(rwc, pxy.GetLimiter()), limit.NewWriter(rwc, pxy.GetLimiter()), func() error { |
| 143 | return rwc.Close() |
| 144 | }) |
| 145 | } |
| 146 |
| 147 | workConn = netpkg.WrapReadWriteCloserToConn(rwc, tmpConn) |
| 148 | workConn = netpkg.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn) |
| 149 | metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConfigurer().GetBaseConfig().Type) |
| 150 | return |
| 151 | } |
| 152 |
| 153 | func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) { |
| 154 | name := pxy.GetName() |
| 155 | proxyType := pxy.GetConfigurer().GetBaseConfig().Type |
| 156 | metrics.Server.CloseConnection(name, proxyType) |
| 157 | metrics.Server.AddTrafficIn(name, proxyType, totalWrite) |
| 158 | metrics.Server.AddTrafficOut(name, proxyType, totalRead) |
| 159 | } |
| 160 |
| 161 | func (pxy *HTTPProxy) Close() { |
| 162 | pxy.BaseProxy.Close() |
| 163 | for _, closeFn := range pxy.closeFuncs { |
| 164 | closeFn() |
| 165 | } |
| 166 | } |