vless_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package main
  2. import (
  3. "net/netip"
  4. "os"
  5. "testing"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/option"
  8. "github.com/sagernet/sing-box/transport/vless"
  9. "github.com/gofrs/uuid/v5"
  10. "github.com/spyzhov/ajson"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestVLESS(t *testing.T) {
  14. content, err := os.ReadFile("config/vless-server.json")
  15. require.NoError(t, err)
  16. config, err := ajson.Unmarshal(content)
  17. require.NoError(t, err)
  18. user := newUUID()
  19. inbound := config.MustKey("inbounds").MustIndex(0)
  20. inbound.MustKey("port").SetNumeric(float64(serverPort))
  21. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  22. content, err = ajson.Marshal(config)
  23. require.NoError(t, err)
  24. startDockerContainer(t, DockerOptions{
  25. Image: ImageV2RayCore,
  26. Ports: []uint16{serverPort},
  27. EntryPoint: "v2ray",
  28. Cmd: []string{"run"},
  29. Stdin: content,
  30. })
  31. startInstance(t, option.Options{
  32. Inbounds: []option.Inbound{
  33. {
  34. Type: C.TypeMixed,
  35. MixedOptions: option.HTTPMixedInboundOptions{
  36. ListenOptions: option.ListenOptions{
  37. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  38. ListenPort: clientPort,
  39. },
  40. },
  41. },
  42. },
  43. Outbounds: []option.Outbound{
  44. {
  45. Type: C.TypeVLESS,
  46. VLESSOptions: option.VLESSOutboundOptions{
  47. ServerOptions: option.ServerOptions{
  48. Server: "127.0.0.1",
  49. ServerPort: serverPort,
  50. },
  51. UUID: user.String(),
  52. },
  53. },
  54. },
  55. })
  56. testTCP(t, clientPort, testPort)
  57. }
  58. func TestVLESSXRay(t *testing.T) {
  59. t.Run("origin", func(t *testing.T) {
  60. testVLESSXrayOutbound(t, "", "")
  61. })
  62. t.Run("xudp", func(t *testing.T) {
  63. testVLESSXrayOutbound(t, "xudp", "")
  64. })
  65. t.Run("vision", func(t *testing.T) {
  66. testVLESSXrayOutbound(t, "xudp", vless.FlowVision)
  67. })
  68. }
  69. func testVLESSXrayOutbound(t *testing.T, packetEncoding string, flow string) {
  70. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  71. content, err := os.ReadFile("config/vless-tls-server.json")
  72. require.NoError(t, err)
  73. config, err := ajson.Unmarshal(content)
  74. require.NoError(t, err)
  75. userID := newUUID()
  76. inbound := config.MustKey("inbounds").MustIndex(0)
  77. inbound.MustKey("port").SetNumeric(float64(serverPort))
  78. user := inbound.MustKey("settings").MustKey("clients").MustIndex(0)
  79. user.MustKey("id").SetString(userID.String())
  80. user.MustKey("flow").SetString(flow)
  81. content, err = ajson.Marshal(config)
  82. require.NoError(t, err)
  83. startDockerContainer(t, DockerOptions{
  84. Image: ImageXRayCore,
  85. Ports: []uint16{serverPort},
  86. EntryPoint: "xray",
  87. Stdin: content,
  88. Bind: map[string]string{
  89. certPem: "/path/to/certificate.crt",
  90. keyPem: "/path/to/private.key",
  91. },
  92. })
  93. startInstance(t, option.Options{
  94. Inbounds: []option.Inbound{
  95. {
  96. Type: C.TypeMixed,
  97. MixedOptions: option.HTTPMixedInboundOptions{
  98. ListenOptions: option.ListenOptions{
  99. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  100. ListenPort: clientPort,
  101. },
  102. },
  103. },
  104. {
  105. Type: C.TypeTrojan,
  106. Tag: "trojan",
  107. TrojanOptions: option.TrojanInboundOptions{
  108. ListenOptions: option.ListenOptions{
  109. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  110. ListenPort: otherPort,
  111. },
  112. Users: []option.TrojanUser{
  113. {
  114. Name: "sekai",
  115. Password: userID.String(),
  116. },
  117. },
  118. TLS: &option.InboundTLSOptions{
  119. Enabled: true,
  120. ServerName: "example.org",
  121. CertificatePath: certPem,
  122. KeyPath: keyPem,
  123. },
  124. },
  125. },
  126. },
  127. Outbounds: []option.Outbound{
  128. {
  129. Type: C.TypeTrojan,
  130. TrojanOptions: option.TrojanOutboundOptions{
  131. ServerOptions: option.ServerOptions{
  132. Server: "host.docker.internal",
  133. ServerPort: otherPort,
  134. },
  135. Password: userID.String(),
  136. TLS: &option.OutboundTLSOptions{
  137. Enabled: true,
  138. ServerName: "example.org",
  139. CertificatePath: certPem,
  140. },
  141. DialerOptions: option.DialerOptions{
  142. Detour: "vless",
  143. },
  144. },
  145. },
  146. {
  147. Type: C.TypeVLESS,
  148. Tag: "vless",
  149. VLESSOptions: option.VLESSOutboundOptions{
  150. ServerOptions: option.ServerOptions{
  151. Server: "127.0.0.1",
  152. ServerPort: serverPort,
  153. },
  154. UUID: userID.String(),
  155. Flow: flow,
  156. PacketEncoding: &packetEncoding,
  157. TLS: &option.OutboundTLSOptions{
  158. Enabled: true,
  159. ServerName: "example.org",
  160. CertificatePath: certPem,
  161. },
  162. },
  163. },
  164. {
  165. Type: C.TypeDirect,
  166. Tag: "direct",
  167. },
  168. },
  169. Route: &option.RouteOptions{
  170. Rules: []option.Rule{
  171. {
  172. DefaultOptions: option.DefaultRule{
  173. Inbound: []string{"trojan"},
  174. Outbound: "direct",
  175. },
  176. },
  177. },
  178. },
  179. })
  180. testSuit(t, clientPort, testPort)
  181. }
  182. func TestVLESSSelf(t *testing.T) {
  183. t.Run("origin", func(t *testing.T) {
  184. testVLESSSelf(t, "")
  185. })
  186. t.Run("vision", func(t *testing.T) {
  187. testVLESSSelf(t, vless.FlowVision)
  188. })
  189. t.Run("vision-tls", func(t *testing.T) {
  190. testVLESSSelfTLS(t, vless.FlowVision)
  191. })
  192. }
  193. func testVLESSSelf(t *testing.T, flow string) {
  194. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  195. userUUID := newUUID()
  196. startInstance(t, option.Options{
  197. Inbounds: []option.Inbound{
  198. {
  199. Type: C.TypeMixed,
  200. Tag: "mixed-in",
  201. MixedOptions: option.HTTPMixedInboundOptions{
  202. ListenOptions: option.ListenOptions{
  203. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  204. ListenPort: clientPort,
  205. },
  206. },
  207. },
  208. {
  209. Type: C.TypeVLESS,
  210. VLESSOptions: option.VLESSInboundOptions{
  211. ListenOptions: option.ListenOptions{
  212. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  213. ListenPort: serverPort,
  214. },
  215. Users: []option.VLESSUser{
  216. {
  217. Name: "sekai",
  218. UUID: userUUID.String(),
  219. Flow: flow,
  220. },
  221. },
  222. TLS: &option.InboundTLSOptions{
  223. Enabled: true,
  224. ServerName: "example.org",
  225. CertificatePath: certPem,
  226. KeyPath: keyPem,
  227. },
  228. },
  229. },
  230. },
  231. Outbounds: []option.Outbound{
  232. {
  233. Type: C.TypeDirect,
  234. },
  235. {
  236. Type: C.TypeVLESS,
  237. Tag: "vless-out",
  238. VLESSOptions: option.VLESSOutboundOptions{
  239. ServerOptions: option.ServerOptions{
  240. Server: "127.0.0.1",
  241. ServerPort: serverPort,
  242. },
  243. UUID: userUUID.String(),
  244. Flow: flow,
  245. TLS: &option.OutboundTLSOptions{
  246. Enabled: true,
  247. ServerName: "example.org",
  248. CertificatePath: certPem,
  249. },
  250. },
  251. },
  252. },
  253. Route: &option.RouteOptions{
  254. Rules: []option.Rule{
  255. {
  256. DefaultOptions: option.DefaultRule{
  257. Inbound: []string{"mixed-in"},
  258. Outbound: "vless-out",
  259. },
  260. },
  261. },
  262. },
  263. })
  264. testSuit(t, clientPort, testPort)
  265. }
  266. func testVLESSSelfTLS(t *testing.T, flow string) {
  267. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  268. userUUID := newUUID()
  269. startInstance(t, option.Options{
  270. Inbounds: []option.Inbound{
  271. {
  272. Type: C.TypeMixed,
  273. Tag: "mixed-in",
  274. MixedOptions: option.HTTPMixedInboundOptions{
  275. ListenOptions: option.ListenOptions{
  276. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  277. ListenPort: clientPort,
  278. },
  279. },
  280. },
  281. {
  282. Type: C.TypeVLESS,
  283. VLESSOptions: option.VLESSInboundOptions{
  284. ListenOptions: option.ListenOptions{
  285. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  286. ListenPort: serverPort,
  287. },
  288. Users: []option.VLESSUser{
  289. {
  290. Name: "sekai",
  291. UUID: userUUID.String(),
  292. Flow: flow,
  293. },
  294. },
  295. TLS: &option.InboundTLSOptions{
  296. Enabled: true,
  297. ServerName: "example.org",
  298. CertificatePath: certPem,
  299. KeyPath: keyPem,
  300. },
  301. },
  302. },
  303. {
  304. Type: C.TypeTrojan,
  305. Tag: "trojan",
  306. TrojanOptions: option.TrojanInboundOptions{
  307. ListenOptions: option.ListenOptions{
  308. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  309. ListenPort: otherPort,
  310. },
  311. Users: []option.TrojanUser{
  312. {
  313. Name: "sekai",
  314. Password: userUUID.String(),
  315. },
  316. },
  317. TLS: &option.InboundTLSOptions{
  318. Enabled: true,
  319. ServerName: "example.org",
  320. CertificatePath: certPem,
  321. KeyPath: keyPem,
  322. },
  323. },
  324. },
  325. },
  326. Outbounds: []option.Outbound{
  327. {
  328. Type: C.TypeDirect,
  329. },
  330. {
  331. Type: C.TypeTrojan,
  332. Tag: "trojan-out",
  333. TrojanOptions: option.TrojanOutboundOptions{
  334. ServerOptions: option.ServerOptions{
  335. Server: "127.0.0.1",
  336. ServerPort: otherPort,
  337. },
  338. Password: userUUID.String(),
  339. TLS: &option.OutboundTLSOptions{
  340. Enabled: true,
  341. ServerName: "example.org",
  342. CertificatePath: certPem,
  343. },
  344. DialerOptions: option.DialerOptions{
  345. Detour: "vless-out",
  346. },
  347. },
  348. },
  349. {
  350. Type: C.TypeVLESS,
  351. Tag: "vless-out",
  352. VLESSOptions: option.VLESSOutboundOptions{
  353. ServerOptions: option.ServerOptions{
  354. Server: "127.0.0.1",
  355. ServerPort: serverPort,
  356. },
  357. UUID: userUUID.String(),
  358. Flow: flow,
  359. TLS: &option.OutboundTLSOptions{
  360. Enabled: true,
  361. ServerName: "example.org",
  362. CertificatePath: certPem,
  363. },
  364. },
  365. },
  366. },
  367. Route: &option.RouteOptions{
  368. Rules: []option.Rule{
  369. {
  370. DefaultOptions: option.DefaultRule{
  371. Inbound: []string{"mixed-in"},
  372. Outbound: "trojan-out",
  373. },
  374. },
  375. },
  376. },
  377. })
  378. testSuit(t, clientPort, testPort)
  379. }
  380. func TestVLESSXrayInbound(t *testing.T) {
  381. testVLESSXrayInbound(t, vless.FlowVision)
  382. }
  383. func testVLESSXrayInbound(t *testing.T, flow string) {
  384. userId, err := uuid.DefaultGenerator.NewV4()
  385. require.NoError(t, err)
  386. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  387. startInstance(t, option.Options{
  388. Inbounds: []option.Inbound{
  389. {
  390. Type: C.TypeVLESS,
  391. VLESSOptions: option.VLESSInboundOptions{
  392. ListenOptions: option.ListenOptions{
  393. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  394. ListenPort: serverPort,
  395. },
  396. Users: []option.VLESSUser{
  397. {
  398. Name: "sekai",
  399. UUID: userId.String(),
  400. Flow: flow,
  401. },
  402. },
  403. TLS: &option.InboundTLSOptions{
  404. Enabled: true,
  405. ServerName: "example.org",
  406. CertificatePath: certPem,
  407. KeyPath: keyPem,
  408. },
  409. },
  410. },
  411. {
  412. Type: C.TypeTrojan,
  413. Tag: "trojan",
  414. TrojanOptions: option.TrojanInboundOptions{
  415. ListenOptions: option.ListenOptions{
  416. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  417. ListenPort: otherPort,
  418. },
  419. Users: []option.TrojanUser{
  420. {
  421. Name: "sekai",
  422. Password: userId.String(),
  423. },
  424. },
  425. TLS: &option.InboundTLSOptions{
  426. Enabled: true,
  427. ServerName: "example.org",
  428. CertificatePath: certPem,
  429. KeyPath: keyPem,
  430. },
  431. },
  432. },
  433. },
  434. })
  435. startInstance(t, option.Options{
  436. Inbounds: []option.Inbound{
  437. {
  438. Type: C.TypeMixed,
  439. Tag: "mixed-in",
  440. MixedOptions: option.HTTPMixedInboundOptions{
  441. ListenOptions: option.ListenOptions{
  442. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  443. ListenPort: otherClientPort,
  444. },
  445. },
  446. },
  447. },
  448. Outbounds: []option.Outbound{
  449. {
  450. Type: C.TypeTrojan,
  451. Tag: "trojan-out",
  452. TrojanOptions: option.TrojanOutboundOptions{
  453. ServerOptions: option.ServerOptions{
  454. Server: "127.0.0.1",
  455. ServerPort: otherPort,
  456. },
  457. Password: userId.String(),
  458. TLS: &option.OutboundTLSOptions{
  459. Enabled: true,
  460. ServerName: "example.org",
  461. CertificatePath: certPem,
  462. },
  463. DialerOptions: option.DialerOptions{
  464. Detour: "vless-out",
  465. },
  466. },
  467. },
  468. {
  469. Type: C.TypeSOCKS,
  470. Tag: "vless-out",
  471. SocksOptions: option.SocksOutboundOptions{
  472. ServerOptions: option.ServerOptions{
  473. Server: "127.0.0.1",
  474. ServerPort: clientPort,
  475. },
  476. },
  477. },
  478. },
  479. })
  480. content, err := os.ReadFile("config/vless-tls-client.json")
  481. require.NoError(t, err)
  482. config, err := ajson.Unmarshal(content)
  483. require.NoError(t, err)
  484. config.MustKey("inbounds").MustIndex(0).MustKey("port").SetNumeric(float64(clientPort))
  485. outbound := config.MustKey("outbounds").MustIndex(0)
  486. settings := outbound.MustKey("settings").MustKey("vnext").MustIndex(0)
  487. settings.MustKey("port").SetNumeric(float64(serverPort))
  488. user := settings.MustKey("users").MustIndex(0)
  489. user.MustKey("id").SetString(userId.String())
  490. user.MustKey("flow").SetString(flow)
  491. content, err = ajson.Marshal(config)
  492. require.NoError(t, err)
  493. content, err = ajson.Marshal(config)
  494. require.NoError(t, err)
  495. startDockerContainer(t, DockerOptions{
  496. Image: ImageXRayCore,
  497. Ports: []uint16{clientPort},
  498. EntryPoint: "xray",
  499. Stdin: content,
  500. Bind: map[string]string{
  501. certPem: "/path/to/certificate.crt",
  502. keyPem: "/path/to/private.key",
  503. },
  504. })
  505. testTCP(t, otherClientPort, testPort)
  506. }