pkg/msg: change UDPPacket.Content from string to []byte to avoid redundant base64 encode/decode (#5198)

This commit is contained in:
fatedier
2026-03-06 01:38:24 +08:00
committed by GitHub
parent 541878af4d
commit 462c987f6d
5 changed files with 13 additions and 13 deletions

View File

@@ -129,7 +129,7 @@ func (pxy *UDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
return return
} }
if errRet := errors.PanicToError(func() { if errRet := errors.PanicToError(func() {
xl.Tracef("get udp package from workConn: %s", udpMsg.Content) xl.Tracef("get udp package from workConn, len: %d", len(udpMsg.Content))
readCh <- &udpMsg readCh <- &udpMsg
}); errRet != nil { }); errRet != nil {
xl.Infof("reader goroutine for udp work connection closed: %v", errRet) xl.Infof("reader goroutine for udp work connection closed: %v", errRet)
@@ -145,7 +145,7 @@ func (pxy *UDPProxy) InWorkConn(conn net.Conn, _ *msg.StartWorkConn) {
for rawMsg := range sendCh { for rawMsg := range sendCh {
switch m := rawMsg.(type) { switch m := rawMsg.(type) {
case *msg.UDPPacket: case *msg.UDPPacket:
xl.Tracef("send udp package to workConn: %s", m.Content) xl.Tracef("send udp package to workConn, len: %d", len(m.Content))
case *msg.Ping: case *msg.Ping:
xl.Tracef("send ping message to udp workConn") xl.Tracef("send ping message to udp workConn")
} }

View File

@@ -147,7 +147,7 @@ func (sv *SUDPVisitor) worker(workConn net.Conn, firstPacket *msg.UDPPacket) {
case *msg.UDPPacket: case *msg.UDPPacket:
if errRet := errors.PanicToError(func() { if errRet := errors.PanicToError(func() {
sv.readCh <- m sv.readCh <- m
xl.Tracef("frpc visitor get udp packet from workConn: %s", m.Content) xl.Tracef("frpc visitor get udp packet from workConn, len: %d", len(m.Content))
}); errRet != nil { }); errRet != nil {
xl.Infof("reader goroutine for udp work connection closed") xl.Infof("reader goroutine for udp work connection closed")
return return
@@ -169,7 +169,7 @@ func (sv *SUDPVisitor) worker(workConn net.Conn, firstPacket *msg.UDPPacket) {
xl.Warnf("sender goroutine for udp work connection closed: %v", errRet) xl.Warnf("sender goroutine for udp work connection closed: %v", errRet)
return return
} }
xl.Tracef("send udp package to workConn: %s", firstPacket.Content) xl.Tracef("send udp package to workConn, len: %d", len(firstPacket.Content))
} }
for { for {
@@ -184,7 +184,7 @@ func (sv *SUDPVisitor) worker(workConn net.Conn, firstPacket *msg.UDPPacket) {
xl.Warnf("sender goroutine for udp work connection closed: %v", errRet) xl.Warnf("sender goroutine for udp work connection closed: %v", errRet)
return return
} }
xl.Tracef("send udp package to workConn: %s", udpMsg.Content) xl.Tracef("send udp package to workConn, len: %d", len(udpMsg.Content))
case <-closeCh: case <-closeCh:
return return
} }

View File

@@ -184,7 +184,7 @@ type Pong struct {
} }
type UDPPacket struct { type UDPPacket struct {
Content string `json:"c,omitempty"` Content []byte `json:"c,omitempty"`
LocalAddr *net.UDPAddr `json:"l,omitempty"` LocalAddr *net.UDPAddr `json:"l,omitempty"`
RemoteAddr *net.UDPAddr `json:"r,omitempty"` RemoteAddr *net.UDPAddr `json:"r,omitempty"`
} }

View File

@@ -15,7 +15,6 @@
package udp package udp
import ( import (
"encoding/base64"
"net" "net"
"sync" "sync"
"time" "time"
@@ -28,16 +27,17 @@ import (
) )
func NewUDPPacket(buf []byte, laddr, raddr *net.UDPAddr) *msg.UDPPacket { func NewUDPPacket(buf []byte, laddr, raddr *net.UDPAddr) *msg.UDPPacket {
content := make([]byte, len(buf))
copy(content, buf)
return &msg.UDPPacket{ return &msg.UDPPacket{
Content: base64.StdEncoding.EncodeToString(buf), Content: content,
LocalAddr: laddr, LocalAddr: laddr,
RemoteAddr: raddr, RemoteAddr: raddr,
} }
} }
func GetContent(m *msg.UDPPacket) (buf []byte, err error) { func GetContent(m *msg.UDPPacket) (buf []byte, err error) {
buf, err = base64.StdEncoding.DecodeString(m.Content) return m.Content, nil
return
} }
func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UDPPacket, sendCh chan<- *msg.UDPPacket, bufSize int) { func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UDPPacket, sendCh chan<- *msg.UDPPacket, bufSize int) {
@@ -60,7 +60,7 @@ func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UDPPacket, sendCh
if err != nil { if err != nil {
return return
} }
// buf[:n] will be encoded to string, so the bytes can be reused // NewUDPPacket copies buf[:n], so the read buffer can be reused
udpMsg := NewUDPPacket(buf[:n], nil, remoteAddr) udpMsg := NewUDPPacket(buf[:n], nil, remoteAddr)
select { select {

View File

@@ -136,7 +136,7 @@ func (pxy *UDPProxy) Run() (remoteAddr string, err error) {
continue continue
case *msg.UDPPacket: case *msg.UDPPacket:
if errRet := errors.PanicToError(func() { if errRet := errors.PanicToError(func() {
xl.Tracef("get udp message from workConn: %s", m.Content) xl.Tracef("get udp message from workConn, len: %d", len(m.Content))
pxy.readCh <- m pxy.readCh <- m
metrics.Server.AddTrafficOut( metrics.Server.AddTrafficOut(
pxy.GetName(), pxy.GetName(),
@@ -167,7 +167,7 @@ func (pxy *UDPProxy) Run() (remoteAddr string, err error) {
conn.Close() conn.Close()
return return
} }
xl.Tracef("send message to udp workConn: %s", udpMsg.Content) xl.Tracef("send message to udp workConn, len: %d", len(udpMsg.Content))
metrics.Server.AddTrafficIn( metrics.Server.AddTrafficIn(
pxy.GetName(), pxy.GetName(),
pxy.GetConfigurer().GetBaseConfig().Type, pxy.GetConfigurer().GetBaseConfig().Type,