subClashService.go 18 KB

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