update_core.sh 6.4 KB

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