trojan_test.go 4.9 KB

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