trojan_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.NewListenAddress(netip.IPv4Unspecified()),
  26. ListenPort: clientPort,
  27. },
  28. },
  29. },
  30. },
  31. LegacyOutbounds: []option.LegacyOutbound{
  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. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  41. TLS: &option.OutboundTLSOptions{
  42. Enabled: true,
  43. ServerName: "example.org",
  44. CertificatePath: certPem,
  45. },
  46. },
  47. },
  48. },
  49. },
  50. })
  51. testSuit(t, clientPort, testPort)
  52. }
  53. func TestTrojanSelf(t *testing.T) {
  54. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  55. startInstance(t, option.Options{
  56. Inbounds: []option.Inbound{
  57. {
  58. Type: C.TypeMixed,
  59. Tag: "mixed-in",
  60. MixedOptions: option.HTTPMixedInboundOptions{
  61. ListenOptions: option.ListenOptions{
  62. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  63. ListenPort: clientPort,
  64. },
  65. },
  66. },
  67. {
  68. Type: C.TypeTrojan,
  69. TrojanOptions: option.TrojanInboundOptions{
  70. ListenOptions: option.ListenOptions{
  71. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  72. ListenPort: serverPort,
  73. },
  74. Users: []option.TrojanUser{
  75. {
  76. Name: "sekai",
  77. Password: "password",
  78. },
  79. },
  80. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  81. TLS: &option.InboundTLSOptions{
  82. Enabled: true,
  83. ServerName: "example.org",
  84. CertificatePath: certPem,
  85. KeyPath: keyPem,
  86. },
  87. },
  88. },
  89. },
  90. },
  91. LegacyOutbounds: []option.LegacyOutbound{
  92. {
  93. Type: C.TypeDirect,
  94. },
  95. {
  96. Type: C.TypeTrojan,
  97. Tag: "trojan-out",
  98. TrojanOptions: option.TrojanOutboundOptions{
  99. ServerOptions: option.ServerOptions{
  100. Server: "127.0.0.1",
  101. ServerPort: serverPort,
  102. },
  103. Password: "password",
  104. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  105. TLS: &option.OutboundTLSOptions{
  106. Enabled: true,
  107. ServerName: "example.org",
  108. CertificatePath: certPem,
  109. },
  110. },
  111. },
  112. },
  113. },
  114. Route: &option.RouteOptions{
  115. Rules: []option.Rule{
  116. {
  117. Type: C.RuleTypeDefault,
  118. DefaultOptions: option.DefaultRule{
  119. RawDefaultRule: option.RawDefaultRule{
  120. Inbound: []string{"mixed-in"},
  121. },
  122. RuleAction: option.RuleAction{
  123. Action: C.RuleActionTypeRoute,
  124. RouteOptions: option.RouteActionOptions{
  125. Outbound: "trojan-out",
  126. },
  127. },
  128. },
  129. },
  130. },
  131. },
  132. })
  133. testSuit(t, clientPort, testPort)
  134. }
  135. func TestTrojanPlainSelf(t *testing.T) {
  136. startInstance(t, option.Options{
  137. Inbounds: []option.Inbound{
  138. {
  139. Type: C.TypeMixed,
  140. Tag: "mixed-in",
  141. MixedOptions: option.HTTPMixedInboundOptions{
  142. ListenOptions: option.ListenOptions{
  143. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  144. ListenPort: clientPort,
  145. },
  146. },
  147. },
  148. {
  149. Type: C.TypeTrojan,
  150. TrojanOptions: option.TrojanInboundOptions{
  151. ListenOptions: option.ListenOptions{
  152. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  153. ListenPort: serverPort,
  154. },
  155. Users: []option.TrojanUser{
  156. {
  157. Name: "sekai",
  158. Password: "password",
  159. },
  160. },
  161. },
  162. },
  163. },
  164. LegacyOutbounds: []option.LegacyOutbound{
  165. {
  166. Type: C.TypeDirect,
  167. },
  168. {
  169. Type: C.TypeTrojan,
  170. Tag: "trojan-out",
  171. TrojanOptions: option.TrojanOutboundOptions{
  172. ServerOptions: option.ServerOptions{
  173. Server: "127.0.0.1",
  174. ServerPort: serverPort,
  175. },
  176. Password: "password",
  177. },
  178. },
  179. },
  180. Route: &option.RouteOptions{
  181. Rules: []option.Rule{
  182. {
  183. Type: C.RuleTypeDefault,
  184. DefaultOptions: option.DefaultRule{
  185. RawDefaultRule: option.RawDefaultRule{
  186. Inbound: []string{"mixed-in"},
  187. },
  188. RuleAction: option.RuleAction{
  189. Action: C.RuleActionTypeRoute,
  190. RouteOptions: option.RouteActionOptions{
  191. Outbound: "trojan-out",
  192. },
  193. },
  194. },
  195. },
  196. },
  197. },
  198. })
  199. testSuit(t, clientPort, testPort)
  200. }