clash_service.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package sub
  2. import (
  3. "fmt"
  4. "maps"
  5. "strings"
  6. "github.com/goccy/go-json"
  7. yaml "github.com/goccy/go-yaml"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. )
  10. type SubClashService struct {
  11. enableRouting bool
  12. clashRules string
  13. SubService *SubService
  14. }
  15. func NewSubClashService(enableRouting bool, clashRules string, subService *SubService) *SubClashService {
  16. return &SubClashService{enableRouting: enableRouting, clashRules: clashRules, SubService: subService}
  17. }
  18. func (s *SubClashService) GetClash(subId string, host string) (string, string, error) {
  19. // Set per-request state so resolveInboundAddress sees the node map.
  20. s.SubService.PrepareForRequest(host)
  21. inbounds, err := s.SubService.getInboundsBySubId(subId)
  22. if err != nil {
  23. return "", "", err
  24. }
  25. externalLinks, err := s.SubService.getClientExternalLinksBySubId(subId)
  26. if err != nil {
  27. return "", "", err
  28. }
  29. if len(inbounds) == 0 && len(externalLinks) == 0 {
  30. return "", "", nil
  31. }
  32. var proxies []map[string]any
  33. seenEmails := make(map[string]struct{})
  34. for _, inbound := range inbounds {
  35. clients := s.SubService.matchingClients(inbound, subId)
  36. if len(clients) == 0 {
  37. continue
  38. }
  39. s.SubService.projectThroughFallbackMaster(inbound)
  40. for _, client := range clients {
  41. seenEmails[client.Email] = struct{}{}
  42. proxies = append(proxies, s.getProxies(inbound, client, host)...)
  43. }
  44. }
  45. for _, ext := range externalLinks {
  46. for _, el := range expandEntry(ext) {
  47. name := el.Name
  48. if name == "" {
  49. name = ext.Email
  50. }
  51. if proxy := s.clashProxyFromExternal(el.Link, name); proxy != nil {
  52. seenEmails[ext.Email] = struct{}{}
  53. proxies = append(proxies, proxy)
  54. }
  55. }
  56. }
  57. if len(proxies) == 0 {
  58. return "", "", nil
  59. }
  60. ensureUniqueProxyNames(proxies)
  61. emails := make([]string, 0, len(seenEmails))
  62. for e := range seenEmails {
  63. emails = append(emails, e)
  64. }
  65. traffic, _ := s.SubService.AggregateTrafficByEmails(emails)
  66. proxyNames := make([]string, 0, len(proxies)+1)
  67. for _, proxy := range proxies {
  68. if name, ok := proxy["name"].(string); ok && name != "" {
  69. proxyNames = append(proxyNames, name)
  70. }
  71. }
  72. proxyNames = append(proxyNames, "DIRECT")
  73. config := map[string]any{
  74. "proxies": proxies,
  75. "proxy-groups": []map[string]any{{
  76. "name": "PROXY",
  77. "type": "select",
  78. "proxies": proxyNames,
  79. }},
  80. "rules": []string{"MATCH,PROXY"},
  81. }
  82. if s.enableRouting {
  83. if err := mergeClashRulesYAML(config, s.clashRules); err != nil {
  84. return "", "", err
  85. }
  86. }
  87. finalYAML, err := yaml.Marshal(config)
  88. if err != nil {
  89. return "", "", err
  90. }
  91. header := fmt.Sprintf("upload=%d; download=%d; total=%d; expire=%d", traffic.Up, traffic.Down, traffic.Total, traffic.ExpiryTime/1000)
  92. return string(finalYAML), header, nil
  93. }
  94. // ensureUniqueProxyNames keeps every proxy "name" non-empty and unique:
  95. // mihomo rejects the whole config on a duplicate name (the empty string
  96. // genRemark returns for a remark-less inbound counts), vanishing the Clash
  97. // profile on refresh. See issue #4641.
  98. func ensureUniqueProxyNames(proxies []map[string]any) {
  99. seen := make(map[string]struct{}, len(proxies))
  100. for i, proxy := range proxies {
  101. base, _ := proxy["name"].(string)
  102. if base == "" {
  103. base = fallbackProxyName(proxy, i)
  104. }
  105. name := base
  106. for n := 2; ; n++ {
  107. if _, dup := seen[name]; !dup {
  108. break
  109. }
  110. name = fmt.Sprintf("%s-%d", base, n)
  111. }
  112. seen[name] = struct{}{}
  113. proxy["name"] = name
  114. }
  115. }
  116. func fallbackProxyName(proxy map[string]any, idx int) string {
  117. typ, _ := proxy["type"].(string)
  118. server, _ := proxy["server"].(string)
  119. if typ != "" && server != "" {
  120. return fmt.Sprintf("%s-%s-%v", typ, server, proxy["port"])
  121. }
  122. return fmt.Sprintf("proxy-%d", idx+1)
  123. }
  124. func (s *SubClashService) getProxies(inbound *model.Inbound, client model.Client, host string) []map[string]any {
  125. stream := s.streamData(inbound.StreamSettings)
  126. // For node-managed inbounds the Clash proxy "server" must be the
  127. // node's address, not the request host. resolveInboundAddress handles
  128. // the node→subscriber-host fallback chain.
  129. defaultDest := s.SubService.resolveInboundAddress(inbound)
  130. if defaultDest == "" {
  131. defaultDest = host
  132. }
  133. externalProxies, ok := stream["externalProxy"].([]any)
  134. hasExternalProxy := ok && len(externalProxies) > 0
  135. if !hasExternalProxy {
  136. externalProxies = []any{map[string]any{
  137. "forceTls": "same",
  138. "dest": defaultDest,
  139. "port": float64(inbound.Port),
  140. "remark": "",
  141. }}
  142. }
  143. delete(stream, "externalProxy")
  144. proxies := make([]map[string]any, 0, len(externalProxies))
  145. for _, ep := range externalProxies {
  146. extPrxy := ep.(map[string]any)
  147. workingInbound := *inbound
  148. workingInbound.Listen = extPrxy["dest"].(string)
  149. workingInbound.Port = int(extPrxy["port"].(float64))
  150. workingStream := cloneStreamForExternalProxy(stream)
  151. switch extPrxy["forceTls"].(string) {
  152. case "tls":
  153. if workingStream["security"] != "tls" {
  154. workingStream["security"] = "tls"
  155. workingStream["tlsSettings"] = map[string]any{}
  156. }
  157. case "none":
  158. if workingStream["security"] != "none" {
  159. workingStream["security"] = "none"
  160. delete(workingStream, "tlsSettings")
  161. delete(workingStream, "realitySettings")
  162. }
  163. }
  164. security, _ := workingStream["security"].(string)
  165. if hasExternalProxy {
  166. applyExternalProxyTLSToStream(extPrxy, workingStream, security)
  167. }
  168. proxy := s.buildProxy(&workingInbound, client, workingStream, extPrxy["remark"].(string))
  169. if len(proxy) > 0 {
  170. proxies = append(proxies, proxy)
  171. }
  172. }
  173. return proxies
  174. }
  175. func (s *SubClashService) buildProxy(inbound *model.Inbound, client model.Client, stream map[string]any, extraRemark string) map[string]any {
  176. // Hysteria has its own transport + TLS model, applyTransport /
  177. // applySecurity don't fit.
  178. if inbound.Protocol == model.Hysteria {
  179. return s.buildHysteriaProxy(inbound, client, extraRemark)
  180. }
  181. proxy := map[string]any{
  182. "name": s.SubService.genRemark(inbound, client.Email, extraRemark),
  183. "server": inbound.Listen,
  184. "port": inbound.Port,
  185. "udp": true,
  186. }
  187. network, _ := stream["network"].(string)
  188. if !s.applyTransport(proxy, network, stream) {
  189. return nil
  190. }
  191. switch inbound.Protocol {
  192. case model.VMESS:
  193. proxy["type"] = "vmess"
  194. proxy["uuid"] = client.ID
  195. proxy["alterId"] = 0
  196. cipher := client.Security
  197. if cipher == "" {
  198. cipher = "auto"
  199. }
  200. proxy["cipher"] = cipher
  201. case model.VLESS:
  202. proxy["type"] = "vless"
  203. proxy["uuid"] = client.ID
  204. var inboundSettings map[string]any
  205. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  206. streamSecurity, _ := stream["security"].(string)
  207. if client.Flow != "" && vlessFlowAllowed(network, streamSecurity, inboundSettings) {
  208. proxy["flow"] = client.Flow
  209. }
  210. if encryption, ok := inboundSettings["encryption"].(string); ok {
  211. encryption = strings.TrimSpace(encryption)
  212. if encryption != "" && encryption != "none" {
  213. proxy["encryption"] = encryption
  214. }
  215. }
  216. case model.Trojan:
  217. proxy["type"] = "trojan"
  218. proxy["password"] = client.Password
  219. case model.Shadowsocks:
  220. proxy["type"] = "ss"
  221. proxy["password"] = client.Password
  222. var inboundSettings map[string]any
  223. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  224. method, _ := inboundSettings["method"].(string)
  225. if method == "" {
  226. return nil
  227. }
  228. proxy["cipher"] = method
  229. if strings.HasPrefix(method, "2022") {
  230. if serverPassword, ok := inboundSettings["password"].(string); ok && serverPassword != "" {
  231. proxy["password"] = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  232. }
  233. }
  234. default:
  235. return nil
  236. }
  237. security, _ := stream["security"].(string)
  238. if !s.applySecurity(proxy, security, stream) {
  239. return nil
  240. }
  241. return proxy
  242. }
  243. // buildHysteriaProxy produces a mihomo-compatible Clash entry for a
  244. // Hysteria (v1) or Hysteria2 inbound. It reads `inbound.StreamSettings`
  245. // directly instead of going through streamData/tlsData, because those
  246. // helpers prune fields (like `allowInsecure` / the salamander obfs
  247. // block) that the hysteria proxy wants preserved.
  248. func (s *SubClashService) buildHysteriaProxy(inbound *model.Inbound, client model.Client, extraRemark string) map[string]any {
  249. var inboundSettings map[string]any
  250. _ = json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  251. proxyType := "hysteria2"
  252. authKey := "password"
  253. if v, ok := inboundSettings["version"].(float64); ok && int(v) == 1 {
  254. proxyType = "hysteria"
  255. authKey = "auth-str"
  256. }
  257. proxy := map[string]any{
  258. "name": s.SubService.genRemark(inbound, client.Email, extraRemark),
  259. "type": proxyType,
  260. "server": inbound.Listen,
  261. "port": inbound.Port,
  262. "udp": true,
  263. authKey: client.Auth,
  264. }
  265. var rawStream map[string]any
  266. _ = json.Unmarshal([]byte(inbound.StreamSettings), &rawStream)
  267. // TLS details — hysteria always uses TLS.
  268. if tlsSettings, ok := rawStream["tlsSettings"].(map[string]any); ok {
  269. if serverName, ok := tlsSettings["serverName"].(string); ok && serverName != "" {
  270. proxy["sni"] = serverName
  271. }
  272. if alpnList, ok := tlsSettings["alpn"].([]any); ok && len(alpnList) > 0 {
  273. out := make([]string, 0, len(alpnList))
  274. for _, a := range alpnList {
  275. if s, ok := a.(string); ok && s != "" {
  276. out = append(out, s)
  277. }
  278. }
  279. if len(out) > 0 {
  280. proxy["alpn"] = out
  281. }
  282. }
  283. if inner, ok := tlsSettings["settings"].(map[string]any); ok {
  284. if insecure, ok := inner["allowInsecure"].(bool); ok && insecure {
  285. proxy["skip-cert-verify"] = true
  286. }
  287. if fp, ok := inner["fingerprint"].(string); ok && fp != "" {
  288. proxy["client-fingerprint"] = fp
  289. }
  290. }
  291. }
  292. // Salamander obfs (Hysteria2). Read the same finalmask.udp[salamander]
  293. // block the subscription link generator uses.
  294. if finalmask, ok := rawStream["finalmask"].(map[string]any); ok {
  295. if udpMasks, ok := finalmask["udp"].([]any); ok {
  296. for _, m := range udpMasks {
  297. mask, _ := m.(map[string]any)
  298. if mask == nil || mask["type"] != "salamander" {
  299. continue
  300. }
  301. settings, _ := mask["settings"].(map[string]any)
  302. if pw, ok := settings["password"].(string); ok && pw != "" {
  303. proxy["obfs"] = "salamander"
  304. proxy["obfs-password"] = pw
  305. break
  306. }
  307. }
  308. }
  309. }
  310. // UDP port hopping. mihomo reads the range from a dedicated `ports`
  311. // field (the base `port` stays as the redirect target).
  312. if hopPorts := hysteriaHopPorts(rawStream); hopPorts != "" {
  313. proxy["ports"] = hopPorts
  314. }
  315. return proxy
  316. }
  317. func (s *SubClashService) applyTransport(proxy map[string]any, network string, stream map[string]any) bool {
  318. switch network {
  319. case "", "tcp":
  320. proxy["network"] = "tcp"
  321. tcp, _ := stream["tcpSettings"].(map[string]any)
  322. if tcp != nil {
  323. header, _ := tcp["header"].(map[string]any)
  324. if header != nil {
  325. typeStr, _ := header["type"].(string)
  326. if typeStr != "" && typeStr != "none" {
  327. return false
  328. }
  329. }
  330. }
  331. return true
  332. case "ws":
  333. proxy["network"] = "ws"
  334. ws, _ := stream["wsSettings"].(map[string]any)
  335. wsOpts := map[string]any{}
  336. if ws != nil {
  337. if path, ok := ws["path"].(string); ok && path != "" {
  338. wsOpts["path"] = path
  339. }
  340. host := ""
  341. if v, ok := ws["host"].(string); ok && v != "" {
  342. host = v
  343. } else if headers, ok := ws["headers"].(map[string]any); ok {
  344. host = searchHost(headers)
  345. }
  346. if host != "" {
  347. wsOpts["headers"] = map[string]any{"Host": host}
  348. }
  349. }
  350. if len(wsOpts) > 0 {
  351. proxy["ws-opts"] = wsOpts
  352. }
  353. return true
  354. case "grpc":
  355. proxy["network"] = "grpc"
  356. grpc, _ := stream["grpcSettings"].(map[string]any)
  357. grpcOpts := map[string]any{}
  358. if grpc != nil {
  359. if serviceName, ok := grpc["serviceName"].(string); ok && serviceName != "" {
  360. grpcOpts["grpc-service-name"] = serviceName
  361. }
  362. }
  363. if len(grpcOpts) > 0 {
  364. proxy["grpc-opts"] = grpcOpts
  365. }
  366. return true
  367. case "httpupgrade":
  368. proxy["network"] = "httpupgrade"
  369. hu, _ := stream["httpupgradeSettings"].(map[string]any)
  370. opts := map[string]any{}
  371. if hu != nil {
  372. if path, ok := hu["path"].(string); ok && path != "" {
  373. opts["path"] = path
  374. }
  375. host := ""
  376. if v, ok := hu["host"].(string); ok && v != "" {
  377. host = v
  378. } else if headers, ok := hu["headers"].(map[string]any); ok {
  379. host = searchHost(headers)
  380. }
  381. if host != "" {
  382. opts["headers"] = map[string]any{"Host": host}
  383. }
  384. }
  385. if len(opts) > 0 {
  386. proxy["http-upgrade-opts"] = opts
  387. }
  388. return true
  389. case "xhttp":
  390. proxy["network"] = "xhttp"
  391. xhttp, _ := stream["xhttpSettings"].(map[string]any)
  392. opts := map[string]any{}
  393. if xhttp != nil {
  394. if path, ok := xhttp["path"].(string); ok && path != "" {
  395. opts["path"] = path
  396. }
  397. host := ""
  398. if v, ok := xhttp["host"].(string); ok && v != "" {
  399. host = v
  400. } else if headers, ok := xhttp["headers"].(map[string]any); ok {
  401. host = searchHost(headers)
  402. }
  403. if host != "" {
  404. opts["host"] = host
  405. }
  406. if mode, ok := xhttp["mode"].(string); ok && mode != "" {
  407. opts["mode"] = mode
  408. }
  409. }
  410. if len(opts) > 0 {
  411. proxy["xhttp-opts"] = opts
  412. }
  413. return true
  414. default:
  415. return false
  416. }
  417. }
  418. func (s *SubClashService) applySecurity(proxy map[string]any, security string, stream map[string]any) bool {
  419. switch security {
  420. case "", "none":
  421. proxy["tls"] = false
  422. return true
  423. case "tls":
  424. proxy["tls"] = true
  425. tlsSettings, _ := stream["tlsSettings"].(map[string]any)
  426. if tlsSettings != nil {
  427. if serverName, ok := tlsSettings["serverName"].(string); ok && serverName != "" {
  428. proxy["servername"] = serverName
  429. switch proxy["type"] {
  430. case "trojan":
  431. proxy["sni"] = serverName
  432. }
  433. }
  434. if fingerprint, ok := tlsSettings["fingerprint"].(string); ok && fingerprint != "" {
  435. proxy["client-fingerprint"] = fingerprint
  436. }
  437. if alpn, ok := externalProxyALPNList(tlsSettings["alpn"]); ok {
  438. out := make([]string, 0, len(alpn))
  439. for _, item := range alpn {
  440. if s, ok := item.(string); ok && s != "" {
  441. out = append(out, s)
  442. }
  443. }
  444. if len(out) > 0 {
  445. proxy["alpn"] = out
  446. }
  447. }
  448. }
  449. return true
  450. case "reality":
  451. proxy["tls"] = true
  452. realitySettings, _ := stream["realitySettings"].(map[string]any)
  453. if realitySettings == nil {
  454. return false
  455. }
  456. if serverName, ok := realitySettings["serverName"].(string); ok && serverName != "" {
  457. proxy["servername"] = serverName
  458. }
  459. realityOpts := map[string]any{}
  460. if publicKey, ok := realitySettings["publicKey"].(string); ok && publicKey != "" {
  461. realityOpts["public-key"] = publicKey
  462. }
  463. if shortID, ok := realitySettings["shortId"].(string); ok && shortID != "" {
  464. realityOpts["short-id"] = shortID
  465. }
  466. if len(realityOpts) > 0 {
  467. proxy["reality-opts"] = realityOpts
  468. }
  469. if fingerprint, ok := realitySettings["fingerprint"].(string); ok && fingerprint != "" {
  470. proxy["client-fingerprint"] = fingerprint
  471. }
  472. return true
  473. default:
  474. return false
  475. }
  476. }
  477. func (s *SubClashService) streamData(stream string) map[string]any {
  478. var streamSettings map[string]any
  479. json.Unmarshal([]byte(stream), &streamSettings)
  480. security, _ := streamSettings["security"].(string)
  481. switch security {
  482. case "tls":
  483. if tlsSettings, ok := streamSettings["tlsSettings"].(map[string]any); ok {
  484. streamSettings["tlsSettings"] = s.tlsData(tlsSettings)
  485. }
  486. case "reality":
  487. if realitySettings, ok := streamSettings["realitySettings"].(map[string]any); ok {
  488. streamSettings["realitySettings"] = s.realityData(realitySettings)
  489. }
  490. }
  491. delete(streamSettings, "sockopt")
  492. return streamSettings
  493. }
  494. func (s *SubClashService) tlsData(tData map[string]any) map[string]any {
  495. tlsData := make(map[string]any, 1)
  496. tlsClientSettings, _ := tData["settings"].(map[string]any)
  497. tlsData["serverName"] = tData["serverName"]
  498. tlsData["alpn"] = tData["alpn"]
  499. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  500. tlsData["fingerprint"] = fingerprint
  501. }
  502. if pins, ok := tlsClientSettings["pinnedPeerCertSha256"].([]any); ok && len(pins) > 0 {
  503. tlsData["pin-sha256"] = pins
  504. }
  505. return tlsData
  506. }
  507. func (s *SubClashService) realityData(rData map[string]any) map[string]any {
  508. rDataOut := make(map[string]any, 1)
  509. realityClientSettings, _ := rData["settings"].(map[string]any)
  510. if publicKey, ok := realityClientSettings["publicKey"].(string); ok {
  511. rDataOut["publicKey"] = publicKey
  512. }
  513. if fingerprint, ok := realityClientSettings["fingerprint"].(string); ok {
  514. rDataOut["fingerprint"] = fingerprint
  515. }
  516. if serverNames, ok := rData["serverNames"].([]any); ok && len(serverNames) > 0 {
  517. rDataOut["serverName"] = fmt.Sprint(serverNames[0])
  518. }
  519. if shortIDs, ok := rData["shortIds"].([]any); ok && len(shortIDs) > 0 {
  520. rDataOut["shortId"] = fmt.Sprint(shortIDs[0])
  521. }
  522. return rDataOut
  523. }
  524. func cloneMap(src map[string]any) map[string]any {
  525. if src == nil {
  526. return nil
  527. }
  528. dst := make(map[string]any, len(src))
  529. maps.Copy(dst, src)
  530. return dst
  531. }
  532. func mergeClashRulesYAML(base map[string]any, raw string) error {
  533. raw = strings.TrimSpace(raw)
  534. if raw == "" {
  535. return nil
  536. }
  537. var custom any
  538. if err := yaml.Unmarshal([]byte(raw), &custom); err != nil {
  539. mergeClashRules(base, linesToClashRules(raw))
  540. return nil
  541. }
  542. switch typed := custom.(type) {
  543. case []any:
  544. mergeClashRules(base, typed)
  545. case map[string]any:
  546. for key, value := range typed {
  547. if key == "rules" {
  548. if ruleList, ok := asAnySlice(value); ok {
  549. mergeClashRules(base, ruleList)
  550. }
  551. continue
  552. }
  553. base[key] = value
  554. }
  555. default:
  556. mergeClashRules(base, linesToClashRules(raw))
  557. }
  558. return nil
  559. }
  560. func mergeClashRules(base map[string]any, customRules []any) {
  561. if len(customRules) == 0 {
  562. return
  563. }
  564. baseRules, _ := asAnySlice(base["rules"])
  565. if hasClashMatchRule(customRules) {
  566. base["rules"] = customRules
  567. return
  568. }
  569. merged := make([]any, 0, len(customRules)+len(baseRules))
  570. merged = append(merged, customRules...)
  571. merged = append(merged, baseRules...)
  572. base["rules"] = merged
  573. }
  574. func asAnySlice(value any) ([]any, bool) {
  575. switch typed := value.(type) {
  576. case []any:
  577. return typed, true
  578. case []string:
  579. out := make([]any, 0, len(typed))
  580. for _, item := range typed {
  581. out = append(out, item)
  582. }
  583. return out, true
  584. case []map[string]any:
  585. out := make([]any, 0, len(typed))
  586. for _, item := range typed {
  587. out = append(out, item)
  588. }
  589. return out, true
  590. default:
  591. return nil, false
  592. }
  593. }
  594. func hasClashMatchRule(rules []any) bool {
  595. for _, rule := range rules {
  596. ruleText, ok := rule.(string)
  597. if !ok {
  598. continue
  599. }
  600. parts := strings.SplitN(ruleText, ",", 2)
  601. if strings.EqualFold(strings.TrimSpace(parts[0]), "MATCH") {
  602. return true
  603. }
  604. }
  605. return false
  606. }
  607. func linesToClashRules(raw string) []any {
  608. lines := strings.Split(raw, "\n")
  609. rules := make([]any, 0, len(lines))
  610. for _, line := range lines {
  611. line = strings.TrimSpace(line)
  612. if line == "" || strings.HasPrefix(line, "#") {
  613. continue
  614. }
  615. rules = append(rules, line)
  616. }
  617. return rules
  618. }