1
0

vmess_test.go 8.1 KB

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