123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- package main
- import (
- "fmt"
- "testing"
- "time"
- "github.com/docker/docker/api/types/container"
- "github.com/metacubex/mihomo/adapter/outbound"
- C "github.com/metacubex/mihomo/constant"
- "github.com/stretchr/testify/require"
- )
- func TestMihomo_Vmess(t *testing.T) {
- configPath := C.Path.Resolve("vmess.json")
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)},
- }
- id, err := startContainer(cfg, hostCfg, "vmess")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- UDP: true,
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessAuthenticatedLength(t *testing.T) {
- configPath := C.Path.Resolve("vmess.json")
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)},
- }
- id, err := startContainer(cfg, hostCfg, "vmess")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- UDP: true,
- AuthenticatedLength: true,
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessPacketAddr(t *testing.T) {
- configPath := C.Path.Resolve("vmess.json")
- cfg := &container.Config{
- Image: ImageVmessLatest,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)},
- }
- id, err := startContainer(cfg, hostCfg, "vmess")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- UDP: true,
- PacketAddr: true,
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessTLS(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-tls.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, "vmess-tls")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- TLS: true,
- SkipCertVerify: true,
- ServerName: "example.org",
- UDP: true,
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessHTTP2(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-http2.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, "vmess-http2")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "h2",
- TLS: true,
- SkipCertVerify: true,
- ServerName: "example.org",
- UDP: true,
- HTTP2Opts: outbound.HTTP2Options{
- Host: []string{"example.org"},
- Path: "/test",
- },
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessHTTP(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-http.json")),
- },
- }
- id, err := startContainer(cfg, hostCfg, "vmess-http")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "http",
- UDP: true,
- HTTPOpts: outbound.HTTPOptions{
- Method: "GET",
- Path: []string{"/"},
- Headers: map[string][]string{
- "Host": {"www.amazon.com"},
- "User-Agent": {
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.49",
- },
- "Accept-Encoding": {
- "gzip, deflate",
- },
- "Connection": {
- "keep-alive",
- },
- "Pragma": {"no-cache"},
- },
- },
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessWebsocket(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-ws.json")),
- },
- }
- id, err := startContainer(cfg, hostCfg, "vmess-ws")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "ws",
- UDP: true,
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessWebsocketTLS(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-ws-tls.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, "vmess-ws")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "ws",
- TLS: true,
- SkipCertVerify: true,
- UDP: true,
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessGrpc(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-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, "vmess-grpc")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "grpc",
- TLS: true,
- SkipCertVerify: true,
- UDP: true,
- ServerName: "example.org",
- GrpcOpts: outbound.GrpcOptions{
- GrpcServiceName: "example!",
- },
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessWebsocket0RTT(t *testing.T) {
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{
- fmt.Sprintf("%s:/etc/v2ray/config.json", C.Path.Resolve("vmess-ws-0rtt.json")),
- },
- }
- id, err := startContainer(cfg, hostCfg, "vmess-ws-0rtt")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "ws",
- UDP: true,
- ServerName: "example.org",
- WSOpts: outbound.WSOptions{
- MaxEarlyData: 2048,
- EarlyDataHeaderName: "Sec-WebSocket-Protocol",
- },
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func TestMihomo_VmessWebsocketXray0RTT(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("vmess-ws-0rtt.json")),
- },
- }
- id, err := startContainer(cfg, hostCfg, "vmess-xray-ws-0rtt")
- require.NoError(t, err)
- t.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- Network: "ws",
- UDP: true,
- ServerName: "example.org",
- WSOpts: outbound.WSOptions{
- Path: "/?ed=2048",
- },
- })
- require.NoError(t, err)
- time.Sleep(waitTime)
- testSuit(t, proxy)
- }
- func Benchmark_Vmess(b *testing.B) {
- configPath := C.Path.Resolve("vmess.json")
- cfg := &container.Config{
- Image: ImageVmess,
- ExposedPorts: defaultExposedPorts,
- }
- hostCfg := &container.HostConfig{
- PortBindings: defaultPortBindings,
- Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)},
- }
- id, err := startContainer(cfg, hostCfg, "vmess-bench")
- require.NoError(b, err)
- b.Cleanup(func() {
- cleanContainer(id)
- })
- proxy, err := outbound.NewVmess(outbound.VmessOption{
- Name: "vmess",
- Server: localIP.String(),
- Port: 10002,
- UUID: "b831381d-6324-4d53-ad4f-8cda48b30811",
- Cipher: "auto",
- AlterID: 0,
- UDP: true,
- })
- require.NoError(b, err)
- time.Sleep(waitTime)
- benchmarkProxy(b, proxy)
- }
|