From 314bcdff95d985e31c76322b3f2ac2c45bb1b9f0 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 17 Apr 2025 13:04:32 +0800 Subject: [PATCH] chore: add singMux inbound test for shadowsocks/trojan/vless/vmess --- listener/inbound/mux_test.go | 30 ++++++++++++++++++++++++++++ listener/inbound/shadowsocks_test.go | 2 ++ listener/inbound/trojan_test.go | 2 ++ listener/inbound/vless_test.go | 2 ++ listener/inbound/vmess_test.go | 2 ++ listener/sing/sing.go | 2 +- log/sing.go | 13 ++++++++++++ transport/gun/transport.go | 6 ++++++ 8 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 listener/inbound/mux_test.go diff --git a/listener/inbound/mux_test.go b/listener/inbound/mux_test.go new file mode 100644 index 00000000..2f0681b8 --- /dev/null +++ b/listener/inbound/mux_test.go @@ -0,0 +1,30 @@ +package inbound_test + +import ( + "testing" + + "github.com/metacubex/mihomo/adapter/outbound" + + "github.com/stretchr/testify/assert" +) + +var singMuxProtocolList = []string{"h2mux", "smux", "yamux"} + +func testSingMux(t *testing.T, tunnel *TestTunnel, out outbound.ProxyAdapter) { + t.Run("singmux", func(t *testing.T) { + for _, protocol := range singMuxProtocolList { + t.Run(protocol, func(t *testing.T) { + t.Parallel() + singMuxOption := outbound.SingMuxOption{ + Enabled: true, + Protocol: protocol, + } + out, err := outbound.NewSingMux(singMuxOption, out) + assert.NoError(t, err) + defer out.Close() + + tunnel.DoTest(t, out) + }) + } + }) +} diff --git a/listener/inbound/shadowsocks_test.go b/listener/inbound/shadowsocks_test.go index 34772a21..a957886b 100644 --- a/listener/inbound/shadowsocks_test.go +++ b/listener/inbound/shadowsocks_test.go @@ -77,6 +77,8 @@ func testInboundShadowSocks0(t *testing.T, inboundOptions inbound.ShadowSocksOpt defer out.Close() tunnel.DoTest(t, out) + + testSingMux(t, tunnel, out) } func TestInboundShadowSocks_Basic(t *testing.T) { diff --git a/listener/inbound/trojan_test.go b/listener/inbound/trojan_test.go index 971a25c7..cfc4476d 100644 --- a/listener/inbound/trojan_test.go +++ b/listener/inbound/trojan_test.go @@ -42,6 +42,8 @@ func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outbou defer out.Close() tunnel.DoTest(t, out) + + testSingMux(t, tunnel, out) } func TestInboundTrojan_TLS(t *testing.T) { diff --git a/listener/inbound/vless_test.go b/listener/inbound/vless_test.go index f1e37ac5..a307753b 100644 --- a/listener/inbound/vless_test.go +++ b/listener/inbound/vless_test.go @@ -42,6 +42,8 @@ func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outbound defer out.Close() tunnel.DoTest(t, out) + + testSingMux(t, tunnel, out) } func TestInboundVless_TLS(t *testing.T) { diff --git a/listener/inbound/vmess_test.go b/listener/inbound/vmess_test.go index aa877861..3456d1fd 100644 --- a/listener/inbound/vmess_test.go +++ b/listener/inbound/vmess_test.go @@ -44,6 +44,8 @@ func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outbound defer out.Close() tunnel.DoTest(t, out) + + testSingMux(t, tunnel, out) } func TestInboundVMess_Basic(t *testing.T) { diff --git a/listener/sing/sing.go b/listener/sing/sing.go index eb13e6c9..af7cdbc9 100644 --- a/listener/sing/sing.go +++ b/listener/sing/sing.go @@ -72,7 +72,7 @@ func NewListenerHandler(lc ListenerConfig) (h *ListenerHandler, err error) { NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context { return ctx }, - Logger: log.SingLogger, + Logger: log.SingInfoToDebugLogger, // convert sing-mux info log to debug Handler: h, Padding: lc.MuxOption.Padding, Brutal: mux.BrutalOptions{ diff --git a/log/sing.go b/log/sing.go index 818acc79..7fdd3f12 100644 --- a/log/sing.go +++ b/log/sing.go @@ -65,4 +65,17 @@ func (l singLogger) Panic(args ...any) { Fatalln(fmt.Sprint(args...)) } +type singInfoToDebugLogger struct { + singLogger +} + +func (l singInfoToDebugLogger) InfoContext(ctx context.Context, args ...any) { + Debugln(fmt.Sprint(args...)) +} + +func (l singInfoToDebugLogger) Info(args ...any) { + Debugln(fmt.Sprint(args...)) +} + var SingLogger L.ContextLogger = singLogger{} +var SingInfoToDebugLogger L.ContextLogger = singInfoToDebugLogger{} diff --git a/transport/gun/transport.go b/transport/gun/transport.go index 9c5c4375..fec7ff78 100644 --- a/transport/gun/transport.go +++ b/transport/gun/transport.go @@ -29,9 +29,15 @@ type netAddr struct { } func (addr netAddr) RemoteAddr() net.Addr { + if addr.remoteAddr == nil { + return &net.TCPAddr{IP: net.IPv4zero, Port: 0} + } return addr.remoteAddr } func (addr netAddr) LocalAddr() net.Addr { + if addr.localAddr == nil { + return &net.TCPAddr{IP: net.IPv4zero, Port: 0} + } return addr.localAddr }