sing-box/test/mux_test.go

199 lines
4.3 KiB
Go
Raw Normal View History

2022-07-29 16:29:22 +00:00
package main
import (
"net/netip"
"testing"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
2024-11-18 10:59:19 +00:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/json/badoption"
2022-08-04 02:38:20 +00:00
2023-04-09 07:37:06 +00:00
"github.com/gofrs/uuid/v5"
2022-07-29 16:29:22 +00:00
)
2023-07-11 06:03:55 +00:00
var muxProtocols = []string{
"h2mux",
"smux",
"yamux",
2022-08-04 02:38:20 +00:00
}
2022-09-23 05:14:31 +00:00
func TestVMessSMux(t *testing.T) {
testVMessMux(t, option.OutboundMultiplexOptions{
2023-04-20 03:16:22 +00:00
Enabled: true,
2023-07-11 06:03:55 +00:00
Protocol: "smux",
2023-04-20 03:16:22 +00:00
})
2022-08-03 13:51:34 +00:00
}
2022-09-23 05:14:31 +00:00
func TestShadowsocksMux(t *testing.T) {
2022-08-04 02:38:20 +00:00
for _, protocol := range muxProtocols {
2023-07-11 06:03:55 +00:00
t.Run(protocol, func(t *testing.T) {
testShadowsocksMux(t, option.OutboundMultiplexOptions{
2023-04-20 03:16:22 +00:00
Enabled: true,
2023-07-11 06:03:55 +00:00
Protocol: protocol,
2023-04-20 03:16:22 +00:00
})
2022-08-04 02:38:20 +00:00
})
}
}
2023-04-20 03:16:22 +00:00
func TestShadowsockH2Mux(t *testing.T) {
testShadowsocksMux(t, option.OutboundMultiplexOptions{
2023-04-20 03:16:22 +00:00
Enabled: true,
2023-07-11 06:03:55 +00:00
Protocol: "h2mux",
2023-04-20 03:16:22 +00:00
Padding: true,
})
}
func TestShadowsockSMuxPadding(t *testing.T) {
testShadowsocksMux(t, option.OutboundMultiplexOptions{
2023-04-20 03:16:22 +00:00
Enabled: true,
2023-07-11 06:03:55 +00:00
Protocol: "smux",
2023-04-20 03:16:22 +00:00
Padding: true,
})
}
func testShadowsocksMux(t *testing.T, options option.OutboundMultiplexOptions) {
2022-07-29 16:29:22 +00:00
method := shadowaead_2022.List[0]
password := mkBase64(t, 16)
startInstance(t, option.Options{
2024-11-18 10:59:19 +00:00
Inbounds: []option.Inbound{
2022-07-29 16:29:22 +00:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 10:59:19 +00:00
Options: &option.HTTPMixedInboundOptions{
2022-07-29 16:29:22 +00:00
ListenOptions: option.ListenOptions{
2024-11-18 10:59:19 +00:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-07-29 16:29:22 +00:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeShadowsocks,
2024-11-18 10:59:19 +00:00
Options: &option.ShadowsocksInboundOptions{
2022-07-29 16:29:22 +00:00
ListenOptions: option.ListenOptions{
2024-11-18 10:59:19 +00:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-07-29 16:29:22 +00:00
ListenPort: serverPort,
},
Method: method,
Password: password,
2023-12-27 02:30:04 +00:00
Multiplex: &option.InboundMultiplexOptions{
Enabled: true,
},
2022-07-29 16:29:22 +00:00
},
},
},
2024-11-18 10:59:19 +00:00
Outbounds: []option.Outbound{
2022-07-29 16:29:22 +00:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeShadowsocks,
Tag: "ss-out",
2024-11-18 10:59:19 +00:00
Options: &option.ShadowsocksOutboundOptions{
2022-07-29 16:29:22 +00:00
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Method: method,
Password: password,
Multiplex: &options,
2022-07-29 16:29:22 +00:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 15:38:34 +00:00
Type: C.RuleTypeDefault,
2022-07-29 16:29:22 +00:00
DefaultOptions: option.DefaultRule{
2024-10-21 15:38:34 +00:00
RawDefaultRule: option.RawDefaultRule{
Inbound: []string{"mixed-in"},
},
RuleAction: option.RuleAction{
Action: C.RuleActionTypeRoute,
RouteOptions: option.RouteActionOptions{
Outbound: "ss-out",
},
},
2022-07-29 16:29:22 +00:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}
2022-08-04 02:38:20 +00:00
func testVMessMux(t *testing.T, options option.OutboundMultiplexOptions) {
2022-08-04 02:38:20 +00:00
user, _ := uuid.NewV4()
startInstance(t, option.Options{
2024-11-18 10:59:19 +00:00
Inbounds: []option.Inbound{
2022-08-04 02:38:20 +00:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 10:59:19 +00:00
Options: &option.HTTPMixedInboundOptions{
2022-08-04 02:38:20 +00:00
ListenOptions: option.ListenOptions{
2024-11-18 10:59:19 +00:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-04 02:38:20 +00:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
2024-11-18 10:59:19 +00:00
Options: &option.VMessInboundOptions{
2022-08-04 02:38:20 +00:00
ListenOptions: option.ListenOptions{
2024-11-18 10:59:19 +00:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-04 02:38:20 +00:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
UUID: user.String(),
},
},
Multiplex: &option.InboundMultiplexOptions{
Enabled: true,
},
2022-08-04 02:38:20 +00:00
},
},
},
2024-11-18 10:59:19 +00:00
Outbounds: []option.Outbound{
2022-08-04 02:38:20 +00:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
2024-11-18 10:59:19 +00:00
Options: &option.VMessOutboundOptions{
2022-08-04 02:38:20 +00:00
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
2023-04-20 03:16:22 +00:00
Security: "auto",
UUID: user.String(),
Multiplex: &options,
2022-08-04 02:38:20 +00:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 15:38:34 +00:00
Type: C.RuleTypeDefault,
2022-08-04 02:38:20 +00:00
DefaultOptions: option.DefaultRule{
2024-10-21 15:38:34 +00:00
RawDefaultRule: option.RawDefaultRule{
Inbound: []string{"mixed-in"},
},
RuleAction: option.RuleAction{
Action: C.RuleActionTypeRoute,
RouteOptions: option.RouteActionOptions{
Outbound: "vmess-out",
},
},
2022-08-04 02:38:20 +00:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}