2022-09-17 11:54:04 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-11-18 18:59:19 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/json/badoption"
|
2022-09-17 11:54:04 +08:00
|
|
|
|
|
|
|
"github.com/spyzhov/ajson"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMuxCoolServer(t *testing.T) {
|
|
|
|
userId := newUUID()
|
|
|
|
content, err := os.ReadFile("config/vmess-mux-client.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
config, err := ajson.Unmarshal(content)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
config.MustKey("inbounds").MustIndex(0).MustKey("port").SetNumeric(float64(clientPort))
|
|
|
|
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(userId.String())
|
|
|
|
|
|
|
|
content, err = ajson.Marshal(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
startDockerContainer(t, DockerOptions{
|
|
|
|
Image: ImageV2RayCore,
|
|
|
|
Ports: []uint16{serverPort, testPort},
|
|
|
|
EntryPoint: "v2ray",
|
2023-07-11 14:03:55 +08:00
|
|
|
Cmd: []string{"run"},
|
2022-09-17 11:54:04 +08:00
|
|
|
Stdin: content,
|
|
|
|
})
|
|
|
|
|
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-09-17 11:54:04 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeVMess,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.VMessInboundOptions{
|
2022-09-17 11:54:04 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-09-17 11:54:04 +08:00
|
|
|
ListenPort: serverPort,
|
|
|
|
},
|
|
|
|
Users: []option.VMessUser{
|
|
|
|
{
|
|
|
|
Name: "sekai",
|
|
|
|
UUID: userId.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-09-29 22:27:16 +08:00
|
|
|
testSuitSimple(t, clientPort, testPort)
|
2022-09-17 11:54:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxCoolClient(t *testing.T) {
|
|
|
|
user := newUUID()
|
|
|
|
content, err := os.ReadFile("config/vmess-server.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
config, err := ajson.Unmarshal(content)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
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{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-09-17 11:54:04 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeMixed,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.HTTPMixedInboundOptions{
|
2022-09-17 11:54:04 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-09-17 11:54:04 +08:00
|
|
|
ListenPort: clientPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:59:19 +08:00
|
|
|
Outbounds: []option.Outbound{
|
2022-09-17 11:54:04 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeVMess,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.VMessOutboundOptions{
|
2022-09-17 11:54:04 +08:00
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "127.0.0.1",
|
|
|
|
ServerPort: serverPort,
|
|
|
|
},
|
|
|
|
UUID: user.String(),
|
|
|
|
PacketEncoding: "xudp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-09-29 22:27:16 +08:00
|
|
|
testSuitSimple(t, clientPort, testPort)
|
2022-09-17 11:54:04 +08:00
|
|
|
}
|
2022-09-23 13:14:31 +08:00
|
|
|
|
|
|
|
func TestMuxCoolSelf(t *testing.T) {
|
|
|
|
user := newUUID()
|
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-09-23 13:14:31 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeMixed,
|
|
|
|
Tag: "mixed-in",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.HTTPMixedInboundOptions{
|
2022-09-23 13:14:31 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-09-23 13:14:31 +08:00
|
|
|
ListenPort: clientPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeVMess,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.VMessInboundOptions{
|
2022-09-23 13:14:31 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-09-23 13:14:31 +08:00
|
|
|
ListenPort: serverPort,
|
|
|
|
},
|
|
|
|
Users: []option.VMessUser{
|
|
|
|
{
|
|
|
|
Name: "sekai",
|
|
|
|
UUID: user.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:59:19 +08:00
|
|
|
Outbounds: []option.Outbound{
|
2022-09-23 13:14:31 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeDirect,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeVMess,
|
|
|
|
Tag: "vmess-out",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.VMessOutboundOptions{
|
2022-09-23 13:14:31 +08:00
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "127.0.0.1",
|
|
|
|
ServerPort: serverPort,
|
|
|
|
},
|
|
|
|
UUID: user.String(),
|
|
|
|
PacketEncoding: "xudp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Route: &option.RouteOptions{
|
|
|
|
Rules: []option.Rule{
|
|
|
|
{
|
2024-10-21 23:38:34 +08:00
|
|
|
Type: C.RuleTypeDefault,
|
2022-09-23 13:14:31 +08:00
|
|
|
DefaultOptions: option.DefaultRule{
|
2024-10-21 23:38:34 +08:00
|
|
|
RawDefaultRule: option.RawDefaultRule{
|
|
|
|
Inbound: []string{"mixed-in"},
|
|
|
|
},
|
|
|
|
RuleAction: option.RuleAction{
|
|
|
|
Action: C.RuleActionTypeRoute,
|
|
|
|
|
|
|
|
RouteOptions: option.RouteActionOptions{
|
|
|
|
Outbound: "vmess-out",
|
|
|
|
},
|
|
|
|
},
|
2022-09-23 13:14:31 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-09-29 22:27:16 +08:00
|
|
|
testSuitSimple(t, clientPort, testPort)
|
2022-09-23 13:14:31 +08:00
|
|
|
}
|