2023-05-28 10:50:43 +02: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 04:19:35 +01:00
|
|
|
//go:build !frps
|
|
|
|
|
2023-05-28 10:50:43 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-05-30 16:18:56 +02:00
|
|
|
"reflect"
|
2023-05-28 10:50:43 +02:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatedier/golib/errors"
|
2023-05-29 08:10:34 +02:00
|
|
|
libio "github.com/fatedier/golib/io"
|
2023-05-28 10:50:43 +02:00
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2023-05-28 10:50:43 +02:00
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
|
|
|
"github.com/fatedier/frp/pkg/proto/udp"
|
|
|
|
"github.com/fatedier/frp/pkg/util/limit"
|
2023-11-27 08:47:49 +01:00
|
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
2023-05-28 10:50:43 +02:00
|
|
|
)
|
|
|
|
|
2023-05-30 16:18:56 +02:00
|
|
|
func init() {
|
2023-09-06 04:18:02 +02:00
|
|
|
RegisterProxyFactory(reflect.TypeOf(&v1.SUDPProxyConfig{}), NewSUDPProxy)
|
2023-05-30 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2023-05-28 10:50:43 +02:00
|
|
|
type SUDPProxy struct {
|
|
|
|
*BaseProxy
|
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
cfg *v1.SUDPProxyConfig
|
2023-05-28 10:50:43 +02:00
|
|
|
|
|
|
|
localAddr *net.UDPAddr
|
|
|
|
|
|
|
|
closeCh chan struct{}
|
|
|
|
}
|
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
func NewSUDPProxy(baseProxy *BaseProxy, cfg v1.ProxyConfigurer) Proxy {
|
|
|
|
unwrapped, ok := cfg.(*v1.SUDPProxyConfig)
|
2023-05-30 16:18:56 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &SUDPProxy{
|
|
|
|
BaseProxy: baseProxy,
|
|
|
|
cfg: unwrapped,
|
|
|
|
closeCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 10:50:43 +02:00
|
|
|
func (pxy *SUDPProxy) 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 *SUDPProxy) Close() {
|
|
|
|
pxy.mu.Lock()
|
|
|
|
defer pxy.mu.Unlock()
|
|
|
|
select {
|
|
|
|
case <-pxy.closeCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(pxy.closeCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 04:30:46 +02:00
|
|
|
func (pxy *SUDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
|
2023-05-28 10:50:43 +02:00
|
|
|
xl := pxy.xl
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Infof("incoming a new work connection for sudp proxy, %s", conn.RemoteAddr().String())
|
2023-05-28 10:50:43 +02:00
|
|
|
|
|
|
|
var rwc io.ReadWriteCloser = conn
|
|
|
|
var err error
|
|
|
|
if pxy.limiter != nil {
|
2023-05-29 08:10:34 +02:00
|
|
|
rwc = libio.WrapReadWriteCloser(limit.NewReader(conn, pxy.limiter), limit.NewWriter(conn, pxy.limiter), func() error {
|
2023-05-28 10:50:43 +02:00
|
|
|
return conn.Close()
|
|
|
|
})
|
|
|
|
}
|
2023-09-06 04:18:02 +02:00
|
|
|
if pxy.cfg.Transport.UseEncryption {
|
|
|
|
rwc, err = libio.WithEncryption(rwc, []byte(pxy.clientCfg.Auth.Token))
|
2023-05-28 10:50:43 +02:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Errorf("create encryption stream error: %v", err)
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-09-06 04:18:02 +02:00
|
|
|
if pxy.cfg.Transport.UseCompression {
|
2023-07-25 15:31:26 +02:00
|
|
|
rwc = libio.WithCompression(rwc)
|
2023-05-28 10:50:43 +02:00
|
|
|
}
|
2023-11-27 08:47:49 +01:00
|
|
|
conn = netpkg.WrapReadWriteCloserToConn(rwc, conn)
|
2023-05-28 10:50:43 +02:00
|
|
|
|
|
|
|
workConn := conn
|
|
|
|
readCh := make(chan *msg.UDPPacket, 1024)
|
|
|
|
sendCh := make(chan msg.Message, 1024)
|
|
|
|
isClose := false
|
|
|
|
|
|
|
|
mu := &sync.Mutex{}
|
|
|
|
|
|
|
|
closeFn := func() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if isClose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isClose = true
|
|
|
|
if workConn != nil {
|
|
|
|
workConn.Close()
|
|
|
|
}
|
|
|
|
close(readCh)
|
|
|
|
close(sendCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// udp service <- frpc <- frps <- frpc visitor <- user
|
|
|
|
workConnReaderFn := func(conn net.Conn, readCh chan *msg.UDPPacket) {
|
|
|
|
defer closeFn()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// first to check sudp proxy is closed or not
|
|
|
|
select {
|
|
|
|
case <-pxy.closeCh:
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Tracef("frpc sudp proxy is closed")
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var udpMsg msg.UDPPacket
|
|
|
|
if errRet := msg.ReadMsgInto(conn, &udpMsg); errRet != nil {
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Warnf("read from workConn for sudp error: %v", errRet)
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if errRet := errors.PanicToError(func() {
|
|
|
|
readCh <- &udpMsg
|
|
|
|
}); errRet != nil {
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Warnf("reader goroutine for sudp work connection closed: %v", errRet)
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// udp service -> frpc -> frps -> frpc visitor -> user
|
|
|
|
workConnSenderFn := func(conn net.Conn, sendCh chan msg.Message) {
|
|
|
|
defer func() {
|
|
|
|
closeFn()
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Infof("writer goroutine for sudp work connection closed")
|
2023-05-28 10:50:43 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
var errRet error
|
|
|
|
for rawMsg := range sendCh {
|
|
|
|
switch m := rawMsg.(type) {
|
|
|
|
case *msg.UDPPacket:
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Tracef("frpc send udp package to frpc visitor, [udp local: %v, remote: %v], [tcp work conn local: %v, remote: %v]",
|
2023-05-28 10:50:43 +02:00
|
|
|
m.LocalAddr.String(), m.RemoteAddr.String(), conn.LocalAddr().String(), conn.RemoteAddr().String())
|
|
|
|
case *msg.Ping:
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Tracef("frpc send ping message to frpc visitor")
|
2023-05-28 10:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if errRet = msg.WriteMsg(conn, rawMsg); errRet != nil {
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Errorf("sudp work write error: %v", errRet)
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
heartbeatFn := func(sendCh chan msg.Message) {
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
closeFn()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var errRet error
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if errRet = errors.PanicToError(func() {
|
|
|
|
sendCh <- &msg.Ping{}
|
|
|
|
}); errRet != nil {
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Warnf("heartbeat goroutine for sudp work connection closed")
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-pxy.closeCh:
|
2024-03-12 06:58:53 +01:00
|
|
|
xl.Tracef("frpc sudp proxy is closed")
|
2023-05-28 10:50:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go workConnSenderFn(workConn, sendCh)
|
|
|
|
go workConnReaderFn(workConn, readCh)
|
|
|
|
go heartbeatFn(sendCh)
|
|
|
|
|
|
|
|
udp.Forwarder(pxy.localAddr, readCh, sendCh, int(pxy.clientCfg.UDPPacketSize))
|
|
|
|
}
|