vmess_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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"
  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. Stdin: content,
  166. Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
  167. })
  168. startInstance(t, option.Options{
  169. Inbounds: []option.Inbound{
  170. {
  171. Type: C.TypeVMess,
  172. VMessOptions: option.VMessInboundOptions{
  173. ListenOptions: option.ListenOptions{
  174. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  175. ListenPort: serverPort,
  176. },
  177. Users: []option.VMessUser{
  178. {
  179. Name: "sekai",
  180. UUID: userId.String(),
  181. AlterId: alterId,
  182. },
  183. },
  184. },
  185. },
  186. },
  187. })
  188. testSuitSimple(t, clientPort, testPort)
  189. }
  190. func testVMessOutboundWithV2Ray(t *testing.T, security string, globalPadding bool, authenticatedLength bool, alterId int) {
  191. user := newUUID()
  192. content, err := os.ReadFile("config/vmess-server.json")
  193. require.NoError(t, err)
  194. config, err := ajson.Unmarshal(content)
  195. require.NoError(t, err)
  196. inbound := config.MustKey("inbounds").MustIndex(0)
  197. inbound.MustKey("port").SetNumeric(float64(serverPort))
  198. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
  199. inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("alterId").SetNumeric(float64(alterId))
  200. content, err = ajson.Marshal(config)
  201. require.NoError(t, err)
  202. startDockerContainer(t, DockerOptions{
  203. Image: ImageV2RayCore,
  204. Ports: []uint16{serverPort, testPort},
  205. EntryPoint: "v2ray",
  206. Stdin: content,
  207. Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
  208. })
  209. startInstance(t, option.Options{
  210. Inbounds: []option.Inbound{
  211. {
  212. Type: C.TypeMixed,
  213. MixedOptions: option.HTTPMixedInboundOptions{
  214. ListenOptions: option.ListenOptions{
  215. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  216. ListenPort: clientPort,
  217. },
  218. },
  219. },
  220. },
  221. Outbounds: []option.Outbound{
  222. {
  223. Type: C.TypeVMess,
  224. VMessOptions: option.VMessOutboundOptions{
  225. ServerOptions: option.ServerOptions{
  226. Server: "127.0.0.1",
  227. ServerPort: serverPort,
  228. },
  229. Security: security,
  230. UUID: user.String(),
  231. GlobalPadding: globalPadding,
  232. AuthenticatedLength: authenticatedLength,
  233. AlterId: alterId,
  234. },
  235. },
  236. },
  237. })
  238. testSuit(t, clientPort, testPort)
  239. }
  240. func testVMessSelf(t *testing.T, security string, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) {
  241. user := newUUID()
  242. startInstance(t, option.Options{
  243. Inbounds: []option.Inbound{
  244. {
  245. Type: C.TypeMixed,
  246. Tag: "mixed-in",
  247. MixedOptions: option.HTTPMixedInboundOptions{
  248. ListenOptions: option.ListenOptions{
  249. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  250. ListenPort: clientPort,
  251. },
  252. },
  253. },
  254. {
  255. Type: C.TypeVMess,
  256. VMessOptions: option.VMessInboundOptions{
  257. ListenOptions: option.ListenOptions{
  258. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  259. ListenPort: serverPort,
  260. },
  261. Users: []option.VMessUser{
  262. {
  263. Name: "sekai",
  264. UUID: user.String(),
  265. AlterId: alterId,
  266. },
  267. },
  268. },
  269. },
  270. },
  271. Outbounds: []option.Outbound{
  272. {
  273. Type: C.TypeDirect,
  274. },
  275. {
  276. Type: C.TypeVMess,
  277. Tag: "vmess-out",
  278. VMessOptions: option.VMessOutboundOptions{
  279. ServerOptions: option.ServerOptions{
  280. Server: "127.0.0.1",
  281. ServerPort: serverPort,
  282. },
  283. Security: security,
  284. UUID: user.String(),
  285. AlterId: alterId,
  286. GlobalPadding: globalPadding,
  287. AuthenticatedLength: authenticatedLength,
  288. PacketEncoding: "packetaddr",
  289. },
  290. },
  291. },
  292. Route: &option.RouteOptions{
  293. Rules: []option.Rule{
  294. {
  295. DefaultOptions: option.DefaultRule{
  296. Inbound: []string{"mixed-in"},
  297. Outbound: "vmess-out",
  298. },
  299. },
  300. },
  301. },
  302. })
  303. testSuit(t, clientPort, testPort)
  304. }