update_core.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/bin/bash
  2. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  3. binpath=$(uci get AdGuardHome.AdGuardHome.binpath)
  4. if [ -z "$binpath" ]; then
  5. uci set AdGuardHome.AdGuardHome.binpath="/tmp/AdGuardHome/AdGuardHome"
  6. binpath="/tmp/AdGuardHome/AdGuardHome"
  7. fi
  8. mkdir -p ${binpath%/*}
  9. upxflag=$(uci get AdGuardHome.AdGuardHome.upxflag 2>&1 >/dev/null)
  10. check_if_already_running(){
  11. running_tasks="$(ps |grep "AdGuardHome" |grep "update_core" |grep -v "grep" |awk '{print $1}' |wc -l)"
  12. [ "${running_tasks}" -gt "2" ] && echo -e "\nA task is already running." && exit 2
  13. }
  14. clean_log(){
  15. echo "" > /tmp/AdGuardHome_update.log
  16. }
  17. check_latest_version(){
  18. latest_ver="$(wget -O- https://api.github.com/repos/AdguardTeam/AdGuardHome/releases/latest 2>/dev/null|grep -E 'tag_name' |grep -E 'v[0-9.]+' -o 2>/dev/null)"
  19. if [ -z "${latest_ver}" ]; then
  20. wget -V | grep +https >/dev/null || (opkg update && opkg remove wget-nossl --force-depends && opkg install wget && check_latest_version && exit 0)
  21. echo -e "\nFailed to check latest version, please try again later." && exit 1
  22. fi
  23. touch /var/run/AdGfakeconfig
  24. now_ver="$($binpath -c /var/run/AdGfakeconfig --check-config 2>&1| grep -m 1 -E 'v[0-9.]+' -o)"
  25. rm /var/run/AdGfakeconfig
  26. if [ "${latest_ver}"x != "${now_ver}"x ]; then
  27. clean_log
  28. echo -e "Local version: ${now_ver}., cloud version: ${latest_ver}."
  29. doupdate_core
  30. else
  31. echo -e "\nLocal version: ${now_ver}, cloud version: ${latest_ver}."
  32. echo -e "You're already using the latest version."
  33. if [ ! -z "$upxflag" ]; then
  34. filesize=$(ls -l $binpath | awk '{ print $5 }')
  35. if [ $filesize -gt 8000000 ]; then
  36. echo -e "start upx may take a long time"
  37. doupx
  38. mkdir -p "/tmp/AdGuardHomeupdate/AdGuardHome" >/dev/null 2>&1
  39. rm -fr /tmp/AdGuardHomeupdate/AdGuardHome/${binpath##*/}
  40. /tmp/upx-${upx_latest_ver}-${Arch}_linux/upx $upxflag $binpath -o /tmp/AdGuardHomeupdate/AdGuardHome/${binpath##*/}
  41. rm -rf /tmp/upx-${upx_latest_ver}-${Arch}_linux
  42. /etc/init.d/AdGuardHome stop
  43. rm $binpath
  44. mv -f /tmp/AdGuardHomeupdate/AdGuardHome/${binpath##*/} $binpath
  45. /etc/init.d/AdGuardHome start
  46. echo -e "finished"
  47. fi
  48. fi
  49. exit 0
  50. fi
  51. }
  52. doupx(){
  53. Archt="$(opkg info kernel | grep Architecture | awk -F "[ _]" '{print($2)}')"
  54. case $Archt in
  55. "i386")
  56. Arch="i386"
  57. ;;
  58. "i686")
  59. Arch="i386"
  60. echo -e "i686 use $Arch may have bug"
  61. ;;
  62. "x86")
  63. Arch="amd64"
  64. ;;
  65. "mipsel")
  66. Arch="mipsel"
  67. ;;
  68. "mips64el")
  69. Arch="mips64el"
  70. Arch="mipsel"
  71. echo -e "mips64el use $Arch may have bug"
  72. ;;
  73. "mips")
  74. Arch="mips"
  75. ;;
  76. "mips64")
  77. Arch="mips64"
  78. Arch="mips"
  79. echo -e "mips64 use $Arch may have bug"
  80. ;;
  81. "arm")
  82. Arch="arm"
  83. ;;
  84. "armeb")
  85. Arch="armeb"
  86. ;;
  87. "aarch64")
  88. Arch="arm64"
  89. ;;
  90. "powerpc")
  91. Arch="powerpc"
  92. ;;
  93. "powerpc64")
  94. Arch="powerpc64"
  95. ;;
  96. *)
  97. echo -e "error not support $Archt if you can use offical release please issue a bug"
  98. exit 1
  99. ;;
  100. esac
  101. upx_latest_ver="$(wget -O- https://api.github.com/repos/upx/upx/releases/latest 2>/dev/null|grep -E 'tag_name' |grep -E '[0-9.]+' -o 2>/dev/null)"
  102. wget-ssl --no-check-certificate -t 1 -T 10 -O /tmp/upx-${upx_latest_ver}-${Arch}_linux.tar.xz "https://github.com/upx/upx/releases/download/v${upx_latest_ver}/upx-${upx_latest_ver}-${Arch}_linux.tar.xz" 2>&1
  103. #tar xvJf
  104. which xz || (opkg update && opkg install xz) || (echo "xz download fail" && exit 1)
  105. mkdir -p /tmp/upx-${upx_latest_ver}-${Arch}_linux
  106. xz -d -c /tmp/upx-${upx_latest_ver}-${Arch}_linux.tar.xz| tar -x -C "/tmp" >/dev/null 2>&1
  107. if [ ! -e "/tmp/upx-${upx_latest_ver}-${Arch}_linux/upx" ]; then
  108. echo -e "Failed to download upx."
  109. exit 1
  110. fi
  111. rm /tmp/upx-${upx_latest_ver}-${Arch}_linux.tar.xz
  112. }
  113. doupdate_core(){
  114. echo -e "Updating core..."
  115. mkdir -p "/tmp/AdGuardHomeupdate"
  116. rm -rf /tmp/AdGuardHomeupdate/* >/dev/null 2>&1
  117. Archt="$(opkg info kernel | grep Architecture | awk -F "[ _]" '{print($2)}')"
  118. case $Archt in
  119. "i386")
  120. Arch="386"
  121. ;;
  122. "i686")
  123. Arch="386"
  124. ;;
  125. "x86")
  126. Arch="amd64"
  127. ;;
  128. "mipsel")
  129. Arch="mipsle"
  130. ;;
  131. "mips64el")
  132. Arch="mips64le"
  133. Arch="mipsle"
  134. echo -e "mips64el use $Arch may have bug"
  135. ;;
  136. "mips")
  137. Arch="mips"
  138. ;;
  139. "mips64")
  140. Arch="mips64"
  141. Arch="mips"
  142. echo -e "mips64 use $Arch may have bug"
  143. ;;
  144. "arm")
  145. Arch="arm"
  146. ;;
  147. "aarch64")
  148. Arch="arm64"
  149. ;;
  150. "powerpc")
  151. Arch="ppc"
  152. echo -e "error not support $Archt"
  153. exit 1
  154. ;;
  155. "powerpc64")
  156. Arch="ppc64"
  157. echo -e "error not support $Archt"
  158. exit 1
  159. ;;
  160. *)
  161. echo -e "error not support $Archt if you can use offical release please issue a bug"
  162. exit 1
  163. ;;
  164. esac
  165. echo -e "start download"
  166. while read link
  167. do
  168. eval link=$link
  169. wget-ssl --no-check-certificate -t 2 -T 20 -O /tmp/AdGuardHomeupdate/${link##*/} "$link" 2>&1
  170. if [ "$?" != "0" ]; then
  171. echo "download failed try another download"
  172. rm -f /tmp/AdGuardHomeupdate/${link##*/}
  173. else
  174. local success="1"
  175. break
  176. fi
  177. done < $(grep -v "^#" /usr/share/AdGuardHome/links.txt)
  178. [ -z "$success" ] && echo "no download success" && exit 1
  179. if [ "${link##*.}" == "gz" ]; then
  180. tar -zxf "/tmp/AdGuardHomeupdate/${link##*/}" -C "/tmp/AdGuardHomeupdate/"
  181. if [ ! -e "/tmp/AdGuardHomeupdate/AdGuardHome" ]; then
  182. echo -e "Failed to download core."
  183. rm -rf "/tmp/AdGuardHomeupdate" >/dev/null 2>&1
  184. exit 1
  185. fi
  186. downloadbin="/tmp/AdGuardHomeupdate/AdGuardHome/AdGuardHome"
  187. else
  188. downloadbin="/tmp/AdGuardHomeupdate/${link##*/}"
  189. fi
  190. chmod 755 $downloadbin
  191. echo -e "download success start copy"
  192. if [ ! -z "$upxflag" ]; then
  193. echo -e "start upx may take a long time"
  194. doupx
  195. /tmp/upx-${upx_latest_ver}-${Arch}_linux/upx $upxflag $downloadbin
  196. rm -rf /tmp/upx-${upx_latest_ver}-${Arch}_linux
  197. fi
  198. echo -e "start copy"
  199. /etc/init.d/AdGuardHome stop
  200. rm "$binpath"
  201. mv -f "$downloadbin" "$binpath"
  202. if [ "$?" == "1" ]; then
  203. echo "mv failed maybe not enough space please use upx or change bin to /tmp/AdGuardHome"
  204. exit 1
  205. fi
  206. /etc/init.d/AdGuardHome start
  207. rm -rf "/tmp/AdGuardHomeupdate" >/dev/null 2>&1
  208. echo -e "Succeeded in updating core."
  209. echo -e "Local version: ${latest_ver}, cloud version: ${latest_ver}.\n"
  210. }
  211. main(){
  212. check_if_already_running
  213. check_latest_version
  214. }
  215. main