outbound.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. userInfo = userB64 // not b64, rare
  319. }
  320. colon := strings.LastIndex(hp, ":")
  321. if colon < 0 {
  322. return nil, fmt.Errorf("bad ss host:port")
  323. }
  324. host := hp[:colon]
  325. port, _ := strconv.Atoi(hp[colon+1:])
  326. method, pass := splitMethodPass(userInfo)
  327. identity := "ss:" + method + ":" + pass + "@" + host + ":" + strconv.Itoa(port)
  328. ob := Outbound{
  329. "protocol": "shadowsocks",
  330. "tag": remark,
  331. "settings": map[string]any{
  332. "servers": []any{
  333. map[string]any{"address": host, "port": port, "password": pass, "method": method},
  334. },
  335. },
  336. }
  337. return &ParseResult{Outbound: ob, Identity: identity}, nil
  338. }
  339. // legacy: whole thing b64
  340. dec, err := base64DecodeFlexible(core)
  341. if err != nil {
  342. return nil, err
  343. }
  344. at = strings.Index(dec, "@")
  345. if at < 0 {
  346. return nil, fmt.Errorf("bad legacy ss")
  347. }
  348. userInfo := dec[:at]
  349. hp := dec[at+1:]
  350. colon := strings.LastIndex(hp, ":")
  351. if colon < 0 {
  352. return nil, fmt.Errorf("bad legacy ss hp")
  353. }
  354. host := hp[:colon]
  355. port, _ := strconv.Atoi(hp[colon+1:])
  356. method, pass := splitMethodPass(userInfo)
  357. identity := "ss:" + method + ":" + pass + "@" + host + ":" + strconv.Itoa(port)
  358. ob := Outbound{
  359. "protocol": "shadowsocks",
  360. "tag": remark,
  361. "settings": map[string]any{
  362. "servers": []any{
  363. map[string]any{"address": host, "port": port, "password": pass, "method": method},
  364. },
  365. },
  366. }
  367. return &ParseResult{Outbound: ob, Identity: identity}, nil
  368. }
  369. func splitMethodPass(userInfo string) (string, string) {
  370. colon := strings.Index(userInfo, ":")
  371. if colon < 0 {
  372. return "2022-blake3-aes-128-gcm", userInfo // guess
  373. }
  374. return userInfo[:colon], userInfo[colon+1:]
  375. }
  376. // --- hysteria2 ---
  377. func parseHysteria2(link string) (*ParseResult, error) {
  378. u, err := url.Parse(link)
  379. if err != nil {
  380. return nil, err
  381. }
  382. if u.Scheme != "hysteria2" && u.Scheme != "hy2" {
  383. return nil, fmt.Errorf("not hysteria2")
  384. }
  385. auth := u.User.Username()
  386. host := u.Hostname()
  387. port := defaultPort(u.Port(), 443)
  388. params := u.Query()
  389. stream := map[string]any{
  390. "network": "hysteria",
  391. "security": "tls",
  392. "hysteriaSettings": map[string]any{
  393. "version": 2,
  394. "auth": auth,
  395. "udpIdleTimeout": 60,
  396. },
  397. "tlsSettings": map[string]any{
  398. "serverName": params.Get("sni"),
  399. "alpn": splitCommaOrDefault(params.Get("alpn"), []string{"h3"}),
  400. "fingerprint": params.Get("fp"),
  401. "echConfigList": params.Get("ech"),
  402. "verifyPeerCertByName": "",
  403. "pinnedPeerCertSha256": params.Get("pinSHA256"),
  404. },
  405. }
  406. applyFinalMask(stream, params)
  407. identity := "hysteria2:" + auth + "@" + host + ":" + strconv.Itoa(port) + "?" + canonicalQuery(params)
  408. ob := Outbound{
  409. "protocol": "hysteria",
  410. "tag": decodeHash(u.Fragment),
  411. "settings": map[string]any{"address": host, "port": port, "version": 2},
  412. "streamSettings": stream,
  413. }
  414. return &ParseResult{Outbound: ob, Identity: identity}, nil
  415. }
  416. // --- wireguard ---
  417. func parseWireguard(link string) (*ParseResult, error) {
  418. u, err := url.Parse(link)
  419. if err != nil {
  420. return nil, err
  421. }
  422. if u.Scheme != "wireguard" && u.Scheme != "wg" {
  423. return nil, fmt.Errorf("not wireguard")
  424. }
  425. secret, _ := url.QueryUnescape(u.User.Username())
  426. params := u.Query()
  427. host := u.Hostname()
  428. portStr := u.Port()
  429. endpoint := host
  430. if portStr != "" {
  431. endpoint = host + ":" + portStr
  432. }
  433. addrRaw := firstParam(params, "address", "ip")
  434. allowedRaw := firstParam(params, "allowedips", "allowed_ips")
  435. addrs := splitComma(addrRaw)
  436. if len(addrs) == 0 {
  437. addrs = []string{"0.0.0.0/0", "::/0"}
  438. }
  439. allowed := splitComma(allowedRaw)
  440. if len(allowed) == 0 {
  441. allowed = []string{"0.0.0.0/0", "::/0"}
  442. }
  443. peer := map[string]any{
  444. "publicKey": firstParam(params, "publickey", "publicKey", "public_key", "peerPublicKey"),
  445. "endpoint": endpoint,
  446. "allowedIPs": allowed,
  447. }
  448. if psk := firstParam(params, "presharedkey", "preshared_key", "pre-shared-key", "psk"); psk != "" {
  449. peer["preSharedKey"] = psk
  450. }
  451. if ka := firstParam(params, "keepalive", "persistentkeepalive", "persistent_keepalive"); ka != "" {
  452. if n, err := strconv.Atoi(ka); err == nil {
  453. peer["keepAlive"] = n
  454. }
  455. }
  456. settings := map[string]any{
  457. "secretKey": secret,
  458. "address": addrs,
  459. "peers": []any{peer},
  460. }
  461. if mtu := params.Get("mtu"); mtu != "" {
  462. if n, err := strconv.Atoi(mtu); err == nil {
  463. settings["mtu"] = n
  464. }
  465. }
  466. if res := params.Get("reserved"); res != "" {
  467. parts := splitComma(res)
  468. var iv []int
  469. for _, p := range parts {
  470. if n, err := strconv.Atoi(strings.TrimSpace(p)); err == nil {
  471. iv = append(iv, n)
  472. }
  473. }
  474. if len(iv) > 0 {
  475. settings["reserved"] = iv
  476. }
  477. }
  478. identity := "wireguard:" + secret + "@" + endpoint + "?" + canonicalQuery(params)
  479. ob := Outbound{
  480. "protocol": "wireguard",
  481. "tag": decodeHash(u.Fragment),
  482. "settings": settings,
  483. }
  484. return &ParseResult{Outbound: ob, Identity: identity}, nil
  485. }
  486. // --- helpers ---
  487. func buildStream(network, security string) map[string]any {
  488. stream := map[string]any{"network": network, "security": security}
  489. switch network {
  490. case "tcp":
  491. stream["tcpSettings"] = map[string]any{"header": map[string]any{"type": "none"}}
  492. case "kcp":
  493. stream["kcpSettings"] = map[string]any{
  494. "mtu": 1350, "tti": 20, "uplinkCapacity": 5, "downlinkCapacity": 20,
  495. "cwndMultiplier": 1, "maxSendingWindow": 2097152,
  496. }
  497. case "ws":
  498. stream["wsSettings"] = map[string]any{"path": "/", "host": "", "headers": map[string]any{}, "heartbeatPeriod": 0}
  499. case "grpc":
  500. stream["grpcSettings"] = map[string]any{"serviceName": "", "authority": "", "multiMode": false}
  501. case "httpupgrade":
  502. stream["httpupgradeSettings"] = map[string]any{"path": "/", "host": "", "headers": map[string]any{}}
  503. case "xhttp":
  504. stream["xhttpSettings"] = map[string]any{
  505. "path": "/", "host": "", "mode": "auto", "headers": map[string]any{},
  506. "xPaddingBytes": "100-1000", "scMaxEachPostBytes": "1000000",
  507. }
  508. default:
  509. stream["tcpSettings"] = map[string]any{"header": map[string]any{"type": "none"}}
  510. }
  511. switch security {
  512. case "tls":
  513. stream["tlsSettings"] = map[string]any{
  514. "serverName": "", "alpn": []any{}, "fingerprint": "",
  515. "echConfigList": "", "verifyPeerCertByName": "", "pinnedPeerCertSha256": "",
  516. }
  517. case "reality":
  518. stream["realitySettings"] = map[string]any{
  519. "publicKey": "", "fingerprint": "chrome", "serverName": "",
  520. "shortId": "", "spiderX": "", "mldsa65Verify": "",
  521. }
  522. }
  523. return stream
  524. }
  525. func setWS(stream map[string]any, host, path string) {
  526. ws := stream["wsSettings"].(map[string]any)
  527. ws["host"] = host
  528. ws["path"] = path
  529. }
  530. func setHTTPUpgrade(stream map[string]any, host, path string) {
  531. h := stream["httpupgradeSettings"].(map[string]any)
  532. h["host"] = host
  533. h["path"] = path
  534. }
  535. func applyTransport(stream map[string]any, p url.Values) {
  536. net := stream["network"].(string)
  537. host := p.Get("host")
  538. path := firstNonEmpty(p.Get("path"), "/")
  539. switch net {
  540. case "ws":
  541. setWS(stream, host, path)
  542. case "grpc":
  543. gs := stream["grpcSettings"].(map[string]any)
  544. gs["serviceName"] = firstNonEmpty(p.Get("serviceName"), p.Get("path"))
  545. gs["authority"] = p.Get("authority")
  546. gs["multiMode"] = p.Get("mode") == "multi"
  547. case "httpupgrade":
  548. setHTTPUpgrade(stream, host, path)
  549. case "xhttp":
  550. xh := stream["xhttpSettings"].(map[string]any)
  551. xh["host"] = host
  552. xh["path"] = path
  553. if m := p.Get("mode"); m != "" {
  554. xh["mode"] = m
  555. }
  556. // A few advanced xhttp fields that are commonly carried
  557. for _, k := range []string{"xPaddingBytes", "scMaxEachPostBytes", "scMinPostsIntervalMs", "uplinkChunkSize"} {
  558. if v := p.Get(k); v != "" {
  559. xh[k] = v
  560. }
  561. }
  562. case "tcp":
  563. if p.Get("headerType") == "http" || p.Get("type") == "http" {
  564. stream["tcpSettings"] = map[string]any{
  565. "header": map[string]any{
  566. "type": "http",
  567. "request": map[string]any{
  568. "version": "1.1",
  569. "method": "GET",
  570. "path": splitComma(path),
  571. "headers": map[string]any{"Host": splitComma(host)},
  572. },
  573. },
  574. }
  575. }
  576. }
  577. }
  578. func applySecurity(stream map[string]any, p url.Values) {
  579. sec := stream["security"].(string)
  580. switch sec {
  581. case "tls":
  582. tls := stream["tlsSettings"].(map[string]any)
  583. tls["serverName"] = p.Get("sni")
  584. tls["fingerprint"] = p.Get("fp")
  585. if alpn := p.Get("alpn"); alpn != "" {
  586. tls["alpn"] = splitComma(alpn)
  587. }
  588. tls["echConfigList"] = p.Get("ech")
  589. tls["pinnedPeerCertSha256"] = p.Get("pcs")
  590. case "reality":
  591. re := stream["realitySettings"].(map[string]any)
  592. re["serverName"] = p.Get("sni")
  593. re["fingerprint"] = firstNonEmpty(p.Get("fp"), "chrome")
  594. re["publicKey"] = p.Get("pbk")
  595. re["shortId"] = p.Get("sid")
  596. re["spiderX"] = p.Get("spx")
  597. re["mldsa65Verify"] = p.Get("pqv")
  598. }
  599. }
  600. func applyFinalMask(stream map[string]any, p url.Values) {
  601. if fm := p.Get("fm"); fm != "" {
  602. var parsed any
  603. if json.Unmarshal([]byte(fm), &parsed) == nil {
  604. stream["finalmask"] = parsed
  605. }
  606. }
  607. }
  608. func firstNonEmpty(a, b string) string {
  609. if a != "" {
  610. return a
  611. }
  612. return b
  613. }
  614. func firstParam(p url.Values, keys ...string) string {
  615. for _, k := range keys {
  616. if v := p.Get(k); v != "" {
  617. return v
  618. }
  619. }
  620. return ""
  621. }
  622. func canonicalQuery(p url.Values) string {
  623. // Sort keys for stable identity
  624. keys := make([]string, 0, len(p))
  625. for k := range p {
  626. keys = append(keys, k)
  627. }
  628. // simple sort
  629. for i := 0; i < len(keys); i++ {
  630. for j := i + 1; j < len(keys); j++ {
  631. if keys[j] < keys[i] {
  632. keys[i], keys[j] = keys[j], keys[i]
  633. }
  634. }
  635. }
  636. parts := make([]string, 0, len(keys))
  637. for _, k := range keys {
  638. for _, v := range p[k] {
  639. parts = append(parts, k+"="+v)
  640. }
  641. }
  642. return strings.Join(parts, "&")
  643. }
  644. func decodeHash(h string) string {
  645. if h == "" {
  646. return ""
  647. }
  648. if dec, err := url.QueryUnescape(h); err == nil {
  649. return dec
  650. }
  651. return h
  652. }
  653. func defaultPort(p string, def int) int {
  654. if p == "" {
  655. return def
  656. }
  657. n, err := strconv.Atoi(p)
  658. if err != nil || n <= 0 {
  659. return def
  660. }
  661. return n
  662. }
  663. func num(v any) int {
  664. switch x := v.(type) {
  665. case float64:
  666. return int(x)
  667. case int:
  668. return x
  669. case int64:
  670. return int(x)
  671. case string:
  672. n, _ := strconv.Atoi(x)
  673. return n
  674. }
  675. return 0
  676. }
  677. func getString(m map[string]any, key, def string) string {
  678. if v, ok := m[key]; ok {
  679. if s, ok := v.(string); ok {
  680. return s
  681. }
  682. }
  683. return def
  684. }
  685. func splitComma(s string) []string {
  686. if s == "" {
  687. return nil
  688. }
  689. parts := strings.Split(s, ",")
  690. out := make([]string, 0, len(parts))
  691. for _, p := range parts {
  692. p = strings.TrimSpace(p)
  693. if p != "" {
  694. out = append(out, p)
  695. }
  696. }
  697. return out
  698. }
  699. func splitCommaOrDefault(s string, def []string) []string {
  700. if s == "" {
  701. return def
  702. }
  703. return splitComma(s)
  704. }
  705. func padBase64(s string) string {
  706. for len(s)%4 != 0 {
  707. s += "="
  708. }
  709. return s
  710. }
  711. func base64DecodeFlexible(s string) (string, error) {
  712. s = padBase64(s)
  713. if b, err := base64.StdEncoding.DecodeString(s); err == nil {
  714. return string(b), nil
  715. }
  716. if b, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(s, "=")); err == nil {
  717. return string(b), nil
  718. }
  719. return "", fmt.Errorf("base64 decode failed")
  720. }
  721. // SlugRemark turns a free-form remark into a conservative DNS-ish tag segment.
  722. var slugRe = regexp.MustCompile(`[^a-z0-9]+`)
  723. func SlugRemark(remark string) string {
  724. s := strings.ToLower(strings.TrimSpace(remark))
  725. s = slugRe.ReplaceAllString(s, "-")
  726. s = strings.Trim(s, "-")
  727. if s == "" {
  728. return ""
  729. }
  730. // collapse runs of dashes
  731. for strings.Contains(s, "--") {
  732. s = strings.ReplaceAll(s, "--", "-")
  733. }
  734. return s
  735. }
  736. // SuggestTag builds a tag from a prefix and a remark (or index fallback).
  737. // It is intended for initial assignment; stability is handled by the service layer.
  738. func SuggestTag(prefix, remark string, idx int) string {
  739. base := SlugRemark(remark)
  740. if base == "" {
  741. base = fmt.Sprintf("%d", idx)
  742. }
  743. p := strings.TrimSuffix(prefix, "-")
  744. if p != "" {
  745. return p + "-" + base
  746. }
  747. return base
  748. }