frp/server/proxy/http.go

196 lines
5.4 KiB
Go
Raw Normal View History

2019-01-14 17:11:08 +01: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 04:51:01 +02:00
"net"
2019-01-14 17:11:08 +01:00
"strings"
2023-05-29 08:10:34 +02:00
libio "github.com/fatedier/golib/io"
"golang.org/x/time/rate"
2022-08-28 19:02:53 +02:00
2020-09-23 07:49:14 +02:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/util/limit"
2023-05-29 08:10:34 +02:00
utilnet "github.com/fatedier/frp/pkg/util/net"
2020-09-23 07:49:14 +02:00
"github.com/fatedier/frp/pkg/util/util"
"github.com/fatedier/frp/pkg/util/vhost"
"github.com/fatedier/frp/server/metrics"
2019-01-14 17:11:08 +01:00
)
2020-05-24 11:48:37 +02:00
type HTTPProxy struct {
2019-01-31 09:49:23 +01:00
*BaseProxy
2020-05-24 11:48:37 +02:00
cfg *config.HTTPProxyConf
2019-01-14 17:11:08 +01:00
closeFuncs []func()
}
2020-05-24 11:48:37 +02:00
func (pxy *HTTPProxy) Run() (remoteAddr string, err error) {
2019-10-12 14:13:12 +02:00
xl := pxy.xl
2020-05-24 11:48:37 +02: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-14 17:11:08 +01:00
}
locations := pxy.cfg.Locations
if len(locations) == 0 {
locations = []string{""}
}
2019-07-30 18:41:58 +02:00
defer func() {
if err != nil {
pxy.Close()
}
}()
2019-01-14 17:11:08 +01:00
addrs := make([]string, 0)
for _, domain := range pxy.cfg.CustomDomains {
2019-03-29 10:12:44 +01:00
if domain == "" {
continue
}
2019-01-14 17:11:08 +01:00
routeConfig.Domain = domain
for _, location := range locations {
routeConfig.Location = location
tmpRouteConfig := routeConfig
2019-07-30 18:41:58 +02: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-30 18:41:58 +02:00
})
} else {
// no group
2020-05-24 11:48:37 +02:00
err = pxy.rc.HTTPReverseProxy.Register(routeConfig)
2019-07-30 18:41:58 +02:00
if err != nil {
return
}
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.rc.HTTPReverseProxy.UnRegister(tmpRouteConfig)
2019-07-30 18:41:58 +02:00
})
}
2022-08-28 19:02:53 +02: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-14 17:11:08 +01:00
}
}
if pxy.cfg.SubDomain != "" {
routeConfig.Domain = pxy.cfg.SubDomain + "." + pxy.serverCfg.SubDomainHost
2019-01-14 17:11:08 +01:00
for _, location := range locations {
routeConfig.Location = location
tmpRouteConfig := routeConfig
2019-07-30 18:41:58 +02: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-30 18:41:58 +02:00
})
} else {
2020-05-24 11:48:37 +02:00
err = pxy.rc.HTTPReverseProxy.Register(routeConfig)
2019-07-30 18:41:58 +02:00
if err != nil {
return
}
pxy.closeFuncs = append(pxy.closeFuncs, func() {
pxy.rc.HTTPReverseProxy.UnRegister(tmpRouteConfig)
2019-07-30 18:41:58 +02:00
})
}
addrs = append(addrs, util.CanonicalAddr(tmpRouteConfig.Domain, pxy.serverCfg.VhostHTTPPort))
2019-07-30 18:41:58 +02: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-14 17:11:08 +01:00
}
}
remoteAddr = strings.Join(addrs, ",")
return
}
2020-05-24 11:48:37 +02:00
func (pxy *HTTPProxy) GetConf() config.ProxyConf {
2019-01-14 17:11:08 +01:00
return pxy.cfg
}
func (pxy *HTTPProxy) GetLimiter() *rate.Limiter {
return pxy.limiter
}
2020-05-24 11:48:37 +02:00
func (pxy *HTTPProxy) GetRealConn(remoteAddr string) (workConn net.Conn, err error) {
2019-10-12 14:13:12 +02:00
xl := pxy.xl
2019-04-10 04:51:01 +02:00
rAddr, errRet := net.ResolveTCPAddr("tcp", remoteAddr)
if errRet != nil {
2019-10-12 14:13:12 +02:00
xl.Warn("resolve TCP addr [%s] error: %v", remoteAddr, errRet)
2019-04-10 04:51:01 +02: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-14 17:11:08 +01:00
if errRet != nil {
err = errRet
return
}
var rwc io.ReadWriteCloser = tmpConn
if pxy.cfg.UseEncryption {
2023-05-29 08:10:34 +02:00
rwc, err = libio.WithEncryption(rwc, []byte(pxy.serverCfg.Token))
2019-01-14 17:11:08 +01:00
if err != nil {
2019-10-12 14:13:12 +02:00
xl.Error("create encryption stream error: %v", err)
2019-01-14 17:11:08 +01:00
return
}
}
if pxy.cfg.UseCompression {
2023-05-29 08:10:34 +02:00
rwc = libio.WithCompression(rwc)
2019-01-14 17:11:08 +01:00
}
if pxy.GetLimiter() != nil {
2023-05-29 08:10:34 +02:00
rwc = libio.WrapReadWriteCloser(limit.NewReader(rwc, pxy.GetLimiter()), limit.NewWriter(rwc, pxy.GetLimiter()), func() error {
return rwc.Close()
})
}
2023-05-29 08:10:34 +02:00
workConn = utilnet.WrapReadWriteCloserToConn(rwc, tmpConn)
workConn = utilnet.WrapStatsConn(workConn, pxy.updateStatsAfterClosedConn)
2023-05-30 14:25:22 +02:00
metrics.Server.OpenConnection(pxy.GetName(), pxy.GetConf().GetBaseConfig().ProxyType)
2019-01-14 17:11:08 +01:00
return
}
2020-05-24 11:48:37 +02:00
func (pxy *HTTPProxy) updateStatsAfterClosedConn(totalRead, totalWrite int64) {
2019-01-14 17:11:08 +01:00
name := pxy.GetName()
2023-05-30 14:25:22 +02: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-14 17:11:08 +01:00
}
2020-05-24 11:48:37 +02:00
func (pxy *HTTPProxy) Close() {
2019-01-14 17:11:08 +01:00
pxy.BaseProxy.Close()
for _, closeFn := range pxy.closeFuncs {
closeFn()
}
}