Add vless outbound and xudp
This commit is contained in:
@@ -38,6 +38,7 @@ const (
|
||||
ImageNginx = "nginx:stable"
|
||||
ImageShadowTLS = "ghcr.io/ihciah/shadow-tls:latest"
|
||||
ImageShadowsocksR = "teddysun/shadowsocks-r:latest"
|
||||
ImageXRayCore = "teddysun/xray:latest"
|
||||
)
|
||||
|
||||
var allImages = []string{
|
||||
@@ -51,6 +52,7 @@ var allImages = []string{
|
||||
ImageNginx,
|
||||
ImageShadowTLS,
|
||||
ImageShadowsocksR,
|
||||
ImageXRayCore,
|
||||
}
|
||||
|
||||
var localIP = netip.MustParseAddr("127.0.0.1")
|
||||
@@ -379,7 +381,7 @@ func testLargeDataWithPacketConn(t *testing.T, port uint16, pcc func() (net.Pack
|
||||
|
||||
rAddr := &net.UDPAddr{IP: localIP.AsSlice(), Port: int(port)}
|
||||
|
||||
times := 50
|
||||
times := 2
|
||||
chunkSize := int64(1024)
|
||||
|
||||
pingCh, pongCh, test := newLargeDataPair()
|
||||
|
||||
25
test/config/vless-server.json
Normal file
25
test/config/vless-server.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"log": {
|
||||
"loglevel": "debug"
|
||||
},
|
||||
"inbounds": [
|
||||
{
|
||||
"listen": "0.0.0.0",
|
||||
"port": 1234,
|
||||
"protocol": "vless",
|
||||
"settings": {
|
||||
"decryption": "none",
|
||||
"clients": [
|
||||
{
|
||||
"id": "b831381d-6324-4d53-ad4f-8cda48b30811"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"outbounds": [
|
||||
{
|
||||
"protocol": "freedom"
|
||||
}
|
||||
]
|
||||
}
|
||||
120
test/vless_test.go
Normal file
120
test/vless_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
|
||||
"github.com/spyzhov/ajson"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestVLESS(t *testing.T) {
|
||||
content, err := os.ReadFile("config/vless-server.json")
|
||||
require.NoError(t, err)
|
||||
config, err := ajson.Unmarshal(content)
|
||||
require.NoError(t, err)
|
||||
|
||||
user := newUUID()
|
||||
inbound := config.MustKey("inbounds").MustIndex(0)
|
||||
inbound.MustKey("port").SetNumeric(float64(serverPort))
|
||||
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
|
||||
|
||||
content, err = ajson.Marshal(config)
|
||||
require.NoError(t, err)
|
||||
|
||||
startDockerContainer(t, DockerOptions{
|
||||
Image: ImageV2RayCore,
|
||||
Ports: []uint16{serverPort, testPort},
|
||||
EntryPoint: "v2ray",
|
||||
Stdin: content,
|
||||
})
|
||||
|
||||
startInstance(t, option.Options{
|
||||
Inbounds: []option.Inbound{
|
||||
{
|
||||
Type: C.TypeMixed,
|
||||
MixedOptions: option.HTTPMixedInboundOptions{
|
||||
ListenOptions: option.ListenOptions{
|
||||
Listen: option.ListenAddress(netip.IPv4Unspecified()),
|
||||
ListenPort: clientPort,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Outbounds: []option.Outbound{
|
||||
{
|
||||
Type: C.TypeVLESS,
|
||||
VLESSOptions: option.VLESSOutboundOptions{
|
||||
ServerOptions: option.ServerOptions{
|
||||
Server: "127.0.0.1",
|
||||
ServerPort: serverPort,
|
||||
},
|
||||
UUID: user.String(),
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
testSuit(t, clientPort, testPort)
|
||||
}
|
||||
|
||||
func TestVLESSXRay(t *testing.T) {
|
||||
testVLESSXray(t, "")
|
||||
}
|
||||
|
||||
func TestVLESSXUDP(t *testing.T) {
|
||||
testVLESSXray(t, "xudp")
|
||||
}
|
||||
|
||||
func testVLESSXray(t *testing.T, packetEncoding string) {
|
||||
content, err := os.ReadFile("config/vless-server.json")
|
||||
require.NoError(t, err)
|
||||
config, err := ajson.Unmarshal(content)
|
||||
require.NoError(t, err)
|
||||
|
||||
user := newUUID()
|
||||
inbound := config.MustKey("inbounds").MustIndex(0)
|
||||
inbound.MustKey("port").SetNumeric(float64(serverPort))
|
||||
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
|
||||
|
||||
content, err = ajson.Marshal(config)
|
||||
require.NoError(t, err)
|
||||
|
||||
startDockerContainer(t, DockerOptions{
|
||||
Image: ImageXRayCore,
|
||||
Ports: []uint16{serverPort, testPort},
|
||||
EntryPoint: "xray",
|
||||
Stdin: content,
|
||||
})
|
||||
|
||||
startInstance(t, option.Options{
|
||||
Inbounds: []option.Inbound{
|
||||
{
|
||||
Type: C.TypeMixed,
|
||||
MixedOptions: option.HTTPMixedInboundOptions{
|
||||
ListenOptions: option.ListenOptions{
|
||||
Listen: option.ListenAddress(netip.IPv4Unspecified()),
|
||||
ListenPort: clientPort,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Outbounds: []option.Outbound{
|
||||
{
|
||||
Type: C.TypeVLESS,
|
||||
VLESSOptions: option.VLESSOutboundOptions{
|
||||
ServerOptions: option.ServerOptions{
|
||||
Server: "127.0.0.1",
|
||||
ServerPort: serverPort,
|
||||
},
|
||||
UUID: user.String(),
|
||||
PacketEncoding: packetEncoding,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
testSuit(t, clientPort, testPort)
|
||||
}
|
||||
@@ -13,21 +13,24 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newUUID() uuid.UUID {
|
||||
user, _ := uuid.DefaultGenerator.NewV4()
|
||||
return user
|
||||
}
|
||||
|
||||
func _TestVMessAuto(t *testing.T) {
|
||||
security := "auto"
|
||||
user, err := uuid.DefaultGenerator.NewV4()
|
||||
require.NoError(t, err)
|
||||
t.Run("self", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, false)
|
||||
testVMessSelf(t, security, 0, false, false, false)
|
||||
})
|
||||
t.Run("packetaddr", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, true)
|
||||
testVMessSelf(t, security, 0, false, false, true)
|
||||
})
|
||||
t.Run("inbound", func(t *testing.T) {
|
||||
testVMessInboundWithV2Ray(t, security, user, 0, false)
|
||||
testVMessInboundWithV2Ray(t, security, 0, false)
|
||||
})
|
||||
t.Run("outbound", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 0)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,102 +59,97 @@ func TestVMess(t *testing.T) {
|
||||
}
|
||||
|
||||
func testVMess0(t *testing.T, security string) {
|
||||
user, err := uuid.DefaultGenerator.NewV4()
|
||||
require.NoError(t, err)
|
||||
t.Run("self", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, false)
|
||||
testVMessSelf(t, security, 0, false, false, false)
|
||||
})
|
||||
t.Run("self-legacy", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 1, false, false, false)
|
||||
testVMessSelf(t, security, 1, false, false, false)
|
||||
})
|
||||
t.Run("packetaddr", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, true)
|
||||
testVMessSelf(t, security, 0, false, false, true)
|
||||
})
|
||||
t.Run("outbound", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 0)
|
||||
})
|
||||
t.Run("outbound-legacy", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 1)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func testVMess1(t *testing.T, security string) {
|
||||
user, err := uuid.DefaultGenerator.NewV4()
|
||||
require.NoError(t, err)
|
||||
t.Run("self", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, false)
|
||||
testVMessSelf(t, security, 0, false, false, false)
|
||||
})
|
||||
t.Run("self-legacy", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 1, false, false, false)
|
||||
testVMessSelf(t, security, 1, false, false, false)
|
||||
})
|
||||
t.Run("packetaddr", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, true)
|
||||
testVMessSelf(t, security, 0, false, false, true)
|
||||
})
|
||||
t.Run("inbound", func(t *testing.T) {
|
||||
testVMessInboundWithV2Ray(t, security, user, 0, false)
|
||||
testVMessInboundWithV2Ray(t, security, 0, false)
|
||||
})
|
||||
t.Run("outbound", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 0)
|
||||
})
|
||||
t.Run("outbound-legacy", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 1)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func testVMess2(t *testing.T, security string) {
|
||||
user, err := uuid.DefaultGenerator.NewV4()
|
||||
require.NoError(t, err)
|
||||
t.Run("self", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, false)
|
||||
testVMessSelf(t, security, 0, false, false, false)
|
||||
})
|
||||
t.Run("self-padding", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, true, false, false)
|
||||
testVMessSelf(t, security, 0, true, false, false)
|
||||
})
|
||||
t.Run("self-authid", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, true, false)
|
||||
testVMessSelf(t, security, 0, false, true, false)
|
||||
})
|
||||
t.Run("self-padding-authid", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, true, true, false)
|
||||
testVMessSelf(t, security, 0, true, true, false)
|
||||
})
|
||||
t.Run("self-legacy", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 1, false, false, false)
|
||||
testVMessSelf(t, security, 1, false, false, false)
|
||||
})
|
||||
t.Run("self-legacy-padding", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 1, true, false, false)
|
||||
testVMessSelf(t, security, 1, true, false, false)
|
||||
})
|
||||
t.Run("packetaddr", func(t *testing.T) {
|
||||
testVMessSelf(t, security, user, 0, false, false, true)
|
||||
testVMessSelf(t, security, 0, false, false, true)
|
||||
})
|
||||
t.Run("inbound", func(t *testing.T) {
|
||||
testVMessInboundWithV2Ray(t, security, user, 0, false)
|
||||
testVMessInboundWithV2Ray(t, security, 0, false)
|
||||
})
|
||||
t.Run("inbound-authid", func(t *testing.T) {
|
||||
testVMessInboundWithV2Ray(t, security, user, 0, true)
|
||||
testVMessInboundWithV2Ray(t, security, 0, true)
|
||||
})
|
||||
t.Run("inbound-legacy", func(t *testing.T) {
|
||||
testVMessInboundWithV2Ray(t, security, user, 64, false)
|
||||
testVMessInboundWithV2Ray(t, security, 64, false)
|
||||
})
|
||||
t.Run("outbound", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 0)
|
||||
})
|
||||
t.Run("outbound-padding", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, true, false, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, true, false, 0)
|
||||
})
|
||||
t.Run("outbound-authid", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, true, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, false, true, 0)
|
||||
})
|
||||
t.Run("outbound-padding-authid", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, true, true, 0)
|
||||
testVMessOutboundWithV2Ray(t, security, true, true, 0)
|
||||
})
|
||||
t.Run("outbound-legacy", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, false, false, 1)
|
||||
testVMessOutboundWithV2Ray(t, security, false, false, 1)
|
||||
})
|
||||
t.Run("outbound-legacy-padding", func(t *testing.T) {
|
||||
testVMessOutboundWithV2Ray(t, security, user, true, false, 1)
|
||||
testVMessOutboundWithV2Ray(t, security, true, false, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, alterId int, authenticatedLength bool) {
|
||||
func testVMessInboundWithV2Ray(t *testing.T, security string, alterId int, authenticatedLength bool) {
|
||||
userId := newUUID()
|
||||
content, err := os.ReadFile("config/vmess-client.json")
|
||||
require.NoError(t, err)
|
||||
config, err := ajson.Unmarshal(content)
|
||||
@@ -161,7 +159,7 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, al
|
||||
outbound := config.MustKey("outbounds").MustIndex(0).MustKey("settings").MustKey("vnext").MustIndex(0)
|
||||
outbound.MustKey("port").SetNumeric(float64(serverPort))
|
||||
user := outbound.MustKey("users").MustIndex(0)
|
||||
user.MustKey("id").SetString(uuid.String())
|
||||
user.MustKey("id").SetString(userId.String())
|
||||
user.MustKey("alterId").SetNumeric(float64(alterId))
|
||||
user.MustKey("security").SetString(security)
|
||||
var experiments string
|
||||
@@ -193,7 +191,7 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, al
|
||||
Users: []option.VMessUser{
|
||||
{
|
||||
Name: "sekai",
|
||||
UUID: uuid.String(),
|
||||
UUID: userId.String(),
|
||||
AlterId: alterId,
|
||||
},
|
||||
},
|
||||
@@ -205,7 +203,8 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, al
|
||||
testSuitSimple(t, clientPort, testPort)
|
||||
}
|
||||
|
||||
func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, globalPadding bool, authenticatedLength bool, alterId int) {
|
||||
func testVMessOutboundWithV2Ray(t *testing.T, security string, globalPadding bool, authenticatedLength bool, alterId int) {
|
||||
user := newUUID()
|
||||
content, err := os.ReadFile("config/vmess-server.json")
|
||||
require.NoError(t, err)
|
||||
config, err := ajson.Unmarshal(content)
|
||||
@@ -213,7 +212,7 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g
|
||||
|
||||
inbound := config.MustKey("inbounds").MustIndex(0)
|
||||
inbound.MustKey("port").SetNumeric(float64(serverPort))
|
||||
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(uuid.String())
|
||||
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
|
||||
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("alterId").SetNumeric(float64(alterId))
|
||||
|
||||
content, err = ajson.Marshal(config)
|
||||
@@ -248,7 +247,7 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g
|
||||
ServerPort: serverPort,
|
||||
},
|
||||
Security: security,
|
||||
UUID: uuid.String(),
|
||||
UUID: user.String(),
|
||||
GlobalPadding: globalPadding,
|
||||
AuthenticatedLength: authenticatedLength,
|
||||
AlterId: alterId,
|
||||
@@ -259,7 +258,8 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g
|
||||
testSuitSimple(t, clientPort, testPort)
|
||||
}
|
||||
|
||||
func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) {
|
||||
func testVMessSelf(t *testing.T, security string, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) {
|
||||
user := newUUID()
|
||||
startInstance(t, option.Options{
|
||||
Inbounds: []option.Inbound{
|
||||
{
|
||||
@@ -282,7 +282,7 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, g
|
||||
Users: []option.VMessUser{
|
||||
{
|
||||
Name: "sekai",
|
||||
UUID: uuid.String(),
|
||||
UUID: user.String(),
|
||||
AlterId: alterId,
|
||||
},
|
||||
},
|
||||
@@ -302,7 +302,7 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, g
|
||||
ServerPort: serverPort,
|
||||
},
|
||||
Security: security,
|
||||
UUID: uuid.String(),
|
||||
UUID: user.String(),
|
||||
AlterId: alterId,
|
||||
GlobalPadding: globalPadding,
|
||||
AuthenticatedLength: authenticatedLength,
|
||||
|
||||
Reference in New Issue
Block a user