gfw2adg.sh 2.3 KB

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