diff --git a/listener/inbound/anytls_test.go b/listener/inbound/anytls_test.go index 444c943a4..b28ec2338 100644 --- a/listener/inbound/anytls_test.go +++ b/listener/inbound/anytls_test.go @@ -41,6 +41,7 @@ func testInboundAnyTLS(t *testing.T, inboundOptions inbound.AnyTLSOption, outbou outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) outboundOptions.Password = userUUID + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewAnyTLS(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/common_test.go b/listener/inbound/common_test.go index f4aa36cd1..6c980a924 100644 --- a/listener/inbound/common_test.go +++ b/listener/inbound/common_test.go @@ -58,6 +58,29 @@ func init() { realityPublickey = base64.RawURLEncoding.EncodeToString(privateKey.PublicKey().Bytes()) } +type TestDialer struct{ dialer C.Dialer } + +func (t *TestDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { +start: + conn, err := t.dialer.DialContext(ctx, network, address) + if err != nil && ctx.Err() == nil { + // we are testing in local, should never fail, so just retry when ctx not canceled + // it usually happens when the test is running in parallel + goto start + } + return conn, err +} + +func (t *TestDialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) { + return t.dialer.ListenPacket(ctx, network, address, rAddrPort) +} + +func NewTestDialer() *TestDialer { + return &TestDialer{dialer: dialer.NewDialer()} +} + +var _ C.Dialer = (*TestDialer)(nil) + type TestTunnel struct { HandleTCPConnFn func(conn net.Conn, metadata *C.Metadata) HandleUDPPacketFn func(packet C.UDPPacket, metadata *C.Metadata) diff --git a/listener/inbound/hysteria2_test.go b/listener/inbound/hysteria2_test.go index 52fc07b7e..d866792ee 100644 --- a/listener/inbound/hysteria2_test.go +++ b/listener/inbound/hysteria2_test.go @@ -41,6 +41,7 @@ func testInboundHysteria2(t *testing.T, inboundOptions inbound.Hysteria2Option, outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) outboundOptions.Password = userUUID + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewHysteria2(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/mieru_test.go b/listener/inbound/mieru_test.go index 8bd149996..ceee2df5d 100644 --- a/listener/inbound/mieru_test.go +++ b/listener/inbound/mieru_test.go @@ -237,6 +237,7 @@ func testInboundMieruTCP(t *testing.T, handshakeMode string) { Password: "password", HandshakeMode: handshakeMode, } + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewMieru(outboundOptions) if !assert.NoError(t, err) { return @@ -292,6 +293,7 @@ func testInboundMieruUDP(t *testing.T, handshakeMode string) { Password: "password", HandshakeMode: handshakeMode, } + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewMieru(outboundOptions) if !assert.NoError(t, err) { return diff --git a/listener/inbound/shadowsocks_test.go b/listener/inbound/shadowsocks_test.go index d29af7122..5f2c5359a 100644 --- a/listener/inbound/shadowsocks_test.go +++ b/listener/inbound/shadowsocks_test.go @@ -85,6 +85,7 @@ func testInboundShadowSocks0(t *testing.T, inboundOptions inbound.ShadowSocksOpt outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) outboundOptions.Password = password + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewShadowSocks(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/sudoku_test.go b/listener/inbound/sudoku_test.go index 9ba814daf..e8101a56a 100644 --- a/listener/inbound/sudoku_test.go +++ b/listener/inbound/sudoku_test.go @@ -43,6 +43,7 @@ func testInboundSudoku(t *testing.T, inboundOptions inbound.SudokuOption, outbou outboundOptions.Name = "sudoku_outbound" outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewSudoku(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/trojan_test.go b/listener/inbound/trojan_test.go index 7b92b0856..d29a2977a 100644 --- a/listener/inbound/trojan_test.go +++ b/listener/inbound/trojan_test.go @@ -43,6 +43,7 @@ func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outbou outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) outboundOptions.Password = userUUID + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewTrojan(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/trusttunnel_test.go b/listener/inbound/trusttunnel_test.go index 5283d0880..fb939c4bc 100644 --- a/listener/inbound/trusttunnel_test.go +++ b/listener/inbound/trusttunnel_test.go @@ -42,6 +42,7 @@ func testInboundTrustTunnel(t *testing.T, inboundOptions inbound.TrustTunnelOpti outboundOptions.Port = int(addrPort.Port()) outboundOptions.UserName = "test" outboundOptions.Password = userUUID + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewTrustTunnel(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/tuic_test.go b/listener/inbound/tuic_test.go index f887609de..094f706ee 100644 --- a/listener/inbound/tuic_test.go +++ b/listener/inbound/tuic_test.go @@ -69,6 +69,7 @@ func testInboundTuic0(t *testing.T, inboundOptions inbound.TuicOption, outboundO outboundOptions.Name = "tuic_outbound" outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewTuic(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/vless_test.go b/listener/inbound/vless_test.go index a3e707a5c..e7975ad3c 100644 --- a/listener/inbound/vless_test.go +++ b/listener/inbound/vless_test.go @@ -44,6 +44,7 @@ func testInboundVless(t *testing.T, inboundOptions inbound.VlessOption, outbound outboundOptions.Server = addrPort.Addr().String() outboundOptions.Port = int(addrPort.Port()) outboundOptions.UUID = userUUID + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewVless(outboundOptions) if !assert.NoError(t, err) { diff --git a/listener/inbound/vmess_test.go b/listener/inbound/vmess_test.go index de3a62d20..848add472 100644 --- a/listener/inbound/vmess_test.go +++ b/listener/inbound/vmess_test.go @@ -45,6 +45,7 @@ func testInboundVMess(t *testing.T, inboundOptions inbound.VmessOption, outbound outboundOptions.UUID = userUUID outboundOptions.AlterID = 0 outboundOptions.Cipher = "auto" + outboundOptions.DialerForAPI = NewTestDialer() out, err := outbound.NewVmess(outboundOptions) if !assert.NoError(t, err) {