2022-08-31 14:21:53 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-10-06 22:47:11 +08:00
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-08-31 14:21:53 +08:00
|
|
|
"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 18:59:19 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-08-31 14:21:53 +08:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
2024-11-18 18:59:19 +08:00
|
|
|
"github.com/sagernet/sing/common/json/badoption"
|
2022-10-06 22:47:11 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-31 14:21:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShadowTLS(t *testing.T) {
|
2022-10-06 22:47:11 +08:00
|
|
|
t.Run("v1", func(t *testing.T) {
|
2023-02-20 14:08:13 +08:00
|
|
|
testShadowTLS(t, 1, "", false)
|
2022-10-06 22:47:11 +08:00
|
|
|
})
|
|
|
|
t.Run("v2", func(t *testing.T) {
|
2023-02-20 14:08:13 +08:00
|
|
|
testShadowTLS(t, 2, "hello", false)
|
2023-02-18 14:55:47 +08:00
|
|
|
})
|
|
|
|
t.Run("v3", func(t *testing.T) {
|
2023-02-20 14:08:13 +08:00
|
|
|
testShadowTLS(t, 3, "hello", false)
|
|
|
|
})
|
|
|
|
t.Run("v2-utls", func(t *testing.T) {
|
|
|
|
testShadowTLS(t, 2, "hello", true)
|
|
|
|
})
|
|
|
|
t.Run("v3-utls", func(t *testing.T) {
|
|
|
|
testShadowTLS(t, 3, "hello", true)
|
2022-10-06 22:47:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-20 14:08:13 +08:00
|
|
|
func testShadowTLS(t *testing.T, version int, password string, utlsEanbled bool) {
|
2022-08-31 14:21:53 +08:00
|
|
|
method := shadowaead_2022.List[0]
|
2022-10-06 22:47:11 +08:00
|
|
|
ssPassword := mkBase64(t, 16)
|
2022-08-31 14:21:53 +08:00
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-08-31 14:21:53 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeMixed,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.HTTPMixedInboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenPort: clientPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowTLS,
|
|
|
|
Tag: "in",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowTLSInboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenPort: serverPort,
|
2024-11-18 18:59:19 +08:00
|
|
|
|
|
|
|
InboundOptions: option.InboundOptions{
|
|
|
|
Detour: "detour",
|
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
Handshake: option.ShadowTLSHandshakeOptions{
|
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "google.com",
|
|
|
|
ServerPort: 443,
|
|
|
|
},
|
|
|
|
},
|
2022-10-08 20:30:52 +08:00
|
|
|
Version: version,
|
2022-10-06 22:47:11 +08:00
|
|
|
Password: password,
|
2023-02-21 16:07:08 +08:00
|
|
|
Users: []option.ShadowTLSUser{{Password: password}},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowsocks,
|
|
|
|
Tag: "detour",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowsocksInboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenPort: otherPort,
|
|
|
|
},
|
|
|
|
Method: method,
|
2022-10-06 22:47:11 +08:00
|
|
|
Password: ssPassword,
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:59:19 +08:00
|
|
|
Outbounds: []option.Outbound{
|
2022-08-31 14:21:53 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeShadowsocks,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowsocksOutboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
Method: method,
|
2022-10-06 22:47:11 +08:00
|
|
|
Password: ssPassword,
|
2022-09-03 12:55:10 +08:00
|
|
|
DialerOptions: option.DialerOptions{
|
|
|
|
Detour: "detour",
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowTLS,
|
|
|
|
Tag: "detour",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowTLSOutboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "127.0.0.1",
|
|
|
|
ServerPort: serverPort,
|
|
|
|
},
|
2023-12-11 18:36:06 +08:00
|
|
|
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
|
|
|
|
TLS: &option.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
ServerName: "google.com",
|
|
|
|
UTLS: &option.OutboundUTLSOptions{
|
|
|
|
Enabled: utlsEanbled,
|
|
|
|
},
|
2023-02-20 14:08:13 +08:00
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
2022-10-08 20:30:52 +08:00
|
|
|
Version: version,
|
2022-10-06 22:47:11 +08:00
|
|
|
Password: password,
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeDirect,
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Route: &option.RouteOptions{
|
2024-10-21 23:38:34 +08:00
|
|
|
Rules: []option.Rule{
|
|
|
|
{
|
|
|
|
Type: C.RuleTypeDefault,
|
|
|
|
DefaultOptions: option.DefaultRule{
|
|
|
|
RawDefaultRule: option.RawDefaultRule{
|
|
|
|
Inbound: []string{"detour"},
|
|
|
|
},
|
|
|
|
RuleAction: option.RuleAction{
|
|
|
|
Action: C.RuleActionTypeRoute,
|
|
|
|
|
|
|
|
RouteOptions: option.RouteActionOptions{
|
|
|
|
Outbound: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
2024-10-21 23:38:34 +08:00
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
})
|
2023-02-20 13:53:06 +08:00
|
|
|
testTCP(t, clientPort, testPort)
|
2022-08-31 14:21:53 +08:00
|
|
|
}
|
|
|
|
|
2023-02-18 14:55:47 +08:00
|
|
|
func TestShadowTLSFallback(t *testing.T) {
|
2022-10-06 22:47:11 +08:00
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-10-06 22:47:11 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeShadowTLS,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowTLSInboundOptions{
|
2022-10-06 22:47:11 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-10-06 22:47:11 +08:00
|
|
|
ListenPort: serverPort,
|
|
|
|
},
|
|
|
|
Handshake: option.ShadowTLSHandshakeOptions{
|
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "google.com",
|
|
|
|
ServerPort: 443,
|
|
|
|
},
|
|
|
|
},
|
2023-07-11 14:03:55 +08:00
|
|
|
Version: 3,
|
|
|
|
Users: []option.ShadowTLSUser{
|
|
|
|
{Password: "hello"},
|
|
|
|
},
|
2022-10-06 22:47:11 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
var d net.Dialer
|
|
|
|
return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
response, err := client.Get("https://google.com")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, response.StatusCode, 200)
|
2022-10-31 13:54:24 +08:00
|
|
|
response.Body.Close()
|
2022-10-06 22:47:11 +08:00
|
|
|
client.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-10-10 11:31:03 +08:00
|
|
|
func TestShadowTLSInbound(t *testing.T) {
|
|
|
|
method := shadowaead_2022.List[0]
|
|
|
|
password := mkBase64(t, 16)
|
|
|
|
startDockerContainer(t, DockerOptions{
|
|
|
|
Image: ImageShadowTLS,
|
|
|
|
Ports: []uint16{serverPort, otherPort},
|
|
|
|
EntryPoint: "shadow-tls",
|
2023-02-18 14:55:47 +08:00
|
|
|
Cmd: []string{"--v3", "--threads", "1", "client", "--listen", "0.0.0.0:" + F.ToString(otherPort), "--server", "127.0.0.1:" + F.ToString(serverPort), "--sni", "google.com", "--password", password},
|
2022-10-10 11:31:03 +08:00
|
|
|
})
|
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-10-10 11:31:03 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeMixed,
|
|
|
|
Tag: "in",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.HTTPMixedInboundOptions{
|
2022-10-10 11:31:03 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-10-10 11:31:03 +08:00
|
|
|
ListenPort: clientPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowTLS,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowTLSInboundOptions{
|
2022-10-10 11:31:03 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-10-10 11:31:03 +08:00
|
|
|
ListenPort: serverPort,
|
2024-11-18 18:59:19 +08:00
|
|
|
InboundOptions: option.InboundOptions{
|
|
|
|
Detour: "detour",
|
|
|
|
},
|
2022-10-10 11:31:03 +08:00
|
|
|
},
|
|
|
|
Handshake: option.ShadowTLSHandshakeOptions{
|
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "google.com",
|
|
|
|
ServerPort: 443,
|
|
|
|
},
|
|
|
|
},
|
2023-07-11 14:03:55 +08:00
|
|
|
Version: 3,
|
|
|
|
Users: []option.ShadowTLSUser{
|
|
|
|
{Password: password},
|
|
|
|
},
|
2022-10-10 11:31:03 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowsocks,
|
|
|
|
Tag: "detour",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowsocksInboundOptions{
|
2022-10-10 11:31:03 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-10-10 11:31:03 +08:00
|
|
|
},
|
|
|
|
Method: method,
|
|
|
|
Password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:59:19 +08:00
|
|
|
Outbounds: []option.Outbound{
|
2022-10-10 11:31:03 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeDirect,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowsocks,
|
|
|
|
Tag: "out",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowsocksOutboundOptions{
|
2022-10-10 11:31:03 +08:00
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "127.0.0.1",
|
|
|
|
ServerPort: otherPort,
|
|
|
|
},
|
|
|
|
Method: method,
|
|
|
|
Password: password,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Route: &option.RouteOptions{
|
2024-10-21 23:38:34 +08:00
|
|
|
Rules: []option.Rule{
|
|
|
|
{
|
|
|
|
Type: C.RuleTypeDefault,
|
|
|
|
DefaultOptions: option.DefaultRule{
|
|
|
|
RawDefaultRule: option.RawDefaultRule{
|
|
|
|
Inbound: []string{"in"},
|
|
|
|
},
|
|
|
|
RuleAction: option.RuleAction{
|
|
|
|
Action: C.RuleActionTypeRoute,
|
|
|
|
|
|
|
|
RouteOptions: option.RouteActionOptions{
|
|
|
|
Outbound: "out",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-10 11:31:03 +08:00
|
|
|
},
|
2024-10-21 23:38:34 +08:00
|
|
|
},
|
2022-10-10 11:31:03 +08:00
|
|
|
},
|
|
|
|
})
|
2023-02-18 14:55:47 +08:00
|
|
|
testTCP(t, clientPort, testPort)
|
2022-10-10 11:31:03 +08:00
|
|
|
}
|
|
|
|
|
2022-08-31 14:21:53 +08:00
|
|
|
func TestShadowTLSOutbound(t *testing.T) {
|
2022-10-06 22:47:11 +08:00
|
|
|
method := shadowaead_2022.List[0]
|
|
|
|
password := mkBase64(t, 16)
|
2022-08-31 14:21:53 +08:00
|
|
|
startDockerContainer(t, DockerOptions{
|
|
|
|
Image: ImageShadowTLS,
|
|
|
|
Ports: []uint16{serverPort, otherPort},
|
|
|
|
EntryPoint: "shadow-tls",
|
2023-02-18 14:55:47 +08:00
|
|
|
Cmd: []string{"--v3", "--threads", "1", "server", "--listen", "0.0.0.0:" + F.ToString(serverPort), "--server", "127.0.0.1:" + F.ToString(otherPort), "--tls", "google.com:443", "--password", "hello"},
|
|
|
|
Env: []string{"RUST_LOG=trace"},
|
2022-08-31 14:21:53 +08:00
|
|
|
})
|
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-08-31 14:21:53 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeMixed,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.HTTPMixedInboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenPort: clientPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-10-06 22:47:11 +08:00
|
|
|
Type: C.TypeShadowsocks,
|
2022-08-31 14:21:53 +08:00
|
|
|
Tag: "detour",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowsocksInboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-08-31 14:21:53 +08:00
|
|
|
ListenPort: otherPort,
|
|
|
|
},
|
2022-10-06 22:47:11 +08:00
|
|
|
Method: method,
|
|
|
|
Password: password,
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:59:19 +08:00
|
|
|
Outbounds: []option.Outbound{
|
2022-08-31 14:21:53 +08:00
|
|
|
{
|
2022-10-06 22:47:11 +08:00
|
|
|
Type: C.TypeShadowsocks,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowsocksOutboundOptions{
|
2022-10-06 22:47:11 +08:00
|
|
|
Method: method,
|
|
|
|
Password: password,
|
2022-09-03 12:55:10 +08:00
|
|
|
DialerOptions: option.DialerOptions{
|
|
|
|
Detour: "detour",
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeShadowTLS,
|
|
|
|
Tag: "detour",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.ShadowTLSOutboundOptions{
|
2022-08-31 14:21:53 +08:00
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "127.0.0.1",
|
|
|
|
ServerPort: serverPort,
|
|
|
|
},
|
2023-12-11 18:36:06 +08:00
|
|
|
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
|
|
|
|
TLS: &option.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
ServerName: "google.com",
|
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
2023-02-18 14:55:47 +08:00
|
|
|
Version: 3,
|
2022-10-06 22:47:11 +08:00
|
|
|
Password: "hello",
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: C.TypeDirect,
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Route: &option.RouteOptions{
|
2024-10-21 23:38:34 +08:00
|
|
|
Rules: []option.Rule{
|
|
|
|
{
|
|
|
|
Type: C.RuleTypeDefault,
|
|
|
|
DefaultOptions: option.DefaultRule{
|
|
|
|
RawDefaultRule: option.RawDefaultRule{
|
|
|
|
Inbound: []string{"detour"},
|
|
|
|
},
|
|
|
|
RuleAction: option.RuleAction{
|
|
|
|
Action: C.RuleActionTypeRoute,
|
|
|
|
|
|
|
|
RouteOptions: option.RouteActionOptions{
|
|
|
|
Outbound: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
2024-10-21 23:38:34 +08:00
|
|
|
},
|
2022-08-31 14:21:53 +08:00
|
|
|
},
|
|
|
|
})
|
2023-02-18 14:55:47 +08:00
|
|
|
testTCP(t, clientPort, testPort)
|
2022-08-31 14:21:53 +08:00
|
|
|
}
|