gfw2adg.sh 2.0 KB

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