json_service_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. package sub
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
  8. )
  9. func hasDirectOutOutbound(svc *SubJsonService) bool {
  10. for _, raw := range svc.defaultOutbounds {
  11. var outbound map[string]any
  12. if err := json.Unmarshal(raw, &outbound); err != nil {
  13. continue
  14. }
  15. if outbound["tag"] == "direct_out" {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. func outboundSettings(t *testing.T, raw []byte) map[string]any {
  22. t.Helper()
  23. var parsed map[string]any
  24. if err := json.Unmarshal(raw, &parsed); err != nil {
  25. t.Fatalf("failed to unmarshal outbound: %v", err)
  26. }
  27. settings, _ := parsed["settings"].(map[string]any)
  28. if settings == nil {
  29. t.Fatal("outbound has no settings")
  30. }
  31. return settings
  32. }
  33. func TestDefaultJSONUsesCompatibleLocalInbounds(t *testing.T) {
  34. svc := NewSubJsonService("", "", "", nil)
  35. inbounds, ok := svc.configJson["inbounds"].([]any)
  36. if !ok {
  37. t.Fatalf("default JSON inbounds = %#v, want array", svc.configJson["inbounds"])
  38. }
  39. byPort := make(map[float64]map[string]any, len(inbounds))
  40. for _, raw := range inbounds {
  41. inbound, ok := raw.(map[string]any)
  42. if !ok {
  43. t.Fatalf("default JSON inbound = %#v, want object", raw)
  44. }
  45. port, ok := inbound["port"].(float64)
  46. if !ok {
  47. t.Fatalf("default JSON inbound port = %#v, want number", inbound["port"])
  48. }
  49. byPort[port] = inbound
  50. }
  51. socks := byPort[10808]
  52. if socks == nil {
  53. t.Fatal("default JSON is missing the local inbound on port 10808")
  54. }
  55. if socks["protocol"] != "socks" || socks["tag"] != "mixed" {
  56. t.Fatalf("port 10808 protocol/tag = %v/%v, want socks/mixed", socks["protocol"], socks["tag"])
  57. }
  58. settings, _ := socks["settings"].(map[string]any)
  59. if settings == nil || settings["udp"] != true {
  60. t.Fatalf("port 10808 settings = %#v, want udp enabled", socks["settings"])
  61. }
  62. if socks["listen"] != "127.0.0.1" {
  63. t.Fatalf("port 10808 listen = %#v, want 127.0.0.1 (an unbound local inbound is exposed to the LAN and is not reachable by iOS packet tunnels)", socks["listen"])
  64. }
  65. http := byPort[10809]
  66. if http == nil || http["protocol"] != "http" {
  67. t.Fatalf("port 10809 inbound = %#v, want http protocol", http)
  68. }
  69. if http["listen"] != "127.0.0.1" {
  70. t.Fatalf("port 10809 listen = %#v, want 127.0.0.1", http["listen"])
  71. }
  72. }
  73. func TestSubJsonServiceVisionFlowDisablesTCPMuxOnly(t *testing.T) {
  74. globalMux := `{"enabled":true,"concurrency":8,"xudpConcurrency":16,"xudpProxyUDP443":"reject"}`
  75. svc := NewSubJsonService(globalMux, "", "", nil)
  76. inbound := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.VLESS, Settings: `{"encryption":"none"}`}
  77. decode := func(raw []byte) map[string]any {
  78. t.Helper()
  79. var ob map[string]any
  80. if err := json.Unmarshal(raw, &ob); err != nil {
  81. t.Fatalf("unmarshal outbound: %v", err)
  82. }
  83. return ob
  84. }
  85. vision := decode([]byte(svc.genVless(&SubService{}, inbound, nil, model.Client{ID: "uuid-1", Flow: "xtls-rprx-vision"}, globalMux)))
  86. mux, _ := vision["mux"].(map[string]any)
  87. if mux == nil {
  88. t.Fatalf("vision outbound must keep its mux object for the XUDP keys, got %#v", vision["mux"])
  89. }
  90. if mux["concurrency"] != float64(-1) {
  91. t.Fatalf("vision outbound mux.concurrency = %v, want -1 (TCP mux.cool is rejected by XTLS flows)", mux["concurrency"])
  92. }
  93. if mux["enabled"] != true || mux["xudpConcurrency"] != float64(16) || mux["xudpProxyUDP443"] != "reject" {
  94. t.Fatalf("vision outbound lost its XUDP settings: %#v", mux)
  95. }
  96. plain := decode([]byte(svc.genVless(&SubService{}, inbound, nil, model.Client{ID: "uuid-1"}, globalMux)))
  97. mux, _ = plain["mux"].(map[string]any)
  98. if mux == nil || mux["concurrency"] != float64(8) {
  99. t.Fatalf("flow-less outbound must keep the global mux unchanged, got %#v", plain["mux"])
  100. }
  101. noMux := decode([]byte(svc.genVless(&SubService{}, inbound, nil, model.Client{ID: "uuid-1", Flow: "xtls-rprx-vision"}, "")))
  102. if _, has := noMux["mux"]; has {
  103. t.Fatalf("no global mux must still mean no mux key, got %#v", noMux["mux"])
  104. }
  105. }
  106. func TestSubJsonServiceInjectsGlobalFinalMask(t *testing.T) {
  107. finalMask := `{"tcp":[{"type":"fragment","settings":{"packets":"tlshello","length":"100-200","delay":"10-20"}}],"udp":[{"type":"noise","settings":{"noise":[{"type":"base64","packet":"SGVsbG8="}]}}],"quicParams":{"congestion":"bbr"}}`
  108. svc := NewSubJsonService("", "", finalMask, nil)
  109. if hasDirectOutOutbound(svc) {
  110. t.Fatal("direct_out outbound must never be emitted")
  111. }
  112. stream := svc.streamData(`{"network":"tcp","security":"none","tcpSettings":{"header":{"type":"none"}}}`, "")
  113. if _, ok := stream["sockopt"]; ok {
  114. t.Fatal("legacy direct_out dialerProxy sockopt must never be set")
  115. }
  116. finalmask, _ := stream["finalmask"].(map[string]any)
  117. if finalmask == nil {
  118. t.Fatal("streamSettings is missing finalmask")
  119. }
  120. tcp, _ := finalmask["tcp"].([]any)
  121. if len(tcp) != 1 {
  122. t.Fatalf("tcp masks len = %d, want 1", len(tcp))
  123. }
  124. if first, _ := tcp[0].(map[string]any); first["type"] != "fragment" {
  125. t.Fatalf("tcp[0] type = %v, want fragment", first["type"])
  126. }
  127. udp, _ := finalmask["udp"].([]any)
  128. if len(udp) != 1 {
  129. t.Fatalf("udp masks len = %d, want 1", len(udp))
  130. }
  131. quic, _ := finalmask["quicParams"].(map[string]any)
  132. if quic == nil || quic["congestion"] != "bbr" {
  133. t.Fatalf("quicParams missing/wrong: %#v", finalmask["quicParams"])
  134. }
  135. }
  136. func TestSubJsonServiceMergesWithExistingFinalMask(t *testing.T) {
  137. finalMask := `{"tcp":[{"type":"fragment","settings":{"packets":"tlshello"}}]}`
  138. svc := NewSubJsonService("", "", finalMask, nil)
  139. stream := svc.streamData(`{
  140. "network":"tcp","security":"none","tcpSettings":{"header":{"type":"none"}},
  141. "finalmask":{"tcp":[{"type":"sudoku"}]}
  142. }`, "")
  143. finalmask, _ := stream["finalmask"].(map[string]any)
  144. tcp, _ := finalmask["tcp"].([]any)
  145. if len(tcp) != 2 {
  146. t.Fatalf("tcp masks len = %d, want 2 (existing + global)", len(tcp))
  147. }
  148. a, _ := tcp[0].(map[string]any)
  149. b, _ := tcp[1].(map[string]any)
  150. if a["type"] != "sudoku" || b["type"] != "fragment" {
  151. t.Fatalf("tcp masks = %#v, want existing sudoku then global fragment", tcp)
  152. }
  153. }
  154. func TestSubJsonServiceNoFinalMaskWhenEmpty(t *testing.T) {
  155. svc := NewSubJsonService("", "", "", nil)
  156. stream := svc.streamData(`{"network":"tcp","security":"none","tcpSettings":{"header":{"type":"none"}}}`, "")
  157. if _, ok := stream["finalmask"]; ok {
  158. t.Fatal("no finalmask should be emitted when subJsonFinalMask is empty")
  159. }
  160. if _, ok := stream["sockopt"]; ok {
  161. t.Fatal("legacy direct_out sockopt must never be set")
  162. }
  163. }
  164. // xray-core parses tlsSettings.pinnedPeerCertSha256 as a comma-separated string;
  165. // the JSON subscription must emit that form, not an array, or v2ray clients fail
  166. // to import the config (#5401).
  167. func TestSubJsonServicePinnedCertJoinedToString(t *testing.T) {
  168. svc := NewSubJsonService("", "", "", nil)
  169. stream := svc.streamData(`{"network":"tcp","security":"tls","tlsSettings":{"serverName":"a.example.com","settings":{"pinnedPeerCertSha256":["aa11","bb22"]}}}`, "")
  170. tls, _ := stream["tlsSettings"].(map[string]any)
  171. if tls == nil {
  172. t.Fatalf("tlsSettings missing: %#v", stream)
  173. }
  174. if got := tls["pinnedPeerCertSha256"]; got != "aa11,bb22" {
  175. t.Fatalf("pinnedPeerCertSha256 = %#v, want comma-separated string \"aa11,bb22\"", got)
  176. }
  177. }
  178. func TestSubJsonServiceTLSCipherSuitesForwarded(t *testing.T) {
  179. svc := NewSubJsonService("", "", "", nil)
  180. stream := svc.streamData(`{"network":"tcp","security":"tls","tlsSettings":{"serverName":"a.example.com","cipherSuites":"TLS_AES_256_GCM_SHA384","settings":{}}}`, "")
  181. tls, _ := stream["tlsSettings"].(map[string]any)
  182. if got := tls["cipherSuites"]; got != "TLS_AES_256_GCM_SHA384" {
  183. t.Fatalf("cipherSuites = %#v, want %q", got, "TLS_AES_256_GCM_SHA384")
  184. }
  185. stream = svc.streamData(`{"network":"tcp","security":"tls","tlsSettings":{"serverName":"a.example.com","cipherSuites":"","settings":{}}}`, "")
  186. tls, _ = stream["tlsSettings"].(map[string]any)
  187. if _, present := tls["cipherSuites"]; present {
  188. t.Fatalf("empty cipherSuites must be omitted, got %#v", tls["cipherSuites"])
  189. }
  190. }
  191. func TestSubJsonServiceVlessFlattened(t *testing.T) {
  192. inbound := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.VLESS, Settings: `{"encryption":"none"}`}
  193. client := model.Client{ID: "uuid-1", Flow: "xtls-rprx-vision"}
  194. settings := outboundSettings(t, NewSubJsonService("", "", "", nil).genVless(&SubService{}, inbound, nil, client, ""))
  195. if _, ok := settings["vnext"]; ok {
  196. t.Fatal("vless outbound must not use vnext")
  197. }
  198. if settings["address"] != "1.2.3.4" || settings["id"] != "uuid-1" || settings["encryption"] != "none" || settings["flow"] != "xtls-rprx-vision" {
  199. t.Fatalf("flat vless settings wrong: %#v", settings)
  200. }
  201. }
  202. func TestSubJsonServiceVlessFlowSuppressedByDisableFlow(t *testing.T) {
  203. inbound := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.VLESS, Settings: `{"encryption":"none"}`, DisableFlow: true}
  204. client := model.Client{ID: "uuid-1", Flow: "xtls-rprx-vision"}
  205. settings := outboundSettings(t, NewSubJsonService("", "", "", nil).genVless(&SubService{}, inbound, nil, client, ""))
  206. if _, ok := settings["flow"]; ok {
  207. t.Fatalf("DisableFlow inbound must not carry a flow in the JSON outbound: %#v", settings)
  208. }
  209. }
  210. func TestSubJsonServiceVmessFlattened(t *testing.T) {
  211. inbound := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.VMESS, Settings: `{}`}
  212. client := model.Client{ID: "uuid-2"}
  213. settings := outboundSettings(t, NewSubJsonService("", "", "", nil).genVnext(inbound, nil, client, ""))
  214. if _, ok := settings["vnext"]; ok {
  215. t.Fatal("vmess outbound must not use vnext")
  216. }
  217. if settings["id"] != "uuid-2" || settings["security"] != "auto" {
  218. t.Fatalf("flat vmess settings wrong: %#v", settings)
  219. }
  220. }
  221. // Shadowsocks/Trojan outbounds must use the standard "servers" array so older
  222. // bundled xray-cores (e.g. v2rayN) parse them; the flat top-level form only
  223. // works on very recent xray-core.
  224. func TestSubJsonServiceServerUsesServersArray(t *testing.T) {
  225. trojan := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.Trojan, Settings: `{}`}
  226. client := model.Client{Password: "p4ss"}
  227. settings := outboundSettings(t, NewSubJsonService("", "", "", nil).genServer(&SubService{}, trojan, nil, client, ""))
  228. server := firstServer(settings)
  229. if server == nil {
  230. t.Fatalf("trojan outbound must use a servers array, got: %#v", settings)
  231. }
  232. if server["password"] != "p4ss" || server["address"] != "1.2.3.4" {
  233. t.Fatalf("trojan server entry wrong: %#v", server)
  234. }
  235. if _, ok := server["method"]; ok {
  236. t.Fatalf("trojan must not carry method: %#v", server)
  237. }
  238. ss := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.Shadowsocks, Settings: `{"method":"aes-256-gcm"}`}
  239. ssSettings := outboundSettings(t, NewSubJsonService("", "", "", nil).genServer(&SubService{}, ss, nil, client, ""))
  240. ssServer := firstServer(ssSettings)
  241. if ssServer == nil {
  242. t.Fatalf("shadowsocks outbound must use a servers array, got: %#v", ssSettings)
  243. }
  244. if ssServer["method"] != "aes-256-gcm" {
  245. t.Fatalf("shadowsocks server entry must carry method: %#v", ssServer)
  246. }
  247. }
  248. func TestSubJsonServiceXmuxSuppressesGlobalMux(t *testing.T) {
  249. globalMux := `{"enabled":true,"concurrency":8}`
  250. svc := NewSubJsonService(globalMux, "", "", nil)
  251. // When xmux is present in xhttpSettings, the per-inbound xmux handles
  252. // multiplexing and the legacy outbound.Mux must NOT be set.
  253. stream := `{"network":"xhttp","security":"tls","tlsSettings":{"serverName":"example.com"},"xhttpSettings":{"path":"/api","mode":"packet-up","xmux":{"maxConcurrency":"16-32"}}}`
  254. parsed := svc.streamData(stream, "")
  255. mux := globalMux
  256. if xhttp, ok := parsed["xhttpSettings"].(map[string]any); ok {
  257. if _, hasXmux := xhttp["xmux"]; hasXmux {
  258. mux = ""
  259. }
  260. }
  261. streamSettings, _ := json.Marshal(parsed)
  262. inbound := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.VLESS, Settings: `{"encryption":"none"}`}
  263. client := model.Client{ID: "uuid-1"}
  264. raw := svc.genVless(&SubService{}, inbound, streamSettings, client, mux)
  265. var ob map[string]any
  266. if err := json.Unmarshal(raw, &ob); err != nil {
  267. t.Fatalf("unmarshal outbound: %v", err)
  268. }
  269. if _, has := ob["mux"]; has {
  270. t.Fatal("outbound.Mux must NOT be set when per-inbound xmux is present")
  271. }
  272. // Verify xmux is still inside xhttpSettings in streamSettings.
  273. ss, _ := ob["streamSettings"].(map[string]any)
  274. if ss == nil {
  275. t.Fatal("streamSettings missing from outbound")
  276. }
  277. xhttp, _ := ss["xhttpSettings"].(map[string]any)
  278. if xhttp == nil {
  279. t.Fatal("xhttpSettings missing from streamSettings")
  280. }
  281. xmux, _ := xhttp["xmux"].(map[string]any)
  282. if xmux == nil {
  283. t.Fatal("xmux missing from xhttpSettings — per-inbound xmux must survive streamData()")
  284. }
  285. if xmux["maxConcurrency"] != "16-32" {
  286. t.Fatalf("xmux.maxConcurrency = %v, want 16-32", xmux["maxConcurrency"])
  287. }
  288. }
  289. func TestSubJsonServiceGlobalMuxWhenNoXmux(t *testing.T) {
  290. globalMux := `{"enabled":true,"concurrency":8}`
  291. svc := NewSubJsonService(globalMux, "", "", nil)
  292. // When no xmux is present, the global subJsonMux should be used.
  293. stream := `{"network":"xhttp","security":"tls","tlsSettings":{"serverName":"example.com"},"xhttpSettings":{"path":"/api","mode":"packet-up"}}`
  294. parsed := svc.streamData(stream, "")
  295. mux := globalMux
  296. if xhttp, ok := parsed["xhttpSettings"].(map[string]any); ok {
  297. if _, hasXmux := xhttp["xmux"]; hasXmux {
  298. mux = ""
  299. }
  300. }
  301. streamSettings, _ := json.Marshal(parsed)
  302. inbound := &model.Inbound{Listen: "1.2.3.4", Port: 443, Protocol: model.VLESS, Settings: `{"encryption":"none"}`}
  303. client := model.Client{ID: "uuid-1"}
  304. raw := svc.genVless(&SubService{}, inbound, streamSettings, client, mux)
  305. var ob map[string]any
  306. if err := json.Unmarshal(raw, &ob); err != nil {
  307. t.Fatalf("unmarshal outbound: %v", err)
  308. }
  309. m, has := ob["mux"]
  310. if !has {
  311. t.Fatal("outbound.Mux must be set when global subJsonMux is configured and no per-inbound xmux")
  312. }
  313. mm, _ := m.(map[string]any)
  314. if mm["enabled"] != true || mm["concurrency"] != float64(8) {
  315. t.Fatalf("mux payload wrong: %#v", m)
  316. }
  317. }
  318. func realitySpiderXFromStream(t *testing.T, svc *SubJsonService, clientKey string) string {
  319. t.Helper()
  320. stream := svc.streamData(`{
  321. "network":"tcp","security":"reality","tcpSettings":{"header":{"type":"none"}},
  322. "realitySettings":{
  323. "serverNames":["reality.example.com"],
  324. "shortIds":["ab12cd"],
  325. "settings":{"publicKey":"PBKvalue","fingerprint":"firefox","spiderX":"/seed"}
  326. }
  327. }`, clientKey)
  328. rlty, _ := stream["realitySettings"].(map[string]any)
  329. if rlty == nil {
  330. t.Fatal("streamData dropped realitySettings")
  331. }
  332. spx, _ := rlty["spiderX"].(string)
  333. if len(spx) != 16 || spx[0] != '/' {
  334. t.Fatalf("spiderX = %q, want a 16-char /-prefixed value", spx)
  335. }
  336. return spx
  337. }
  338. func TestSubJsonServiceRealityDataDerivesPerClientSpiderX(t *testing.T) {
  339. svc := NewSubJsonService("", "", "", nil)
  340. alice := realitySpiderXFromStream(t, svc, "subAlice")
  341. if again := realitySpiderXFromStream(t, svc, "subAlice"); again != alice {
  342. t.Fatalf("spiderX not stable for the same client: %q vs %q", alice, again)
  343. }
  344. if bob := realitySpiderXFromStream(t, svc, "subBob"); bob == alice {
  345. t.Fatalf("spiderX identical across clients (fingerprintable): %q", alice)
  346. }
  347. }
  348. // streamData must tolerate malformed stored inbounds: unparseable stream JSON
  349. // (with a finalMask configured, which writes into the map) and tls/reality
  350. // security whose settings key is missing or null previously panicked the
  351. // subscription request.
  352. func TestSubJsonServiceStreamDataMalformedInputs(t *testing.T) {
  353. withMask := NewSubJsonService("", "", `{"tcp":[{"type":"fragment"}]}`, nil)
  354. stream := withMask.streamData("not-json", "clientKey")
  355. if _, ok := stream["finalmask"]; !ok {
  356. t.Fatal("finalMask must still apply when stream settings fail to parse")
  357. }
  358. svc := NewSubJsonService("", "", "", nil)
  359. noReality := svc.streamData(`{"network":"tcp","security":"reality"}`, "clientKey")
  360. if v, ok := noReality["realitySettings"]; ok {
  361. t.Fatalf("missing realitySettings must stay absent, got %v", v)
  362. }
  363. nullTls := svc.streamData(`{"network":"tcp","security":"tls","tlsSettings":null}`, "")
  364. if v, ok := nullTls["tlsSettings"]; ok {
  365. t.Fatalf("null tlsSettings must be dropped, got %v", v)
  366. }
  367. }
  368. func TestSubJsonServiceRealityDataSpiderXFallsBackWhenNoClientKey(t *testing.T) {
  369. svc := NewSubJsonService("", "", "", nil)
  370. stream := svc.streamData(`{
  371. "network":"tcp","security":"reality","tcpSettings":{"header":{"type":"none"}},
  372. "realitySettings":{
  373. "serverNames":["reality.example.com"],
  374. "shortIds":["ab12cd"],
  375. "settings":{"publicKey":"PBKvalue","fingerprint":"firefox"}
  376. }
  377. }`, "")
  378. rlty, _ := stream["realitySettings"].(map[string]any)
  379. if rlty == nil {
  380. t.Fatal("streamData dropped realitySettings")
  381. }
  382. spx, _ := rlty["spiderX"].(string)
  383. if len(spx) != 16 || spx[0] != '/' {
  384. t.Fatalf("spiderX fallback = %q, want random 16-char /-prefixed value", spx)
  385. }
  386. }
  387. func TestSubJsonServiceWireguard(t *testing.T) {
  388. serverPriv, serverPub, err := wgutil.GenerateWireguardKeypair()
  389. if err != nil {
  390. t.Fatalf("server keypair: %v", err)
  391. }
  392. clientPriv, _, err := wgutil.GenerateWireguardKeypair()
  393. if err != nil {
  394. t.Fatalf("client keypair: %v", err)
  395. }
  396. inbound := &model.Inbound{
  397. Listen: "203.0.113.9",
  398. Port: 51820,
  399. Protocol: model.WireGuard,
  400. Settings: `{"secretKey":"` + serverPriv + `","mtu":1420}`,
  401. }
  402. client := model.Client{
  403. Email: "user",
  404. PrivateKey: clientPriv,
  405. PreSharedKey: "psk-value",
  406. KeepAlive: 25,
  407. AllowedIPs: []string{"10.0.0.2/32", "fd00::2/128"},
  408. }
  409. raw := NewSubJsonService("", "", "", nil).genWireguard(inbound, client)
  410. if raw == nil {
  411. t.Fatal("genWireguard returned nil for a valid wireguard client")
  412. }
  413. settings := outboundSettings(t, raw)
  414. if settings["secretKey"] != clientPriv {
  415. t.Fatalf("secretKey = %v, want client private key", settings["secretKey"])
  416. }
  417. address, _ := settings["address"].([]any)
  418. if len(address) != 2 || address[0] != "10.0.0.2/32" || address[1] != "fd00::2/128" {
  419. t.Fatalf("address = %v, want client tunnel addresses", settings["address"])
  420. }
  421. if settings["mtu"] != float64(1420) {
  422. t.Fatalf("mtu = %v, want 1420", settings["mtu"])
  423. }
  424. peers, _ := settings["peers"].([]any)
  425. if len(peers) != 1 {
  426. t.Fatalf("peers len = %d, want 1", len(peers))
  427. }
  428. peer, _ := peers[0].(map[string]any)
  429. if peer["publicKey"] != serverPub {
  430. t.Fatalf("peer publicKey = %v, want %v (derived from inbound secretKey)", peer["publicKey"], serverPub)
  431. }
  432. if peer["endpoint"] != "203.0.113.9:51820" {
  433. t.Fatalf("peer endpoint = %v, want 203.0.113.9:51820", peer["endpoint"])
  434. }
  435. if peer["preSharedKey"] != "psk-value" {
  436. t.Fatalf("peer preSharedKey = %v, want psk-value", peer["preSharedKey"])
  437. }
  438. if peer["keepAlive"] != float64(25) {
  439. t.Fatalf("peer keepAlive = %v, want 25", peer["keepAlive"])
  440. }
  441. allowed, _ := peer["allowedIPs"].([]any)
  442. if !reflect.DeepEqual(allowed, []any{"0.0.0.0/0", "::/0"}) {
  443. t.Fatalf("peer allowedIPs = %v, want full tunnel", peer["allowedIPs"])
  444. }
  445. }
  446. func TestSubJsonServiceWireguardNoKey(t *testing.T) {
  447. inbound := &model.Inbound{Listen: "203.0.113.9", Port: 51820, Protocol: model.WireGuard, Settings: `{}`}
  448. client := model.Client{Email: "user"}
  449. if raw := NewSubJsonService("", "", "", nil).genWireguard(inbound, client); raw != nil {
  450. t.Fatalf("genWireguard = %s, want nil for a keyless wireguard client", raw)
  451. }
  452. }