reality_test.go 8.9 KB

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