Advertisement
Guest User

update-rules.sh

a guest
Nov 19th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.64 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -e
  4.  
  5. TEMP_RULES_FILE="/tmp/clash-rules-temp.txt"
  6. CUSTOM_RULES_FILE="$HOME/.config/clash/custom-rules.txt"
  7. CONFIG_FILE="$HOME/.config/clash/config.yaml"
  8.  
  9. CLASH_API_URL="http://127.0.0.1:9090/configs"
  10.  
  11. CHINA_DOMAINS_URL="https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf"
  12. CHINA_IPS_URL="https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt"
  13. GFWList_URL="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
  14.  
  15. GOOGLE_URL="https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/google.china.conf"
  16. APPLE_URL="https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf"
  17.  
  18.  
  19. echo "⚡️ 正在获取中国 DIRECT 域名列表……"
  20. curl -sSL $CHINA_DOMAINS_URL | awk -F '/' '{printf "  - DOMAIN-SUFFIX,%s,DIRECT\n", $2}' > $TEMP_RULES_FILE
  21. echo "✅ 已生成中国 DIRECT 域名列表"
  22.  
  23. echo "⚡️ 正在获取 Google PROXY 域名列表……"
  24. curl -sSL $GOOGLE_URL | awk -F '/' '{printf "  - DOMAIN-SUFFIX,%s,PROXY\n", $2}' >> $TEMP_RULES_FILE
  25. echo "✅ 已生成 Google PROXY 域名列表"
  26.  
  27. echo "⚡️ 正在获取 Apple PROXY 域名列表……"
  28. curl -sSL $APPLE_URL | awk -F '/' '{printf "  - DOMAIN-SUFFIX,%s,PROXY\n", $2}' >> $TEMP_RULES_FILE
  29. echo "✅ 已生成 Apple PROXY 域名列表"
  30.  
  31. echo "⚡️ 正在获取 GFWList 列表……"
  32. curl -sSL $GFWList_URL | \
  33.   base64 -Dd | \
  34.   grep -Eo '^(\|\||\.)(([0-9a-zA-Z-]+\.)+[0-9a-zA-Z-]+)' | \
  35.   grep -Eo '([0-9a-zA-Z-]+\.)+[0-9a-zA-Z-]+' | \
  36.   sort -su | \
  37.   awk '{printf "  - DOMAIN-SUFFIX,%s,PROXY\n", $0}' >> $TEMP_RULES_FILE
  38. echo "✅ 已生成 GFWList PROXY 域名列表并去重"
  39.  
  40. echo "⚡️ 正在获取中国 DIRECT IP 列表……"
  41. curl -sSL $CHINA_IPS_URL | awk '{printf "  - IP-CIDR,%s,DIRECT\n", $0}' >> $TEMP_RULES_FILE
  42. echo "✅ 已生成中国 DIRECT IP 列表"
  43.  
  44. echo "------------附加内容-------------"
  45. cat $CUSTOM_RULES_FILE >> $TEMP_RULES_FILE
  46. cat $CUSTOM_RULES_FILE
  47. echo "------------插入完成-------------"
  48.  
  49. # 在 Rule 后面新增行,作为查找删除内容的标志
  50. sed -i '' '/Rule:/a\
  51. This is a temp line' $CONFIG_FILE
  52.  
  53. # 删除 This is a temp line 及后面所有内容
  54. sed -i '' '/^This\ is\ a\ temp\ line/,$d' $CONFIG_FILE
  55.  
  56. echo "⚡️ 正在写入新路由规则……"
  57. # 将新规则插入在 Rule 之后
  58. sed -i '' "/Rule:/ r $TEMP_RULES_FILE" $CONFIG_FILE
  59. echo "✅ 已成功写入新路由规则"
  60.  
  61. echo "⚡️ 正在重新载入 Clash 配置……"
  62. curl -X PUT -H 'Content-Type: application/json' -d "{'path':'$CONFIG_FILE'}" $CLASH_API_URL
  63. echo "✅ 成功啦!🍺🍟🍔"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement