frp/server/proxy/http.go

196 lines
5.4 KiB
Go
Raw Normal View History

2019-01-15 00:11:08 +08:00
// Copyright 2019 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proxy
import (
"io"
2019-04-10 10:51:01 +08:00
"net"
2019-01-15 00:11:08 +08:00
"strings"
2023-05-29 14:10:34 +08:00
libio "github.com/fatedier/golib/io"
"golang.org/x/time/rate"
2022-08-29 01:02:53 +08:00
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/util/limit"
2023-05-29 14:10:34 +08:00
utilnet "github.com/fatedier/frp/pkg/util/net"
2020-09-23 13:49:14 +08:00
"github.com/fatedier/frp/pkg/util/util"
"github.com/fatedier/frp/pkg/util/vhost"
"github.com/fatedier/frp/server/metrics"
2019-01-15 00:11:08 +08:00
)
2020-05-24 17:48:37 +08:00
type HTTPProxy struct {
2019-01-31 16:49:23 +08:00
*BaseProxy
2020-05-24 17:48:37 +08:00
cfg *config.HTTPProxyConf
2019-01-15 00:11:08 +08:00
closeFuncs []func()
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) Run() (remoteAddr string, err error) {
2019-10-12 20:13:12 +08:00
xl := pxy.xl
2020-05-24 17:48:37 +08:00
routeConfig := vhost.RouteConfig{
RewriteHost: pxy.cfg.HostHeaderRewrite,
RouteByHTTPUser: pxy.cfg.RouteByHTTPUser,
Headers: pxy.cfg.Headers,
Username: pxy.cfg.HTTPUser,
Password: pxy.cfg.HTTPPwd,
CreateConnFn: pxy.GetRealConn,
2019-01-15 00:11:08 +08:00
}
locations := pxy.cfg.Locations
if len(locations) == 0 {
locations = []string{""}
}
2019-07-31 00:41:58 +08:00
defer func() {
if err != nil {
pxy.Close()
}
}()
2019-01-15 00:11:08 +08:00
addrs := make([]string, 0)
for _, domain := range pxy.cfg.CustomDomains {
2019-03-29 17:12:44 +08:00
if domain == "" {
continue
}
2019-01-15 00:11:08 +08:00
routeConfig.Domain = domain
for _, location := range locations {
routeConfig.Location = location
tmpRouteConfig := routeConfig
2019-07-31 00:41:58 +08:00
// handle group
if pxy.cfg.Group != "" {
err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, routeConfig)
if err != nil {
return
}
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.Group, tmpRouteConfig)
2019-07-31 00:41:58 +08:00
})
} else {
// no group
2020-05-24 17:48:37 +08:00
err = pxy.rc.HTTPReverseProxy.Register(routeConfig)
2019-07-31 00:41:58 +08:00
if err != nil {
return
}
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.rc.HTTPReverseProxy.UnRegister(tmpRouteConfig)
2019-07-31 00:41:58 +08:00
})
}
2022-08-29 01:02:53 +08:00
addrs = append(addrs, util.CanonicalAddr(routeConfig.Domain, pxy.serverCfg.VhostHTTPPort))
xl.Info("http proxy listen for host [%s] location [%s] group [%s], routeByHTTPUser [%s]",
routeConfig.Domain, routeConfig.Location, pxy.cfg.Group, pxy.cfg.RouteByHTTPUser)
2019-01-15 00:11:08 +08:00
}
}
if pxy.cfg.SubDomain != "" {
routeConfig.Domain = pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
2019-01-15 00:11:08 +08:00
for _, location := range locations {
routeConfig.Location = location
tmpRouteConfig := routeConfig
2019-07-31 00:41:58 +08:00
// handle group
if pxy.cfg.Group != "" {
err = pxy.rc.HTTPGroupCtl.Register(pxy.name, pxy.cfg.Group, pxy.cfg.GroupKey, routeConfig)
if err != nil {
return
}
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.rc.HTTPGroupCtl.UnRegister(pxy.name, pxy.cfg.Group, tmpRouteConfig)
2019-07-31 00:41:58 +08:00
})
} else {
2020-05-24 17:48:37 +08:00
err = pxy.rc.HTTPReverseProxy.Register(routeConfig)
2019-07-31 00:41:58 +08:00
if err != nil {
return
}
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.rc.HTTPReverseProxy.UnRegister(tmpRouteConfig)
2019-07-31 00:41:58 +08:00
})
}
addrs = append(addrs, util.CanonicalAddr(tmpRouteConfig.Domain, pxy.serverCfg.VhostHTTPPort))
2019-07-31 00:41:58 +08:00
xl.Info("http proxy listen for host [%s] location [%s] group [%s], routeByHTTPUser [%s]",
routeConfig.Domain, routeConfig.Location, pxy.cfg.Group, pxy.cfg.RouteByHTTPUser)
2019-01-15 00:11:08 +08:00
}
}
remoteAddr = strings.Join(addrs, ",")
return
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) GetConf() config.ProxyConf {
2019-01-15 00:11:08 +08:00
return pxy.cfg
}
func (pxy *HTTPProxy) GetLimiter() *rate.Limiter {
return pxy.limiter
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) GetRealConn(remoteAddr string) (workConn net.Conn, err error) {
2019-10-12 20:13:12 +08:00
xl := pxy.xl
2019-04-10 10:51:01 +08:00
rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr)
if errRet != nil {
2019-10-12 20:13:12 +08:00
xl.Warn("resolve TCP addr [%s] error: %v", remoteAddr, errRet)
2019-04-10 10:51:01 +08:00
// we do not return error here since remoteAddr is not necessary for proxies without proxy protocol enabled
}
tmpConn, errRet := pxy.GetWorkConnFromPool(rAddr, nil)
2019-01-15 00:11:08 +08:00
if errRet != nil {
err = errRet
return
}
var rwc io.ReadWriteCloser = tmpConn
if pxy.cfg.UseEncryption {
2023-05-29 14:10:34 +08:00
rwc, err = libio.WithEncryption(rwc, []byte(pxy.serverCfg.Token))
2019-01-15 00:11:08 +08:00
if err != nil {
2019-10-12 20:13:12 +08:00
xl.Error("create encryption stream error: %v", err)
2019-01-15 00:11:08 +08:00
return
}
}
if pxy.cfg.UseCompression {
2023-05-29 14:10:34 +08:00
rwc = libio.WithCompression(rwc)
2019-01-15 00:11:08 +08:00
}
if pxy.GetLimiter() != nil {
2023-05-29 14:10:34 +08:00
rwc = libio.WrapReadWriteCloser(limit.NewReader(rwc, pxy.GetLimiter()), limit.NewWriter(rwc, pxy.GetLimiter()), func() error {
return rwc.Close()
})
}
2023-05-29 14:10:34 +08:00
workConn = utilnet.WrapReadWriteCloserToConn(rwc, tmpConn)
workConn = utilnet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
2023-05-30 20:25:22 +08:00
metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConf().GetBaseConfig().ProxyType)
2019-01-15 00:11:08 +08:00
return
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
2019-01-15 00:11:08 +08:00
name := pxy.GetName()
2023-05-30 20:25:22 +08:00
proxyType := pxy.GetConf().GetBaseConfig().ProxyType
metrics.Server.CloseConnection(name, proxyType)
metrics.Server.AddTrafficIn(name, proxyType, totalWrite)
metrics.Server.AddTrafficOut(name, proxyType, totalRead)
2019-01-15 00:11:08 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) Close() {
2019-01-15 00:11:08 +08:00
pxy.BaseProxy.Close()
for _, closeFn := range pxy.closeFuncs {
closeFn()
}
}