clash_service.go 18 KB

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