sing-box/test/mux_test.go

197 lines
4.3 KiB
Go
Raw Normal View History

2022-07-30 00:29:22 +08: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"
2022-08-04 10:38:20 +08:00
2023-04-09 15:37:06 +08:00
"github.com/gofrs/uuid/v5"
2022-07-30 00:29:22 +08:00
)
2023-07-11 14:03:55 +08:00
var muxProtocols = []string{
"h2mux",
"smux",
"yamux",
2022-08-04 10:38:20 +08:00
}
2022-09-23 13:14:31 +08:00
func TestVMessSMux(t *testing.T) {
testVMessMux(t, option.OutboundMultiplexOptions{
2023-04-20 11:16:22 +08:00
Enabled: true,
2023-07-11 14:03:55 +08:00
Protocol: "smux",
2023-04-20 11:16:22 +08:00
})
2022-08-03 21:51:34 +08:00
}
2022-09-23 13:14:31 +08:00
func TestShadowsocksMux(t *testing.T) {
2022-08-04 10:38:20 +08:00
for _, protocol := range muxProtocols {
2023-07-11 14:03:55 +08:00
t.Run(protocol, func(t *testing.T) {
testShadowsocksMux(t, option.OutboundMultiplexOptions{
2023-04-20 11:16:22 +08:00
Enabled: true,
2023-07-11 14:03:55 +08:00
Protocol: protocol,
2023-04-20 11:16:22 +08:00
})
2022-08-04 10:38:20 +08:00
})
}
}
2023-04-20 11:16:22 +08:00
func TestShadowsockH2Mux(t *testing.T) {
testShadowsocksMux(t, option.OutboundMultiplexOptions{
2023-04-20 11:16:22 +08:00
Enabled: true,
2023-07-11 14:03:55 +08:00
Protocol: "h2mux",
2023-04-20 11:16:22 +08:00
Padding: true,
})
}
func TestShadowsockSMuxPadding(t *testing.T) {
testShadowsocksMux(t, option.OutboundMultiplexOptions{
2023-04-20 11:16:22 +08:00
Enabled: true,
2023-07-11 14:03:55 +08:00
Protocol: "smux",
2023-04-20 11:16:22 +08:00
Padding: true,
})
}
func testShadowsocksMux(t *testing.T, options option.OutboundMultiplexOptions) {
2022-07-30 00:29:22 +08:00
method := shadowaead_2022.List[0]
password := mkBase64(t, 16)
startInstance(t, option.Options{
2024-11-02 00:39:02 +08:00
Inbounds: []option.LegacyInbound{
2022-07-30 00:29:22 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 20:46:22 +08:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-07-30 00:29:22 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeShadowsocks,
ShadowsocksOptions: option.ShadowsocksInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 20:46:22 +08:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-07-30 00:29:22 +08:00
ListenPort: serverPort,
},
Method: method,
Password: password,
2023-12-27 10:30:04 +08:00
Multiplex: &option.InboundMultiplexOptions{
Enabled: true,
},
2022-07-30 00:29:22 +08:00
},
},
},
2024-11-02 00:39:02 +08:00
LegacyOutbounds: []option.LegacyOutbound{
2022-07-30 00:29:22 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeShadowsocks,
Tag: "ss-out",
ShadowsocksOptions: option.ShadowsocksOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Method: method,
Password: password,
Multiplex: &options,
2022-07-30 00:29:22 +08:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-07-30 00:29:22 +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: "ss-out",
},
},
2022-07-30 00:29:22 +08:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}
2022-08-04 10:38:20 +08:00
func testVMessMux(t *testing.T, options option.OutboundMultiplexOptions) {
2022-08-04 10:38:20 +08:00
user, _ := uuid.NewV4()
startInstance(t, option.Options{
2024-11-02 00:39:02 +08:00
Inbounds: []option.LegacyInbound{
2022-08-04 10:38:20 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 20:46:22 +08:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-04 10:38:20 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 20:46:22 +08:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-04 10:38:20 +08:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
UUID: user.String(),
},
},
Multiplex: &option.InboundMultiplexOptions{
Enabled: true,
},
2022-08-04 10:38:20 +08:00
},
},
},
2024-11-02 00:39:02 +08:00
LegacyOutbounds: []option.LegacyOutbound{
2022-08-04 10:38:20 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
VMessOptions: option.VMessOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
2023-04-20 11:16:22 +08:00
Security: "auto",
UUID: user.String(),
Multiplex: &options,
2022-08-04 10:38:20 +08:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-08-04 10:38:20 +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-08-04 10:38:20 +08:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}