update_core.sh 6.2 KB

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