frp/client/proxy/proxy.go

369 lines
8.9 KiB
Go
Raw Normal View History

2017-03-09 02:03:47 +08: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.
2018-12-09 22:06:22 +08:00
package proxy
2017-03-09 02:03:47 +08:00
import (
2017-10-24 22:53:20 +08:00
"bytes"
2019-10-12 20:13:12 +08:00
"context"
2017-03-09 02:03:47 +08:00
"io"
2017-03-13 02:44:47 +08:00
"net"
2019-03-11 15:53:58 +08:00
"strconv"
"strings"
2017-04-25 00:34:14 +08:00
"sync"
"time"
2017-03-09 02:03:47 +08:00
2018-05-08 23:42:04 +08:00
frpIo "github.com/fatedier/golib/io"
2022-01-20 20:03:07 +08:00
libdial "github.com/fatedier/golib/net/dial"
2019-03-29 19:01:18 +08:00
pp "github.com/pires/go-proxyproto"
2019-11-03 01:20:49 +08:00
"golang.org/x/time/rate"
2022-08-29 01:02:53 +08:00
"github.com/fatedier/frp/pkg/config"
"github.com/fatedier/frp/pkg/msg"
plugin "github.com/fatedier/frp/pkg/plugin/client"
"github.com/fatedier/frp/pkg/transport"
2022-08-29 01:02:53 +08:00
"github.com/fatedier/frp/pkg/util/limit"
"github.com/fatedier/frp/pkg/util/xlog"
2017-03-09 02:03:47 +08:00
)
2018-11-06 18:35:05 +08:00
// Proxy defines how to handle work connections for different proxy type.
2017-03-09 02:03:47 +08:00
type Proxy interface {
2017-03-12 02:03:24 +08:00
Run() error
2017-03-10 01:42:06 +08:00
// InWorkConn accept work connections registered to server.
2019-10-12 20:13:12 +08:00
InWorkConn(net.Conn, *msg.StartWorkConn)
2018-01-17 01:09:33 +08:00
2017-03-09 02:03:47 +08:00
Close()
}
func NewProxy(
ctx context.Context,
pxyConf config.ProxyConf,
clientCfg config.ClientCommonConf,
msgTransporter transport.MessageTransporter,
) (pxy Proxy) {
2019-11-03 01:20:49 +08:00
var limiter *rate.Limiter
2019-11-09 01:13:30 +08:00
limitBytes := pxyConf.GetBaseInfo().BandwidthLimit.Bytes()
if limitBytes > 0 && pxyConf.GetBaseInfo().BandwidthLimitMode == config.BandwidthLimitModeClient {
2019-11-03 01:20:49 +08:00
limiter = rate.NewLimiter(rate.Limit(float64(limitBytes)), int(limitBytes))
}
2017-03-13 02:44:47 +08:00
baseProxy := BaseProxy{
clientCfg: clientCfg,
limiter: limiter,
msgTransporter: msgTransporter,
xl: xlog.FromContextSafe(ctx),
ctx: ctx,
2017-03-13 02:44:47 +08:00
}
2017-03-09 02:03:47 +08:00
switch cfg := pxyConf.(type) {
2020-05-24 17:48:37 +08:00
case *config.TCPProxyConf:
pxy = &TCPProxy{
2019-01-31 16:49:23 +08:00
BaseProxy: &baseProxy,
2017-03-13 02:44:47 +08:00
cfg: cfg,
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
case *config.TCPMuxProxyConf:
pxy = &TCPMuxProxy{
BaseProxy: &baseProxy,
cfg: cfg,
}
2020-05-24 17:48:37 +08:00
case *config.UDPProxyConf:
pxy = &UDPProxy{
2019-01-31 16:49:23 +08:00
BaseProxy: &baseProxy,
2017-03-13 02:44:47 +08:00
cfg: cfg,
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
case *config.HTTPProxyConf:
pxy = &HTTPProxy{
2019-01-31 16:49:23 +08:00
BaseProxy: &baseProxy,
2017-03-13 02:44:47 +08:00
cfg: cfg,
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
case *config.HTTPSProxyConf:
pxy = &HTTPSProxy{
2019-01-31 16:49:23 +08:00
BaseProxy: &baseProxy,
2017-03-13 02:44:47 +08:00
cfg: cfg,
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
case *config.STCPProxyConf:
pxy = &STCPProxy{
2019-01-31 16:49:23 +08:00
BaseProxy: &baseProxy,
2017-06-26 03:02:33 +08:00
cfg: cfg,
}
2020-05-24 17:48:37 +08:00
case *config.XTCPProxyConf:
pxy = &XTCPProxy{
2019-01-31 16:49:23 +08:00
BaseProxy: &baseProxy,
2017-10-24 18:20:07 +08:00
cfg: cfg,
}
2020-05-24 17:48:37 +08:00
case *config.SUDPProxyConf:
pxy = &SUDPProxy{
2020-04-22 21:37:45 +08:00
BaseProxy: &baseProxy,
cfg: cfg,
closeCh: make(chan struct{}),
}
2017-03-09 02:03:47 +08:00
}
return
}
2017-03-13 02:44:47 +08:00
type BaseProxy struct {
closed bool
clientCfg config.ClientCommonConf
msgTransporter transport.MessageTransporter
limiter *rate.Limiter
2019-10-12 20:13:12 +08:00
mu sync.RWMutex
xl *xlog.Logger
ctx context.Context
2017-03-13 02:44:47 +08:00
}
2017-03-09 02:03:47 +08:00
// TCP
2020-05-24 17:48:37 +08:00
type TCPProxy struct {
2019-01-31 16:49:23 +08:00
*BaseProxy
2017-03-13 02:44:47 +08:00
2020-05-24 17:48:37 +08:00
cfg *config.TCPProxyConf
proxyPlugin plugin.Plugin
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *TCPProxy) Run() (err error) {
if pxy.cfg.Plugin != "" {
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
if err != nil {
return
}
}
2017-03-12 02:03:24 +08:00
return
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *TCPProxy) Close() {
if pxy.proxyPlugin != nil {
pxy.proxyPlugin.Close()
}
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *TCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
2019-11-03 01:20:49 +08:00
conn, []byte(pxy.clientCfg.Token), m)
2017-03-09 02:03:47 +08:00
}
// TCP Multiplexer
2020-05-24 17:48:37 +08:00
type TCPMuxProxy struct {
*BaseProxy
2020-05-24 17:48:37 +08:00
cfg *config.TCPMuxProxyConf
proxyPlugin plugin.Plugin
}
2020-05-24 17:48:37 +08:00
func (pxy *TCPMuxProxy) Run() (err error) {
if pxy.cfg.Plugin != "" {
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
if err != nil {
return
}
}
return
}
2020-05-24 17:48:37 +08:00
func (pxy *TCPMuxProxy) Close() {
if pxy.proxyPlugin != nil {
pxy.proxyPlugin.Close()
}
}
2020-05-24 17:48:37 +08:00
func (pxy *TCPMuxProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
conn, []byte(pxy.clientCfg.Token), m)
}
2017-03-09 02:03:47 +08:00
// HTTP
2020-05-24 17:48:37 +08:00
type HTTPProxy struct {
2019-01-31 16:49:23 +08:00
*BaseProxy
2017-03-13 02:44:47 +08:00
2020-05-24 17:48:37 +08:00
cfg *config.HTTPProxyConf
proxyPlugin plugin.Plugin
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) Run() (err error) {
if pxy.cfg.Plugin != "" {
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
if err != nil {
return
}
}
2017-03-12 02:03:24 +08:00
return
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) Close() {
if pxy.proxyPlugin != nil {
pxy.proxyPlugin.Close()
}
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
2019-11-03 01:20:49 +08:00
conn, []byte(pxy.clientCfg.Token), m)
2017-03-09 02:03:47 +08:00
}
// HTTPS
2020-05-24 17:48:37 +08:00
type HTTPSProxy struct {
2019-01-31 16:49:23 +08:00
*BaseProxy
2017-03-13 02:44:47 +08:00
2020-05-24 17:48:37 +08:00
cfg *config.HTTPSProxyConf
proxyPlugin plugin.Plugin
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPSProxy) Run() (err error) {
if pxy.cfg.Plugin != "" {
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
if err != nil {
return
}
}
2017-03-12 02:03:24 +08:00
return
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPSProxy) Close() {
if pxy.proxyPlugin != nil {
pxy.proxyPlugin.Close()
}
2017-03-09 02:03:47 +08:00
}
2020-05-24 17:48:37 +08:00
func (pxy *HTTPSProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
2019-11-03 01:20:49 +08:00
conn, []byte(pxy.clientCfg.Token), m)
2017-03-10 02:01:17 +08:00
}
2017-06-26 03:02:33 +08:00
// STCP
2020-05-24 17:48:37 +08:00
type STCPProxy struct {
2019-01-31 16:49:23 +08:00
*BaseProxy
2017-06-26 03:02:33 +08:00
2020-05-24 17:48:37 +08:00
cfg *config.STCPProxyConf
2017-06-26 03:02:33 +08:00
proxyPlugin plugin.Plugin
}
2020-05-24 17:48:37 +08:00
func (pxy *STCPProxy) Run() (err error) {
2017-06-26 03:02:33 +08:00
if pxy.cfg.Plugin != "" {
pxy.proxyPlugin, err = plugin.Create(pxy.cfg.Plugin, pxy.cfg.PluginParams)
if err != nil {
return
}
}
return
}
2020-05-24 17:48:37 +08:00
func (pxy *STCPProxy) Close() {
2017-06-26 03:02:33 +08:00
if pxy.proxyPlugin != nil {
pxy.proxyPlugin.Close()
}
}
2020-05-24 17:48:37 +08:00
func (pxy *STCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
HandleTCPWorkConnection(pxy.ctx, &pxy.cfg.LocalSvrConf, pxy.proxyPlugin, pxy.cfg.GetBaseInfo(), pxy.limiter,
2019-11-03 01:20:49 +08:00
conn, []byte(pxy.clientCfg.Token), m)
2017-06-26 03:02:33 +08:00
}
2017-03-10 02:01:17 +08:00
// Common handler for tcp work connections.
2020-05-24 17:48:37 +08:00
func HandleTCPWorkConnection(ctx context.Context, localInfo *config.LocalSvrConf, proxyPlugin plugin.Plugin,
2022-08-29 01:02:53 +08:00
baseInfo *config.BaseProxyConf, limiter *rate.Limiter, workConn net.Conn, encKey []byte, m *msg.StartWorkConn,
) {
2019-10-12 20:13:12 +08:00
xl := xlog.FromContextSafe(ctx)
var (
remote io.ReadWriteCloser
err error
)
2017-03-10 02:01:17 +08:00
remote = workConn
2019-11-03 01:20:49 +08:00
if limiter != nil {
remote = frpIo.WrapReadWriteCloser(limit.NewReader(workConn, limiter), limit.NewWriter(workConn, limiter), func() error {
return workConn.Close()
})
}
2018-01-23 16:31:59 +08:00
2019-10-12 20:13:12 +08:00
xl.Trace("handle tcp work connection, use_encryption: %t, use_compression: %t",
baseInfo.UseEncryption, baseInfo.UseCompression)
2017-03-10 02:01:17 +08:00
if baseInfo.UseEncryption {
2017-10-25 02:49:56 +08:00
remote, err = frpIo.WithEncryption(remote, encKey)
2017-03-10 02:01:17 +08:00
if err != nil {
2018-01-23 16:31:59 +08:00
workConn.Close()
2019-10-12 20:13:12 +08:00
xl.Error("create encryption stream error: %v", err)
2017-03-10 02:01:17 +08:00
return
}
}
if baseInfo.UseCompression {
2017-06-06 18:48:40 +08:00
remote = frpIo.WithCompression(remote)
2017-03-10 02:01:17 +08:00
}
// check if we need to send proxy protocol info
var extraInfo []byte
if baseInfo.ProxyProtocolVersion != "" {
if m.SrcAddr != "" && m.SrcPort != 0 {
if m.DstAddr == "" {
m.DstAddr = "127.0.0.1"
}
srcAddr, _ := net.ResolveTCPAddr("tcp", net.JoinHostPort(m.SrcAddr, strconv.Itoa(int(m.SrcPort))))
dstAddr, _ := net.ResolveTCPAddr("tcp", net.JoinHostPort(m.DstAddr, strconv.Itoa(int(m.DstPort))))
h := &pp.Header{
Command: pp.PROXY,
SourceAddr: srcAddr,
DestinationAddr: dstAddr,
}
if strings.Contains(m.SrcAddr, ".") {
h.TransportProtocol = pp.TCPv4
} else {
h.TransportProtocol = pp.TCPv6
}
if baseInfo.ProxyProtocolVersion == "v1" {
h.Version = 1
} else if baseInfo.ProxyProtocolVersion == "v2" {
h.Version = 2
}
buf := bytes.NewBuffer(nil)
2022-08-29 01:02:53 +08:00
_, _ = h.WriteTo(buf)
extraInfo = buf.Bytes()
}
}
if proxyPlugin != nil {
// if plugin is set, let plugin handle connections first
2019-10-12 20:13:12 +08:00
xl.Debug("handle by plugin: %s", proxyPlugin.Name())
proxyPlugin.Handle(remote, workConn, extraInfo)
2019-10-12 20:13:12 +08:00
xl.Debug("handle by plugin finished")
return
2020-05-24 17:48:37 +08:00
}
localConn, err := libdial.Dial(
net.JoinHostPort(localInfo.LocalIP, strconv.Itoa(localInfo.LocalPort)),
libdial.WithTimeout(10*time.Second),
)
2020-05-24 17:48:37 +08:00
if err != nil {
workConn.Close()
xl.Error("connect to local service [%s:%d] error: %v", localInfo.LocalIP, localInfo.LocalPort, err)
return
}
2019-03-29 19:01:18 +08:00
2020-05-24 17:48:37 +08:00
xl.Debug("join connections, localConn(l[%s] r[%s]) workConn(l[%s] r[%s])", localConn.LocalAddr().String(),
localConn.RemoteAddr().String(), workConn.LocalAddr().String(), workConn.RemoteAddr().String())
2019-03-29 19:01:18 +08:00
2020-05-24 17:48:37 +08:00
if len(extraInfo) > 0 {
2022-08-29 01:02:53 +08:00
if _, err := localConn.Write(extraInfo); err != nil {
workConn.Close()
xl.Error("write extraInfo to local conn error: %v", err)
return
}
}
2020-05-24 17:48:37 +08:00
2023-03-11 19:34:06 +08:00
_, _, errs := frpIo.Join(localConn, remote)
2020-05-24 17:48:37 +08:00
xl.Debug("join connections closed")
2023-03-11 19:34:06 +08:00
if len(errs) > 0 {
xl.Trace("join connections errors: %v", errs)
}
2017-03-09 02:03:47 +08:00
}