mirror of
https://github.com/fatedier/frp.git
synced 2026-01-11 22:23:12 +00:00
add proxy protocol support for UDP proxies (#4810)
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/fatedier/golib/pool"
|
||||
|
||||
"github.com/fatedier/frp/pkg/msg"
|
||||
netpkg "github.com/fatedier/frp/pkg/util/net"
|
||||
)
|
||||
|
||||
func NewUDPPacket(buf []byte, laddr, raddr *net.UDPAddr) *msg.UDPPacket {
|
||||
@@ -69,7 +70,7 @@ func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UDPPacket, sendCh
|
||||
}
|
||||
}
|
||||
|
||||
func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UDPPacket, sendCh chan<- msg.Message, bufSize int) {
|
||||
func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UDPPacket, sendCh chan<- msg.Message, bufSize int, proxyProtocolVersion string) {
|
||||
var mu sync.RWMutex
|
||||
udpConnMap := make(map[string]*net.UDPConn)
|
||||
|
||||
@@ -110,6 +111,7 @@ func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UDPPacket, sendCh chan<-
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
udpConn, ok := udpConnMap[udpMsg.RemoteAddr.String()]
|
||||
if !ok {
|
||||
@@ -122,6 +124,18 @@ func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UDPPacket, sendCh chan<-
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
// Add proxy protocol header if configured
|
||||
if proxyProtocolVersion != "" && udpMsg.RemoteAddr != nil {
|
||||
ppBuf, err := netpkg.BuildProxyProtocolHeader(udpMsg.RemoteAddr, dstAddr, proxyProtocolVersion)
|
||||
if err == nil {
|
||||
// Prepend proxy protocol header to the UDP payload
|
||||
finalBuf := make([]byte, len(ppBuf)+len(buf))
|
||||
copy(finalBuf, ppBuf)
|
||||
copy(finalBuf[len(ppBuf):], buf)
|
||||
buf = finalBuf
|
||||
}
|
||||
}
|
||||
|
||||
_, err = udpConn.Write(buf)
|
||||
if err != nil {
|
||||
udpConn.Close()
|
||||
|
||||
Reference in New Issue
Block a user