outbound.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. // Package link provides parsers for VPN share links (vmess://, vless://, etc.)
  2. // and subscription bodies (typically base64-encoded newline lists of such links).
  3. // The output shape matches the wire format used by the panel's Xray template
  4. // outbounds array so that parsed objects can be injected directly.
  5. package link
  6. import (
  7. "encoding/base64"
  8. "encoding/json"
  9. "fmt"
  10. "net/url"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. )
  15. // Outbound is the minimal shape we emit for each parsed link.
  16. // Extra fields (mux, etc.) are carried inside settings/streamSettings.
  17. type Outbound map[string]any
  18. // ParseResult holds a parsed outbound together with a stable identity string
  19. // that can be used to correlate the same logical server across refreshes
  20. // (even if the remark changes).
  21. type ParseResult struct {
  22. Outbound Outbound
  23. Identity string
  24. }
  25. // ParseSubscriptionBody accepts the raw body returned by a subscription URL.
  26. // It handles the common case where the body is a base64-encoded blob of
  27. // newline-separated links, and also tolerates an already-decoded text body.
  28. // It returns the list of successfully parsed outbounds (in order) and their
  29. // corresponding identities.
  30. func ParseSubscriptionBody(body []byte) ([]Outbound, []string, error) {
  31. text := strings.TrimSpace(string(body))
  32. if text == "" {
  33. return nil, nil, nil
  34. }
  35. // Try base64 decode first (standard and URL-safe variants).
  36. if decoded, ok := tryBase64(text); ok {
  37. text = strings.TrimSpace(decoded)
  38. }
  39. lines := splitLines(text)
  40. var outbounds []Outbound
  41. var identities []string
  42. for _, ln := range lines {
  43. ln = strings.TrimSpace(ln)
  44. if ln == "" || strings.HasPrefix(ln, "#") {
  45. continue
  46. }
  47. res, err := ParseLink(ln)
  48. if err != nil || res == nil {
  49. // Ignore unparseable lines (comments, unsupported protocols, etc.)
  50. continue
  51. }
  52. outbounds = append(outbounds, res.Outbound)
  53. identities = append(identities, res.Identity)
  54. }
  55. return outbounds, identities, nil
  56. }
  57. func tryBase64(s string) (string, bool) {
  58. // Remove whitespace that some providers insert.
  59. clean := strings.Map(func(r rune) rune {
  60. if r == ' ' || r == '\n' || r == '\r' || r == '\t' {
  61. return -1
  62. }
  63. return r
  64. }, s)
  65. // Common padding fix
  66. for len(clean)%4 != 0 {
  67. clean += "="
  68. }
  69. // Standard
  70. if b, err := base64.StdEncoding.DecodeString(clean); err == nil {
  71. return string(b), true
  72. }
  73. // URL-safe (no padding)
  74. if b, err := base64.RawURLEncoding.DecodeString(clean); err == nil {
  75. return string(b), true
  76. }
  77. // URL-safe with padding
  78. if b, err := base64.URLEncoding.DecodeString(clean); err == nil {
  79. return string(b), true
  80. }
  81. return "", false
  82. }
  83. func splitLines(s string) []string {
  84. // Accept \n, \r\n, and also some providers use literal \n in the text.
  85. s = strings.ReplaceAll(s, `\n`, "\n")
  86. return strings.FieldsFunc(s, func(r rune) bool { return r == '\n' || r == '\r' })
  87. }
  88. // ParseLink parses a single share link and returns the outbound object plus
  89. // a stable identity for tag correlation. Supported schemes:
  90. // - vmess://
  91. // - vless://
  92. // - trojan://
  93. // - ss:// (modern and legacy)
  94. // - hysteria2:// (also hy2://)
  95. // - wireguard:// (also wg://)
  96. func ParseLink(link string) (*ParseResult, error) {
  97. link = strings.TrimSpace(link)
  98. switch {
  99. case strings.HasPrefix(link, "vmess://"):
  100. return parseVmess(link)
  101. case strings.HasPrefix(link, "vless://"):
  102. return parseVless(link)
  103. case strings.HasPrefix(link, "trojan://"):
  104. return parseTrojan(link)
  105. case strings.HasPrefix(link, "ss://"):
  106. return parseShadowsocks(link)
  107. case strings.HasPrefix(link, "hysteria2://"), strings.HasPrefix(link, "hy2://"):
  108. return parseHysteria2(link)
  109. case strings.HasPrefix(link, "wireguard://"), strings.HasPrefix(link, "wg://"):
  110. return parseWireguard(link)
  111. default:
  112. return nil, fmt.Errorf("unsupported link scheme")
  113. }
  114. }
  115. // --- vmess ---
  116. func parseVmess(link string) (*ParseResult, error) {
  117. b64 := strings.TrimPrefix(link, "vmess://")
  118. // vmess:// base64(json)
  119. raw, err := base64.StdEncoding.DecodeString(padBase64(b64))
  120. if err != nil {
  121. // Some providers use raw URL-safe
  122. raw, err = base64.RawURLEncoding.DecodeString(b64)
  123. }
  124. if err != nil {
  125. return nil, fmt.Errorf("vmess decode: %w", err)
  126. }
  127. var j map[string]any
  128. if err := json.Unmarshal(raw, &j); err != nil {
  129. return nil, fmt.Errorf("vmess json: %w", err)
  130. }
  131. identity := vmessIdentity(j)
  132. network := getString(j, "net", "tcp")
  133. security := "none"
  134. if tls, _ := j["tls"].(string); tls == "tls" {
  135. security = "tls"
  136. }
  137. stream := buildStream(network, security)
  138. // Map known fields (best effort, matching frontend parser coverage)
  139. switch network {
  140. case "ws":
  141. if host, ok := j["host"].(string); ok {
  142. setWS(stream, host, getString(j, "path", "/"))
  143. }
  144. case "grpc":
  145. svc := getString(j, "path", "")
  146. if auth, ok := j["authority"].(string); ok && auth != "" {
  147. (stream["grpcSettings"].(map[string]any))["authority"] = auth
  148. }
  149. (stream["grpcSettings"].(map[string]any))["serviceName"] = svc
  150. (stream["grpcSettings"].(map[string]any))["multiMode"] = getString(j, "type", "") == "multi"
  151. case "httpupgrade":
  152. setHTTPUpgrade(stream, getString(j, "host", ""), getString(j, "path", "/"))
  153. case "xhttp":
  154. xh := stream["xhttpSettings"].(map[string]any)
  155. xh["host"] = getString(j, "host", "")
  156. xh["path"] = getString(j, "path", "/")
  157. if m := getString(j, "mode", ""); m != "" {
  158. xh["mode"] = m
  159. }
  160. // xhttp advanced keys are passed through if present in the json
  161. for _, k := range []string{"xPaddingBytes", "scMaxEachPostBytes", "scMinPostsIntervalMs"} {
  162. if v, ok := j[k]; ok {
  163. xh[k] = v
  164. }
  165. }
  166. case "tcp":
  167. if getString(j, "type", "") == "http" {
  168. stream["tcpSettings"] = map[string]any{
  169. "header": map[string]any{
  170. "type": "http",
  171. "request": map[string]any{
  172. "version": "1.1",
  173. "method": "GET",
  174. "path": splitComma(getString(j, "path", "/")),
  175. "headers": map[string]any{"Host": splitComma(getString(j, "host", ""))},
  176. },
  177. },
  178. }
  179. }
  180. }
  181. if security == "tls" {
  182. tls := stream["tlsSettings"].(map[string]any)
  183. tls["serverName"] = getString(j, "sni", "")
  184. tls["fingerprint"] = getString(j, "fp", "")
  185. if alpn := getString(j, "alpn", ""); alpn != "" {
  186. tls["alpn"] = splitComma(alpn)
  187. }
  188. }
  189. port := num(j["port"])
  190. ob := Outbound{
  191. "protocol": "vmess",
  192. "tag": getString(j, "ps", ""),
  193. "settings": map[string]any{
  194. "vnext": []any{
  195. map[string]any{
  196. "address": getString(j, "add", ""),
  197. "port": port,
  198. "users": []any{
  199. map[string]any{
  200. "id": getString(j, "id", ""),
  201. "security": getString(j, "scy", "auto"),
  202. },
  203. },
  204. },
  205. },
  206. },
  207. "streamSettings": stream,
  208. }
  209. return &ParseResult{Outbound: ob, Identity: identity}, nil
  210. }
  211. func vmessIdentity(j map[string]any) string {
  212. // Remove ps (remark) for identity
  213. core := map[string]any{}
  214. for k, v := range j {
  215. if k == "ps" {
  216. continue
  217. }
  218. core[k] = v
  219. }
  220. b, _ := json.Marshal(core)
  221. return "vmess:" + string(b)
  222. }
  223. // --- vless / trojan (URL forms) ---
  224. func parseVless(link string) (*ParseResult, error) {
  225. u, err := url.Parse(link)
  226. if err != nil {
  227. return nil, err
  228. }
  229. if u.Scheme != "vless" {
  230. return nil, fmt.Errorf("not vless")
  231. }
  232. id := u.User.Username()
  233. host := u.Hostname()
  234. port := defaultPort(u.Port(), 443)
  235. params := u.Query()
  236. network := params.Get("type")
  237. if network == "" {
  238. network = "tcp"
  239. }
  240. security := params.Get("security")
  241. if security == "" {
  242. security = "none"
  243. }
  244. stream := buildStream(network, security)
  245. applyTransport(stream, params)
  246. applySecurity(stream, params)
  247. applyFinalMask(stream, params)
  248. identity := "vless:" + u.Scheme + "://" + id + "@" + host + ":" + strconv.Itoa(port) + "?" + canonicalQuery(params)
  249. ob := Outbound{
  250. "protocol": "vless",
  251. "tag": decodeHash(u.Fragment),
  252. "settings": map[string]any{
  253. "address": host,
  254. "port": port,
  255. "id": id,
  256. "flow": params.Get("flow"),
  257. "encryption": firstNonEmpty(params.Get("encryption"), "none"),
  258. },
  259. "streamSettings": stream,
  260. }
  261. return &ParseResult{Outbound: ob, Identity: identity}, nil
  262. }
  263. func parseTrojan(link string) (*ParseResult, error) {
  264. u, err := url.Parse(link)
  265. if err != nil {
  266. return nil, err
  267. }
  268. if u.Scheme != "trojan" {
  269. return nil, fmt.Errorf("not trojan")
  270. }
  271. pw := u.User.Username()
  272. host := u.Hostname()
  273. port := defaultPort(u.Port(), 443)
  274. params := u.Query()
  275. network := params.Get("type")
  276. if network == "" {
  277. network = "tcp"
  278. }
  279. security := params.Get("security")
  280. if security == "" {
  281. security = "tls"
  282. }
  283. stream := buildStream(network, security)
  284. applyTransport(stream, params)
  285. applySecurity(stream, params)
  286. applyFinalMask(stream, params)
  287. identity := "trojan:" + u.Scheme + "://" + pw + "@" + host + ":" + strconv.Itoa(port) + "?" + canonicalQuery(params)
  288. ob := Outbound{
  289. "protocol": "trojan",
  290. "tag": decodeHash(u.Fragment),
  291. "settings": map[string]any{
  292. "servers": []any{
  293. map[string]any{"address": host, "port": port, "password": pw},
  294. },
  295. },
  296. "streamSettings": stream,
  297. }
  298. return &ParseResult{Outbound: ob, Identity: identity}, nil
  299. }
  300. // --- shadowsocks ---
  301. func parseShadowsocks(link string) (*ParseResult, error) {
  302. // Two shapes:
  303. // ss://base64(method:pass)@host:port#remark
  304. // ss://base64(method:pass@host:port)#remark
  305. remark := ""
  306. if i := strings.Index(link, "#"); i >= 0 {
  307. remark, _ = url.QueryUnescape(link[i+1:])
  308. link = link[:i]
  309. }
  310. core := strings.TrimPrefix(link, "ss://")
  311. at := strings.Index(core, "@")
  312. if at >= 0 {
  313. // modern
  314. userB64 := core[:at]
  315. hp := core[at+1:]
  316. userInfo, err := base64DecodeFlexible(userB64)
  317. if err != nil {
  318. // SIP022 (2022-blake3-*) userinfo is percent-encoded, not base64.
  319. if dec, uerr := url.QueryUnescape(userB64); uerr == nil {
  320. userInfo = dec
  321. } else {
  322. userInfo = userB64 // not b64, rare
  323. }
  324. }
  325. colon := strings.LastIndex(hp, ":")
  326. if colon < 0 {
  327. return nil, fmt.Errorf("bad ss host:port")
  328. }
  329. host := hp[:colon]
  330. port, _ := strconv.Atoi(hp[colon+1:])
  331. method, pass := splitMethodPass(userInfo)
  332. identity := "ss:" + method + ":" + pass + "@" + host + ":" + strconv.Itoa(port)
  333. ob := Outbound{
  334. "protocol": "shadowsocks",
  335. "tag": remark,
  336. "settings": map[string]any{
  337. "servers": []any{
  338. map[string]any{"address": host, "port": port, "password": pass, "method": method},
  339. },
  340. },
  341. }
  342. return &ParseResult{Outbound: ob, Identity: identity}, nil
  343. }
  344. // legacy: whole thing b64
  345. dec, err := base64DecodeFlexible(core)
  346. if err != nil {
  347. return nil, err
  348. }
  349. at = strings.Index(dec, "@")
  350. if at < 0 {
  351. return nil, fmt.Errorf("bad legacy ss")
  352. }
  353. userInfo := dec[:at]
  354. hp := dec[at+1:]
  355. colon := strings.LastIndex(hp, ":")
  356. if colon < 0 {
  357. return nil, fmt.Errorf("bad legacy ss hp")
  358. }
  359. host := hp[:colon]
  360. port, _ := strconv.Atoi(hp[colon+1:])
  361. method, pass := splitMethodPass(userInfo)
  362. identity := "ss:" + method + ":" + pass + "@" + host + ":" + strconv.Itoa(port)
  363. ob := Outbound{
  364. "protocol": "shadowsocks",
  365. "tag": remark,
  366. "settings": map[string]any{
  367. "servers": []any{
  368. map[string]any{"address": host, "port": port, "password": pass, "method": method},
  369. },
  370. },
  371. }
  372. return &ParseResult{Outbound: ob, Identity: identity}, nil
  373. }
  374. func splitMethodPass(userInfo string) (string, string) {
  375. before, after, ok := strings.Cut(userInfo, ":")
  376. if !ok {
  377. return "2022-blake3-aes-128-gcm", userInfo // guess
  378. }
  379. return before, after
  380. }
  381. // --- hysteria2 ---
  382. func parseHysteria2(link string) (*ParseResult, error) {
  383. u, err := url.Parse(link)
  384. if err != nil {
  385. return nil, err
  386. }
  387. if u.Scheme != "hysteria2" && u.Scheme != "hy2" {
  388. return nil, fmt.Errorf("not hysteria2")
  389. }
  390. auth := u.User.Username()
  391. host := u.Hostname()
  392. port := defaultPort(u.Port(), 443)
  393. params := u.Query()
  394. stream := map[string]any{
  395. "network": "hysteria",
  396. "security": "tls",
  397. "hysteriaSettings": map[string]any{
  398. "version": 2,
  399. "auth": auth,
  400. "udpIdleTimeout": 60,
  401. },
  402. "tlsSettings": map[string]any{
  403. "serverName": params.Get("sni"),
  404. "alpn": splitCommaOrDefault(params.Get("alpn"), []string{"h3"}),
  405. "fingerprint": params.Get("fp"),
  406. "echConfigList": params.Get("ech"),
  407. "verifyPeerCertByName": "",
  408. "pinnedPeerCertSha256": params.Get("pinSHA256"),
  409. },
  410. }
  411. applyFinalMask(stream, params)
  412. identity := "hysteria2:" + auth + "@" + host + ":" + strconv.Itoa(port) + "?" + canonicalQuery(params)
  413. ob := Outbound{
  414. "protocol": "hysteria",
  415. "tag": decodeHash(u.Fragment),
  416. "settings": map[string]any{"address": host, "port": port, "version": 2},
  417. "streamSettings": stream,
  418. }
  419. return &ParseResult{Outbound: ob, Identity: identity}, nil
  420. }
  421. // --- wireguard ---
  422. func parseWireguard(link string) (*ParseResult, error) {
  423. u, err := url.Parse(link)
  424. if err != nil {
  425. return nil, err
  426. }
  427. if u.Scheme != "wireguard" && u.Scheme != "wg" {
  428. return nil, fmt.Errorf("not wireguard")
  429. }
  430. secret, _ := url.QueryUnescape(u.User.Username())
  431. params := u.Query()
  432. host := u.Hostname()
  433. portStr := u.Port()
  434. endpoint := host
  435. if portStr != "" {
  436. endpoint = host + ":" + portStr
  437. }
  438. addrRaw := firstParam(params, "address", "ip")
  439. allowedRaw := firstParam(params, "allowedips", "allowed_ips")
  440. addrs := splitComma(addrRaw)
  441. if len(addrs) == 0 {
  442. addrs = []string{"0.0.0.0/0", "::/0"}
  443. }
  444. allowed := splitComma(allowedRaw)
  445. if len(allowed) == 0 {
  446. allowed = []string{"0.0.0.0/0", "::/0"}
  447. }
  448. peer := map[string]any{
  449. "publicKey": firstParam(params, "publickey", "publicKey", "public_key", "peerPublicKey"),
  450. "endpoint": endpoint,
  451. "allowedIPs": allowed,
  452. }
  453. if psk := firstParam(params, "presharedkey", "preshared_key", "pre-shared-key", "psk"); psk != "" {
  454. peer["preSharedKey"] = psk
  455. }
  456. if ka := firstParam(params, "keepalive", "persistentkeepalive", "persistent_keepalive"); ka != "" {
  457. if n, err := strconv.Atoi(ka); err == nil {
  458. peer["keepAlive"] = n
  459. }
  460. }
  461. settings := map[string]any{
  462. "secretKey": secret,
  463. "address": addrs,
  464. "peers": []any{peer},
  465. }
  466. if mtu := params.Get("mtu"); mtu != "" {
  467. if n, err := strconv.Atoi(mtu); err == nil {
  468. settings["mtu"] = n
  469. }
  470. }
  471. if res := params.Get("reserved"); res != "" {
  472. parts := splitComma(res)
  473. var iv []int
  474. for _, p := range parts {
  475. if n, err := strconv.Atoi(strings.TrimSpace(p)); err == nil {
  476. iv = append(iv, n)
  477. }
  478. }
  479. if len(iv) > 0 {
  480. settings["reserved"] = iv
  481. }
  482. }
  483. identity := "wireguard:" + secret + "@" + endpoint + "?" + canonicalQuery(params)
  484. ob := Outbound{
  485. "protocol": "wireguard",
  486. "tag": decodeHash(u.Fragment),
  487. "settings": settings,
  488. }
  489. return &ParseResult{Outbound: ob, Identity: identity}, nil
  490. }
  491. // --- helpers ---
  492. func buildStream(network, security string) map[string]any {
  493. stream := map[string]any{"network": network, "security": security}
  494. switch network {
  495. case "tcp":
  496. stream["tcpSettings"] = map[string]any{"header": map[string]any{"type": "none"}}
  497. case "kcp":
  498. stream["kcpSettings"] = map[string]any{
  499. "mtu": 1350, "tti": 20, "uplinkCapacity": 5, "downlinkCapacity": 20,
  500. "cwndMultiplier": 1, "maxSendingWindow": 2097152,
  501. }
  502. case "ws":
  503. stream["wsSettings"] = map[string]any{"path": "/", "host": "", "headers": map[string]any{}, "heartbeatPeriod": 0}
  504. case "grpc":
  505. stream["grpcSettings"] = map[string]any{"serviceName": "", "authority": "", "multiMode": false}
  506. case "httpupgrade":
  507. stream["httpupgradeSettings"] = map[string]any{"path": "/", "host": "", "headers": map[string]any{}}
  508. case "xhttp":
  509. // No scMaxEachPostBytes/scMinPostsIntervalMs seed: xray-core's own
  510. // defaults apply, and the literal values fingerprint traffic (#5141).
  511. stream["xhttpSettings"] = map[string]any{
  512. "path": "/", "host": "", "mode": "auto", "headers": map[string]any{},
  513. "xPaddingBytes": "100-1000",
  514. }
  515. default:
  516. stream["tcpSettings"] = map[string]any{"header": map[string]any{"type": "none"}}
  517. }
  518. switch security {
  519. case "tls":
  520. stream["tlsSettings"] = map[string]any{
  521. "serverName": "", "alpn": []any{}, "fingerprint": "",
  522. "echConfigList": "", "verifyPeerCertByName": "", "pinnedPeerCertSha256": "",
  523. }
  524. case "reality":
  525. stream["realitySettings"] = map[string]any{
  526. "publicKey": "", "fingerprint": "chrome", "serverName": "",
  527. "shortId": "", "spiderX": "", "mldsa65Verify": "",
  528. }
  529. }
  530. return stream
  531. }
  532. func setWS(stream map[string]any, host, path string) {
  533. ws := stream["wsSettings"].(map[string]any)
  534. ws["host"] = host
  535. ws["path"] = path
  536. }
  537. func setHTTPUpgrade(stream map[string]any, host, path string) {
  538. h := stream["httpupgradeSettings"].(map[string]any)
  539. h["host"] = host
  540. h["path"] = path
  541. }
  542. func applyTransport(stream map[string]any, p url.Values) {
  543. net := stream["network"].(string)
  544. host := p.Get("host")
  545. path := firstNonEmpty(p.Get("path"), "/")
  546. switch net {
  547. case "ws":
  548. setWS(stream, host, path)
  549. case "grpc":
  550. gs := stream["grpcSettings"].(map[string]any)
  551. gs["serviceName"] = firstNonEmpty(p.Get("serviceName"), p.Get("path"))
  552. gs["authority"] = p.Get("authority")
  553. gs["multiMode"] = p.Get("mode") == "multi"
  554. case "httpupgrade":
  555. setHTTPUpgrade(stream, host, path)
  556. case "xhttp":
  557. xh := stream["xhttpSettings"].(map[string]any)
  558. xh["host"] = host
  559. xh["path"] = path
  560. if m := p.Get("mode"); m != "" {
  561. xh["mode"] = m
  562. }
  563. // A few advanced xhttp fields that are commonly carried
  564. for _, k := range []string{"xPaddingBytes", "scMaxEachPostBytes", "scMinPostsIntervalMs", "uplinkChunkSize"} {
  565. if v := p.Get(k); v != "" {
  566. xh[k] = v
  567. }
  568. }
  569. case "tcp":
  570. if p.Get("headerType") == "http" || p.Get("type") == "http" {
  571. stream["tcpSettings"] = map[string]any{
  572. "header": map[string]any{
  573. "type": "http",
  574. "request": map[string]any{
  575. "version": "1.1",
  576. "method": "GET",
  577. "path": splitComma(path),
  578. "headers": map[string]any{"Host": splitComma(host)},
  579. },
  580. },
  581. }
  582. }
  583. }
  584. }
  585. func applySecurity(stream map[string]any, p url.Values) {
  586. sec := stream["security"].(string)
  587. switch sec {
  588. case "tls":
  589. tls := stream["tlsSettings"].(map[string]any)
  590. tls["serverName"] = p.Get("sni")
  591. tls["fingerprint"] = p.Get("fp")
  592. if alpn := p.Get("alpn"); alpn != "" {
  593. tls["alpn"] = splitComma(alpn)
  594. }
  595. tls["echConfigList"] = p.Get("ech")
  596. tls["pinnedPeerCertSha256"] = p.Get("pcs")
  597. case "reality":
  598. re := stream["realitySettings"].(map[string]any)
  599. re["serverName"] = p.Get("sni")
  600. re["fingerprint"] = firstNonEmpty(p.Get("fp"), "chrome")
  601. re["publicKey"] = p.Get("pbk")
  602. re["shortId"] = p.Get("sid")
  603. re["spiderX"] = p.Get("spx")
  604. re["mldsa65Verify"] = p.Get("pqv")
  605. }
  606. }
  607. func applyFinalMask(stream map[string]any, p url.Values) {
  608. if fm := p.Get("fm"); fm != "" {
  609. var parsed any
  610. if json.Unmarshal([]byte(fm), &parsed) == nil {
  611. stream["finalmask"] = parsed
  612. }
  613. }
  614. }
  615. func firstNonEmpty(a, b string) string {
  616. if a != "" {
  617. return a
  618. }
  619. return b
  620. }
  621. func firstParam(p url.Values, keys ...string) string {
  622. for _, k := range keys {
  623. if v := p.Get(k); v != "" {
  624. return v
  625. }
  626. }
  627. return ""
  628. }
  629. func canonicalQuery(p url.Values) string {
  630. // Sort keys for stable identity
  631. keys := make([]string, 0, len(p))
  632. for k := range p {
  633. keys = append(keys, k)
  634. }
  635. // simple sort
  636. for i := 0; i < len(keys); i++ {
  637. for j := i + 1; j < len(keys); j++ {
  638. if keys[j] < keys[i] {
  639. keys[i], keys[j] = keys[j], keys[i]
  640. }
  641. }
  642. }
  643. parts := make([]string, 0, len(keys))
  644. for _, k := range keys {
  645. for _, v := range p[k] {
  646. parts = append(parts, k+"="+v)
  647. }
  648. }
  649. return strings.Join(parts, "&")
  650. }
  651. func decodeHash(h string) string {
  652. if h == "" {
  653. return ""
  654. }
  655. if dec, err := url.QueryUnescape(h); err == nil {
  656. return dec
  657. }
  658. return h
  659. }
  660. func defaultPort(p string, def int) int {
  661. if p == "" {
  662. return def
  663. }
  664. n, err := strconv.Atoi(p)
  665. if err != nil || n <= 0 {
  666. return def
  667. }
  668. return n
  669. }
  670. func num(v any) int {
  671. switch x := v.(type) {
  672. case float64:
  673. return int(x)
  674. case int:
  675. return x
  676. case int64:
  677. return int(x)
  678. case string:
  679. n, _ := strconv.Atoi(x)
  680. return n
  681. }
  682. return 0
  683. }
  684. func getString(m map[string]any, key, def string) string {
  685. if v, ok := m[key]; ok {
  686. if s, ok := v.(string); ok {
  687. return s
  688. }
  689. }
  690. return def
  691. }
  692. func splitComma(s string) []string {
  693. if s == "" {
  694. return nil
  695. }
  696. parts := strings.Split(s, ",")
  697. out := make([]string, 0, len(parts))
  698. for _, p := range parts {
  699. p = strings.TrimSpace(p)
  700. if p != "" {
  701. out = append(out, p)
  702. }
  703. }
  704. return out
  705. }
  706. func splitCommaOrDefault(s string, def []string) []string {
  707. if s == "" {
  708. return def
  709. }
  710. return splitComma(s)
  711. }
  712. func padBase64(s string) string {
  713. for len(s)%4 != 0 {
  714. s += "="
  715. }
  716. return s
  717. }
  718. func base64DecodeFlexible(s string) (string, error) {
  719. s = padBase64(s)
  720. if b, err := base64.StdEncoding.DecodeString(s); err == nil {
  721. return string(b), nil
  722. }
  723. if b, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(s, "=")); err == nil {
  724. return string(b), nil
  725. }
  726. return "", fmt.Errorf("base64 decode failed")
  727. }
  728. // SlugRemark turns a free-form remark into a tag segment, keeping Unicode
  729. // letters and digits (so non-ASCII remarks like Cyrillic stay readable) and
  730. // replacing every other run of characters with a single dash.
  731. var slugRe = regexp.MustCompile(`[^\p{L}\p{N}]+`)
  732. func SlugRemark(remark string) string {
  733. s := strings.ToLower(strings.TrimSpace(remark))
  734. s = slugRe.ReplaceAllString(s, "-")
  735. s = strings.Trim(s, "-")
  736. if s == "" {
  737. return ""
  738. }
  739. // collapse runs of dashes
  740. for strings.Contains(s, "--") {
  741. s = strings.ReplaceAll(s, "--", "-")
  742. }
  743. return s
  744. }
  745. // SuggestTag builds a tag from a prefix and a remark (or index fallback).
  746. // It is intended for initial assignment; stability is handled by the service layer.
  747. func SuggestTag(prefix, remark string, idx int) string {
  748. base := SlugRemark(remark)
  749. if base == "" {
  750. base = fmt.Sprintf("%d", idx)
  751. }
  752. p := strings.TrimSuffix(prefix, "-")
  753. if p != "" {
  754. return p + "-" + base
  755. }
  756. return base
  757. }