trojan_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types/container"
  8. "github.com/metacubex/mihomo/adapter/outbound"
  9. C "github.com/metacubex/mihomo/constant"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestMihomo_Trojan(t *testing.T) {
  13. cfg := &container.Config{
  14. Image: ImageTrojan,
  15. ExposedPorts: defaultExposedPorts,
  16. }
  17. hostCfg := &container.HostConfig{
  18. PortBindings: defaultPortBindings,
  19. Binds: []string{
  20. fmt.Sprintf("%s:/config/config.json", C.Path.Resolve("trojan.json")),
  21. fmt.Sprintf("%s:/path/to/certificate.crt", C.Path.Resolve("example.org.pem")),
  22. fmt.Sprintf("%s:/path/to/private.key", C.Path.Resolve("example.org-key.pem")),
  23. },
  24. }
  25. id, err := startContainer(cfg, hostCfg, "trojan")
  26. require.NoError(t, err)
  27. t.Cleanup(func() {
  28. cleanContainer(id)
  29. })
  30. proxy, err := outbound.NewTrojan(outbound.TrojanOption{
  31. Name: "trojan",
  32. Server: localIP.String(),
  33. Port: 10002,
  34. Password: "password",
  35. SNI: "example.org",
  36. SkipCertVerify: true,
  37. UDP: true,
  38. })
  39. require.NoError(t, err)
  40. time.Sleep(waitTime)
  41. testSuit(t, proxy)
  42. }
  43. func TestMihomo_TrojanGrpc(t *testing.T) {
  44. cfg := &container.Config{
  45. Image: ImageXray,
  46. ExposedPorts: defaultExposedPorts,
  47. }
  48. hostCfg := &container.HostConfig{
  49. PortBindings: defaultPortBindings,
  50. Binds: []string{
  51. fmt.Sprintf("%s:/etc/xray/config.json", C.Path.Resolve("trojan-grpc.json")),
  52. fmt.Sprintf("%s:/etc/ssl/v2ray/fullchain.pem", C.Path.Resolve("example.org.pem")),
  53. fmt.Sprintf("%s:/etc/ssl/v2ray/privkey.pem", C.Path.Resolve("example.org-key.pem")),
  54. },
  55. }
  56. id, err := startContainer(cfg, hostCfg, "trojan-grpc")
  57. require.NoError(t, err)
  58. t.Cleanup(func() {
  59. cleanContainer(id)
  60. })
  61. proxy, err := outbound.NewTrojan(outbound.TrojanOption{
  62. Name: "trojan",
  63. Server: localIP.String(),
  64. Port: 10002,
  65. Password: "example",
  66. SNI: "example.org",
  67. SkipCertVerify: true,
  68. UDP: true,
  69. Network: "grpc",
  70. GrpcOpts: outbound.GrpcOptions{
  71. GrpcServiceName: "example",
  72. },
  73. })
  74. require.NoError(t, err)
  75. time.Sleep(waitTime)
  76. testSuit(t, proxy)
  77. }
  78. func TestMihomo_TrojanWebsocket(t *testing.T) {
  79. cfg := &container.Config{
  80. Image: ImageTrojanGo,
  81. ExposedPorts: defaultExposedPorts,
  82. }
  83. hostCfg := &container.HostConfig{
  84. PortBindings: defaultPortBindings,
  85. Binds: []string{
  86. fmt.Sprintf("%s:/etc/trojan-go/config.json", C.Path.Resolve("trojan-ws.json")),
  87. fmt.Sprintf("%s:/fullchain.pem", C.Path.Resolve("example.org.pem")),
  88. fmt.Sprintf("%s:/privkey.pem", C.Path.Resolve("example.org-key.pem")),
  89. },
  90. }
  91. id, err := startContainer(cfg, hostCfg, "trojan-ws")
  92. require.NoError(t, err)
  93. t.Cleanup(func() {
  94. cleanContainer(id)
  95. })
  96. proxy, err := outbound.NewTrojan(outbound.TrojanOption{
  97. Name: "trojan",
  98. Server: localIP.String(),
  99. Port: 10002,
  100. Password: "example",
  101. SNI: "example.org",
  102. SkipCertVerify: true,
  103. UDP: true,
  104. Network: "ws",
  105. })
  106. require.NoError(t, err)
  107. time.Sleep(waitTime)
  108. testSuit(t, proxy)
  109. }
  110. func TestMihomo_TrojanXTLS(t *testing.T) {
  111. cfg := &container.Config{
  112. Image: ImageXray,
  113. ExposedPorts: defaultExposedPorts,
  114. }
  115. hostCfg := &container.HostConfig{
  116. PortBindings: defaultPortBindings,
  117. Binds: []string{
  118. fmt.Sprintf("%s:/etc/xray/config.json", C.Path.Resolve("trojan-xtls.json")),
  119. fmt.Sprintf("%s:/etc/ssl/v2ray/fullchain.pem", C.Path.Resolve("example.org.pem")),
  120. fmt.Sprintf("%s:/etc/ssl/v2ray/privkey.pem", C.Path.Resolve("example.org-key.pem")),
  121. },
  122. }
  123. id, err := startContainer(cfg, hostCfg, "trojan-xtls")
  124. if err != nil {
  125. require.NoError(t, err)
  126. }
  127. defer cleanContainer(id)
  128. proxy, err := outbound.NewTrojan(outbound.TrojanOption{
  129. Name: "trojan",
  130. Server: localIP.String(),
  131. Port: 10002,
  132. Password: "example",
  133. SNI: "example.org",
  134. SkipCertVerify: true,
  135. UDP: true,
  136. Network: "tcp",
  137. Flow: "xtls-rprx-direct",
  138. FlowShow: true,
  139. })
  140. if err != nil {
  141. require.NoError(t, err)
  142. }
  143. time.Sleep(waitTime)
  144. testSuit(t, proxy)
  145. }
  146. func Benchmark_Trojan(b *testing.B) {
  147. cfg := &container.Config{
  148. Image: ImageTrojan,
  149. ExposedPorts: defaultExposedPorts,
  150. }
  151. hostCfg := &container.HostConfig{
  152. PortBindings: defaultPortBindings,
  153. Binds: []string{
  154. fmt.Sprintf("%s:/config/config.json", C.Path.Resolve("trojan.json")),
  155. fmt.Sprintf("%s:/path/to/certificate.crt", C.Path.Resolve("example.org.pem")),
  156. fmt.Sprintf("%s:/path/to/private.key", C.Path.Resolve("example.org-key.pem")),
  157. },
  158. }
  159. id, err := startContainer(cfg, hostCfg, "trojan-bench")
  160. require.NoError(b, err)
  161. b.Cleanup(func() {
  162. cleanContainer(id)
  163. })
  164. proxy, err := outbound.NewTrojan(outbound.TrojanOption{
  165. Name: "trojan",
  166. Server: localIP.String(),
  167. Port: 10002,
  168. Password: "password",
  169. SNI: "example.org",
  170. SkipCertVerify: true,
  171. UDP: true,
  172. })
  173. require.NoError(b, err)
  174. require.True(b, TCPing(net.JoinHostPort(localIP.String(), "10002")))
  175. benchmarkProxy(b, proxy)
  176. }