mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 18:56:47 +00:00
chore: better mieru inbound test implementation
This commit is contained in:
1 parent
0daa3ff722
commit
acbc92f021
1 file changed
+42
-8
@@ -1,6 +1,7 @@
|
||||
package inbound_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
@@ -193,18 +194,28 @@ func TestInboundMieru(t *testing.T) {
|
||||
|
||||
func testInboundMieruTCP(t *testing.T, handshakeMode string) {
|
||||
t.Parallel()
|
||||
// mieru must listen on a specific port, so we first create a socket, get the port, and then inject it via ListenConfigForAPI
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
port := l.Addr().(*net.TCPAddr).Port
|
||||
l.Close()
|
||||
defer l.Close()
|
||||
lc := mieruTestInboundListenConfig{
|
||||
ListenFn: func(ctx context.Context, network, address string) (net.Listener, error) {
|
||||
return l, nil
|
||||
},
|
||||
ListenPacketFn: func(ctx context.Context, network, address string) (net.PacketConn, error) {
|
||||
panic("should not be called")
|
||||
},
|
||||
}
|
||||
|
||||
inboundOptions := inbound.MieruOption{
|
||||
BaseOption: inbound.BaseOption{
|
||||
NameStr: "mieru_inbound_tcp",
|
||||
Listen: "127.0.0.1",
|
||||
Port: strconv.Itoa(port),
|
||||
NameStr: "mieru_inbound_tcp",
|
||||
Listen: "127.0.0.1",
|
||||
Port: strconv.Itoa(port),
|
||||
ListenConfigForAPI: lc,
|
||||
},
|
||||
Transport: "TCP",
|
||||
Users: map[string]string{"test": "password"},
|
||||
@@ -250,18 +261,28 @@ func testInboundMieruTCP(t *testing.T, handshakeMode string) {
|
||||
|
||||
func testInboundMieruUDP(t *testing.T, handshakeMode string) {
|
||||
t.Parallel()
|
||||
// mieru must listen on a specific port, so we first create a socket, get the port, and then inject it via ListenConfigForAPI
|
||||
l, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
port := l.LocalAddr().(*net.UDPAddr).Port
|
||||
l.Close()
|
||||
defer l.Close()
|
||||
lc := mieruTestInboundListenConfig{
|
||||
ListenFn: func(ctx context.Context, network, address string) (net.Listener, error) {
|
||||
panic("should not be called")
|
||||
},
|
||||
ListenPacketFn: func(ctx context.Context, network, address string) (net.PacketConn, error) {
|
||||
return l, nil
|
||||
},
|
||||
}
|
||||
|
||||
inboundOptions := inbound.MieruOption{
|
||||
BaseOption: inbound.BaseOption{
|
||||
NameStr: "mieru_inbound_udp",
|
||||
Listen: "127.0.0.1",
|
||||
Port: strconv.Itoa(port),
|
||||
NameStr: "mieru_inbound_udp",
|
||||
Listen: "127.0.0.1",
|
||||
Port: strconv.Itoa(port),
|
||||
ListenConfigForAPI: lc,
|
||||
},
|
||||
Transport: "UDP",
|
||||
Users: map[string]string{"test": "password"},
|
||||
@@ -303,3 +324,16 @@ func testInboundMieruUDP(t *testing.T, handshakeMode string) {
|
||||
|
||||
tunnel.DoSequentialTest(t, out)
|
||||
}
|
||||
|
||||
type mieruTestInboundListenConfig struct {
|
||||
ListenFn func(ctx context.Context, network, address string) (net.Listener, error)
|
||||
ListenPacketFn func(ctx context.Context, network, address string) (net.PacketConn, error)
|
||||
}
|
||||
|
||||
func (m mieruTestInboundListenConfig) Listen(ctx context.Context, network, address string) (net.Listener, error) {
|
||||
return m.ListenFn(ctx, network, address)
|
||||
}
|
||||
|
||||
func (m mieruTestInboundListenConfig) ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) {
|
||||
return m.ListenPacketFn(ctx, network, address)
|
||||
}
|
||||
Reference in new issue
Block a user