1
0

vmess_test.go 9.2 KB

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