gfw2adg.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/sh
  2. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  3. checkmd5(){
  4. local nowmd5=$(md5sum /tmp/adguard.list 2>/dev/null)
  5. nowmd5=${nowmd5%% *}
  6. local lastmd5=$(uci get AdGuardHome.AdGuardHome.gfwlistmd5)
  7. if [ "$nowmd5" != "$lastmd5" ]; then
  8. /etc/init.d/AdGuardHome reload
  9. uci get AdGuardHome.AdGuardHome.gfwlistmd5="$nowmd5"
  10. [ "$2" == "noreload" ] && return
  11. uci commit AdGuardHome
  12. fi
  13. }
  14. configpath=$(uci get AdGuardHome.AdGuardHome.configpath)
  15. [ "$1" == "del" ] && sed -i '/programaddstart/,/programaddend/d' $configpath && checkmd5 && exit 0
  16. gfwupstream=$(uci get AdGuardHome.AdGuardHome.gfwupstream)
  17. if [ -z $gfwupstream ]; then
  18. gfwupstream="tcp://208.67.220.220:5353"
  19. fi
  20. if [ ! -f "$configpath" ]; then
  21. echo "please make a config first"
  22. exit 1
  23. fi
  24. wget-ssl --no-check-certificate https://cdn.jsdelivr.net/gh/gfwlist/gfwlist/gfwlist.txt -O- | base64 -d > /tmp/gfwlist.txt
  25. cat /tmp/gfwlist.txt | awk -v upst="$gfwupstream" 'BEGIN{getline;}{
  26. s1=substr($0,1,1);
  27. if (s1=="!")
  28. {next;}
  29. if (s1=="@"){
  30. $0=substr($0,3);
  31. s1=substr($0,1,1);
  32. white=1;}
  33. else{
  34. white=0;
  35. }
  36. if (s1=="|")
  37. {s2=substr($0,2,1);
  38. if (s2=="|")
  39. {
  40. $0=substr($0,3);
  41. split($0,d,"/");
  42. $0=d[1];
  43. }else{
  44. split($0,d,"/");
  45. $0=d[3];
  46. }}
  47. else{
  48. split($0,d,"/");
  49. $0=d[1];
  50. }
  51. star=index($0,"*");
  52. if (star!=0)
  53. {
  54. $0=substr($0,star+1);
  55. dot=index($0,".");
  56. if (dot!=0)
  57. $0=substr($0,dot+1);
  58. else
  59. next;
  60. s1=substr($0,1,1);
  61. }
  62. if (s1==".")
  63. {fin=substr($0,2);}
  64. else{fin=$0;}
  65. if (index(fin,".")==0) next;
  66. if (index(fin,"%")!=0) next;
  67. if (index(fin,":")!=0) next;
  68. match(fin,"^[0-9\.]+")
  69. if (RSTART==1 && RLENGTH==length(fin)) {print "ipset add gfwlist "fin>"/tmp/doipset.sh";next;}
  70. if (fin=="" || finl==fin) next;
  71. finl=fin;
  72. if (white==0)
  73. {print(" - '\''[/"fin"/]"upst"'\''");}
  74. else{
  75. print(" - '\''[/"fin"/]#'\''");}
  76. }END{print(" - '\''[/programaddend/]#'\''")}' > /tmp/adguard.list
  77. grep programaddstart $configpath
  78. if [ "$?" == "0" ]; then
  79. sed -i '/programaddstart/,/programaddend/c\ - '\''\[\/programaddstart\/\]#'\''' $configpath
  80. sed -i '/programaddstart/'r/tmp/adguard.list $configpath
  81. else
  82. sed -i '1i\ - '\''[/programaddstart/]#'\''' /tmp/adguard.list
  83. sed -i '/upstream_dns:/'r/tmp/adguard.list $configpath
  84. fi
  85. checkmd5
  86. rm -f /tmp/gfwlist.txt /tmp/adguard.list