subClashService.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 && encryption != "" {
  199. proxy["packet-encoding"] = encryption
  200. }
  201. case model.Trojan:
  202. proxy["type"] = "trojan"
  203. proxy["password"] = client.Password
  204. case model.Shadowsocks:
  205. proxy["type"] = "ss"
  206. proxy["password"] = client.Password
  207. var inboundSettings map[string]any
  208. json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  209. method, _ := inboundSettings["method"].(string)
  210. if method == "" {
  211. return nil
  212. }
  213. proxy["cipher"] = method
  214. if strings.HasPrefix(method, "2022") {
  215. if serverPassword, ok := inboundSettings["password"].(string); ok && serverPassword != "" {
  216. proxy["password"] = fmt.Sprintf("%s:%s", serverPassword, client.Password)
  217. }
  218. }
  219. default:
  220. return nil
  221. }
  222. security, _ := stream["security"].(string)
  223. if !s.applySecurity(proxy, security, stream) {
  224. return nil
  225. }
  226. return proxy
  227. }
  228. // buildHysteriaProxy produces a mihomo-compatible Clash entry for a
  229. // Hysteria (v1) or Hysteria2 inbound. It reads `inbound.StreamSettings`
  230. // directly instead of going through streamData/tlsData, because those
  231. // helpers prune fields (like `allowInsecure` / the salamander obfs
  232. // block) that the hysteria proxy wants preserved.
  233. func (s *SubClashService) buildHysteriaProxy(inbound *model.Inbound, client model.Client, extraRemark string) map[string]any {
  234. var inboundSettings map[string]any
  235. _ = json.Unmarshal([]byte(inbound.Settings), &inboundSettings)
  236. proxyType := "hysteria2"
  237. authKey := "password"
  238. if v, ok := inboundSettings["version"].(float64); ok && int(v) == 1 {
  239. proxyType = "hysteria"
  240. authKey = "auth-str"
  241. }
  242. proxy := map[string]any{
  243. "name": s.SubService.genRemark(inbound, client.Email, extraRemark),
  244. "type": proxyType,
  245. "server": inbound.Listen,
  246. "port": inbound.Port,
  247. "udp": true,
  248. authKey: client.Auth,
  249. }
  250. var rawStream map[string]any
  251. _ = json.Unmarshal([]byte(inbound.StreamSettings), &rawStream)
  252. // TLS details — hysteria always uses TLS.
  253. if tlsSettings, ok := rawStream["tlsSettings"].(map[string]any); ok {
  254. if serverName, ok := tlsSettings["serverName"].(string); ok && serverName != "" {
  255. proxy["sni"] = serverName
  256. }
  257. if alpnList, ok := tlsSettings["alpn"].([]any); ok && len(alpnList) > 0 {
  258. out := make([]string, 0, len(alpnList))
  259. for _, a := range alpnList {
  260. if s, ok := a.(string); ok && s != "" {
  261. out = append(out, s)
  262. }
  263. }
  264. if len(out) > 0 {
  265. proxy["alpn"] = out
  266. }
  267. }
  268. if inner, ok := tlsSettings["settings"].(map[string]any); ok {
  269. if insecure, ok := inner["allowInsecure"].(bool); ok && insecure {
  270. proxy["skip-cert-verify"] = true
  271. }
  272. if fp, ok := inner["fingerprint"].(string); ok && fp != "" {
  273. proxy["client-fingerprint"] = fp
  274. }
  275. }
  276. }
  277. // Salamander obfs (Hysteria2). Read the same finalmask.udp[salamander]
  278. // block the subscription link generator uses.
  279. if finalmask, ok := rawStream["finalmask"].(map[string]any); ok {
  280. if udpMasks, ok := finalmask["udp"].([]any); ok {
  281. for _, m := range udpMasks {
  282. mask, _ := m.(map[string]any)
  283. if mask == nil || mask["type"] != "salamander" {
  284. continue
  285. }
  286. settings, _ := mask["settings"].(map[string]any)
  287. if pw, ok := settings["password"].(string); ok && pw != "" {
  288. proxy["obfs"] = "salamander"
  289. proxy["obfs-password"] = pw
  290. break
  291. }
  292. }
  293. }
  294. }
  295. // UDP port hopping. mihomo reads the range from a dedicated `ports`
  296. // field (the base `port` stays as the redirect target).
  297. if hopPorts := hysteriaHopPorts(rawStream); hopPorts != "" {
  298. proxy["ports"] = hopPorts
  299. }
  300. return proxy
  301. }
  302. func (s *SubClashService) applyTransport(proxy map[string]any, network string, stream map[string]any) bool {
  303. switch network {
  304. case "", "tcp":
  305. proxy["network"] = "tcp"
  306. tcp, _ := stream["tcpSettings"].(map[string]any)
  307. if tcp != nil {
  308. header, _ := tcp["header"].(map[string]any)
  309. if header != nil {
  310. typeStr, _ := header["type"].(string)
  311. if typeStr != "" && typeStr != "none" {
  312. return false
  313. }
  314. }
  315. }
  316. return true
  317. case "ws":
  318. proxy["network"] = "ws"
  319. ws, _ := stream["wsSettings"].(map[string]any)
  320. wsOpts := map[string]any{}
  321. if ws != nil {
  322. if path, ok := ws["path"].(string); ok && path != "" {
  323. wsOpts["path"] = path
  324. }
  325. host := ""
  326. if v, ok := ws["host"].(string); ok && v != "" {
  327. host = v
  328. } else if headers, ok := ws["headers"].(map[string]any); ok {
  329. host = searchHost(headers)
  330. }
  331. if host != "" {
  332. wsOpts["headers"] = map[string]any{"Host": host}
  333. }
  334. }
  335. if len(wsOpts) > 0 {
  336. proxy["ws-opts"] = wsOpts
  337. }
  338. return true
  339. case "grpc":
  340. proxy["network"] = "grpc"
  341. grpc, _ := stream["grpcSettings"].(map[string]any)
  342. grpcOpts := map[string]any{}
  343. if grpc != nil {
  344. if serviceName, ok := grpc["serviceName"].(string); ok && serviceName != "" {
  345. grpcOpts["grpc-service-name"] = serviceName
  346. }
  347. }
  348. if len(grpcOpts) > 0 {
  349. proxy["grpc-opts"] = grpcOpts
  350. }
  351. return true
  352. case "httpupgrade":
  353. proxy["network"] = "httpupgrade"
  354. hu, _ := stream["httpupgradeSettings"].(map[string]any)
  355. opts := map[string]any{}
  356. if hu != nil {
  357. if path, ok := hu["path"].(string); ok && path != "" {
  358. opts["path"] = path
  359. }
  360. host := ""
  361. if v, ok := hu["host"].(string); ok && v != "" {
  362. host = v
  363. } else if headers, ok := hu["headers"].(map[string]any); ok {
  364. host = searchHost(headers)
  365. }
  366. if host != "" {
  367. opts["headers"] = map[string]any{"Host": host}
  368. }
  369. }
  370. if len(opts) > 0 {
  371. proxy["http-upgrade-opts"] = opts
  372. }
  373. return true
  374. case "xhttp":
  375. proxy["network"] = "xhttp"
  376. xhttp, _ := stream["xhttpSettings"].(map[string]any)
  377. opts := map[string]any{}
  378. if xhttp != nil {
  379. if path, ok := xhttp["path"].(string); ok && path != "" {
  380. opts["path"] = path
  381. }
  382. host := ""
  383. if v, ok := xhttp["host"].(string); ok && v != "" {
  384. host = v
  385. } else if headers, ok := xhttp["headers"].(map[string]any); ok {
  386. host = searchHost(headers)
  387. }
  388. if host != "" {
  389. opts["host"] = host
  390. }
  391. if mode, ok := xhttp["mode"].(string); ok && mode != "" {
  392. opts["mode"] = mode
  393. }
  394. }
  395. if len(opts) > 0 {
  396. proxy["xhttp-opts"] = opts
  397. }
  398. return true
  399. default:
  400. return false
  401. }
  402. }
  403. func (s *SubClashService) applySecurity(proxy map[string]any, security string, stream map[string]any) bool {
  404. switch security {
  405. case "", "none":
  406. proxy["tls"] = false
  407. return true
  408. case "tls":
  409. proxy["tls"] = true
  410. tlsSettings, _ := stream["tlsSettings"].(map[string]any)
  411. if tlsSettings != nil {
  412. if serverName, ok := tlsSettings["serverName"].(string); ok && serverName != "" {
  413. proxy["servername"] = serverName
  414. switch proxy["type"] {
  415. case "trojan":
  416. proxy["sni"] = serverName
  417. }
  418. }
  419. if fingerprint, ok := tlsSettings["fingerprint"].(string); ok && fingerprint != "" {
  420. proxy["client-fingerprint"] = fingerprint
  421. }
  422. if alpn, ok := externalProxyALPNList(tlsSettings["alpn"]); ok {
  423. out := make([]string, 0, len(alpn))
  424. for _, item := range alpn {
  425. if s, ok := item.(string); ok && s != "" {
  426. out = append(out, s)
  427. }
  428. }
  429. if len(out) > 0 {
  430. proxy["alpn"] = out
  431. }
  432. }
  433. }
  434. return true
  435. case "reality":
  436. proxy["tls"] = true
  437. realitySettings, _ := stream["realitySettings"].(map[string]any)
  438. if realitySettings == nil {
  439. return false
  440. }
  441. if serverName, ok := realitySettings["serverName"].(string); ok && serverName != "" {
  442. proxy["servername"] = serverName
  443. }
  444. realityOpts := map[string]any{}
  445. if publicKey, ok := realitySettings["publicKey"].(string); ok && publicKey != "" {
  446. realityOpts["public-key"] = publicKey
  447. }
  448. if shortID, ok := realitySettings["shortId"].(string); ok && shortID != "" {
  449. realityOpts["short-id"] = shortID
  450. }
  451. if len(realityOpts) > 0 {
  452. proxy["reality-opts"] = realityOpts
  453. }
  454. if fingerprint, ok := realitySettings["fingerprint"].(string); ok && fingerprint != "" {
  455. proxy["client-fingerprint"] = fingerprint
  456. }
  457. return true
  458. default:
  459. return false
  460. }
  461. }
  462. func (s *SubClashService) streamData(stream string) map[string]any {
  463. var streamSettings map[string]any
  464. json.Unmarshal([]byte(stream), &streamSettings)
  465. security, _ := streamSettings["security"].(string)
  466. switch security {
  467. case "tls":
  468. if tlsSettings, ok := streamSettings["tlsSettings"].(map[string]any); ok {
  469. streamSettings["tlsSettings"] = s.tlsData(tlsSettings)
  470. }
  471. case "reality":
  472. if realitySettings, ok := streamSettings["realitySettings"].(map[string]any); ok {
  473. streamSettings["realitySettings"] = s.realityData(realitySettings)
  474. }
  475. }
  476. delete(streamSettings, "sockopt")
  477. return streamSettings
  478. }
  479. func (s *SubClashService) tlsData(tData map[string]any) map[string]any {
  480. tlsData := make(map[string]any, 1)
  481. tlsClientSettings, _ := tData["settings"].(map[string]any)
  482. tlsData["serverName"] = tData["serverName"]
  483. tlsData["alpn"] = tData["alpn"]
  484. if fingerprint, ok := tlsClientSettings["fingerprint"].(string); ok {
  485. tlsData["fingerprint"] = fingerprint
  486. }
  487. if pins, ok := tlsClientSettings["pinnedPeerCertSha256"].([]any); ok && len(pins) > 0 {
  488. tlsData["pin-sha256"] = pins
  489. }
  490. return tlsData
  491. }
  492. func (s *SubClashService) realityData(rData map[string]any) map[string]any {
  493. rDataOut := make(map[string]any, 1)
  494. realityClientSettings, _ := rData["settings"].(map[string]any)
  495. if publicKey, ok := realityClientSettings["publicKey"].(string); ok {
  496. rDataOut["publicKey"] = publicKey
  497. }
  498. if fingerprint, ok := realityClientSettings["fingerprint"].(string); ok {
  499. rDataOut["fingerprint"] = fingerprint
  500. }
  501. if serverNames, ok := rData["serverNames"].([]any); ok && len(serverNames) > 0 {
  502. rDataOut["serverName"] = fmt.Sprint(serverNames[0])
  503. }
  504. if shortIDs, ok := rData["shortIds"].([]any); ok && len(shortIDs) > 0 {
  505. rDataOut["shortId"] = fmt.Sprint(shortIDs[0])
  506. }
  507. return rDataOut
  508. }
  509. func cloneMap(src map[string]any) map[string]any {
  510. if src == nil {
  511. return nil
  512. }
  513. dst := make(map[string]any, len(src))
  514. maps.Copy(dst, src)
  515. return dst
  516. }
  517. func mergeClashRulesYAML(base map[string]any, raw string) error {
  518. raw = strings.TrimSpace(raw)
  519. if raw == "" {
  520. return nil
  521. }
  522. var custom any
  523. if err := yaml.Unmarshal([]byte(raw), &custom); err != nil {
  524. mergeClashRules(base, linesToClashRules(raw))
  525. return nil
  526. }
  527. switch typed := custom.(type) {
  528. case []any:
  529. mergeClashRules(base, typed)
  530. case map[string]any:
  531. if rules, ok := typed["rules"]; ok {
  532. if ruleList, ok := asAnySlice(rules); ok {
  533. mergeClashRules(base, ruleList)
  534. }
  535. }
  536. default:
  537. mergeClashRules(base, linesToClashRules(raw))
  538. }
  539. return nil
  540. }
  541. func mergeClashRules(base map[string]any, customRules []any) {
  542. if len(customRules) == 0 {
  543. return
  544. }
  545. baseRules, _ := asAnySlice(base["rules"])
  546. if hasClashMatchRule(customRules) {
  547. base["rules"] = customRules
  548. return
  549. }
  550. merged := make([]any, 0, len(customRules)+len(baseRules))
  551. merged = append(merged, customRules...)
  552. merged = append(merged, baseRules...)
  553. base["rules"] = merged
  554. }
  555. func asAnySlice(value any) ([]any, bool) {
  556. switch typed := value.(type) {
  557. case []any:
  558. return typed, true
  559. case []string:
  560. out := make([]any, 0, len(typed))
  561. for _, item := range typed {
  562. out = append(out, item)
  563. }
  564. return out, true
  565. case []map[string]any:
  566. out := make([]any, 0, len(typed))
  567. for _, item := range typed {
  568. out = append(out, item)
  569. }
  570. return out, true
  571. default:
  572. return nil, false
  573. }
  574. }
  575. func hasClashMatchRule(rules []any) bool {
  576. for _, rule := range rules {
  577. ruleText, ok := rule.(string)
  578. if !ok {
  579. continue
  580. }
  581. parts := strings.SplitN(ruleText, ",", 2)
  582. if strings.EqualFold(strings.TrimSpace(parts[0]), "MATCH") {
  583. return true
  584. }
  585. }
  586. return false
  587. }
  588. func linesToClashRules(raw string) []any {
  589. lines := strings.Split(raw, "\n")
  590. rules := make([]any, 0, len(lines))
  591. for _, line := range lines {
  592. line = strings.TrimSpace(line)
  593. if line == "" || strings.HasPrefix(line, "#") {
  594. continue
  595. }
  596. rules = append(rules, line)
  597. }
  598. return rules
  599. }