123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package main
- import (
- "crypto/rand"
- "encoding/base64"
- "net/netip"
- "testing"
- C "github.com/sagernet/sing-box/constant"
- "github.com/sagernet/sing-box/option"
- "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
- F "github.com/sagernet/sing/common/format"
- "github.com/stretchr/testify/require"
- )
- const (
- serverPort uint16 = 10000 + iota
- clientPort
- testPort
- otherPort
- )
- func TestShadowsocks(t *testing.T) {
- for _, method := range []string{
- "aes-128-gcm",
- "aes-256-gcm",
- "chacha20-ietf-poly1305",
- } {
- t.Run(method+"-inbound", func(t *testing.T) {
- testShadowsocksInboundWithShadowsocksRust(t, method, mkBase64(t, 16))
- })
- t.Run(method+"-outbound", func(t *testing.T) {
- testShadowsocksOutboundWithShadowsocksRust(t, method, mkBase64(t, 16))
- })
- t.Run(method+"-self", func(t *testing.T) {
- testShadowsocksSelf(t, method, mkBase64(t, 16))
- })
- }
- }
- func TestShadowsocks2022(t *testing.T) {
- for _, method16 := range []string{
- "2022-blake3-aes-128-gcm",
- } {
- t.Run(method16+"-inbound", func(t *testing.T) {
- testShadowsocksInboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
- })
- t.Run(method16+"-outbound", func(t *testing.T) {
- testShadowsocksOutboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
- })
- t.Run(method16+"-self", func(t *testing.T) {
- testShadowsocksSelf(t, method16, mkBase64(t, 16))
- })
- }
- for _, method32 := range []string{
- "2022-blake3-aes-256-gcm",
- "2022-blake3-chacha20-poly1305",
- } {
- t.Run(method32+"-inbound", func(t *testing.T) {
- testShadowsocksInboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
- })
- t.Run(method32+"-outbound", func(t *testing.T) {
- testShadowsocksOutboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
- })
- t.Run(method32+"-self", func(t *testing.T) {
- testShadowsocksSelf(t, method32, mkBase64(t, 32))
- })
- }
- }
- func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, password string) {
- startDockerContainer(t, DockerOptions{
- Image: ImageShadowsocksRustClient,
- EntryPoint: "sslocal",
- Ports: []uint16{serverPort, clientPort},
- Cmd: []string{"-s", F.ToString("127.0.0.1:", serverPort), "-b", F.ToString("0.0.0.0:", clientPort), "-m", method, "-k", password, "-U"},
- })
- startInstance(t, option.Options{
- Log: &option.LogOptions{
- Level: "error",
- },
- Inbounds: []option.Inbound{
- {
- Type: C.TypeShadowsocks,
- ShadowsocksOptions: option.ShadowsocksInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.ListenAddress(netip.IPv4Unspecified()),
- ListenPort: serverPort,
- },
- Method: method,
- Password: password,
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, password string) {
- startDockerContainer(t, DockerOptions{
- Image: ImageShadowsocksRustServer,
- EntryPoint: "ssserver",
- Ports: []uint16{serverPort, testPort},
- Cmd: []string{"-s", F.ToString("0.0.0.0:", serverPort), "-m", method, "-k", password, "-U"},
- })
- startInstance(t, option.Options{
- Log: &option.LogOptions{
- Level: "error",
- },
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.ListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeShadowsocks,
- ShadowsocksOptions: option.ShadowsocksOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- Method: method,
- Password: password,
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func testShadowsocksSelf(t *testing.T, method string, password string) {
- startInstance(t, option.Options{
- Log: &option.LogOptions{
- Level: "error",
- },
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.ListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeShadowsocks,
- ShadowsocksOptions: option.ShadowsocksInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.ListenAddress(netip.IPv4Unspecified()),
- ListenPort: serverPort,
- },
- Method: method,
- Password: password,
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- 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,
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- DefaultOptions: option.DefaultRule{
- Inbound: []string{"mixed-in"},
- Outbound: "ss-out",
- },
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func TestShadowsocksUoT(t *testing.T) {
- method := shadowaead_2022.List[0]
- password := mkBase64(t, 16)
- startInstance(t, option.Options{
- Log: &option.LogOptions{
- Level: "error",
- },
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.ListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeShadowsocks,
- ShadowsocksOptions: option.ShadowsocksInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.ListenAddress(netip.IPv4Unspecified()),
- ListenPort: serverPort,
- },
- Method: method,
- Password: password,
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- 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,
- UoT: true,
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- DefaultOptions: option.DefaultRule{
- Inbound: []string{"mixed-in"},
- Outbound: "ss-out",
- },
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func mkBase64(t *testing.T, length int) string {
- psk := make([]byte, length)
- _, err := rand.Read(psk)
- require.NoError(t, err)
- return base64.StdEncoding.EncodeToString(psk)
- }
|