reality_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. 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.NewListenAddress(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 TestVLESSVisionRealityPlain(t *testing.T) {
  140. userUUID := newUUID()
  141. startInstance(t, option.Options{
  142. Inbounds: []option.Inbound{
  143. {
  144. Type: C.TypeMixed,
  145. Tag: "mixed-in",
  146. MixedOptions: option.HTTPMixedInboundOptions{
  147. ListenOptions: option.ListenOptions{
  148. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  149. ListenPort: clientPort,
  150. },
  151. },
  152. },
  153. {
  154. Type: C.TypeVLESS,
  155. VLESSOptions: option.VLESSInboundOptions{
  156. ListenOptions: option.ListenOptions{
  157. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  158. ListenPort: serverPort,
  159. },
  160. Users: []option.VLESSUser{
  161. {
  162. Name: "sekai",
  163. UUID: userUUID.String(),
  164. Flow: vless.FlowVision,
  165. },
  166. },
  167. TLS: &option.InboundTLSOptions{
  168. Enabled: true,
  169. ServerName: "google.com",
  170. Reality: &option.InboundRealityOptions{
  171. Enabled: true,
  172. Handshake: option.InboundRealityHandshakeOptions{
  173. ServerOptions: option.ServerOptions{
  174. Server: "google.com",
  175. ServerPort: 443,
  176. },
  177. },
  178. ShortID: []string{"0123456789abcdef"},
  179. PrivateKey: "UuMBgl7MXTPx9inmQp2UC7Jcnwc6XYbwDNebonM-FCc",
  180. },
  181. },
  182. },
  183. },
  184. },
  185. Outbounds: []option.Outbound{
  186. {
  187. Type: C.TypeDirect,
  188. },
  189. {
  190. Type: C.TypeVLESS,
  191. Tag: "vless-out",
  192. VLESSOptions: option.VLESSOutboundOptions{
  193. ServerOptions: option.ServerOptions{
  194. Server: "127.0.0.1",
  195. ServerPort: serverPort,
  196. },
  197. UUID: userUUID.String(),
  198. Flow: vless.FlowVision,
  199. TLS: &option.OutboundTLSOptions{
  200. Enabled: true,
  201. ServerName: "google.com",
  202. Reality: &option.OutboundRealityOptions{
  203. Enabled: true,
  204. ShortID: "0123456789abcdef",
  205. PublicKey: "jNXHt1yRo0vDuchQlIP6Z0ZvjT3KtzVI-T4E7RoLJS0",
  206. },
  207. UTLS: &option.OutboundUTLSOptions{
  208. Enabled: true,
  209. },
  210. },
  211. },
  212. },
  213. },
  214. Route: &option.RouteOptions{
  215. Rules: []option.Rule{
  216. {
  217. DefaultOptions: option.DefaultRule{
  218. Inbound: []string{"mixed-in"},
  219. Outbound: "vless-out",
  220. },
  221. },
  222. },
  223. },
  224. })
  225. testSuit(t, clientPort, testPort)
  226. }
  227. func TestVLESSRealityTransport(t *testing.T) {
  228. t.Run("grpc", func(t *testing.T) {
  229. testVLESSRealityTransport(t, &option.V2RayTransportOptions{
  230. Type: C.V2RayTransportTypeGRPC,
  231. })
  232. })
  233. t.Run("websocket", func(t *testing.T) {
  234. testVLESSRealityTransport(t, &option.V2RayTransportOptions{
  235. Type: C.V2RayTransportTypeWebsocket,
  236. })
  237. })
  238. t.Run("h2", func(t *testing.T) {
  239. testVLESSRealityTransport(t, &option.V2RayTransportOptions{
  240. Type: C.V2RayTransportTypeHTTP,
  241. })
  242. })
  243. }
  244. func testVLESSRealityTransport(t *testing.T, transport *option.V2RayTransportOptions) {
  245. userUUID := newUUID()
  246. startInstance(t, option.Options{
  247. Inbounds: []option.Inbound{
  248. {
  249. Type: C.TypeMixed,
  250. Tag: "mixed-in",
  251. MixedOptions: option.HTTPMixedInboundOptions{
  252. ListenOptions: option.ListenOptions{
  253. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  254. ListenPort: clientPort,
  255. },
  256. },
  257. },
  258. {
  259. Type: C.TypeVLESS,
  260. VLESSOptions: option.VLESSInboundOptions{
  261. ListenOptions: option.ListenOptions{
  262. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  263. ListenPort: serverPort,
  264. },
  265. Users: []option.VLESSUser{
  266. {
  267. Name: "sekai",
  268. UUID: userUUID.String(),
  269. },
  270. },
  271. TLS: &option.InboundTLSOptions{
  272. Enabled: true,
  273. ServerName: "google.com",
  274. Reality: &option.InboundRealityOptions{
  275. Enabled: true,
  276. Handshake: option.InboundRealityHandshakeOptions{
  277. ServerOptions: option.ServerOptions{
  278. Server: "google.com",
  279. ServerPort: 443,
  280. },
  281. },
  282. ShortID: []string{"0123456789abcdef"},
  283. PrivateKey: "UuMBgl7MXTPx9inmQp2UC7Jcnwc6XYbwDNebonM-FCc",
  284. },
  285. },
  286. Transport: transport,
  287. },
  288. },
  289. },
  290. Outbounds: []option.Outbound{
  291. {
  292. Type: C.TypeDirect,
  293. },
  294. {
  295. Type: C.TypeVLESS,
  296. Tag: "vless-out",
  297. VLESSOptions: option.VLESSOutboundOptions{
  298. ServerOptions: option.ServerOptions{
  299. Server: "127.0.0.1",
  300. ServerPort: serverPort,
  301. },
  302. UUID: userUUID.String(),
  303. TLS: &option.OutboundTLSOptions{
  304. Enabled: true,
  305. ServerName: "google.com",
  306. Reality: &option.OutboundRealityOptions{
  307. Enabled: true,
  308. ShortID: "0123456789abcdef",
  309. PublicKey: "jNXHt1yRo0vDuchQlIP6Z0ZvjT3KtzVI-T4E7RoLJS0",
  310. },
  311. UTLS: &option.OutboundUTLSOptions{
  312. Enabled: true,
  313. },
  314. },
  315. Transport: transport,
  316. },
  317. },
  318. },
  319. Route: &option.RouteOptions{
  320. Rules: []option.Rule{
  321. {
  322. DefaultOptions: option.DefaultRule{
  323. Inbound: []string{"mixed-in"},
  324. Outbound: "vless-out",
  325. },
  326. },
  327. },
  328. },
  329. })
  330. testSuit(t, clientPort, testPort)
  331. }