mirror of
https://github.com/fatedier/frp.git
synced 2026-10-23 16:33:27 +08:00
InWorkConn published readCh and sendCh into pxy fields while holding pxy.mu, then read those same fields again after unlocking to start the sender, reader and heartbeat goroutines and to call udp.Forwarder. Two work connections installed concurrently made that unlocked read race with the next generation's write to the same fields. Create the channels as locals, publish them under the existing lock, and use only those locals for the goroutines and the forwarder. The fields stay guarded by pxy.mu, so Close still closes the current generation. No behavior change for a single generation. Add TestUDPProxyInWorkConnGenerationChannels, which drives two InWorkConn calls concurrently so the race detector covers the field accesses.
172 lines
4.4 KiB
Go
172 lines
4.4 KiB
Go
// 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.
|
|
|
|
//go:build !frps
|
|
|
|
package proxy
|
|
|
|
import (
|
|
"net"
|
|
"reflect"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/fatedier/golib/errors"
|
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
"github.com/fatedier/frp/pkg/msg"
|
|
"github.com/fatedier/frp/pkg/proto/udp"
|
|
netpkg "github.com/fatedier/frp/pkg/util/net"
|
|
)
|
|
|
|
func init() {
|
|
RegisterProxyFactory(reflect.TypeFor[*v1.UDPProxyConfig](), NewUDPProxy)
|
|
}
|
|
|
|
type UDPProxy struct {
|
|
*BaseProxy
|
|
|
|
cfg *v1.UDPProxyConfig
|
|
|
|
localAddr *net.UDPAddr
|
|
readCh chan *msg.UDPPacket
|
|
|
|
// include msg.UDPPacket and msg.Ping
|
|
sendCh chan msg.Message
|
|
workConn net.Conn
|
|
closed bool
|
|
}
|
|
|
|
func NewUDPProxy(baseProxy *BaseProxy, cfg v1.ProxyConfigurer) Proxy {
|
|
unwrapped, ok := cfg.(*v1.UDPProxyConfig)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return &UDPProxy{
|
|
BaseProxy: baseProxy,
|
|
cfg: unwrapped,
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (pxy *UDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
|
|
xl := pxy.xl
|
|
xl.Infof("incoming a new work connection for udp proxy, %s", conn.RemoteAddr().String())
|
|
// close resources related with old workConn
|
|
pxy.Close()
|
|
|
|
remote, _, err := pxy.wrapWorkConn(conn, pxy.encryptionKey)
|
|
if err != nil {
|
|
xl.Errorf("wrap work connection: %v", err)
|
|
return
|
|
}
|
|
|
|
workConn := netpkg.WrapReadWriteCloserToConn(remote, conn)
|
|
// Plain UDP payload follows the configured wire protocol for message framing.
|
|
payloadRW, err := msg.NewUDPPacketReadWriter(workConn, pxy.clientCfg.Transport.WireProtocol, pxy.udpPacketCodec)
|
|
if err != nil {
|
|
xl.Errorf("create UDP packet read writer: %v", err)
|
|
workConn.Close()
|
|
return
|
|
}
|
|
|
|
pxy.mu.Lock()
|
|
pxy.workConn = workConn
|
|
readCh := make(chan *msg.UDPPacket, 1024)
|
|
sendCh := make(chan msg.Message, 1024)
|
|
pxy.readCh = readCh
|
|
pxy.sendCh = sendCh
|
|
pxy.closed = false
|
|
pxy.mu.Unlock()
|
|
|
|
workConnReaderFn := func(rw msg.ReadWriter, readCh chan *msg.UDPPacket) {
|
|
for {
|
|
var udpMsg msg.UDPPacket
|
|
if errRet := rw.ReadMsgInto(&udpMsg); errRet != nil {
|
|
xl.Warnf("read from workConn for udp error: %v", errRet)
|
|
return
|
|
}
|
|
if errRet := errors.PanicToError(func() {
|
|
xl.Tracef("get udp package from workConn, len: %d", len(udpMsg.Content))
|
|
readCh <- &udpMsg
|
|
}); errRet != nil {
|
|
xl.Infof("reader goroutine for udp work connection closed: %v", errRet)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
workConnSenderFn := func(rw msg.ReadWriter, sendCh chan msg.Message) {
|
|
defer func() {
|
|
xl.Infof("writer goroutine for udp work connection closed")
|
|
}()
|
|
var errRet error
|
|
for rawMsg := range sendCh {
|
|
switch m := rawMsg.(type) {
|
|
case *msg.UDPPacket:
|
|
xl.Tracef("send udp package to workConn, len: %d", len(m.Content))
|
|
case *msg.Ping:
|
|
xl.Tracef("send ping message to udp workConn")
|
|
}
|
|
if errRet = rw.WriteMsg(rawMsg); errRet != nil {
|
|
xl.Errorf("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.Tracef("heartbeat goroutine for udp work connection closed")
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
go workConnSenderFn(payloadRW, sendCh)
|
|
go workConnReaderFn(payloadRW, readCh)
|
|
go heartbeatFn(sendCh)
|
|
|
|
// Call Forwarder with proxy protocol version (empty string means no proxy protocol)
|
|
udp.Forwarder(pxy.localAddr, readCh, sendCh, int(pxy.clientCfg.UDPPacketSize), pxy.cfg.Transport.ProxyProtocolVersion)
|
|
}
|