snell_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/metacubex/mihomo/adapter/outbound"
  8. C "github.com/metacubex/mihomo/constant"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestMihomo_SnellObfsHTTP(t *testing.T) {
  12. cfg := &container.Config{
  13. Image: ImageSnell,
  14. ExposedPorts: defaultExposedPorts,
  15. Cmd: []string{"-c", "/config.conf"},
  16. }
  17. hostCfg := &container.HostConfig{
  18. PortBindings: defaultPortBindings,
  19. Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell-http.conf"))},
  20. }
  21. id, err := startContainer(cfg, hostCfg, "snell-http")
  22. require.NoError(t, err)
  23. t.Cleanup(func() {
  24. cleanContainer(id)
  25. })
  26. proxy, err := outbound.NewSnell(outbound.SnellOption{
  27. Name: "snell",
  28. Server: localIP.String(),
  29. Port: 10002,
  30. Psk: "password",
  31. ObfsOpts: map[string]any{
  32. "mode": "http",
  33. },
  34. })
  35. require.NoError(t, err)
  36. time.Sleep(waitTime)
  37. testSuit(t, proxy)
  38. }
  39. func TestMihomo_SnellObfsTLS(t *testing.T) {
  40. cfg := &container.Config{
  41. Image: ImageSnell,
  42. ExposedPorts: defaultExposedPorts,
  43. Cmd: []string{"-c", "/config.conf"},
  44. }
  45. hostCfg := &container.HostConfig{
  46. PortBindings: defaultPortBindings,
  47. Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell-tls.conf"))},
  48. }
  49. id, err := startContainer(cfg, hostCfg, "snell-tls")
  50. require.NoError(t, err)
  51. t.Cleanup(func() {
  52. cleanContainer(id)
  53. })
  54. proxy, err := outbound.NewSnell(outbound.SnellOption{
  55. Name: "snell",
  56. Server: localIP.String(),
  57. Port: 10002,
  58. Psk: "password",
  59. ObfsOpts: map[string]any{
  60. "mode": "tls",
  61. },
  62. })
  63. require.NoError(t, err)
  64. time.Sleep(waitTime)
  65. testSuit(t, proxy)
  66. }
  67. func TestMihomo_Snell(t *testing.T) {
  68. cfg := &container.Config{
  69. Image: ImageSnell,
  70. ExposedPorts: defaultExposedPorts,
  71. Cmd: []string{"-c", "/config.conf"},
  72. }
  73. hostCfg := &container.HostConfig{
  74. PortBindings: defaultPortBindings,
  75. Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell.conf"))},
  76. }
  77. id, err := startContainer(cfg, hostCfg, "snell")
  78. require.NoError(t, err)
  79. t.Cleanup(func() {
  80. cleanContainer(id)
  81. })
  82. proxy, err := outbound.NewSnell(outbound.SnellOption{
  83. Name: "snell",
  84. Server: localIP.String(),
  85. Port: 10002,
  86. Psk: "password",
  87. })
  88. require.NoError(t, err)
  89. time.Sleep(waitTime)
  90. testSuit(t, proxy)
  91. }
  92. func TestMihomo_Snellv3(t *testing.T) {
  93. cfg := &container.Config{
  94. Image: ImageSnell,
  95. ExposedPorts: defaultExposedPorts,
  96. Cmd: []string{"-c", "/config.conf"},
  97. }
  98. hostCfg := &container.HostConfig{
  99. PortBindings: defaultPortBindings,
  100. Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell.conf"))},
  101. }
  102. id, err := startContainer(cfg, hostCfg, "snell")
  103. require.NoError(t, err)
  104. t.Cleanup(func() {
  105. cleanContainer(id)
  106. })
  107. proxy, err := outbound.NewSnell(outbound.SnellOption{
  108. Name: "snell",
  109. Server: localIP.String(),
  110. Port: 10002,
  111. Psk: "password",
  112. UDP: true,
  113. Version: 3,
  114. })
  115. require.NoError(t, err)
  116. time.Sleep(waitTime)
  117. testSuit(t, proxy)
  118. }
  119. func Benchmark_Snell(b *testing.B) {
  120. cfg := &container.Config{
  121. Image: ImageSnell,
  122. ExposedPorts: defaultExposedPorts,
  123. Cmd: []string{"-c", "/config.conf"},
  124. }
  125. hostCfg := &container.HostConfig{
  126. PortBindings: defaultPortBindings,
  127. Binds: []string{fmt.Sprintf("%s:/config.conf", C.Path.Resolve("snell-http.conf"))},
  128. }
  129. id, err := startContainer(cfg, hostCfg, "snell-bench")
  130. require.NoError(b, err)
  131. b.Cleanup(func() {
  132. cleanContainer(id)
  133. })
  134. proxy, err := outbound.NewSnell(outbound.SnellOption{
  135. Name: "snell",
  136. Server: localIP.String(),
  137. Port: 10002,
  138. Psk: "password",
  139. ObfsOpts: map[string]any{
  140. "mode": "http",
  141. },
  142. })
  143. require.NoError(b, err)
  144. time.Sleep(waitTime)
  145. benchmarkProxy(b, proxy)
  146. }