2023-05-28 16:50:43 +08:00
|
|
|
// Copyright 2023 The frp Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-11-21 11:19:35 +08:00
|
|
|
//go:build !frps
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-05-30 22:18:56 +08:00
|
|
|
"reflect"
|
2023-05-28 16:50:43 +08:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/golib/errors"
|
2023-05-29 14:10:34 +08:00
|
|
|
libio "github.com/fatedier/golib/io"
|
2023-05-28 16:50:43 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-28 16:50:43 +08:00
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
|
|
|
"github.com/fatedier/frp/pkg/proto/udp"
|
|
|
|
"github.com/fatedier/frp/pkg/util/limit"
|
2023-05-29 14:10:34 +08:00
|
|
|
utilnet "github.com/fatedier/frp/pkg/util/net"
|
2023-05-28 16:50:43 +08:00
|
|
|
)
|
|
|
|
|
2023-05-30 22:18:56 +08:00
|
|
|
func init() {
|
2023-09-06 10:18:02 +08:00
|
|
|
RegisterProxyFactory(reflect.TypeOf(&v1.UDPProxyConfig{}), NewUDPProxy)
|
2023-05-30 22:18:56 +08:00
|
|
|
}
|
|
|
|
|
2023-05-28 16:50:43 +08:00
|
|
|
type UDPProxy struct {
|
|
|
|
*BaseProxy
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg *v1.UDPProxyConfig
|
2023-05-28 16:50:43 +08:00
|
|
|
|
|
|
|
localAddr *net.UDPAddr
|
|
|
|
readCh chan *msg.UDPPacket
|
|
|
|
|
|
|
|
// include msg.UDPPacket and msg.Ping
|
|
|
|
sendCh chan msg.Message
|
|
|
|
workConn net.Conn
|
2023-05-30 22:18:56 +08:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewUDPProxy(baseProxy *BaseProxy, cfg v1.ProxyConfigurer) Proxy {
|
|
|
|
unwrapped, ok := cfg.(*v1.UDPProxyConfig)
|
2023-05-30 22:18:56 +08:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &UDPProxy{
|
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: unwrapped,
|
|
|
|
}
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *UDPProxy) Run() (err error) {
|
|
|
|
pxy.localAddr, err = net.ResolveUDPAddr("udp", net.JoinHostPort(pxy.cfg.LocalIP, strconv.Itoa(pxy.cfg.LocalPort)))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pxy *UDPProxy) Close() {
|
|
|
|
pxy.mu.Lock()
|
|
|
|
defer pxy.mu.Unlock()
|
|
|
|
|
|
|
|
if !pxy.closed {
|
|
|
|
pxy.closed = true
|
|
|
|
if pxy.workConn != nil {
|
|
|
|
pxy.workConn.Close()
|
|
|
|
}
|
|
|
|
if pxy.readCh != nil {
|
|
|
|
close(pxy.readCh)
|
|
|
|
}
|
|
|
|
if pxy.sendCh != nil {
|
|
|
|
close(pxy.sendCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:30:46 +08:00
|
|
|
func (pxy *UDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
|
2023-05-28 16:50:43 +08:00
|
|
|
xl := pxy.xl
|
|
|
|
xl.Info("incoming a new work connection for udp proxy, %s", conn.RemoteAddr().String())
|
2023-11-22 14:30:22 +08:00
|
|
|
// close resources related with old workConn
|
2023-05-28 16:50:43 +08:00
|
|
|
pxy.Close()
|
|
|
|
|
|
|
|
var rwc io.ReadWriteCloser = conn
|
|
|
|
var err error
|
|
|
|
if pxy.limiter != nil {
|
2023-05-29 14:10:34 +08:00
|
|
|
rwc = libio.WrapReadWriteCloser(limit.NewReader(conn, pxy.limiter), limit.NewWriter(conn, pxy.limiter), func() error {
|
2023-05-28 16:50:43 +08:00
|
|
|
return conn.Close()
|
|
|
|
})
|
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
if pxy.cfg.Transport.UseEncryption {
|
|
|
|
rwc, err = libio.WithEncryption(rwc, []byte(pxy.clientCfg.Auth.Token))
|
2023-05-28 16:50:43 +08:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
xl.Error("create encryption stream error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
if pxy.cfg.Transport.UseCompression {
|
2023-07-25 21:31:26 +08:00
|
|
|
rwc = libio.WithCompression(rwc)
|
2023-05-28 16:50:43 +08:00
|
|
|
}
|
2023-05-29 14:10:34 +08:00
|
|
|
conn = utilnet.WrapReadWriteCloserToConn(rwc, conn)
|
2023-05-28 16:50:43 +08:00
|
|
|
|
|
|
|
pxy.mu.Lock()
|
|
|
|
pxy.workConn = conn
|
|
|
|
pxy.readCh = make(chan *msg.UDPPacket, 1024)
|
|
|
|
pxy.sendCh = make(chan msg.Message, 1024)
|
|
|
|
pxy.closed = false
|
|
|
|
pxy.mu.Unlock()
|
|
|
|
|
|
|
|
workConnReaderFn := func(conn net.Conn, readCh chan *msg.UDPPacket) {
|
|
|
|
for {
|
|
|
|
var udpMsg msg.UDPPacket
|
|
|
|
if errRet := msg.ReadMsgInto(conn, &udpMsg); errRet != nil {
|
|
|
|
xl.Warn("read from workConn for udp error: %v", errRet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if errRet := errors.PanicToError(func() {
|
|
|
|
xl.Trace("get udp package from workConn: %s", udpMsg.Content)
|
|
|
|
readCh <- &udpMsg
|
|
|
|
}); errRet != nil {
|
|
|
|
xl.Info("reader goroutine for udp work connection closed: %v", errRet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
workConnSenderFn := func(conn net.Conn, sendCh chan msg.Message) {
|
|
|
|
defer func() {
|
|
|
|
xl.Info("writer goroutine for udp work connection closed")
|
|
|
|
}()
|
|
|
|
var errRet error
|
|
|
|
for rawMsg := range sendCh {
|
|
|
|
switch m := rawMsg.(type) {
|
|
|
|
case *msg.UDPPacket:
|
|
|
|
xl.Trace("send udp package to workConn: %s", m.Content)
|
|
|
|
case *msg.Ping:
|
|
|
|
xl.Trace("send ping message to udp workConn")
|
|
|
|
}
|
|
|
|
if errRet = msg.WriteMsg(conn, rawMsg); errRet != nil {
|
|
|
|
xl.Error("udp work write error: %v", errRet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
heartbeatFn := func(sendCh chan msg.Message) {
|
|
|
|
var errRet error
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(30) * time.Second)
|
|
|
|
if errRet = errors.PanicToError(func() {
|
|
|
|
sendCh <- &msg.Ping{}
|
|
|
|
}); errRet != nil {
|
|
|
|
xl.Trace("heartbeat goroutine for udp work connection closed")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go workConnSenderFn(pxy.workConn, pxy.sendCh)
|
|
|
|
go workConnReaderFn(pxy.workConn, pxy.readCh)
|
|
|
|
go heartbeatFn(pxy.sendCh)
|
|
|
|
udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh, int(pxy.clientCfg.UDPPacketSize))
|
|
|
|
}
|