mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 06:36:47 +00:00
chore: cleanup interop test code
This commit is contained in:
1 parent
a198581333
commit
de51c4b840
3 files changed
+447
-447
No files matched your search
@@ -0,0 +1,398 @@
|
||||
package inbound_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
N "github.com/metacubex/mihomo/common/net"
|
||||
C "github.com/metacubex/mihomo/constant"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const vmessInteropV2RayRef = "v5.51.2"
|
||||
const vmessInteropV2RayXNetRef = "bd5f1dcf71cf0d6d2424021d0a04f191396a46a7" // http2: initialize Transport on NewClientConn
|
||||
|
||||
func vmessInteropSkip(t *testing.T) {
|
||||
t.Helper()
|
||||
if skip, _ := strconv.ParseBool(os.Getenv("SKIP_INTEROP_TEST")); skip {
|
||||
t.Skip("SKIP_INTEROP_TEST is set")
|
||||
}
|
||||
}
|
||||
|
||||
func vmessInteropV2RayBinary(t *testing.T) string {
|
||||
t.Helper()
|
||||
goBin, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
t.Skip("go toolchain not found, skip real v2ray interop test")
|
||||
}
|
||||
|
||||
root := filepath.Join(os.TempDir(), "mihomo-vmess-interop", vmessInteropV2RayRef)
|
||||
binDir := filepath.Join(root, "bin")
|
||||
exe := ""
|
||||
if runtime.GOOS == "windows" {
|
||||
exe = ".exe"
|
||||
}
|
||||
v2rayBin := filepath.Join(binDir, "v2ray"+exe)
|
||||
if _, err := os.Stat(v2rayBin); err == nil {
|
||||
return v2rayBin
|
||||
}
|
||||
goVersion := vmessInteropGoVersion(t, goBin)
|
||||
goMajor, goMinor, ok := vmessInteropGoVersionMajorMinor(goVersion)
|
||||
if ok && goMajor == 1 && goMinor < 21 {
|
||||
t.Skipf("%s does not support GOTOOLCHAIN toolchain download, skip real v2ray interop test", goVersion)
|
||||
}
|
||||
|
||||
require.NoError(t, os.RemoveAll(root))
|
||||
require.NoError(t, os.MkdirAll(binDir, 0o755))
|
||||
|
||||
vmessInteropGo(t, goBin, root, "mod", "init", "mihomo-vmess-interop")
|
||||
vmessInteropGo(t, goBin, root, "get", "github.com/v2fly/v2ray-core/v5@"+vmessInteropV2RayRef)
|
||||
if ok && (goMajor > 1 || goMajor == 1 && goMinor > 26) {
|
||||
vmessInteropGo(t, goBin, root, "get", "golang.org/x/net@"+vmessInteropV2RayXNetRef)
|
||||
}
|
||||
vmessInteropGo(t, goBin, root, "build", "-mod=mod", "-trimpath", "-o", v2rayBin, "github.com/v2fly/v2ray-core/v5/main")
|
||||
return v2rayBin
|
||||
}
|
||||
|
||||
func vmessInteropGoVersion(t *testing.T, goBin string) string {
|
||||
t.Helper()
|
||||
cmd := exec.Command(goBin, "version")
|
||||
output, err := cmd.Output()
|
||||
require.NoError(t, err, "go version")
|
||||
return vmessInteropParseGoVersion(string(output))
|
||||
}
|
||||
|
||||
func vmessInteropParseGoVersion(output string) string {
|
||||
for _, field := range strings.Fields(output) {
|
||||
if strings.HasPrefix(field, "go1.") {
|
||||
return field
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func vmessInteropGoVersionMajorMinor(version string) (int, int, bool) {
|
||||
version = strings.TrimPrefix(version, "go")
|
||||
parts := strings.SplitN(version, ".", 3)
|
||||
if len(parts) < 2 {
|
||||
return 0, 0, false
|
||||
}
|
||||
major, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
minorText := parts[1]
|
||||
for i, r := range minorText {
|
||||
if r < '0' || r > '9' {
|
||||
minorText = minorText[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if minorText == "" {
|
||||
return 0, 0, false
|
||||
}
|
||||
minor, err := strconv.Atoi(minorText)
|
||||
if err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
return major, minor, true
|
||||
}
|
||||
|
||||
func vmessInteropGo(t *testing.T, goBin, dir string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(goBin, args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = vmessInteropGoEnv()
|
||||
output, err := cmd.CombinedOutput()
|
||||
require.NoError(t, err, "go %s\n%s", strings.Join(args, " "), string(output))
|
||||
}
|
||||
|
||||
func vmessInteropGoEnv() []string {
|
||||
env := os.Environ()
|
||||
hasGoToolchain := false
|
||||
for i, value := range env {
|
||||
if strings.HasPrefix(value, "GOTOOLCHAIN=") {
|
||||
env[i] = "GOTOOLCHAIN=auto"
|
||||
hasGoToolchain = true
|
||||
}
|
||||
}
|
||||
if !hasGoToolchain {
|
||||
env = append(env, "GOTOOLCHAIN=auto")
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func startVMessInteropV2Ray(t *testing.T, v2rayBin string, config []byte, release func(), waitAddr string) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cmd := exec.CommandContext(ctx, v2rayBin, "run", "-format=jsonv5")
|
||||
var output bytes.Buffer
|
||||
cmd.Stdin = bytes.NewReader(config)
|
||||
cmd.Stdout = &output
|
||||
cmd.Stderr = &output
|
||||
if release != nil {
|
||||
release()
|
||||
}
|
||||
require.NoError(t, cmd.Start())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- cmd.Wait()
|
||||
}()
|
||||
waited := false
|
||||
t.Cleanup(func() {
|
||||
cancel()
|
||||
if !waited {
|
||||
<-done
|
||||
}
|
||||
if t.Failed() {
|
||||
t.Log(output.String())
|
||||
}
|
||||
})
|
||||
|
||||
if waitAddr == "" {
|
||||
select {
|
||||
case err := <-done:
|
||||
waited = true
|
||||
require.NoError(t, err, output.String())
|
||||
t.Fatalf("v2ray exited before interop test started\n%s", output.String())
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(10 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
select {
|
||||
case err := <-done:
|
||||
waited = true
|
||||
require.NoError(t, err, output.String())
|
||||
t.Fatalf("v2ray exited before listening on %s\n%s", waitAddr, output.String())
|
||||
default:
|
||||
}
|
||||
conn, err := net.DialTimeout("tcp", waitAddr, 100*time.Millisecond)
|
||||
if err == nil {
|
||||
_ = conn.Close()
|
||||
return
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("v2ray did not listen on %s\n%s", waitAddr, output.String())
|
||||
}
|
||||
|
||||
func vmessInteropBaseConfig() map[string]any {
|
||||
return map[string]any{
|
||||
"log": map[string]any{
|
||||
"error": map[string]any{
|
||||
"type": "Console",
|
||||
"level": "Debug",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func vmessInteropDirectOutbound() map[string]any {
|
||||
return map[string]any{
|
||||
"protocol": "freedom",
|
||||
"tag": "direct",
|
||||
}
|
||||
}
|
||||
|
||||
func vmessInteropMarshalJSONConfig(t *testing.T, config map[string]any) []byte {
|
||||
t.Helper()
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
require.NoError(t, err)
|
||||
data = append(data, '\n')
|
||||
return data
|
||||
}
|
||||
|
||||
func vmessInteropParsePort(t *testing.T, port string) int {
|
||||
t.Helper()
|
||||
value, err := strconv.Atoi(port)
|
||||
require.NoError(t, err)
|
||||
return value
|
||||
}
|
||||
|
||||
func startVMessInteropEcho(t *testing.T) string {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = ln.Close() })
|
||||
go func() {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer conn.Close()
|
||||
_, _ = io.Copy(conn, conn)
|
||||
}()
|
||||
}
|
||||
}()
|
||||
return ln.Addr().String()
|
||||
}
|
||||
|
||||
func vmessInteropDirectTunnel(t *testing.T) *TestTunnel {
|
||||
t.Helper()
|
||||
return &TestTunnel{
|
||||
HandleTCPConnFn: func(conn net.Conn, metadata *C.Metadata) {
|
||||
target, err := net.Dial("tcp", metadata.RemoteAddress())
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return
|
||||
}
|
||||
N.Relay(target, conn)
|
||||
},
|
||||
HandleUDPPacketFn: func(packet C.UDPPacket, metadata *C.Metadata) {
|
||||
packet.Drop()
|
||||
},
|
||||
NatTableFn: func() C.NatTable {
|
||||
return nil
|
||||
},
|
||||
CloseFn: func() error {
|
||||
return nil
|
||||
},
|
||||
NewDialerFn: func() C.Dialer {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func vmessInteropMetadata(t *testing.T, addr string) *C.Metadata {
|
||||
t.Helper()
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
require.NoError(t, err)
|
||||
ip, err := netip.ParseAddr(host)
|
||||
require.NoError(t, err)
|
||||
portNum, err := net.LookupPort("tcp", port)
|
||||
require.NoError(t, err)
|
||||
return &C.Metadata{
|
||||
NetWork: C.TCP,
|
||||
DstIP: ip,
|
||||
DstPort: uint16(portNum),
|
||||
}
|
||||
}
|
||||
|
||||
func vmessInteropRoundTripWithRetry(t *testing.T, dial func() (net.Conn, error), payloadSize int) {
|
||||
t.Helper()
|
||||
var lastErr error
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
conn, err := dial()
|
||||
if err == nil {
|
||||
err = vmessInteropRoundTripConn(conn, payloadSize)
|
||||
} else {
|
||||
err = fmt.Errorf("dial: %w", err)
|
||||
}
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
lastErr = err
|
||||
var netErr net.Error
|
||||
if !errors.As(err, &netErr) || !netErr.Timeout() {
|
||||
break
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
require.NoError(t, lastErr)
|
||||
}
|
||||
|
||||
func vmessInteropRoundTripConn(conn net.Conn, payloadSize int) error {
|
||||
defer conn.Close()
|
||||
if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
||||
return fmt.Errorf("set deadline: %w", err)
|
||||
}
|
||||
if payloadSize == 0 {
|
||||
payloadSize = len("vmess-interop-") * 256
|
||||
}
|
||||
payload := bytes.Repeat([]byte("x"), payloadSize)
|
||||
_, err := conn.Write(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write: %w", err)
|
||||
}
|
||||
got := make([]byte, len(payload))
|
||||
_, err = io.ReadFull(conn, got)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read full: %w", err)
|
||||
}
|
||||
if !bytes.Equal(payload, got) {
|
||||
return fmt.Errorf("unexpected payload: got %d bytes", len(got))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type vmessInteropReservedTCPPort struct {
|
||||
ln net.Listener
|
||||
port int
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func vmessInteropReserveTCPPort(t *testing.T) *vmessInteropReservedTCPPort {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
port := &vmessInteropReservedTCPPort{
|
||||
ln: ln,
|
||||
port: ln.Addr().(*net.TCPAddr).Port,
|
||||
}
|
||||
t.Cleanup(port.Release)
|
||||
return port
|
||||
}
|
||||
|
||||
func (p *vmessInteropReservedTCPPort) Port() int {
|
||||
return p.port
|
||||
}
|
||||
|
||||
func (p *vmessInteropReservedTCPPort) Release() {
|
||||
p.once.Do(func() {
|
||||
_ = p.ln.Close()
|
||||
})
|
||||
}
|
||||
|
||||
type vmessInteropReservedUDPPort struct {
|
||||
pc net.PacketConn
|
||||
port int
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func vmessInteropReserveUDPPort(t *testing.T) *vmessInteropReservedUDPPort {
|
||||
t.Helper()
|
||||
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
port := &vmessInteropReservedUDPPort{
|
||||
pc: pc,
|
||||
port: pc.LocalAddr().(*net.UDPAddr).Port,
|
||||
}
|
||||
t.Cleanup(port.Release)
|
||||
return port
|
||||
}
|
||||
|
||||
func (p *vmessInteropReservedUDPPort) Port() int {
|
||||
return p.port
|
||||
}
|
||||
|
||||
func (p *vmessInteropReservedUDPPort) Release() {
|
||||
p.once.Do(func() {
|
||||
_ = p.pc.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func vmessInteropPort(addr string) string {
|
||||
_, port, _ := net.SplitHostPort(addr)
|
||||
return port
|
||||
}
|
||||
@@ -1,15 +1,10 @@
|
||||
package inbound_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/metacubex/mihomo/adapter/outbound"
|
||||
"github.com/metacubex/mihomo/listener/inbound"
|
||||
@@ -18,11 +13,9 @@ import (
|
||||
)
|
||||
|
||||
func TestInboundVMess_MKCP_V2RayInterop(t *testing.T) {
|
||||
if skip, _ := strconv.ParseBool(os.Getenv("SKIP_INTEROP_TEST")); skip {
|
||||
t.Skip("SKIP_INTEROP_TEST is set")
|
||||
}
|
||||
vmessInteropSkip(t)
|
||||
|
||||
v2rayBin := tlsMirrorInteropV2RayBinary(t)
|
||||
v2rayBin := vmessInteropV2RayBinary(t)
|
||||
mkcpInteropTestCase(t, v2rayBin, "default", "", "")
|
||||
mkcpInteropTestCase(t, v2rayBin, "seed", "mihomo-mkcp-interop", "")
|
||||
mkcpInteropTestCase(t, v2rayBin, "header srtp", "", "srtp")
|
||||
@@ -30,11 +23,11 @@ func TestInboundVMess_MKCP_V2RayInterop(t *testing.T) {
|
||||
|
||||
func mkcpInteropTestCase(t *testing.T, v2rayBin, name, seed, header string) {
|
||||
t.Run(name+"/mihomo client to v2ray server", func(t *testing.T) {
|
||||
echoAddr := startTLSMirrorInteropEcho(t)
|
||||
v2rayPort := mkcpInteropReserveUDPPort(t)
|
||||
echoAddr := startVMessInteropEcho(t)
|
||||
v2rayPort := vmessInteropReserveUDPPort(t)
|
||||
config := mkcpInteropServerConfig(t, v2rayPort.Port(), userUUID, seed, header)
|
||||
|
||||
startMKCPInteropV2Ray(t, v2rayBin, config, v2rayPort.Release)
|
||||
startVMessInteropV2Ray(t, v2rayBin, config, v2rayPort.Release, "")
|
||||
|
||||
out, err := outbound.NewVmess(outbound.VmessOption{
|
||||
Name: "vmess_mkcp_v2ray_server",
|
||||
@@ -48,14 +41,14 @@ func mkcpInteropTestCase(t *testing.T, v2rayBin, name, seed, header string) {
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = out.Close() })
|
||||
|
||||
tlsMirrorInteropRoundTripWithRetry(t, func() (net.Conn, error) {
|
||||
return out.DialContext(context.Background(), tlsMirrorInteropMetadata(t, echoAddr))
|
||||
vmessInteropRoundTripWithRetry(t, func() (net.Conn, error) {
|
||||
return out.DialContext(context.Background(), vmessInteropMetadata(t, echoAddr))
|
||||
}, 128*1024)
|
||||
})
|
||||
|
||||
t.Run(name+"/v2ray client to mihomo server", func(t *testing.T) {
|
||||
echoAddr := startTLSMirrorInteropEcho(t)
|
||||
v2rayPort := tlsMirrorInteropReservePort(t)
|
||||
echoAddr := startVMessInteropEcho(t)
|
||||
v2rayPort := vmessInteropReserveTCPPort(t)
|
||||
|
||||
in, err := inbound.NewVmess(&inbound.VmessOption{
|
||||
BaseOption: inbound.BaseOption{
|
||||
@@ -74,15 +67,15 @@ func mkcpInteropTestCase(t *testing.T, v2rayBin, name, seed, header string) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
tunnel := tlsMirrorInteropDirectTunnel(t)
|
||||
tunnel := vmessInteropDirectTunnel(t)
|
||||
require.NoError(t, in.Listen(tunnel))
|
||||
t.Cleanup(func() { _ = in.Close() })
|
||||
inboundPort := tlsMirrorInteropParsePort(t, tlsMirrorInteropPort(in.Address()))
|
||||
inboundPort := vmessInteropParsePort(t, vmessInteropPort(in.Address()))
|
||||
|
||||
config := mkcpInteropClientConfig(t, v2rayPort.Port(), inboundPort, tlsMirrorInteropPort(echoAddr), userUUID, seed, header)
|
||||
startTLSMirrorInteropV2Ray(t, v2rayBin, config, v2rayPort, net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
config := mkcpInteropClientConfig(t, v2rayPort.Port(), inboundPort, vmessInteropPort(echoAddr), userUUID, seed, header)
|
||||
startVMessInteropV2Ray(t, v2rayBin, config, v2rayPort.Release, net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
|
||||
tlsMirrorInteropRoundTripWithRetry(t, func() (net.Conn, error) {
|
||||
vmessInteropRoundTripWithRetry(t, func() (net.Conn, error) {
|
||||
return net.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
}, 128*1024)
|
||||
})
|
||||
@@ -90,7 +83,7 @@ func mkcpInteropTestCase(t *testing.T, v2rayBin, name, seed, header string) {
|
||||
|
||||
func mkcpInteropServerConfig(t *testing.T, listenPort int, userID, seed, header string) []byte {
|
||||
t.Helper()
|
||||
config := tlsMirrorInteropBaseConfig()
|
||||
config := vmessInteropBaseConfig()
|
||||
config["inbounds"] = []any{map[string]any{
|
||||
"protocol": "vmess",
|
||||
"listen": "127.0.0.1",
|
||||
@@ -100,14 +93,14 @@ func mkcpInteropServerConfig(t *testing.T, listenPort int, userID, seed, header
|
||||
},
|
||||
"streamSettings": mkcpInteropStreamConfig(seed, header),
|
||||
}}
|
||||
config["outbounds"] = []any{tlsMirrorInteropDirectOutbound()}
|
||||
return tlsMirrorInteropMarshalJSONConfig(t, config)
|
||||
config["outbounds"] = []any{vmessInteropDirectOutbound()}
|
||||
return vmessInteropMarshalJSONConfig(t, config)
|
||||
}
|
||||
|
||||
func mkcpInteropClientConfig(t *testing.T, listenPort, serverPort int, targetPort string, userID, seed, header string) []byte {
|
||||
t.Helper()
|
||||
targetPortValue := tlsMirrorInteropParsePort(t, targetPort)
|
||||
config := tlsMirrorInteropBaseConfig()
|
||||
targetPortValue := vmessInteropParsePort(t, targetPort)
|
||||
config := vmessInteropBaseConfig()
|
||||
config["inbounds"] = []any{map[string]any{
|
||||
"protocol": "dokodemo-door",
|
||||
"listen": "127.0.0.1",
|
||||
@@ -129,7 +122,7 @@ func mkcpInteropClientConfig(t *testing.T, listenPort, serverPort int, targetPor
|
||||
},
|
||||
},
|
||||
}
|
||||
return tlsMirrorInteropMarshalJSONConfig(t, config)
|
||||
return vmessInteropMarshalJSONConfig(t, config)
|
||||
}
|
||||
|
||||
func mkcpInteropStreamConfig(seed, header string) map[string]any {
|
||||
@@ -168,62 +161,3 @@ func mkcpInteropHeaderConfig(header string) map[string]any {
|
||||
"@type": "types.v2fly.org/" + typeName,
|
||||
}
|
||||
}
|
||||
|
||||
func startMKCPInteropV2Ray(t *testing.T, v2rayBin string, config []byte, release func()) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cmd := exec.CommandContext(ctx, v2rayBin, "run", "-format=jsonv5")
|
||||
var output bytes.Buffer
|
||||
cmd.Stdin = bytes.NewReader(config)
|
||||
cmd.Stdout = &output
|
||||
cmd.Stderr = &output
|
||||
release()
|
||||
require.NoError(t, cmd.Start())
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- cmd.Wait()
|
||||
}()
|
||||
waited := false
|
||||
t.Cleanup(func() {
|
||||
cancel()
|
||||
if !waited {
|
||||
<-done
|
||||
}
|
||||
if t.Failed() {
|
||||
t.Log(output.String())
|
||||
}
|
||||
})
|
||||
select {
|
||||
case err := <-done:
|
||||
waited = true
|
||||
require.NoError(t, err, output.String())
|
||||
t.Fatalf("v2ray exited before mKCP interop test started\n%s", output.String())
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
||||
type mkcpInteropReservedPacketPort struct {
|
||||
pc net.PacketConn
|
||||
port int
|
||||
}
|
||||
|
||||
func mkcpInteropReserveUDPPort(t *testing.T) *mkcpInteropReservedPacketPort {
|
||||
t.Helper()
|
||||
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
port := pc.LocalAddr().(*net.UDPAddr).Port
|
||||
reserved := &mkcpInteropReservedPacketPort{pc: pc, port: port}
|
||||
t.Cleanup(reserved.Release)
|
||||
return reserved
|
||||
}
|
||||
|
||||
func (p *mkcpInteropReservedPacketPort) Port() int {
|
||||
return p.port
|
||||
}
|
||||
|
||||
func (p *mkcpInteropReservedPacketPort) Release() {
|
||||
if p.pc != nil {
|
||||
_ = p.pc.Close()
|
||||
p.pc = nil
|
||||
}
|
||||
}
|
||||
@@ -2,31 +2,18 @@ package inbound_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/metacubex/mihomo/adapter/outbound"
|
||||
N "github.com/metacubex/mihomo/common/net"
|
||||
"github.com/metacubex/mihomo/component/ca"
|
||||
C "github.com/metacubex/mihomo/constant"
|
||||
"github.com/metacubex/mihomo/listener/inbound"
|
||||
"github.com/metacubex/mihomo/transport/tlsmirror"
|
||||
|
||||
@@ -35,17 +22,12 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const v2rayTLSMirrorInteropRef = "v5.51.2"
|
||||
const v2rayTLSMirrorInteropXNetRef = "bd5f1dcf71cf0d6d2424021d0a04f191396a46a7" // http2: initialize Transport on NewClientConn
|
||||
|
||||
var tlsMirrorInteropPrimaryKey = tlsmirror.GeneratePrimaryKey()
|
||||
|
||||
func TestInboundVMess_TLSMirror_V2RayInterop(t *testing.T) {
|
||||
if skip, _ := strconv.ParseBool(os.Getenv("SKIP_INTEROP_TEST")); skip {
|
||||
t.Skip("SKIP_INTEROP_TEST is set")
|
||||
}
|
||||
vmessInteropSkip(t)
|
||||
|
||||
v2rayBin := tlsMirrorInteropV2RayBinary(t)
|
||||
v2rayBin := vmessInteropV2RayBinary(t)
|
||||
|
||||
tlsMirrorInteropTestCase(t, v2rayBin, "default", tlsMirrorInteropAdvanced{})
|
||||
tlsMirrorInteropTestCase(t, v2rayBin, "padding", tlsMirrorInteropAdvanced{
|
||||
@@ -112,12 +94,12 @@ type tlsMirrorInteropCarrier struct {
|
||||
|
||||
func tlsMirrorInteropMihomoClientH2EmbeddedTrafficGenerator(t *testing.T, v2rayBin string) {
|
||||
t.Run("h2 embedded traffic/mihomo client to v2ray server", func(t *testing.T) {
|
||||
echoAddr := startTLSMirrorInteropEcho(t)
|
||||
echoAddr := startVMessInteropEcho(t)
|
||||
forward := startTLSMirrorInteropCarrierHTTP2(t)
|
||||
v2rayPort := tlsMirrorInteropReservePort(t)
|
||||
config := tlsMirrorInteropServerConfig(t, v2rayPort.Port(), tlsMirrorInteropPort(forward.addr), userUUID, tlsMirrorInteropAdvanced{})
|
||||
v2rayPort := vmessInteropReserveTCPPort(t)
|
||||
config := tlsMirrorInteropServerConfig(t, v2rayPort.Port(), vmessInteropPort(forward.addr), userUUID, tlsMirrorInteropAdvanced{})
|
||||
|
||||
startTLSMirrorInteropV2Ray(t, v2rayBin, config, v2rayPort, net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
startVMessInteropV2Ray(t, v2rayBin, config, v2rayPort.Release, net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
|
||||
out, err := outbound.NewVmess(outbound.VmessOption{
|
||||
Name: "vmess_tlsmirror_v2ray_server_h2",
|
||||
@@ -151,20 +133,20 @@ func tlsMirrorInteropMihomoClientH2EmbeddedTrafficGenerator(t *testing.T, v2rayB
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = out.Close() })
|
||||
|
||||
conn, err := out.DialContext(context.Background(), tlsMirrorInteropMetadata(t, echoAddr))
|
||||
conn, err := out.DialContext(context.Background(), vmessInteropMetadata(t, echoAddr))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, tlsMirrorInteropRoundTripConn(conn, 128))
|
||||
require.NoError(t, vmessInteropRoundTripConn(conn, 128))
|
||||
})
|
||||
}
|
||||
|
||||
func tlsMirrorInteropTestCase(t *testing.T, v2rayBin, name string, advanced tlsMirrorInteropAdvanced) {
|
||||
t.Run(name+"/mihomo client to v2ray server", func(t *testing.T) {
|
||||
echoAddr := startTLSMirrorInteropEcho(t)
|
||||
echoAddr := startVMessInteropEcho(t)
|
||||
forward := startTLSMirrorInteropCarrierTLS(t, advanced.configureCarrierTLS)
|
||||
v2rayPort := tlsMirrorInteropReservePort(t)
|
||||
config := tlsMirrorInteropServerConfig(t, v2rayPort.Port(), tlsMirrorInteropPort(forward.addr), userUUID, advanced)
|
||||
v2rayPort := vmessInteropReserveTCPPort(t)
|
||||
config := tlsMirrorInteropServerConfig(t, v2rayPort.Port(), vmessInteropPort(forward.addr), userUUID, advanced)
|
||||
|
||||
startTLSMirrorInteropV2Ray(t, v2rayBin, config, v2rayPort, net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
startVMessInteropV2Ray(t, v2rayBin, config, v2rayPort.Release, net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
|
||||
out, err := outbound.NewVmess(outbound.VmessOption{
|
||||
Name: "vmess_tlsmirror_v2ray_server",
|
||||
@@ -186,15 +168,15 @@ func tlsMirrorInteropTestCase(t *testing.T, v2rayBin, name string, advanced tlsM
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = out.Close() })
|
||||
|
||||
conn, err := out.DialContext(context.Background(), tlsMirrorInteropMetadata(t, echoAddr))
|
||||
conn, err := out.DialContext(context.Background(), vmessInteropMetadata(t, echoAddr))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, tlsMirrorInteropRoundTripConn(conn, advanced.payloadSize))
|
||||
require.NoError(t, vmessInteropRoundTripConn(conn, advanced.payloadSize))
|
||||
})
|
||||
|
||||
t.Run(name+"/v2ray client to mihomo server", func(t *testing.T) {
|
||||
echoAddr := startTLSMirrorInteropEcho(t)
|
||||
echoAddr := startVMessInteropEcho(t)
|
||||
forward := startTLSMirrorInteropCarrierTLS(t, advanced.configureCarrierTLS)
|
||||
v2rayPort := tlsMirrorInteropReservePort(t)
|
||||
v2rayPort := vmessInteropReserveTCPPort(t)
|
||||
|
||||
in, err := inbound.NewVmess(&inbound.VmessOption{
|
||||
BaseOption: inbound.BaseOption{
|
||||
@@ -216,127 +198,24 @@ func tlsMirrorInteropTestCase(t *testing.T, v2rayBin, name string, advanced tlsM
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
tunnel := tlsMirrorInteropDirectTunnel(t)
|
||||
tunnel := vmessInteropDirectTunnel(t)
|
||||
require.NoError(t, in.Listen(tunnel))
|
||||
t.Cleanup(func() { _ = in.Close() })
|
||||
inboundPort := tlsMirrorInteropParsePort(t, tlsMirrorInteropPort(in.Address()))
|
||||
inboundPort := vmessInteropParsePort(t, vmessInteropPort(in.Address()))
|
||||
|
||||
config := tlsMirrorInteropClientConfig(t, v2rayPort.Port(), inboundPort, tlsMirrorInteropPort(echoAddr), userUUID, forward.certChainHash, advanced)
|
||||
startTLSMirrorInteropV2Ray(t, v2rayBin, config, v2rayPort, "")
|
||||
config := tlsMirrorInteropClientConfig(t, v2rayPort.Port(), inboundPort, vmessInteropPort(echoAddr), userUUID, forward.certChainHash, advanced)
|
||||
startVMessInteropV2Ray(t, v2rayBin, config, v2rayPort.Release, "")
|
||||
|
||||
tlsMirrorInteropRoundTripWithRetry(t, func() (net.Conn, error) {
|
||||
vmessInteropRoundTripWithRetry(t, func() (net.Conn, error) {
|
||||
return net.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(v2rayPort.Port())))
|
||||
}, advanced.payloadSize)
|
||||
})
|
||||
}
|
||||
|
||||
func tlsMirrorInteropV2RayBinary(t *testing.T) string {
|
||||
t.Helper()
|
||||
goBin, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
t.Skip("go toolchain not found, skip real v2ray tlsmirror interop test")
|
||||
}
|
||||
|
||||
root := filepath.Join(os.TempDir(), "mihomo-v2ray-tlsmirror-interop", v2rayTLSMirrorInteropRef)
|
||||
binDir := filepath.Join(root, "bin")
|
||||
exe := ""
|
||||
if runtime.GOOS == "windows" {
|
||||
exe = ".exe"
|
||||
}
|
||||
v2rayBin := filepath.Join(binDir, "v2ray"+exe)
|
||||
if _, err := os.Stat(v2rayBin); err == nil {
|
||||
return v2rayBin
|
||||
}
|
||||
goVersion := tlsMirrorInteropGoVersion(t, goBin)
|
||||
goMajor, goMinor, ok := tlsMirrorInteropGoVersionMajorMinor(goVersion)
|
||||
if ok && goMajor == 1 && goMinor < 21 {
|
||||
t.Skipf("%s does not support GOTOOLCHAIN toolchain download, skip real v2ray tlsmirror interop test", goVersion)
|
||||
}
|
||||
|
||||
require.NoError(t, os.RemoveAll(root))
|
||||
require.NoError(t, os.MkdirAll(binDir, 0o755))
|
||||
|
||||
tlsMirrorInteropGo(t, goBin, root, "mod", "init", "mihomo-v2ray-tlsmirror-interop")
|
||||
tlsMirrorInteropGo(t, goBin, root, "get", "github.com/v2fly/v2ray-core/v5@"+v2rayTLSMirrorInteropRef)
|
||||
if ok && (goMajor > 1 || goMajor == 1 && goMinor > 26) {
|
||||
tlsMirrorInteropGo(t, goBin, root, "get", "golang.org/x/net@"+v2rayTLSMirrorInteropXNetRef)
|
||||
}
|
||||
tlsMirrorInteropGo(t, goBin, root, "build", "-mod=mod", "-trimpath", "-o", v2rayBin, "github.com/v2fly/v2ray-core/v5/main")
|
||||
return v2rayBin
|
||||
}
|
||||
|
||||
func tlsMirrorInteropGoVersion(t *testing.T, goBin string) string {
|
||||
t.Helper()
|
||||
cmd := exec.Command(goBin, "version")
|
||||
output, err := cmd.Output()
|
||||
require.NoError(t, err, "go version")
|
||||
return tlsMirrorInteropParseGoVersion(string(output))
|
||||
}
|
||||
|
||||
func tlsMirrorInteropParseGoVersion(output string) string {
|
||||
for _, field := range strings.Fields(output) {
|
||||
if strings.HasPrefix(field, "go1.") {
|
||||
return field
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func tlsMirrorInteropGoVersionMajorMinor(version string) (int, int, bool) {
|
||||
version = strings.TrimPrefix(version, "go")
|
||||
parts := strings.SplitN(version, ".", 3)
|
||||
if len(parts) < 2 {
|
||||
return 0, 0, false
|
||||
}
|
||||
major, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
minorText := parts[1]
|
||||
for i, r := range minorText {
|
||||
if r < '0' || r > '9' {
|
||||
minorText = minorText[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if minorText == "" {
|
||||
return 0, 0, false
|
||||
}
|
||||
minor, err := strconv.Atoi(minorText)
|
||||
if err != nil {
|
||||
return 0, 0, false
|
||||
}
|
||||
return major, minor, true
|
||||
}
|
||||
|
||||
func tlsMirrorInteropGo(t *testing.T, goBin, dir string, args ...string) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(goBin, args...)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = tlsMirrorInteropGoEnv()
|
||||
output, err := cmd.CombinedOutput()
|
||||
require.NoError(t, err, "go %s\n%s", strings.Join(args, " "), string(output))
|
||||
}
|
||||
|
||||
func tlsMirrorInteropGoEnv() []string {
|
||||
env := os.Environ()
|
||||
hasGoToolchain := false
|
||||
for i, value := range env {
|
||||
if strings.HasPrefix(value, "GOTOOLCHAIN=") {
|
||||
env[i] = "GOTOOLCHAIN=auto"
|
||||
hasGoToolchain = true
|
||||
}
|
||||
}
|
||||
if !hasGoToolchain {
|
||||
env = append(env, "GOTOOLCHAIN=auto")
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func tlsMirrorInteropServerConfig(t *testing.T, listenPort int, forwardPort string, userID string, advanced tlsMirrorInteropAdvanced) []byte {
|
||||
t.Helper()
|
||||
forwardPortValue := tlsMirrorInteropParsePort(t, forwardPort)
|
||||
config := tlsMirrorInteropBaseConfig()
|
||||
forwardPortValue := vmessInteropParsePort(t, forwardPort)
|
||||
config := vmessInteropBaseConfig()
|
||||
config["inbounds"] = []any{map[string]any{
|
||||
"protocol": "vmess",
|
||||
"listen": "127.0.0.1",
|
||||
@@ -346,7 +225,7 @@ func tlsMirrorInteropServerConfig(t *testing.T, listenPort int, forwardPort stri
|
||||
},
|
||||
"streamSettings": tlsMirrorInteropStreamConfig(tlsMirrorInteropServerSettings(advanced, forwardPortValue), nil),
|
||||
}}
|
||||
config["outbounds"] = []any{tlsMirrorInteropDirectOutbound()}
|
||||
config["outbounds"] = []any{vmessInteropDirectOutbound()}
|
||||
if advanced.config.ConnectionEnrolment != nil {
|
||||
config["router"] = map[string]any{
|
||||
"rule": []any{map[string]any{
|
||||
@@ -358,13 +237,13 @@ func tlsMirrorInteropServerConfig(t *testing.T, listenPort int, forwardPort stri
|
||||
}},
|
||||
}
|
||||
}
|
||||
return tlsMirrorInteropMarshalJSONConfig(t, config)
|
||||
return vmessInteropMarshalJSONConfig(t, config)
|
||||
}
|
||||
|
||||
func tlsMirrorInteropClientConfig(t *testing.T, listenPort, serverPort int, targetPort string, userID, carrierCertHash string, advanced tlsMirrorInteropAdvanced) []byte {
|
||||
t.Helper()
|
||||
targetPortValue := tlsMirrorInteropParsePort(t, targetPort)
|
||||
config := tlsMirrorInteropBaseConfig()
|
||||
targetPortValue := vmessInteropParsePort(t, targetPort)
|
||||
config := vmessInteropBaseConfig()
|
||||
config["inbounds"] = []any{map[string]any{
|
||||
"protocol": "dokodemo-door",
|
||||
"listen": "127.0.0.1",
|
||||
@@ -386,7 +265,7 @@ func tlsMirrorInteropClientConfig(t *testing.T, listenPort, serverPort int, targ
|
||||
"uuid": userID,
|
||||
},
|
||||
},
|
||||
tlsMirrorInteropDirectOutbound(),
|
||||
vmessInteropDirectOutbound(),
|
||||
}
|
||||
if advanced.config.ConnectionEnrolment != nil {
|
||||
controlAdvanced := advanced
|
||||
@@ -402,18 +281,7 @@ func tlsMirrorInteropClientConfig(t *testing.T, listenPort, serverPort int, targ
|
||||
},
|
||||
})
|
||||
}
|
||||
return tlsMirrorInteropMarshalJSONConfig(t, config)
|
||||
}
|
||||
|
||||
func tlsMirrorInteropBaseConfig() map[string]any {
|
||||
return map[string]any{
|
||||
"log": map[string]any{
|
||||
"error": map[string]any{
|
||||
"type": "Console",
|
||||
"level": "Debug",
|
||||
},
|
||||
},
|
||||
}
|
||||
return vmessInteropMarshalJSONConfig(t, config)
|
||||
}
|
||||
|
||||
func tlsMirrorInteropStreamConfig(tlsMirrorSettings map[string]any, securitySettings map[string]any) map[string]any {
|
||||
@@ -539,63 +407,6 @@ func tlsMirrorInteropTLSVersion(advanced tlsMirrorInteropAdvanced) string {
|
||||
return "TLS1_3"
|
||||
}
|
||||
|
||||
func tlsMirrorInteropDirectOutbound() map[string]any {
|
||||
return map[string]any{
|
||||
"protocol": "freedom",
|
||||
"tag": "direct",
|
||||
}
|
||||
}
|
||||
|
||||
func tlsMirrorInteropMarshalJSONConfig(t *testing.T, config map[string]any) []byte {
|
||||
t.Helper()
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
require.NoError(t, err)
|
||||
data = append(data, '\n')
|
||||
return data
|
||||
}
|
||||
|
||||
func tlsMirrorInteropParsePort(t *testing.T, port string) int {
|
||||
t.Helper()
|
||||
value, err := strconv.Atoi(port)
|
||||
require.NoError(t, err)
|
||||
return value
|
||||
}
|
||||
|
||||
func startTLSMirrorInteropV2Ray(t *testing.T, v2rayBin string, config []byte, port *tlsMirrorInteropReservedPort, waitAddr string) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cmd := exec.CommandContext(ctx, v2rayBin, "run", "-format=jsonv5")
|
||||
var output bytes.Buffer
|
||||
cmd.Stdin = bytes.NewReader(config)
|
||||
cmd.Stdout = &output
|
||||
cmd.Stderr = &output
|
||||
port.Release()
|
||||
require.NoError(t, cmd.Start())
|
||||
t.Cleanup(func() {
|
||||
cancel()
|
||||
_ = cmd.Wait()
|
||||
if t.Failed() {
|
||||
t.Log(output.String())
|
||||
}
|
||||
})
|
||||
|
||||
if waitAddr == "" {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
return
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(10 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
conn, err := net.DialTimeout("tcp", waitAddr, 100*time.Millisecond)
|
||||
if err == nil {
|
||||
_ = conn.Close()
|
||||
return
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("v2ray did not listen on %s\n%s", waitAddr, output.String())
|
||||
}
|
||||
|
||||
func startTLSMirrorInteropCarrierTLS(t *testing.T, configure ...func(*tls.Config)) tlsMirrorInteropCarrier {
|
||||
t.Helper()
|
||||
certPEM, keyPEM, fingerprint, err := ca.NewRandomTLSKeyPair(ca.KeyPairTypeP256)
|
||||
@@ -693,146 +504,3 @@ func startTLSMirrorInteropCarrierHTTP2(t *testing.T) tlsMirrorInteropCarrier {
|
||||
certChainHash: tlsMirrorInteropCertChainHash([]byte(certPEM)),
|
||||
}
|
||||
}
|
||||
|
||||
func startTLSMirrorInteropEcho(t *testing.T) string {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = ln.Close() })
|
||||
go func() {
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer conn.Close()
|
||||
_, _ = io.Copy(conn, conn)
|
||||
}()
|
||||
}
|
||||
}()
|
||||
return ln.Addr().String()
|
||||
}
|
||||
|
||||
func tlsMirrorInteropDirectTunnel(t *testing.T) *TestTunnel {
|
||||
t.Helper()
|
||||
return &TestTunnel{
|
||||
HandleTCPConnFn: func(conn net.Conn, metadata *C.Metadata) {
|
||||
target, err := net.Dial("tcp", metadata.RemoteAddress())
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return
|
||||
}
|
||||
N.Relay(target, conn)
|
||||
},
|
||||
HandleUDPPacketFn: func(packet C.UDPPacket, metadata *C.Metadata) {
|
||||
packet.Drop()
|
||||
},
|
||||
NatTableFn: func() C.NatTable {
|
||||
return nil
|
||||
},
|
||||
CloseFn: func() error {
|
||||
return nil
|
||||
},
|
||||
NewDialerFn: func() C.Dialer {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func tlsMirrorInteropMetadata(t *testing.T, addr string) *C.Metadata {
|
||||
t.Helper()
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
require.NoError(t, err)
|
||||
ip, err := netip.ParseAddr(host)
|
||||
require.NoError(t, err)
|
||||
portNum, err := net.LookupPort("tcp", port)
|
||||
require.NoError(t, err)
|
||||
return &C.Metadata{
|
||||
NetWork: C.TCP,
|
||||
DstIP: ip,
|
||||
DstPort: uint16(portNum),
|
||||
}
|
||||
}
|
||||
|
||||
func tlsMirrorInteropRoundTripWithRetry(t *testing.T, dial func() (net.Conn, error), payloadSize int) {
|
||||
t.Helper()
|
||||
var lastErr error
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
conn, err := dial()
|
||||
if err == nil {
|
||||
err = tlsMirrorInteropRoundTripConn(conn, payloadSize)
|
||||
} else {
|
||||
err = fmt.Errorf("dial: %w", err)
|
||||
}
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
lastErr = err
|
||||
var netErr net.Error
|
||||
if !errors.As(err, &netErr) || !netErr.Timeout() {
|
||||
break
|
||||
}
|
||||
// v2ray-core registers the tlsmirror carrier outbound asynchronously on
|
||||
// first use, so slower builders may hit a startup-only timeout.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
require.NoError(t, lastErr)
|
||||
}
|
||||
|
||||
func tlsMirrorInteropRoundTripConn(conn net.Conn, payloadSize int) error {
|
||||
defer conn.Close()
|
||||
if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
||||
return fmt.Errorf("set deadline: %w", err)
|
||||
}
|
||||
if payloadSize == 0 {
|
||||
payloadSize = len("tlsmirror-interop-") * 256
|
||||
}
|
||||
payload := bytes.Repeat([]byte("x"), payloadSize)
|
||||
_, err := conn.Write(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write: %w", err)
|
||||
}
|
||||
got := make([]byte, len(payload))
|
||||
_, err = io.ReadFull(conn, got)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read full: %w", err)
|
||||
}
|
||||
if !bytes.Equal(payload, got) {
|
||||
return fmt.Errorf("unexpected payload: got %d bytes", len(got))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type tlsMirrorInteropReservedPort struct {
|
||||
ln net.Listener
|
||||
port int
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func tlsMirrorInteropReservePort(t *testing.T) *tlsMirrorInteropReservedPort {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
port := &tlsMirrorInteropReservedPort{
|
||||
ln: ln,
|
||||
port: ln.Addr().(*net.TCPAddr).Port,
|
||||
}
|
||||
t.Cleanup(port.Release)
|
||||
return port
|
||||
}
|
||||
|
||||
func (p *tlsMirrorInteropReservedPort) Port() int {
|
||||
return p.port
|
||||
}
|
||||
|
||||
func (p *tlsMirrorInteropReservedPort) Release() {
|
||||
p.once.Do(func() {
|
||||
_ = p.ln.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func tlsMirrorInteropPort(addr string) string {
|
||||
_, port, _ := net.SplitHostPort(addr)
|
||||
return port
|
||||
}
|
||||
Reference in new issue
Block a user