vmess_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/gofrs/uuid/v5"
  9. "github.com/spyzhov/ajson"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func newUUID() uuid.UUID {
  13. user, _ := uuid.DefaultGenerator.NewV4()
  14. return user
  15. }
  16. func TestVMessAuto(t *testing.T) {
  17. security := "auto"
  18. t.Run("self", func(t *testing.T) {
  19. testVMessSelf(t, security, 0, false, false, false)
  20. })
  21. t.Run("packetaddr", func(t *testing.T) {
  22. testVMessSelf(t, security, 0, false, false, true)
  23. })
  24. t.Run("inbound", func(t *testing.T) {
  25. testVMessInboundWithV2Ray(t, security, 0, false)
  26. })
  27. t.Run("outbound", func(t *testing.T) {
  28. testVMessOutboundWithV2Ray(t, security, false, false, 0)
  29. })
  30. }
  31. func TestVMess(t *testing.T) {
  32. for _, security := range []string{
  33. "zero",
  34. } {
  35. t.Run(security, func(t *testing.T) {
  36. testVMess0(t, security)
  37. })
  38. }
  39. for _, security := range []string{
  40. "none",
  41. } {
  42. t.Run(security, func(t *testing.T) {
  43. testVMess1(t, security)
  44. })
  45. }
  46. for _, security := range []string{
  47. "aes-128-gcm", "chacha20-poly1305", "aes-128-cfb",
  48. } {
  49. t.Run(security, func(t *testing.T) {
  50. testVMess2(t, security)
  51. })
  52. }
  53. }
  54. func testVMess0(t *testing.T, security string) {
  55. t.Run("self", func(t *testing.T) {
  56. testVMessSelf(t, security, 0, false, false, false)
  57. })
  58. t.Run("self-legacy", func(t *testing.T) {
  59. testVMessSelf(t, security, 1, false, false, false)
  60. })
  61. t.Run("packetaddr", func(t *testing.T) {
  62. testVMessSelf(t, security, 0, false, false, true)
  63. })
  64. t.Run("outbound", func(t *testing.T) {
  65. testVMessOutboundWithV2Ray(t, security, false, false, 0)
  66. })
  67. t.Run("outbound-legacy", func(t *testing.T) {
  68. testVMessOutboundWithV2Ray(t, security, false, false, 1)
  69. })
  70. }
  71. func testVMess1(t *testing.T, security string) {
  72. t.Run("self", func(t *testing.T) {
  73. testVMessSelf(t, security, 0, false, false, false)
  74. })
  75. t.Run("self-legacy", func(t *testing.T) {
  76. testVMessSelf(t, security, 1, false, false, false)
  77. })
  78. t.Run("packetaddr", func(t *testing.T) {
  79. testVMessSelf(t, security, 0, false, false, true)
  80. })
  81. t.Run("inbound", func(t *testing.T) {
  82. testVMessInboundWithV2Ray(t, security, 0, false)
  83. })
  84. t.Run("outbound", func(t *testing.T) {
  85. testVMessOutboundWithV2Ray(t, security, false, false, 0)
  86. })
  87. t.Run("outbound-legacy", func(t *testing.T) {
  88. testVMessOutboundWithV2Ray(t, security, false, false, 1)
  89. })
  90. }
  91. func testVMess2(t *testing.T, security string) {
  92. t.Run("self", func(t *testing.T) {
  93. testVMessSelf(t, security, 0, false, false, false)
  94. })
  95. t.Run("self-padding", func(t *testing.T) {
  96. testVMessSelf(t, security, 0, true, false, false)
  97. })
  98. t.Run("self-authid", func(t *testing.T) {
  99. testVMessSelf(t, security, 0, false, true, false)
  100. })
  101. t.Run("self-padding-authid", func(t *testing.T) {
  102. testVMessSelf(t, security, 0, true, true, false)
  103. })
  104. t.Run("self-legacy", func(t *testing.T) {
  105. testVMessSelf(t, security, 1, false, false, false)
  106. })
  107. t.Run("self-legacy-padding", func(t *testing.T) {
  108. testVMessSelf(t, security, 1, true, false, false)
  109. })
  110. t.Run("packetaddr", func(t *testing.T) {
  111. testVMessSelf(t, security, 0, false, false, true)
  112. })
  113. t.Run("inbound", func(t *testing.T) {
  114. testVMessInboundWithV2Ray(t, security, 0, false)
  115. })
  116. t.Run("inbound-authid", func(t *testing.T) {
  117. testVMessInboundWithV2Ray(t, security, 0, true)
  118. })
  119. t.Run("inbound-legacy", func(t *testing.T) {
  120. testVMessInboundWithV2Ray(t, security, 64, false)
  121. })
  122. t.Run("outbound", func(t *testing.T) {
  123. testVMessOutboundWithV2Ray(t, security, false, false, 0)
  124. })
  125. t.Run("outbound-padding", func(t *testing.T) {
  126. testVMessOutboundWithV2Ray(t, security, true, false, 0)
  127. })
  128. t.Run("outbound-authid", func(t *testing.T) {
  129. testVMessOutboundWithV2Ray(t, security, false, true, 0)
  130. })
  131. t.Run("outbound-padding-authid", func(t *testing.T) {
  132. testVMessOutboundWithV2Ray(t, security, true, true, 0)
  133. })
  134. t.Run("outbound-legacy", func(t *testing.T) {
  135. testVMessOutboundWithV2Ray(t, security, false, false, 1)
  136. })
  137. t.Run("outbound-legacy-padding", func(t *testing.T) {
  138. testVMessOutboundWithV2Ray(t, security, true, false, 1)
  139. })
  140. }
  141. func testVMessInboundWithV2Ray(t *testing.T, security string, alterId int, authenticatedLength bool) {
  142. userId := newUUID()
  143. content, err := os.ReadFile("config/vmess-client.json")
  144. require.NoError(t, err)
  145. config, err := ajson.Unmarshal(content)
  146. require.NoError(t, err)
  147. config.MustKey("inbounds").MustIndex(0).MustKey("port").SetNumeric(float64(clientPort))
  148. outbound := config.MustKey("outbounds").MustIndex(0).MustKey("settings").MustKey("vnext").MustIndex(0)
  149. outbound.MustKey("port").SetNumeric(float64(serverPort))
  150. user := outbound.MustKey("users").MustIndex(0)
  151. user.MustKey("id").SetString(userId.String())
  152. user.MustKey("alterId").SetNumeric(float64(alterId))
  153. user.MustKey("security").SetString(security)
  154. var experiments string
  155. if authenticatedLength {
  156. experiments += "AuthenticatedLength"
  157. }
  158. user.MustKey("experiments").SetString(experiments)
  159. content, err = ajson.Marshal(config)
  160. require.NoError(t, err)
  161. startDockerContainer(t, DockerOptions{
  162. Image: ImageV2RayCore,
  163. Ports: []uint16{serverPort, testPort},
  164. EntryPoint: "v2ray",
  165. Cmd: []string{"run"},
  166. Stdin: content,
  167. Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
  168. })
  169. startInstance(t, option.Options{
  170. Inbounds: []option.Inbound{
  171. {
  172. Type: C.TypeVMess,
  173. VMessOptions: option.VMessInboundOptions{
  174. ListenOptions: option.ListenOptions{
  175. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  176. ListenPort: serverPort,
  177. },
  178. Users: []option.VMessUser{
  179. {
  180. Name: "sekai",
  181. UUID: userId.String(),
  182. AlterId: alterId,
  183. },
  184. },
  185. },
  186. },
  187. },
  188. })
  189. testSuitSimple(t, clientPort, testPort)
  190. }
  191. func testVMessOutboundWithV2Ray(t *testing.T, security string, globalPadding bool, authenticatedLength bool, alterId int) {
  192. user := newUUID()
  193. content, err := os.ReadFile("config/vmess-server.json")
  194. require.NoError(t, err)
  195. config, err := ajson.Unmarshal(content)
  196. require.NoError(t, err)
  197. inbound := config.MustKey("inbounds").MustIndex(0)
  198. inbound.MustKey("port").SetNumeric(float64(serverPort))
  199. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  200. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("alterId").SetNumeric(float64(alterId))
  201. content, err = ajson.Marshal(config)
  202. require.NoError(t, err)
  203. startDockerContainer(t, DockerOptions{
  204. Image: ImageV2RayCore,
  205. Ports: []uint16{serverPort, testPort},
  206. EntryPoint: "v2ray",
  207. Cmd: []string{"run"},
  208. Stdin: content,
  209. Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
  210. })
  211. startInstance(t, option.Options{
  212. Inbounds: []option.Inbound{
  213. {
  214. Type: C.TypeMixed,
  215. MixedOptions: option.HTTPMixedInboundOptions{
  216. ListenOptions: option.ListenOptions{
  217. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  218. ListenPort: clientPort,
  219. },
  220. },
  221. },
  222. },
  223. Outbounds: []option.Outbound{
  224. {
  225. Type: C.TypeVMess,
  226. VMessOptions: option.VMessOutboundOptions{
  227. ServerOptions: option.ServerOptions{
  228. Server: "127.0.0.1",
  229. ServerPort: serverPort,
  230. },
  231. Security: security,
  232. UUID: user.String(),
  233. GlobalPadding: globalPadding,
  234. AuthenticatedLength: authenticatedLength,
  235. AlterId: alterId,
  236. },
  237. },
  238. },
  239. })
  240. testSuit(t, clientPort, testPort)
  241. }
  242. func testVMessSelf(t *testing.T, security string, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) {
  243. user := newUUID()
  244. startInstance(t, option.Options{
  245. Inbounds: []option.Inbound{
  246. {
  247. Type: C.TypeMixed,
  248. Tag: "mixed-in",
  249. MixedOptions: option.HTTPMixedInboundOptions{
  250. ListenOptions: option.ListenOptions{
  251. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  252. ListenPort: clientPort,
  253. },
  254. },
  255. },
  256. {
  257. Type: C.TypeVMess,
  258. VMessOptions: option.VMessInboundOptions{
  259. ListenOptions: option.ListenOptions{
  260. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  261. ListenPort: serverPort,
  262. },
  263. Users: []option.VMessUser{
  264. {
  265. Name: "sekai",
  266. UUID: user.String(),
  267. AlterId: alterId,
  268. },
  269. },
  270. },
  271. },
  272. },
  273. Outbounds: []option.Outbound{
  274. {
  275. Type: C.TypeDirect,
  276. },
  277. {
  278. Type: C.TypeVMess,
  279. Tag: "vmess-out",
  280. VMessOptions: option.VMessOutboundOptions{
  281. ServerOptions: option.ServerOptions{
  282. Server: "127.0.0.1",
  283. ServerPort: serverPort,
  284. },
  285. Security: security,
  286. UUID: user.String(),
  287. AlterId: alterId,
  288. GlobalPadding: globalPadding,
  289. AuthenticatedLength: authenticatedLength,
  290. PacketEncoding: "packetaddr",
  291. },
  292. },
  293. },
  294. Route: &option.RouteOptions{
  295. Rules: []option.Rule{
  296. {
  297. DefaultOptions: option.DefaultRule{
  298. Inbound: []string{"mixed-in"},
  299. Outbound: "vmess-out",
  300. },
  301. },
  302. },
  303. },
  304. })
  305. testSuit(t, clientPort, testPort)
  306. }