gfw2adg.sh 1.9 KB

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