mirror of
https://github.com/fatedier/frp.git
synced 2026-08-01 20:12:54 +08:00
feat: use binary codec for SUDP packets (#5461)
This commit is contained in:
@@ -65,8 +65,12 @@ func (vm *Manager) Listen(name string, sk string, allowUsers []string) (*netpkg.
|
||||
|
||||
func (vm *Manager) NewConn(name string, conn net.Conn, timestamp int64, signKey string,
|
||||
useEncryption bool, useCompression bool, visitorUser string,
|
||||
wireProtocol string,
|
||||
wireProtocol string, udpPacketCodecs ...string,
|
||||
) (err error) {
|
||||
udpPacketCodec := ""
|
||||
if len(udpPacketCodecs) > 0 {
|
||||
udpPacketCodec = udpPacketCodecs[0]
|
||||
}
|
||||
vm.mu.RLock()
|
||||
defer vm.mu.RUnlock()
|
||||
|
||||
@@ -93,8 +97,9 @@ func (vm *Manager) NewConn(name string, conn net.Conn, timestamp int64, signKey
|
||||
}
|
||||
visitorConn := netpkg.WrapReadWriteCloserToConn(rwc, conn)
|
||||
err = l.l.PutConn(&wireProtocolConn{
|
||||
Conn: visitorConn,
|
||||
wireProtocol: wireProtocol,
|
||||
Conn: visitorConn,
|
||||
wireProtocol: wireProtocol,
|
||||
udpPacketCodec: udpPacketCodec,
|
||||
})
|
||||
} else {
|
||||
err = fmt.Errorf("custom listener for [%s] doesn't exist", name)
|
||||
@@ -105,13 +110,18 @@ func (vm *Manager) NewConn(name string, conn net.Conn, timestamp int64, signKey
|
||||
|
||||
type wireProtocolConn struct {
|
||||
net.Conn
|
||||
wireProtocol string
|
||||
wireProtocol string
|
||||
udpPacketCodec string
|
||||
}
|
||||
|
||||
func (c *wireProtocolConn) WireProtocol() string {
|
||||
return c.wireProtocol
|
||||
}
|
||||
|
||||
func (c *wireProtocolConn) UDPPacketCodec() string {
|
||||
return c.udpPacketCodec
|
||||
}
|
||||
|
||||
func (vm *Manager) CloseListener(name string) {
|
||||
vm.mu.Lock()
|
||||
defer vm.mu.Unlock()
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
"github.com/fatedier/frp/pkg/util/util"
|
||||
)
|
||||
|
||||
func TestManagerNewConnCarriesWireProtocol(t *testing.T) {
|
||||
func TestManagerNewConnCarriesWireProtocolAndUDPPacketCodec(t *testing.T) {
|
||||
vm := NewManager()
|
||||
listener, err := vm.Listen("sudp", "secret", []string{"*"})
|
||||
require.NoError(t, err)
|
||||
@@ -47,6 +47,7 @@ func TestManagerNewConnCarriesWireProtocol(t *testing.T) {
|
||||
false,
|
||||
"user",
|
||||
wire.ProtocolV2,
|
||||
wire.UDPPacketCodecBinary,
|
||||
)
|
||||
}()
|
||||
|
||||
@@ -54,8 +55,12 @@ func TestManagerNewConnCarriesWireProtocol(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer acceptedConn.Close()
|
||||
|
||||
getter, ok := acceptedConn.(interface{ WireProtocol() string })
|
||||
metadata, ok := acceptedConn.(interface {
|
||||
WireProtocol() string
|
||||
UDPPacketCodec() string
|
||||
})
|
||||
require.True(t, ok)
|
||||
require.Equal(t, wire.ProtocolV2, getter.WireProtocol())
|
||||
require.Equal(t, wire.ProtocolV2, metadata.WireProtocol())
|
||||
require.Equal(t, wire.UDPPacketCodecBinary, metadata.UDPPacketCodec())
|
||||
require.NoError(t, <-errCh)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user