Add vmess inbound/outbound

This commit is contained in:
世界
2022-07-18 12:32:31 +08:00
parent 02ef795581
commit 4fce3cff24
18 changed files with 476 additions and 105 deletions

View File

@@ -46,15 +46,21 @@ func startInstance(t *testing.T, options option.Options) {
func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "")
dialTCP := func(port uint16) (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", port))
dialTCP := func() (net.Conn, error) {
return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
dialUDP := func(port uint16) (net.PacketConn, error) {
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", port))
dialUDP := func() (net.PacketConn, error) {
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
}
require.NoError(t, testPingPongWithConn(t, dialTCP))
require.NoError(t, testLargeDataWithConn(t, dialTCP))
require.NoError(t, testPingPongWithPacketConn(t, dialUDP))
require.NoError(t, testLargeDataWithPacketConn(t, dialUDP))
require.NoError(t, testPacketConnTimeout(t, dialUDP))
t.Run("tcp", func(t *testing.T) {
t.Parallel()
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
})
t.Run("udp", func(t *testing.T) {
t.Parallel()
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
})
// require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
// require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
// require.NoError(t, testPacketConnTimeout(t, dialUDP))
}

View File

@@ -152,15 +152,14 @@ func newLargeDataPair() (chan hashPair, chan hashPair, func(t *testing.T) error)
return pingCh, pongCh, test
}
func testPingPongWithConn(t *testing.T, cc func(port uint16) (net.Conn, error)) error {
port := mkPort(t)
func testPingPongWithConn(t *testing.T, port uint16, cc func() (net.Conn, error)) error {
l, err := listen("tcp", ":"+F.ToString(port))
if err != nil {
return err
}
defer l.Close()
c, err := cc(port)
c, err := cc()
if err != nil {
return err
}
@@ -199,9 +198,7 @@ func testPingPongWithConn(t *testing.T, cc func(port uint16) (net.Conn, error))
return test(t)
}
func testPingPongWithPacketConn(t *testing.T, pcc func(port uint16) (net.PacketConn, error)) error {
port := mkPort(t)
func testPingPongWithPacketConn(t *testing.T, port uint16, pcc func() (net.PacketConn, error)) error {
l, err := listenPacket("udp", ":"+F.ToString(port))
require.NoError(t, err)
defer l.Close()
@@ -222,7 +219,7 @@ func testPingPongWithPacketConn(t *testing.T, pcc func(port uint16) (net.PacketC
}
}()
pc, err := pcc(port)
pc, err := pcc()
if err != nil {
return err
}
@@ -249,8 +246,7 @@ type hashPair struct {
recvHash map[int][]byte
}
func testLargeDataWithConn(t *testing.T, cc func(port uint16) (net.Conn, error)) error {
port := mkPort(t)
func testLargeDataWithConn(t *testing.T, port uint16, cc func() (net.Conn, error)) error {
l, err := listen("tcp", ":"+F.ToString(port))
require.NoError(t, err)
defer l.Close()
@@ -279,7 +275,7 @@ func testLargeDataWithConn(t *testing.T, cc func(port uint16) (net.Conn, error))
return hashMap, nil
}
c, err := cc(port)
c, err := cc()
if err != nil {
return err
}
@@ -347,8 +343,7 @@ func testLargeDataWithConn(t *testing.T, cc func(port uint16) (net.Conn, error))
return test(t)
}
func testLargeDataWithPacketConn(t *testing.T, pcc func(port uint16) (net.PacketConn, error)) error {
port := mkPort(t)
func testLargeDataWithPacketConn(t *testing.T, port uint16, pcc func() (net.PacketConn, error)) error {
l, err := listenPacket("udp", ":"+F.ToString(port))
require.NoError(t, err)
defer l.Close()
@@ -363,24 +358,22 @@ func testLargeDataWithPacketConn(t *testing.T, pcc func(port uint16) (net.Packet
hashMap := map[int][]byte{}
mux := sync.Mutex{}
for i := 0; i < times; i++ {
go func(idx int) {
buf := make([]byte, chunkSize)
if _, err := rand.Read(buf[1:]); err != nil {
t.Log(err.Error())
return
}
buf[0] = byte(idx)
buf := make([]byte, chunkSize)
if _, err = rand.Read(buf[1:]); err != nil {
t.Log(err.Error())
continue
}
buf[0] = byte(i)
hash := md5.Sum(buf)
mux.Lock()
hashMap[idx] = hash[:]
mux.Unlock()
hash := md5.Sum(buf)
mux.Lock()
hashMap[i] = hash[:]
mux.Unlock()
if _, err := pc.WriteTo(buf, addr); err != nil {
t.Log(err.Error())
return
}
}(i)
if _, err = pc.WriteTo(buf, addr); err != nil {
t.Log(err)
continue
}
}
return hashMap, nil
@@ -414,7 +407,7 @@ func testLargeDataWithPacketConn(t *testing.T, pcc func(port uint16) (net.Packet
}
}()
pc, err := pcc(port)
pc, err := pcc()
if err != nil {
return err
}
@@ -449,8 +442,8 @@ func testLargeDataWithPacketConn(t *testing.T, pcc func(port uint16) (net.Packet
return test(t)
}
func testPacketConnTimeout(t *testing.T, pcc func(port uint16) (net.PacketConn, error)) error {
pc, err := pcc(mkPort(t))
func testPacketConnTimeout(t *testing.T, pcc func() (net.PacketConn, error)) error {
pc, err := pcc()
if err != nil {
return err
}

View File

@@ -2,19 +2,21 @@ module test
go 1.18
require (
github.com/docker/docker v20.10.17+incompatible
github.com/docker/go-connections v0.4.0
github.com/sagernet/sing v0.0.0-20220717063925-00f98eb6bc34
github.com/sagernet/sing-box v0.0.0
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.0.0-20220708220712-1185a9018129
)
require github.com/sagernet/sing-box v0.0.0
replace github.com/sagernet/sing-box => ../
require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/docker/docker v20.10.17+incompatible
github.com/docker/go-connections v0.4.0
github.com/gofrs/uuid v4.2.0+incompatible
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.0.0-20220708220712-1185a9018129
)
require (
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/database64128/tfo-go v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
@@ -35,12 +37,13 @@ require (
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619 // indirect
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f // indirect
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f // indirect
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/vishvananda/netlink v1.1.0 // indirect
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.3.0 // indirect
gvisor.dev/gvisor v0.0.0-20220711011657-cecae2f4234d // indirect

View File

@@ -1,7 +1,7 @@
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/database64128/tfo-go v1.1.0 h1:VO0polyGNSAmr99nYw9GQeMz7ZOcQ/QbjlTwniHwfTQ=
@@ -19,6 +19,8 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/goccy/go-json v0.9.10 h1:hCeNmprSNLB8B8vQKWl6DpuH0t60oEs+TAk9a7CScKc=
github.com/goccy/go-json v0.9.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
@@ -52,14 +54,16 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagernet/sing v0.0.0-20220717063925-00f98eb6bc34 h1:1kFruA2QzuH2R6txJXEDSasfdxzsjNyzC4Z1kZjMkHg=
github.com/sagernet/sing v0.0.0-20220717063925-00f98eb6bc34/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56 h1:r6FHrtIE3dG9c6zl487QjxPFgADq/C0AiDwhpAZFKUw=
github.com/sagernet/sing v0.0.0-20220718035659-3d74b823ed56/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619 h1:oHbOmq1WS0XaZmXp6WpxzyB2xeyRIA1/L8EJKuNntfY=
github.com/sagernet/sing-dns v0.0.0-20220711062726-c64e938e4619/go.mod h1:y2fpvoxukw3G7eApIZwkcpcG/NE4AB8pCQI0Qd8rMqk=
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f h1:F6yiuKbBoXgWiuoP7R0YA14pDEl3emxA1mL7M16Q7gc=
github.com/sagernet/sing-shadowsocks v0.0.0-20220717063942-45a2ad9cd41f/go.mod h1:cDrLwa3zwY8AaW6a4sjipn4xgdIr3CT8TPqSW6iFOi0=
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f h1:o3YN4sFC7lQznAwutagPqBb23hal7MkgVq/VEvd7Vug=
github.com/sagernet/sing-tun v0.0.0-20220717030718-f53aabff275f/go.mod h1:p7QbUBs2ejf6UQsiHyy1xGAWOk9JWQjZTHy8pOmkWmo=
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a h1:durFxTP1xsOMeDt8x0AV/9BXAPa8uMQRKzPaVkGSOS0=
github.com/sagernet/sing-vmess v0.0.0-20220718031323-07c377156e4a/go.mod h1:VjqeHNWtDVoExWInXB7QsCeMp5RozlnJhMgfbW/n4I0=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
@@ -107,8 +111,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JC
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 h1:Dpdu/EMxGMFgq0CeYMh4fazTD2vtlZRYE7wyynxJb9U=
golang.org/x/time v0.0.0-20220609170525-579cf78fd858/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=

View File

@@ -14,6 +14,7 @@ import (
)
func TestShadowsocks(t *testing.T) {
t.Parallel()
for _, method := range []string{
"aes-128-gcm",
"aes-256-gcm",
@@ -32,6 +33,7 @@ func TestShadowsocks(t *testing.T) {
}
func TestShadowsocks2022(t *testing.T) {
t.Parallel()
for _, method16 := range []string{
"2022-blake3-aes-128-gcm",
} {
@@ -74,7 +76,7 @@ func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, pass
})
startInstance(t, option.Options{
Log: &option.LogOption{
Disabled: true,
Level: "error",
},
Inbounds: []option.Inbound{
{
@@ -106,7 +108,7 @@ func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, pas
})
startInstance(t, option.Options{
Log: &option.LogOption{
Disabled: true,
Level: "error",
},
Inbounds: []option.Inbound{
{
@@ -143,7 +145,7 @@ func testShadowsocksSelf(t *testing.T, method string, password string) {
testPort := mkPort(t)
startInstance(t, option.Options{
Log: &option.LogOption{
Disabled: true,
Level: "error",
},
Inbounds: []option.Inbound{
{

129
test/vmess_test.go Normal file
View File

@@ -0,0 +1,129 @@
package main
import (
"net/netip"
"testing"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)
func TestVMessSelf(t *testing.T) {
t.Parallel()
for _, security := range []string{
"zero",
} {
t.Run(security, func(t *testing.T) {
testVMessSelf0(t, security)
})
}
for _, security := range []string{
"aes-128-gcm", "chacha20-poly1305", "aes-128-cfb",
} {
t.Run(security, func(t *testing.T) {
testVMessSelf1(t, security)
})
}
}
func testVMessSelf0(t *testing.T, security string) {
t.Parallel()
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
t.Run("default", func(t *testing.T) {
testVMessSelf2(t, security, user.String(), false, false)
})
t.Run("padding", func(t *testing.T) {
testVMessSelf2(t, security, user.String(), true, false)
})
}
func testVMessSelf1(t *testing.T, security string) {
t.Parallel()
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
t.Run("default", func(t *testing.T) {
testVMessSelf2(t, security, user.String(), false, false)
})
t.Run("padding", func(t *testing.T) {
testVMessSelf2(t, security, user.String(), true, false)
})
t.Run("authid", func(t *testing.T) {
testVMessSelf2(t, security, user.String(), false, true)
})
t.Run("padding-authid", func(t *testing.T) {
testVMessSelf2(t, security, user.String(), true, true)
})
}
func testVMessSelf2(t *testing.T, security string, uuid string, globalPadding bool, authenticatedLength bool) {
t.Parallel()
serverPort := mkPort(t)
clientPort := mkPort(t)
testPort := mkPort(t)
startInstance(t, option.Options{
Log: &option.LogOption{
Level: "error",
},
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
Name: "sekai",
UUID: uuid,
},
},
},
},
},
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
VMessOptions: option.VMessOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Security: security,
UUID: uuid,
GlobalPadding: globalPadding,
AuthenticatedLength: authenticatedLength,
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
DefaultOptions: option.DefaultRule{
Inbound: []string{"mixed-in"},
Outbound: "vmess-out",
},
},
},
},
})
testSuit(t, clientPort, testPort)
}