trojan_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. Log: &option.LogOptions{
  21. Level: "error",
  22. },
  23. Inbounds: []option.Inbound{
  24. {
  25. Type: C.TypeMixed,
  26. MixedOptions: option.HTTPMixedInboundOptions{
  27. ListenOptions: option.ListenOptions{
  28. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  29. ListenPort: clientPort,
  30. },
  31. },
  32. },
  33. },
  34. Outbounds: []option.Outbound{
  35. {
  36. Type: C.TypeTrojan,
  37. TrojanOptions: option.TrojanOutboundOptions{
  38. ServerOptions: option.ServerOptions{
  39. Server: "127.0.0.1",
  40. ServerPort: serverPort,
  41. },
  42. Password: "password",
  43. TLS: &option.OutboundTLSOptions{
  44. Enabled: true,
  45. ServerName: "example.org",
  46. CertificatePath: certPem,
  47. },
  48. },
  49. },
  50. },
  51. })
  52. testSuit(t, clientPort, testPort)
  53. }
  54. func TestTrojanSelf(t *testing.T) {
  55. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  56. startInstance(t, option.Options{
  57. Log: &option.LogOptions{
  58. Level: "error",
  59. },
  60. Inbounds: []option.Inbound{
  61. {
  62. Type: C.TypeMixed,
  63. Tag: "mixed-in",
  64. MixedOptions: option.HTTPMixedInboundOptions{
  65. ListenOptions: option.ListenOptions{
  66. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  67. ListenPort: clientPort,
  68. },
  69. },
  70. },
  71. {
  72. Type: C.TypeTrojan,
  73. TrojanOptions: option.TrojanInboundOptions{
  74. ListenOptions: option.ListenOptions{
  75. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  76. ListenPort: serverPort,
  77. },
  78. Users: []option.TrojanUser{
  79. {
  80. Name: "sekai",
  81. Password: "password",
  82. },
  83. },
  84. TLS: &option.InboundTLSOptions{
  85. Enabled: true,
  86. ServerName: "example.org",
  87. CertificatePath: certPem,
  88. KeyPath: keyPem,
  89. },
  90. },
  91. },
  92. },
  93. Outbounds: []option.Outbound{
  94. {
  95. Type: C.TypeDirect,
  96. },
  97. {
  98. Type: C.TypeTrojan,
  99. Tag: "trojan-out",
  100. TrojanOptions: option.TrojanOutboundOptions{
  101. ServerOptions: option.ServerOptions{
  102. Server: "127.0.0.1",
  103. ServerPort: serverPort,
  104. },
  105. Password: "password",
  106. TLS: &option.OutboundTLSOptions{
  107. Enabled: true,
  108. ServerName: "example.org",
  109. CertificatePath: certPem,
  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. }