Clash.Meta/test/trojan_test.go

201 lines
5.1 KiB
Go
Raw Normal View History

2021-05-17 20:33:00 +08:00
package main
import (
"fmt"
2022-05-21 17:37:06 +08:00
"net"
2021-05-17 20:33:00 +08:00
"testing"
"time"
2022-06-07 10:45:32 +08:00
"github.com/Dreamacro/clash/adapter/outbound"
C "github.com/Dreamacro/clash/constant"
2021-05-17 20:33:00 +08:00
"github.com/docker/docker/api/types/container"
2022-05-21 17:37:06 +08:00
"github.com/stretchr/testify/require"
2021-05-17 20:33:00 +08:00
)
func TestClash_Trojan(t *testing.T) {
cfg := &container.Config{
Image: ImageTrojan,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{
fmt.Sprintf("%s:/config/config.json", C.Path.Resolve("trojan.json")),
fmt.Sprintf("%s:/path/to/certificate.crt", C.Path.Resolve("example.org.pem")),
fmt.Sprintf("%s:/path/to/private.key", C.Path.Resolve("example.org-key.pem")),
},
}
id, err := startContainer(cfg, hostCfg, "trojan")
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.NewTrojan(outbound.TrojanOption{
Name: "trojan",
Server: localIP.String(),
Port: 10002,
Password: "password",
SNI: "example.org",
SkipCertVerify: true,
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)
}
func TestClash_TrojanGrpc(t *testing.T) {
cfg := &container.Config{
Image: ImageXray,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{
fmt.Sprintf("%s:/etc/xray/config.json", C.Path.Resolve("trojan-grpc.json")),
fmt.Sprintf("%s:/etc/ssl/v2ray/fullchain.pem", C.Path.Resolve("example.org.pem")),
fmt.Sprintf("%s:/etc/ssl/v2ray/privkey.pem", C.Path.Resolve("example.org-key.pem")),
},
}
id, err := startContainer(cfg, hostCfg, "trojan-grpc")
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
t.Cleanup(func() {
cleanContainer(id)
})
2021-05-17 20:33:00 +08:00
proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan",
Server: localIP.String(),
Port: 10002,
Password: "example",
SNI: "example.org",
SkipCertVerify: true,
UDP: true,
Network: "grpc",
GrpcOpts: outbound.GrpcOptions{
GrpcServiceName: "example",
},
})
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-05-17 20:33:00 +08:00
time.Sleep(waitTime)
2021-10-16 20:19:59 +08:00
testSuit(t, proxy)
}
func TestClash_TrojanWebsocket(t *testing.T) {
cfg := &container.Config{
Image: ImageTrojanGo,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{
fmt.Sprintf("%s:/etc/trojan-go/config.json", C.Path.Resolve("trojan-ws.json")),
fmt.Sprintf("%s:/fullchain.pem", C.Path.Resolve("example.org.pem")),
fmt.Sprintf("%s:/privkey.pem", C.Path.Resolve("example.org-key.pem")),
},
}
id, err := startContainer(cfg, hostCfg, "trojan-ws")
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
t.Cleanup(func() {
cleanContainer(id)
})
2021-10-16 20:19:59 +08:00
proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan",
Server: localIP.String(),
Port: 10002,
Password: "example",
SNI: "example.org",
SkipCertVerify: true,
UDP: true,
Network: "ws",
})
2022-05-21 17:37:06 +08:00
require.NoError(t, err)
2021-10-16 20:19:59 +08:00
time.Sleep(waitTime)
2022-03-30 00:15:39 +08:00
testSuit(t, proxy)
}
func TestClash_TrojanXTLS(t *testing.T) {
cfg := &container.Config{
Image: ImageXray,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{
fmt.Sprintf("%s:/etc/xray/config.json", C.Path.Resolve("trojan-xtls.json")),
fmt.Sprintf("%s:/etc/ssl/v2ray/fullchain.pem", C.Path.Resolve("example.org.pem")),
fmt.Sprintf("%s:/etc/ssl/v2ray/privkey.pem", C.Path.Resolve("example.org-key.pem")),
},
}
id, err := startContainer(cfg, hostCfg, "trojan-xtls")
if err != nil {
2022-06-09 18:00:41 +08:00
require.NoError(t, err)
2022-03-30 00:15:39 +08:00
}
defer cleanContainer(id)
proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan",
Server: localIP.String(),
Port: 10002,
Password: "example",
SNI: "example.org",
SkipCertVerify: true,
UDP: true,
Network: "tcp",
Flow: "xtls-rprx-direct",
FlowShow: true,
})
if err != nil {
2022-06-09 18:00:41 +08:00
require.NoError(t, err)
2022-03-30 00:15:39 +08:00
}
time.Sleep(waitTime)
2021-05-17 20:33:00 +08:00
testSuit(t, proxy)
}
2021-07-18 17:23:22 +08:00
func Benchmark_Trojan(b *testing.B) {
cfg := &container.Config{
Image: ImageTrojan,
ExposedPorts: defaultExposedPorts,
}
hostCfg := &container.HostConfig{
PortBindings: defaultPortBindings,
Binds: []string{
fmt.Sprintf("%s:/config/config.json", C.Path.Resolve("trojan.json")),
fmt.Sprintf("%s:/path/to/certificate.crt", C.Path.Resolve("example.org.pem")),
fmt.Sprintf("%s:/path/to/private.key", C.Path.Resolve("example.org-key.pem")),
},
}
2022-05-21 17:37:06 +08:00
id, err := startContainer(cfg, hostCfg, "trojan-bench")
require.NoError(b, err)
2021-07-18 17:23:22 +08:00
b.Cleanup(func() {
cleanContainer(id)
})
proxy, err := outbound.NewTrojan(outbound.TrojanOption{
Name: "trojan",
Server: localIP.String(),
Port: 10002,
Password: "password",
SNI: "example.org",
SkipCertVerify: true,
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)
}