mihomo/test/ss_test.go

316 lines
7.1 KiB
Go
Raw Permalink Normal View History

2021-05-17 20:33:00 +08:00
package main
import (
2022-06-09 18:10:00 +08:00
"crypto/rand"
"encoding/base64"
2022-06-18 10:50:18 +08:00
"fmt"
2022-05-21 17:37:06 +08:00
"net"
2021-05-17 20:33:00 +08:00
"testing"
"time"
"github.com/docker/docker/api/types/container"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/adapter/outbound"
C "github.com/metacubex/mihomo/constant"
2022-05-21 17:37:06 +08:00
"github.com/stretchr/testify/require"
2021-05-17 20:33:00 +08:00
)
2023-11-03 21:01:45 +08:00
func TestMihomo_Shadowsocks(t *testing.T) {
2022-06-09 18:10:00 +08:00
for _, method := range []string{
"aes-128-ctr",
"aes-192-ctr",
"aes-256-ctr",
"aes-128-cfb",
"aes-192-cfb",
"aes-256-cfb",
"rc4-md5",
"chacha20-ietf",
"aes-128-gcm",
"aes-256-gcm",
"chacha20-ietf-poly1305",
"xchacha20-ietf-poly1305",
} {
t.Run(method, func(t *testing.T) {
2023-11-03 21:01:45 +08:00
testMihomo_Shadowsocks(t, method, "FzcLbKs2dY9mhL")
2022-06-09 18:10:00 +08:00
})
}
for _, method := range []string{
"aes-128-gcm",
"aes-256-gcm",
"chacha20-ietf-poly1305",
} {
t.Run(method, func(t *testing.T) {
2023-11-03 21:01:45 +08:00
testMihomo_ShadowsocksRust(t, method, "FzcLbKs2dY9mhL")
2022-06-09 18:10:00 +08:00
})
}
}
2023-11-03 21:01:45 +08:00
func TestMihomo_Shadowsocks2022(t *testing.T) {
2022-06-09 18:10:00 +08:00
for _, method := range []string{
"2022-blake3-aes-128-gcm",
} {
t.Run(method, func(t *testing.T) {
2023-11-03 21:01:45 +08:00
testMihomo_ShadowsocksRust(t, method, mkKey(16))
2022-06-09 18:10:00 +08:00
})
}
for _, method := range []string{
"2022-blake3-aes-256-gcm",
"2022-blake3-chacha20-poly1305",
} {
t.Run(method, func(t *testing.T) {
2023-11-03 21:01:45 +08:00
testMihomo_ShadowsocksRust(t, method, mkKey(32))
2022-06-09 18:10:00 +08:00
})
}
}
func mkKey(bits int) string {
k := make([]byte, bits)
rand.Read(k)
return base64.StdEncoding.EncodeToString(k)
}
2023-11-03 21:01:45 +08:00
func testMihomo_Shadowsocks(t *testing.T, method string, password string) {
2022-06-09 18:10:00 +08:00
cfg := &container.Config{
Image: ImageShadowsocks,
Env: []string{
"SS_MODULE=ss-server",
"SS_CONFIG=-s 0.0.0.0 -u -p 10002 -m " + method + " -k " + password,
},
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
id, err := startContainer(cfg, hostCfg, "ss")
require.NoError(t, err)
t.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: password,
Cipher: method,
UDP: true,
})
require.NoError(t, err)
time.Sleep(waitTime)
testSuit(t, proxy)
}
2023-11-03 21:01:45 +08:00
func testMihomo_ShadowsocksRust(t *testing.T, method string, password string) {
2021-05-17 20:33:00 +08:00
cfg := &container.Config{
Image: ImageShadowsocksRust,
Entrypoint: []string{"ssserver"},
2022-06-09 18:10:00 +08:00
Cmd: []string{"-s", "0.0.0.0:10002", "-m", method, "-k", password, "-U", "-v"},
2021-05-17 20:33:00 +08:00
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
2022-06-09 18:10:00 +08:00
id, err := startContainer(cfg, hostCfg, "ss-rust")
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
t.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
2022-06-09 18:10:00 +08:00
Password: password,
Cipher: method,
2021-05-17 20:33:00 +08:00
UDP: true,
})
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
time.Sleep(waitTime)
testSuit(t, proxy)
}
2023-11-03 21:01:45 +08:00
func TestMihomo_ShadowsocksObfsHTTP(t *testing.T) {
2021-05-17 20:33:00 +08:00
cfg := &container.Config{
Image: ImageShadowsocks,
Env: []string{
"SS_MODULE=ss-server",
"SS_CONFIG=-s 0.0.0.0 -u -p 10002 -m chacha20-ietf-poly1305 -k FzcLbKs2dY9mhL --plugin obfs-server --plugin-opts obfs=http",
},
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
id, err := startContainer(cfg, hostCfg, "ss-obfs-http")
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
t.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: "FzcLbKs2dY9mhL",
Cipher: "chacha20-ietf-poly1305",
UDP: true,
Plugin: "obfs",
2022-03-16 12:10:13 +08:00
PluginOpts: map[string]any{
2021-05-17 20:33:00 +08:00
"mode": "http",
},
})
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
time.Sleep(waitTime)
testSuit(t, proxy)
}
2023-11-03 21:01:45 +08:00
func TestMihomo_ShadowsocksObfsTLS(t *testing.T) {
2021-05-17 20:33:00 +08:00
cfg := &container.Config{
Image: ImageShadowsocks,
Env: []string{
"SS_MODULE=ss-server",
"SS_CONFIG=-s 0.0.0.0 -u -p 10002 -m chacha20-ietf-poly1305 -k FzcLbKs2dY9mhL --plugin obfs-server --plugin-opts obfs=tls",
},
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
id, err := startContainer(cfg, hostCfg, "ss-obfs-tls")
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
t.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: "FzcLbKs2dY9mhL",
Cipher: "chacha20-ietf-poly1305",
UDP: true,
Plugin: "obfs",
2022-03-16 12:10:13 +08:00
PluginOpts: map[string]any{
2021-05-17 20:33:00 +08:00
"mode": "tls",
},
})
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
time.Sleep(waitTime)
testSuit(t, proxy)
}
2023-11-03 21:01:45 +08:00
func TestMihomo_ShadowsocksV2RayPlugin(t *testing.T) {
2021-05-17 20:33:00 +08:00
cfg := &container.Config{
Image: ImageShadowsocks,
Env: []string{
"SS_MODULE=ss-server",
"SS_CONFIG=-s 0.0.0.0 -u -p 10002 -m chacha20-ietf-poly1305 -k FzcLbKs2dY9mhL --plugin v2ray-plugin --plugin-opts=server",
},
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
id, err := startContainer(cfg, hostCfg, "ss-v2ray-plugin")
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
t.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: "FzcLbKs2dY9mhL",
Cipher: "chacha20-ietf-poly1305",
UDP: true,
Plugin: "v2ray-plugin",
2022-03-16 12:10:13 +08:00
PluginOpts: map[string]any{
2021-05-17 20:33:00 +08:00
"mode": "websocket",
},
})
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
time.Sleep(waitTime)
testSuit(t, proxy)
}
2021-07-18 17:23:22 +08:00
func Benchmark_Shadowsocks(b *testing.B) {
cfg := &container.Config{
Image: ImageShadowsocksRust,
Entrypoint: []string{"ssserver"},
Cmd: []string{"-s", "0.0.0.0:10002", "-m", "aes-256-gcm", "-k", "FzcLbKs2dY9mhL", "-U"},
2021-07-18 17:23:22 +08:00
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
}
2022-05-23 12:27:34 +08:00
id, err := startContainer(cfg, hostCfg, "ss-bench")
2022-05-21 17:37:06 +08:00
require.NoError(b, err)
2021-07-18 17:23:22 +08:00
b.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: "FzcLbKs2dY9mhL",
Cipher: "aes-256-gcm",
2021-07-18 17:23:22 +08:00
UDP: true,
})
2022-05-21 17:37:06 +08:00
require.NoError(b, err)
2021-07-18 17:23:22 +08:00
2022-05-21 17:37:06 +08:00
require.True(b, TCPing(net.JoinHostPort(localIP.String(), "10002")))
2021-07-18 17:23:22 +08:00
benchmarkProxy(b, proxy)
}
2022-06-18 10:50:18 +08:00
2023-11-03 21:01:45 +08:00
func TestMihomo_ShadowsocksUoT(t *testing.T) {
2022-06-18 10:50:18 +08:00
configPath := C.Path.Resolve("xray-shadowsocks.json")
cfg := &container.Config{
Image: ImageVless,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{fmt.Sprintf("%s:/etc/xray/config.json", configPath)},
}
id, err := startContainer(cfg, hostCfg, "xray-ss")
require.NoError(t, err)
t.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewShadowSocks(outbound.ShadowSocksOption{
Name: "ss",
Server: localIP.String(),
Port: 10002,
Password: "FzcLbKs2dY9mhL",
Cipher: "aes-128-gcm",
UDP: true,
UDPOverTCP: true,
})
require.NoError(t, err)
time.Sleep(waitTime)
testSuit(t, proxy)
}