trojan_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package main
  2. import (
  3. "net/netip"
  4. "testing"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/option"
  7. )
  8. func TestTrojanOutbound(t *testing.T) {
  9. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  10. startDockerContainer(t, DockerOptions{
  11. Image: ImageTrojan,
  12. Ports: []uint16{serverPort, testPort},
  13. Bind: map[string]string{
  14. "trojan.json": "/config/config.json",
  15. certPem: "/path/to/certificate.crt",
  16. keyPem: "/path/to/private.key",
  17. },
  18. })
  19. startInstance(t, option.Options{
  20. Inbounds: []option.Inbound{
  21. {
  22. Type: C.TypeMixed,
  23. MixedOptions: option.HTTPMixedInboundOptions{
  24. ListenOptions: option.ListenOptions{
  25. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  26. ListenPort: clientPort,
  27. },
  28. },
  29. },
  30. },
  31. Outbounds: []option.Outbound{
  32. {
  33. Type: C.TypeTrojan,
  34. TrojanOptions: option.TrojanOutboundOptions{
  35. ServerOptions: option.ServerOptions{
  36. Server: "127.0.0.1",
  37. ServerPort: serverPort,
  38. },
  39. Password: "password",
  40. TLS: &option.OutboundTLSOptions{
  41. Enabled: true,
  42. ServerName: "example.org",
  43. CertificatePath: certPem,
  44. },
  45. },
  46. },
  47. },
  48. })
  49. testSuit(t, clientPort, testPort)
  50. }
  51. func TestTrojanSelf(t *testing.T) {
  52. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  53. startInstance(t, option.Options{
  54. Inbounds: []option.Inbound{
  55. {
  56. Type: C.TypeMixed,
  57. Tag: "mixed-in",
  58. MixedOptions: option.HTTPMixedInboundOptions{
  59. ListenOptions: option.ListenOptions{
  60. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  61. ListenPort: clientPort,
  62. },
  63. },
  64. },
  65. {
  66. Type: C.TypeTrojan,
  67. TrojanOptions: option.TrojanInboundOptions{
  68. ListenOptions: option.ListenOptions{
  69. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  70. ListenPort: serverPort,
  71. },
  72. Users: []option.TrojanUser{
  73. {
  74. Name: "sekai",
  75. Password: "password",
  76. },
  77. },
  78. TLS: &option.InboundTLSOptions{
  79. Enabled: true,
  80. ServerName: "example.org",
  81. CertificatePath: certPem,
  82. KeyPath: keyPem,
  83. },
  84. },
  85. },
  86. },
  87. Outbounds: []option.Outbound{
  88. {
  89. Type: C.TypeDirect,
  90. },
  91. {
  92. Type: C.TypeTrojan,
  93. Tag: "trojan-out",
  94. TrojanOptions: option.TrojanOutboundOptions{
  95. ServerOptions: option.ServerOptions{
  96. Server: "127.0.0.1",
  97. ServerPort: serverPort,
  98. },
  99. Password: "password",
  100. TLS: &option.OutboundTLSOptions{
  101. Enabled: true,
  102. ServerName: "example.org",
  103. CertificatePath: certPem,
  104. },
  105. },
  106. },
  107. },
  108. Route: &option.RouteOptions{
  109. Rules: []option.Rule{
  110. {
  111. DefaultOptions: option.DefaultRule{
  112. Inbound: []string{"mixed-in"},
  113. Outbound: "trojan-out",
  114. },
  115. },
  116. },
  117. },
  118. })
  119. testSuit(t, clientPort, testPort)
  120. }
  121. func TestTrojanPlainSelf(t *testing.T) {
  122. startInstance(t, option.Options{
  123. Inbounds: []option.Inbound{
  124. {
  125. Type: C.TypeMixed,
  126. Tag: "mixed-in",
  127. MixedOptions: option.HTTPMixedInboundOptions{
  128. ListenOptions: option.ListenOptions{
  129. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  130. ListenPort: clientPort,
  131. },
  132. },
  133. },
  134. {
  135. Type: C.TypeTrojan,
  136. TrojanOptions: option.TrojanInboundOptions{
  137. ListenOptions: option.ListenOptions{
  138. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  139. ListenPort: serverPort,
  140. },
  141. Users: []option.TrojanUser{
  142. {
  143. Name: "sekai",
  144. Password: "password",
  145. },
  146. },
  147. },
  148. },
  149. },
  150. Outbounds: []option.Outbound{
  151. {
  152. Type: C.TypeDirect,
  153. },
  154. {
  155. Type: C.TypeTrojan,
  156. Tag: "trojan-out",
  157. TrojanOptions: option.TrojanOutboundOptions{
  158. ServerOptions: option.ServerOptions{
  159. Server: "127.0.0.1",
  160. ServerPort: serverPort,
  161. },
  162. Password: "password",
  163. },
  164. },
  165. },
  166. Route: &option.RouteOptions{
  167. Rules: []option.Rule{
  168. {
  169. DefaultOptions: option.DefaultRule{
  170. Inbound: []string{"mixed-in"},
  171. Outbound: "trojan-out",
  172. },
  173. },
  174. },
  175. },
  176. })
  177. testSuit(t, clientPort, testPort)
  178. }