frp/server/proxy/proxy.go

339 lines
8.9 KiB
Go
Raw Normal View History

2019-01-14 17:11:08 +01:00
// Copyright 2017 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 (
2019-10-12 14:13:12 +02:00
"context"
2019-01-14 17:11:08 +01:00
"fmt"
"io"
2019-03-29 12:01:18 +01:00
"net"
"strconv"
2019-01-14 17:11:08 +01:00
"sync"
"time"
2019-01-14 17:11:08 +01:00
2022-08-28 19:02:53 +02:00
frpIo "github.com/fatedier/golib/io"
2020-09-23 07:49:14 +02:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/msg"
plugin "github.com/fatedier/frp/pkg/plugin/server"
frpNet "github.com/fatedier/frp/pkg/util/net"
"github.com/fatedier/frp/pkg/util/xlog"
2019-01-14 17:11:08 +01:00
"github.com/fatedier/frp/server/controller"
"github.com/fatedier/frp/server/metrics"
2019-01-14 17:11:08 +01:00
)
2019-10-12 14:13:12 +02:00
type GetWorkConnFn func() (net.Conn, error)
2019-01-14 17:11:08 +01:00
type Proxy interface {
2019-10-12 14:13:12 +02:00
Context() context.Context
2019-01-14 17:11:08 +01:00
Run() (remoteAddr string, err error)
GetName() string
GetConf() config.ProxyConf
2019-10-12 14:13:12 +02:00
GetWorkConnFromPool(src, dst net.Addr) (workConn net.Conn, err error)
2019-01-14 17:11:08 +01:00
GetUsedPortsNum() int
GetResourceController() *controller.ResourceController
GetUserInfo() plugin.UserInfo
2019-01-14 17:11:08 +01:00
Close()
}
type BaseProxy struct {
name string
rc *controller.ResourceController
listeners []net.Listener
usedPortsNum int
poolCount int
getWorkConnFn GetWorkConnFn
serverCfg config.ServerCommonConf
userInfo plugin.UserInfo
2019-01-14 17:11:08 +01:00
2019-10-12 14:13:12 +02:00
mu sync.RWMutex
xl *xlog.Logger
ctx context.Context
2019-01-14 17:11:08 +01:00
}
func (pxy *BaseProxy) GetName() string {
return pxy.name
}
2019-10-12 14:13:12 +02:00
func (pxy *BaseProxy) Context() context.Context {
return pxy.ctx
}
2019-01-14 17:11:08 +01:00
func (pxy *BaseProxy) GetUsedPortsNum() int {
return pxy.usedPortsNum
}
func (pxy *BaseProxy) GetResourceController() *controller.ResourceController {
return pxy.rc
}
func (pxy *BaseProxy) GetUserInfo() plugin.UserInfo {
return pxy.userInfo
}
2019-01-14 17:11:08 +01:00
func (pxy *BaseProxy) Close() {
2019-10-12 14:13:12 +02:00
xl := xlog.FromContextSafe(pxy.ctx)
xl.Info("proxy closing")
2019-01-14 17:11:08 +01:00
for _, l := range pxy.listeners {
l.Close()
}
}
2019-07-30 18:41:58 +02:00
// GetWorkConnFromPool try to get a new work connections from pool
// for quickly response, we immediately send the StartWorkConn message to frpc after take out one from pool
2019-10-12 14:13:12 +02:00
func (pxy *BaseProxy) GetWorkConnFromPool(src, dst net.Addr) (workConn net.Conn, err error) {
xl := xlog.FromContextSafe(pxy.ctx)
2019-01-14 17:11:08 +01:00
// try all connections from the pool
for i := 0; i < pxy.poolCount+1; i++ {
if workConn, err = pxy.getWorkConnFn(); err != nil {
2019-10-12 14:13:12 +02:00
xl.Warn("failed to get work connection: %v", err)
2019-01-14 17:11:08 +01:00
return
}
xl.Debug("get a new work connection: [%s]", workConn.RemoteAddr().String())
2019-10-12 14:13:12 +02:00
xl.Spawn().AppendPrefix(pxy.GetName())
2020-05-24 11:48:37 +02:00
workConn = frpNet.NewContextConn(pxy.ctx, workConn)
2019-01-14 17:11:08 +01:00
2019-03-29 12:01:18 +01:00
var (
srcAddr string
dstAddr string
srcPortStr string
dstPortStr string
srcPort int
dstPort int
)
if src != nil {
srcAddr, srcPortStr, _ = net.SplitHostPort(src.String())
srcPort, _ = strconv.Atoi(srcPortStr)
}
if dst != nil {
dstAddr, dstPortStr, _ = net.SplitHostPort(dst.String())
dstPort, _ = strconv.Atoi(dstPortStr)
}
2019-01-14 17:11:08 +01:00
err := msg.WriteMsg(workConn, &msg.StartWorkConn{
ProxyName: pxy.GetName(),
2019-03-29 12:01:18 +01:00
SrcAddr: srcAddr,
SrcPort: uint16(srcPort),
DstAddr: dstAddr,
DstPort: uint16(dstPort),
Error: "",
2019-01-14 17:11:08 +01:00
})
if err != nil {
2019-10-12 14:13:12 +02:00
xl.Warn("failed to send message to work connection from pool: %v, times: %d", err, i)
2019-01-14 17:11:08 +01:00
workConn.Close()
} else {
break
}
}
if err != nil {
2019-10-12 14:13:12 +02:00
xl.Error("try to get work connection failed in the end")
2019-01-14 17:11:08 +01:00
return
}
return
}
// startListenHandler start a goroutine handler for each listener.
// p: p will just be passed to handler(Proxy, frpNet.Conn).
// handler: each proxy type can set different handler function to deal with connections accepted from listeners.
func (pxy *BaseProxy) startListenHandler(p Proxy, handler func(Proxy, net.Conn, config.ServerCommonConf)) {
2019-10-12 14:13:12 +02:00
xl := xlog.FromContextSafe(pxy.ctx)
2019-01-14 17:11:08 +01:00
for _, listener := range pxy.listeners {
2019-10-12 14:13:12 +02:00
go func(l net.Listener) {
var tempDelay time.Duration // how long to sleep on accept failure
2019-01-14 17:11:08 +01:00
for {
// block
// if listener is closed, err returned
c, err := l.Accept()
if err != nil {
if err, ok := err.(interface{ Temporary() bool }); ok && err.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
xl.Info("met temporary error: %s, sleep for %s ...", err, tempDelay)
time.Sleep(tempDelay)
continue
}
xl.Warn("listener is closed: %s", err)
2019-01-14 17:11:08 +01:00
return
}
xl.Info("get a user connection [%s]", c.RemoteAddr().String())
go handler(p, c, pxy.serverCfg)
2019-01-14 17:11:08 +01:00
}
}(listener)
}
}
func NewProxy(ctx context.Context, userInfo plugin.UserInfo, rc *controller.ResourceController, poolCount int,
2022-08-28 19:02:53 +02:00
getWorkConnFn GetWorkConnFn, pxyConf config.ProxyConf, serverCfg config.ServerCommonConf,
) (pxy Proxy, err error) {
2019-10-12 14:13:12 +02:00
xl := xlog.FromContextSafe(ctx).Spawn().AppendPrefix(pxyConf.GetBaseInfo().ProxyName)
2019-01-14 17:11:08 +01:00
basePxy := BaseProxy{
name: pxyConf.GetBaseInfo().ProxyName,
rc: rc,
listeners: make([]net.Listener, 0),
poolCount: poolCount,
getWorkConnFn: getWorkConnFn,
serverCfg: serverCfg,
xl: xl,
ctx: xlog.NewContext(ctx, xl),
userInfo: userInfo,
2019-01-14 17:11:08 +01:00
}
switch cfg := pxyConf.(type) {
2020-05-24 11:48:37 +02:00
case *config.TCPProxyConf:
2019-01-14 17:11:08 +01:00
basePxy.usedPortsNum = 1
2020-05-24 11:48:37 +02:00
pxy = &TCPProxy{
2019-01-31 09:49:23 +01:00
BaseProxy: &basePxy,
2019-01-14 17:11:08 +01:00
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.TCPMuxProxyConf:
pxy = &TCPMuxProxy{
BaseProxy: &basePxy,
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.HTTPProxyConf:
pxy = &HTTPProxy{
2019-01-31 09:49:23 +01:00
BaseProxy: &basePxy,
2019-01-14 17:11:08 +01:00
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.HTTPSProxyConf:
pxy = &HTTPSProxy{
2019-01-31 09:49:23 +01:00
BaseProxy: &basePxy,
2019-01-14 17:11:08 +01:00
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.UDPProxyConf:
2019-01-14 17:11:08 +01:00
basePxy.usedPortsNum = 1
2020-05-24 11:48:37 +02:00
pxy = &UDPProxy{
2019-01-31 09:49:23 +01:00
BaseProxy: &basePxy,
2019-01-14 17:11:08 +01:00
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.STCPProxyConf:
pxy = &STCPProxy{
2019-01-31 09:49:23 +01:00
BaseProxy: &basePxy,
2019-01-14 17:11:08 +01:00
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.XTCPProxyConf:
pxy = &XTCPProxy{
2019-01-31 09:49:23 +01:00
BaseProxy: &basePxy,
2019-01-14 17:11:08 +01:00
cfg: cfg,
}
2020-05-24 11:48:37 +02:00
case *config.SUDPProxyConf:
pxy = &SUDPProxy{
2020-04-22 15:37:45 +02:00
BaseProxy: &basePxy,
cfg: cfg,
}
2019-01-14 17:11:08 +01:00
default:
return pxy, fmt.Errorf("proxy type not support")
}
return
}
2020-05-24 11:48:37 +02:00
// HandleUserTCPConnection is used for incoming user TCP connections.
2019-01-14 17:11:08 +01:00
// It can be used for tcp, http, https type.
2020-05-24 11:48:37 +02:00
func HandleUserTCPConnection(pxy Proxy, userConn net.Conn, serverCfg config.ServerCommonConf) {
2019-10-12 14:13:12 +02:00
xl := xlog.FromContextSafe(pxy.Context())
2019-01-14 17:11:08 +01:00
defer userConn.Close()
// server plugin hook
rc := pxy.GetResourceController()
content := &plugin.NewUserConnContent{
User: pxy.GetUserInfo(),
ProxyName: pxy.GetName(),
ProxyType: pxy.GetConf().GetBaseInfo().ProxyType,
RemoteAddr: userConn.RemoteAddr().String(),
}
_, err := rc.PluginManager.NewUserConn(content)
if err != nil {
xl.Warn("the user conn [%s] was rejected, err:%v", content.RemoteAddr, err)
return
}
2019-01-14 17:11:08 +01:00
// try all connections from the pool
2019-03-29 12:01:18 +01:00
workConn, err := pxy.GetWorkConnFromPool(userConn.RemoteAddr(), userConn.LocalAddr())
2019-01-14 17:11:08 +01:00
if err != nil {
return
}
defer workConn.Close()
var local io.ReadWriteCloser = workConn
cfg := pxy.GetConf().GetBaseInfo()
2019-10-12 14:13:12 +02:00
xl.Trace("handler user tcp connection, use_encryption: %t, use_compression: %t", cfg.UseEncryption, cfg.UseCompression)
2019-01-14 17:11:08 +01:00
if cfg.UseEncryption {
local, err = frpIo.WithEncryption(local, []byte(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 cfg.UseCompression {
local = frpIo.WithCompression(local)
}
2019-10-12 14:13:12 +02:00
xl.Debug("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
2019-01-14 17:11:08 +01:00
workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
name := pxy.GetName()
proxyType := pxy.GetConf().GetBaseInfo().ProxyType
metrics.Server.OpenConnection(name, proxyType)
2019-01-14 17:11:08 +01:00
inCount, outCount := frpIo.Join(local, userConn)
metrics.Server.CloseConnection(name, proxyType)
metrics.Server.AddTrafficIn(name, proxyType, inCount)
metrics.Server.AddTrafficOut(name, proxyType, outCount)
2019-10-12 14:13:12 +02:00
xl.Debug("join connections closed")
2019-01-14 17:11:08 +01:00
}
2020-05-24 11:48:37 +02:00
type Manager struct {
2019-01-14 17:11:08 +01:00
// proxies indexed by proxy name
pxys map[string]Proxy
mu sync.RWMutex
}
2020-05-24 11:48:37 +02:00
func NewManager() *Manager {
return &Manager{
2019-01-14 17:11:08 +01:00
pxys: make(map[string]Proxy),
}
}
2020-05-24 11:48:37 +02:00
func (pm *Manager) Add(name string, pxy Proxy) error {
2019-01-14 17:11:08 +01:00
pm.mu.Lock()
defer pm.mu.Unlock()
if _, ok := pm.pxys[name]; ok {
return fmt.Errorf("proxy name [%s] is already in use", name)
}
pm.pxys[name] = pxy
return nil
}
2020-05-24 11:48:37 +02:00
func (pm *Manager) Del(name string) {
2019-01-14 17:11:08 +01:00
pm.mu.Lock()
defer pm.mu.Unlock()
delete(pm.pxys, name)
}
2020-05-24 11:48:37 +02:00
func (pm *Manager) GetByName(name string) (pxy Proxy, ok bool) {
2019-01-14 17:11:08 +01:00
pm.mu.RLock()
defer pm.mu.RUnlock()
pxy, ok = pm.pxys[name]
return
}