naive_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/auth"
  9. "github.com/sagernet/sing/common/json/badoption"
  10. "github.com/sagernet/sing/common/network"
  11. )
  12. func TestNaiveInboundWithNginx(t *testing.T) {
  13. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  14. startInstance(t, option.Options{
  15. Inbounds: []option.Inbound{
  16. {
  17. Type: C.TypeNaive,
  18. Options: &option.NaiveInboundOptions{
  19. ListenOptions: option.ListenOptions{
  20. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  21. ListenPort: otherPort,
  22. },
  23. Users: []auth.User{
  24. {
  25. Username: "sekai",
  26. Password: "password",
  27. },
  28. },
  29. Network: network.NetworkTCP,
  30. },
  31. },
  32. },
  33. })
  34. startDockerContainer(t, DockerOptions{
  35. Image: ImageNginx,
  36. Ports: []uint16{serverPort, otherPort},
  37. Bind: map[string]string{
  38. "nginx.conf": "/etc/nginx/nginx.conf",
  39. "naive-nginx.conf": "/etc/nginx/conf.d/naive.conf",
  40. certPem: "/etc/nginx/cert.pem",
  41. keyPem: "/etc/nginx/key.pem",
  42. },
  43. })
  44. startDockerContainer(t, DockerOptions{
  45. Image: ImageNaive,
  46. Ports: []uint16{serverPort, clientPort},
  47. Bind: map[string]string{
  48. "naive.json": "/etc/naiveproxy/config.json",
  49. caPem: "/etc/naiveproxy/ca.pem",
  50. },
  51. Env: []string{
  52. "SSL_CERT_FILE=/etc/naiveproxy/ca.pem",
  53. },
  54. })
  55. testTCP(t, clientPort, testPort)
  56. }
  57. func TestNaiveInbound(t *testing.T) {
  58. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  59. startInstance(t, option.Options{
  60. Inbounds: []option.Inbound{
  61. {
  62. Type: C.TypeNaive,
  63. Options: &option.NaiveInboundOptions{
  64. ListenOptions: option.ListenOptions{
  65. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  66. ListenPort: serverPort,
  67. },
  68. Users: []auth.User{
  69. {
  70. Username: "sekai",
  71. Password: "password",
  72. },
  73. },
  74. Network: network.NetworkTCP,
  75. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  76. TLS: &option.InboundTLSOptions{
  77. Enabled: true,
  78. ServerName: "example.org",
  79. CertificatePath: certPem,
  80. KeyPath: keyPem,
  81. },
  82. },
  83. },
  84. },
  85. },
  86. })
  87. startDockerContainer(t, DockerOptions{
  88. Image: ImageNaive,
  89. Ports: []uint16{serverPort, clientPort},
  90. Bind: map[string]string{
  91. "naive.json": "/etc/naiveproxy/config.json",
  92. caPem: "/etc/naiveproxy/ca.pem",
  93. },
  94. Env: []string{
  95. "SSL_CERT_FILE=/etc/naiveproxy/ca.pem",
  96. },
  97. })
  98. testTCP(t, clientPort, testPort)
  99. }
  100. func TestNaiveHTTP3Inbound(t *testing.T) {
  101. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  102. startInstance(t, option.Options{
  103. Inbounds: []option.Inbound{
  104. {
  105. Type: C.TypeNaive,
  106. Options: &option.NaiveInboundOptions{
  107. ListenOptions: option.ListenOptions{
  108. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  109. ListenPort: serverPort,
  110. },
  111. Users: []auth.User{
  112. {
  113. Username: "sekai",
  114. Password: "password",
  115. },
  116. },
  117. Network: network.NetworkUDP,
  118. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  119. TLS: &option.InboundTLSOptions{
  120. Enabled: true,
  121. ServerName: "example.org",
  122. CertificatePath: certPem,
  123. KeyPath: keyPem,
  124. },
  125. },
  126. },
  127. },
  128. },
  129. })
  130. startDockerContainer(t, DockerOptions{
  131. Image: ImageNaive,
  132. Ports: []uint16{serverPort, clientPort},
  133. Bind: map[string]string{
  134. "naive-quic.json": "/etc/naiveproxy/config.json",
  135. caPem: "/etc/naiveproxy/ca.pem",
  136. },
  137. Env: []string{
  138. "SSL_CERT_FILE=/etc/naiveproxy/ca.pem",
  139. },
  140. })
  141. testTCP(t, clientPort, testPort)
  142. }