update_core.sh 6.6 KB

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