trojan_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. 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. 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. Outbounds: []option.Outbound{
  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. DefaultOptions: option.DefaultRule{
  118. Inbound: []string{"mixed-in"},
  119. Outbound: "trojan-out",
  120. },
  121. },
  122. },
  123. },
  124. })
  125. testSuit(t, clientPort, testPort)
  126. }
  127. func TestTrojanPlainSelf(t *testing.T) {
  128. startInstance(t, option.Options{
  129. Inbounds: []option.Inbound{
  130. {
  131. Type: C.TypeMixed,
  132. Tag: "mixed-in",
  133. MixedOptions: option.HTTPMixedInboundOptions{
  134. ListenOptions: option.ListenOptions{
  135. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  136. ListenPort: clientPort,
  137. },
  138. },
  139. },
  140. {
  141. Type: C.TypeTrojan,
  142. TrojanOptions: option.TrojanInboundOptions{
  143. ListenOptions: option.ListenOptions{
  144. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  145. ListenPort: serverPort,
  146. },
  147. Users: []option.TrojanUser{
  148. {
  149. Name: "sekai",
  150. Password: "password",
  151. },
  152. },
  153. },
  154. },
  155. },
  156. Outbounds: []option.Outbound{
  157. {
  158. Type: C.TypeDirect,
  159. },
  160. {
  161. Type: C.TypeTrojan,
  162. Tag: "trojan-out",
  163. TrojanOptions: option.TrojanOutboundOptions{
  164. ServerOptions: option.ServerOptions{
  165. Server: "127.0.0.1",
  166. ServerPort: serverPort,
  167. },
  168. Password: "password",
  169. },
  170. },
  171. },
  172. Route: &option.RouteOptions{
  173. Rules: []option.Rule{
  174. {
  175. DefaultOptions: option.DefaultRule{
  176. Inbound: []string{"mixed-in"},
  177. Outbound: "trojan-out",
  178. },
  179. },
  180. },
  181. },
  182. })
  183. testSuit(t, clientPort, testPort)
  184. }