vless_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/spyzhov/ajson"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestVLESS(t *testing.T) {
  13. content, err := os.ReadFile("config/vless-server.json")
  14. require.NoError(t, err)
  15. config, err := ajson.Unmarshal(content)
  16. require.NoError(t, err)
  17. user := newUUID()
  18. inbound := config.MustKey("inbounds").MustIndex(0)
  19. inbound.MustKey("port").SetNumeric(float64(serverPort))
  20. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  21. content, err = ajson.Marshal(config)
  22. require.NoError(t, err)
  23. startDockerContainer(t, DockerOptions{
  24. Image: ImageV2RayCore,
  25. Ports: []uint16{serverPort},
  26. EntryPoint: "v2ray",
  27. Cmd: []string{"run"},
  28. Stdin: content,
  29. })
  30. startInstance(t, option.Options{
  31. Inbounds: []option.Inbound{
  32. {
  33. Type: C.TypeMixed,
  34. MixedOptions: option.HTTPMixedInboundOptions{
  35. ListenOptions: option.ListenOptions{
  36. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  37. ListenPort: clientPort,
  38. },
  39. },
  40. },
  41. },
  42. Outbounds: []option.Outbound{
  43. {
  44. Type: C.TypeVLESS,
  45. VLESSOptions: option.VLESSOutboundOptions{
  46. ServerOptions: option.ServerOptions{
  47. Server: "127.0.0.1",
  48. ServerPort: serverPort,
  49. },
  50. UUID: user.String(),
  51. },
  52. },
  53. },
  54. })
  55. testTCP(t, clientPort, testPort)
  56. }
  57. func TestVLESSXRay(t *testing.T) {
  58. t.Run("origin", func(t *testing.T) {
  59. testVLESSXray(t, "", "")
  60. })
  61. t.Run("xudp", func(t *testing.T) {
  62. testVLESSXray(t, "xudp", "")
  63. })
  64. t.Run("vision", func(t *testing.T) {
  65. testVLESSXray(t, "", vless.FlowVision)
  66. })
  67. }
  68. func testVLESSXray(t *testing.T, packetEncoding string, flow string) {
  69. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  70. content, err := os.ReadFile("config/vless-tls-server.json")
  71. require.NoError(t, err)
  72. config, err := ajson.Unmarshal(content)
  73. require.NoError(t, err)
  74. userID := newUUID()
  75. inbound := config.MustKey("inbounds").MustIndex(0)
  76. inbound.MustKey("port").SetNumeric(float64(serverPort))
  77. user := inbound.MustKey("settings").MustKey("clients").MustIndex(0)
  78. user.MustKey("id").SetString(userID.String())
  79. user.MustKey("flow").SetString(flow)
  80. content, err = ajson.Marshal(config)
  81. require.NoError(t, err)
  82. startDockerContainer(t, DockerOptions{
  83. Image: ImageXRayCore,
  84. Ports: []uint16{serverPort},
  85. EntryPoint: "xray",
  86. Stdin: content,
  87. Bind: map[string]string{
  88. certPem: "/path/to/certificate.crt",
  89. keyPem: "/path/to/private.key",
  90. },
  91. })
  92. startInstance(t, option.Options{
  93. Inbounds: []option.Inbound{
  94. {
  95. Type: C.TypeMixed,
  96. MixedOptions: option.HTTPMixedInboundOptions{
  97. ListenOptions: option.ListenOptions{
  98. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  99. ListenPort: clientPort,
  100. },
  101. },
  102. },
  103. {
  104. Type: C.TypeTrojan,
  105. Tag: "trojan",
  106. TrojanOptions: option.TrojanInboundOptions{
  107. ListenOptions: option.ListenOptions{
  108. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  109. ListenPort: otherPort,
  110. },
  111. Users: []option.TrojanUser{
  112. {
  113. Name: "sekai",
  114. Password: userID.String(),
  115. },
  116. },
  117. TLS: &option.InboundTLSOptions{
  118. Enabled: true,
  119. ServerName: "example.org",
  120. CertificatePath: certPem,
  121. KeyPath: keyPem,
  122. },
  123. },
  124. },
  125. },
  126. Outbounds: []option.Outbound{
  127. {
  128. Type: C.TypeTrojan,
  129. TrojanOptions: option.TrojanOutboundOptions{
  130. ServerOptions: option.ServerOptions{
  131. Server: "host.docker.internal",
  132. ServerPort: otherPort,
  133. },
  134. Password: userID.String(),
  135. TLS: &option.OutboundTLSOptions{
  136. Enabled: true,
  137. ServerName: "example.org",
  138. CertificatePath: certPem,
  139. },
  140. DialerOptions: option.DialerOptions{
  141. Detour: "vless",
  142. },
  143. },
  144. },
  145. {
  146. Type: C.TypeVLESS,
  147. Tag: "vless",
  148. VLESSOptions: option.VLESSOutboundOptions{
  149. ServerOptions: option.ServerOptions{
  150. Server: "127.0.0.1",
  151. ServerPort: serverPort,
  152. },
  153. UUID: userID.String(),
  154. Flow: flow,
  155. PacketEncoding: &packetEncoding,
  156. TLS: &option.OutboundTLSOptions{
  157. Enabled: true,
  158. ServerName: "example.org",
  159. CertificatePath: certPem,
  160. },
  161. },
  162. },
  163. {
  164. Type: C.TypeDirect,
  165. Tag: "direct",
  166. },
  167. },
  168. Route: &option.RouteOptions{
  169. Rules: []option.Rule{
  170. {
  171. DefaultOptions: option.DefaultRule{
  172. Inbound: []string{"trojan"},
  173. Outbound: "direct",
  174. },
  175. },
  176. },
  177. },
  178. })
  179. testTCP(t, clientPort, testPort)
  180. }
  181. func TestVLESSSelf(t *testing.T) {
  182. t.Run("origin", func(t *testing.T) {
  183. testVLESSSelf(t, "")
  184. })
  185. t.Run("vision", func(t *testing.T) {
  186. testVLESSSelf(t, vless.FlowVision)
  187. })
  188. t.Run("vision-tls", func(t *testing.T) {
  189. testVLESSSelfTLS(t, vless.FlowVision)
  190. })
  191. }
  192. func testVLESSSelf(t *testing.T, flow string) {
  193. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  194. userUUID := newUUID()
  195. startInstance(t, option.Options{
  196. Inbounds: []option.Inbound{
  197. {
  198. Type: C.TypeMixed,
  199. Tag: "mixed-in",
  200. MixedOptions: option.HTTPMixedInboundOptions{
  201. ListenOptions: option.ListenOptions{
  202. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  203. ListenPort: clientPort,
  204. },
  205. },
  206. },
  207. {
  208. Type: C.TypeVLESS,
  209. VLESSOptions: option.VLESSInboundOptions{
  210. ListenOptions: option.ListenOptions{
  211. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  212. ListenPort: serverPort,
  213. },
  214. Users: []option.VLESSUser{
  215. {
  216. Name: "sekai",
  217. UUID: userUUID.String(),
  218. },
  219. },
  220. TLS: &option.InboundTLSOptions{
  221. Enabled: true,
  222. ServerName: "example.org",
  223. CertificatePath: certPem,
  224. KeyPath: keyPem,
  225. },
  226. },
  227. },
  228. },
  229. Outbounds: []option.Outbound{
  230. {
  231. Type: C.TypeDirect,
  232. },
  233. {
  234. Type: C.TypeVLESS,
  235. Tag: "vless-out",
  236. VLESSOptions: option.VLESSOutboundOptions{
  237. ServerOptions: option.ServerOptions{
  238. Server: "127.0.0.1",
  239. ServerPort: serverPort,
  240. },
  241. UUID: userUUID.String(),
  242. Flow: flow,
  243. TLS: &option.OutboundTLSOptions{
  244. Enabled: true,
  245. ServerName: "example.org",
  246. CertificatePath: certPem,
  247. },
  248. },
  249. },
  250. },
  251. Route: &option.RouteOptions{
  252. Rules: []option.Rule{
  253. {
  254. DefaultOptions: option.DefaultRule{
  255. Inbound: []string{"mixed-in"},
  256. Outbound: "vless-out",
  257. },
  258. },
  259. },
  260. },
  261. })
  262. testSuit(t, clientPort, testPort)
  263. }
  264. func testVLESSSelfTLS(t *testing.T, flow string) {
  265. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  266. userUUID := newUUID()
  267. startInstance(t, option.Options{
  268. Inbounds: []option.Inbound{
  269. {
  270. Type: C.TypeMixed,
  271. Tag: "mixed-in",
  272. MixedOptions: option.HTTPMixedInboundOptions{
  273. ListenOptions: option.ListenOptions{
  274. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  275. ListenPort: clientPort,
  276. },
  277. },
  278. },
  279. {
  280. Type: C.TypeVLESS,
  281. VLESSOptions: option.VLESSInboundOptions{
  282. ListenOptions: option.ListenOptions{
  283. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  284. ListenPort: serverPort,
  285. },
  286. Users: []option.VLESSUser{
  287. {
  288. Name: "sekai",
  289. UUID: userUUID.String(),
  290. },
  291. },
  292. TLS: &option.InboundTLSOptions{
  293. Enabled: true,
  294. ServerName: "example.org",
  295. CertificatePath: certPem,
  296. KeyPath: keyPem,
  297. },
  298. },
  299. },
  300. {
  301. Type: C.TypeTrojan,
  302. Tag: "trojan",
  303. TrojanOptions: option.TrojanInboundOptions{
  304. ListenOptions: option.ListenOptions{
  305. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  306. ListenPort: otherPort,
  307. },
  308. Users: []option.TrojanUser{
  309. {
  310. Name: "sekai",
  311. Password: userUUID.String(),
  312. },
  313. },
  314. TLS: &option.InboundTLSOptions{
  315. Enabled: true,
  316. ServerName: "example.org",
  317. CertificatePath: certPem,
  318. KeyPath: keyPem,
  319. },
  320. },
  321. },
  322. },
  323. Outbounds: []option.Outbound{
  324. {
  325. Type: C.TypeDirect,
  326. },
  327. {
  328. Type: C.TypeTrojan,
  329. Tag: "trojan-out",
  330. TrojanOptions: option.TrojanOutboundOptions{
  331. ServerOptions: option.ServerOptions{
  332. Server: "127.0.0.1",
  333. ServerPort: otherPort,
  334. },
  335. Password: userUUID.String(),
  336. TLS: &option.OutboundTLSOptions{
  337. Enabled: true,
  338. ServerName: "example.org",
  339. CertificatePath: certPem,
  340. },
  341. DialerOptions: option.DialerOptions{
  342. Detour: "vless-out",
  343. },
  344. },
  345. },
  346. {
  347. Type: C.TypeVLESS,
  348. Tag: "vless-out",
  349. VLESSOptions: option.VLESSOutboundOptions{
  350. ServerOptions: option.ServerOptions{
  351. Server: "127.0.0.1",
  352. ServerPort: serverPort,
  353. },
  354. UUID: userUUID.String(),
  355. Flow: flow,
  356. TLS: &option.OutboundTLSOptions{
  357. Enabled: true,
  358. ServerName: "example.org",
  359. CertificatePath: certPem,
  360. },
  361. },
  362. },
  363. },
  364. Route: &option.RouteOptions{
  365. Rules: []option.Rule{
  366. {
  367. DefaultOptions: option.DefaultRule{
  368. Inbound: []string{"mixed-in"},
  369. Outbound: "trojan-out",
  370. },
  371. },
  372. },
  373. },
  374. })
  375. testSuit(t, clientPort, testPort)
  376. }