reality_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-box/transport/vless"
  8. )
  9. func TestVLESSVisionReality(t *testing.T) {
  10. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  11. userUUID := newUUID()
  12. startInstance(t, option.Options{
  13. Inbounds: []option.Inbound{
  14. {
  15. Type: C.TypeMixed,
  16. Tag: "mixed-in",
  17. MixedOptions: option.HTTPMixedInboundOptions{
  18. ListenOptions: option.ListenOptions{
  19. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  20. ListenPort: clientPort,
  21. },
  22. },
  23. },
  24. {
  25. Type: C.TypeVLESS,
  26. VLESSOptions: option.VLESSInboundOptions{
  27. ListenOptions: option.ListenOptions{
  28. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  29. ListenPort: serverPort,
  30. },
  31. Users: []option.VLESSUser{
  32. {
  33. Name: "sekai",
  34. UUID: userUUID.String(),
  35. Flow: vless.FlowVision,
  36. },
  37. },
  38. TLS: &option.InboundTLSOptions{
  39. Enabled: true,
  40. ServerName: "google.com",
  41. Reality: &option.InboundRealityOptions{
  42. Enabled: true,
  43. Handshake: option.InboundRealityHandshakeOptions{
  44. ServerOptions: option.ServerOptions{
  45. Server: "google.com",
  46. ServerPort: 443,
  47. },
  48. },
  49. ShortID: []string{"0123456789abcdef"},
  50. PrivateKey: "UuMBgl7MXTPx9inmQp2UC7Jcnwc6XYbwDNebonM-FCc",
  51. },
  52. },
  53. },
  54. },
  55. {
  56. Type: C.TypeTrojan,
  57. Tag: "trojan",
  58. TrojanOptions: option.TrojanInboundOptions{
  59. ListenOptions: option.ListenOptions{
  60. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  61. ListenPort: otherPort,
  62. },
  63. Users: []option.TrojanUser{
  64. {
  65. Name: "sekai",
  66. Password: userUUID.String(),
  67. },
  68. },
  69. TLS: &option.InboundTLSOptions{
  70. Enabled: true,
  71. ServerName: "example.org",
  72. CertificatePath: certPem,
  73. KeyPath: keyPem,
  74. },
  75. },
  76. },
  77. },
  78. Outbounds: []option.Outbound{
  79. {
  80. Type: C.TypeDirect,
  81. },
  82. {
  83. Type: C.TypeTrojan,
  84. Tag: "trojan-out",
  85. TrojanOptions: option.TrojanOutboundOptions{
  86. ServerOptions: option.ServerOptions{
  87. Server: "127.0.0.1",
  88. ServerPort: otherPort,
  89. },
  90. Password: userUUID.String(),
  91. TLS: &option.OutboundTLSOptions{
  92. Enabled: true,
  93. ServerName: "example.org",
  94. CertificatePath: certPem,
  95. },
  96. DialerOptions: option.DialerOptions{
  97. Detour: "vless-out",
  98. },
  99. },
  100. },
  101. {
  102. Type: C.TypeVLESS,
  103. Tag: "vless-out",
  104. VLESSOptions: option.VLESSOutboundOptions{
  105. ServerOptions: option.ServerOptions{
  106. Server: "127.0.0.1",
  107. ServerPort: serverPort,
  108. },
  109. UUID: userUUID.String(),
  110. Flow: vless.FlowVision,
  111. TLS: &option.OutboundTLSOptions{
  112. Enabled: true,
  113. ServerName: "google.com",
  114. Reality: &option.OutboundRealityOptions{
  115. Enabled: true,
  116. ShortID: "0123456789abcdef",
  117. PublicKey: "jNXHt1yRo0vDuchQlIP6Z0ZvjT3KtzVI-T4E7RoLJS0",
  118. },
  119. UTLS: &option.OutboundUTLSOptions{
  120. Enabled: true,
  121. },
  122. },
  123. },
  124. },
  125. },
  126. Route: &option.RouteOptions{
  127. Rules: []option.Rule{
  128. {
  129. DefaultOptions: option.DefaultRule{
  130. Inbound: []string{"mixed-in"},
  131. Outbound: "trojan-out",
  132. },
  133. },
  134. },
  135. },
  136. })
  137. testSuit(t, clientPort, testPort)
  138. }
  139. func TestVLESSRealityTransport(t *testing.T) {
  140. t.Run("grpc", func(t *testing.T) {
  141. testVLESSRealityTransport(t, &option.V2RayTransportOptions{
  142. Type: C.V2RayTransportTypeGRPC,
  143. })
  144. })
  145. t.Run("websocket", func(t *testing.T) {
  146. testVLESSRealityTransport(t, &option.V2RayTransportOptions{
  147. Type: C.V2RayTransportTypeWebsocket,
  148. })
  149. })
  150. t.Run("h2", func(t *testing.T) {
  151. testVLESSRealityTransport(t, &option.V2RayTransportOptions{
  152. Type: C.V2RayTransportTypeHTTP,
  153. })
  154. })
  155. }
  156. func testVLESSRealityTransport(t *testing.T, transport *option.V2RayTransportOptions) {
  157. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  158. userUUID := newUUID()
  159. startInstance(t, option.Options{
  160. Inbounds: []option.Inbound{
  161. {
  162. Type: C.TypeMixed,
  163. Tag: "mixed-in",
  164. MixedOptions: option.HTTPMixedInboundOptions{
  165. ListenOptions: option.ListenOptions{
  166. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  167. ListenPort: clientPort,
  168. },
  169. },
  170. },
  171. {
  172. Type: C.TypeVLESS,
  173. VLESSOptions: option.VLESSInboundOptions{
  174. ListenOptions: option.ListenOptions{
  175. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  176. ListenPort: serverPort,
  177. },
  178. Users: []option.VLESSUser{
  179. {
  180. Name: "sekai",
  181. UUID: userUUID.String(),
  182. },
  183. },
  184. TLS: &option.InboundTLSOptions{
  185. Enabled: true,
  186. ServerName: "google.com",
  187. Reality: &option.InboundRealityOptions{
  188. Enabled: true,
  189. Handshake: option.InboundRealityHandshakeOptions{
  190. ServerOptions: option.ServerOptions{
  191. Server: "google.com",
  192. ServerPort: 443,
  193. },
  194. },
  195. ShortID: []string{"0123456789abcdef"},
  196. PrivateKey: "UuMBgl7MXTPx9inmQp2UC7Jcnwc6XYbwDNebonM-FCc",
  197. },
  198. },
  199. Transport: transport,
  200. },
  201. },
  202. {
  203. Type: C.TypeTrojan,
  204. Tag: "trojan",
  205. TrojanOptions: option.TrojanInboundOptions{
  206. ListenOptions: option.ListenOptions{
  207. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  208. ListenPort: otherPort,
  209. },
  210. Users: []option.TrojanUser{
  211. {
  212. Name: "sekai",
  213. Password: userUUID.String(),
  214. },
  215. },
  216. TLS: &option.InboundTLSOptions{
  217. Enabled: true,
  218. ServerName: "example.org",
  219. CertificatePath: certPem,
  220. KeyPath: keyPem,
  221. },
  222. },
  223. },
  224. },
  225. Outbounds: []option.Outbound{
  226. {
  227. Type: C.TypeDirect,
  228. },
  229. {
  230. Type: C.TypeTrojan,
  231. Tag: "trojan-out",
  232. TrojanOptions: option.TrojanOutboundOptions{
  233. ServerOptions: option.ServerOptions{
  234. Server: "127.0.0.1",
  235. ServerPort: otherPort,
  236. },
  237. Password: userUUID.String(),
  238. TLS: &option.OutboundTLSOptions{
  239. Enabled: true,
  240. ServerName: "example.org",
  241. CertificatePath: certPem,
  242. },
  243. DialerOptions: option.DialerOptions{
  244. Detour: "vless-out",
  245. },
  246. },
  247. },
  248. {
  249. Type: C.TypeVLESS,
  250. Tag: "vless-out",
  251. VLESSOptions: option.VLESSOutboundOptions{
  252. ServerOptions: option.ServerOptions{
  253. Server: "127.0.0.1",
  254. ServerPort: serverPort,
  255. },
  256. UUID: userUUID.String(),
  257. TLS: &option.OutboundTLSOptions{
  258. Enabled: true,
  259. ServerName: "google.com",
  260. Reality: &option.OutboundRealityOptions{
  261. Enabled: true,
  262. ShortID: "0123456789abcdef",
  263. PublicKey: "jNXHt1yRo0vDuchQlIP6Z0ZvjT3KtzVI-T4E7RoLJS0",
  264. },
  265. UTLS: &option.OutboundUTLSOptions{
  266. Enabled: true,
  267. },
  268. },
  269. Transport: transport,
  270. },
  271. },
  272. },
  273. Route: &option.RouteOptions{
  274. Rules: []option.Rule{
  275. {
  276. DefaultOptions: option.DefaultRule{
  277. Inbound: []string{"mixed-in"},
  278. Outbound: "trojan-out",
  279. },
  280. },
  281. },
  282. },
  283. })
  284. testSuit(t, clientPort, testPort)
  285. }