update_core.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 get 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/AdGuardHome/update/AdGuardHome" >/dev/null 2>&1
  56. rm -fr /tmp/AdGuardHome/update/AdGuardHome/${binpath##*/}
  57. /tmp/upx-${upx_latest_ver}-${Arch}_linux/upx $upxflag $binpath -o /tmp/AdGuardHome/update/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/AdGuardHome/update/AdGuardHome/${binpath##*/} $binpath
  62. /etc/init.d/AdGuardHome start
  63. echo -e "finished"
  64. fi
  65. fi
  66. exit 3
  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"
  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" >/dev/null 2>&1
  120. #tar xvJf
  121. which xz || (opkg update && opkg install xz) || 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. rm /tmp/upx-${upx_latest_ver}-${Arch}_linux.tar.xz
  125. }
  126. doupdate_core(){
  127. echo -e "Updating core..."
  128. mkdir -p "/tmp/AdGuardHome/update" >/dev/null 2>&1
  129. rm -rf /tmp/AdGuardHome/update/* >/dev/null 2>&1
  130. Archt="$(opkg info kernel | grep Architecture | awk -F "[ _]" '{print($2)}')"
  131. case $Archt in
  132. "i386")
  133. Arch="386"
  134. ;;
  135. "i686")
  136. Arch="386"
  137. ;;
  138. "x86")
  139. Arch="amd64"
  140. ;;
  141. "mipsel")
  142. Arch="mipsle"
  143. ;;
  144. "mips64el")
  145. Arch="mips64le"
  146. Arch="mipsle"
  147. echo -e "mips64el use $Arch may have bug"
  148. ;;
  149. "mips")
  150. Arch="mips"
  151. ;;
  152. "mips64")
  153. Arch="mips64"
  154. Arch="mips"
  155. echo -e "mips64 use $Arch may have bug"
  156. ;;
  157. "arm")
  158. Arch="arm"
  159. ;;
  160. "aarch64")
  161. Arch="arm64"
  162. ;;
  163. "powerpc")
  164. Arch="ppc"
  165. echo -e "error not support $Archt"
  166. exit 1
  167. ;;
  168. "powerpc64")
  169. Arch="ppc64"
  170. echo -e "error not support $Archt"
  171. exit 1
  172. ;;
  173. *)
  174. echo -e "error not support $Archt"
  175. exit 1
  176. ;;
  177. esac
  178. echo -e "start download ${latest_ver}/AdGuardHome_linux_${Arch}.tar.gz"
  179. wget-ssl --no-check-certificate -t 1 -T 10 -O /tmp/AdGuardHome/update/AdGuardHome_linux_${Arch}.tar.gz "https://github.com/AdguardTeam/AdGuardHome/releases/download/${latest_ver}/AdGuardHome_linux_${Arch}.tar.gz" >/dev/null 2>&1
  180. tar -zxf "/tmp/AdGuardHome/update/AdGuardHome_linux_${Arch}.tar.gz" -C "/tmp/AdGuardHome/update/" >/dev/null 2>&1
  181. if [ ! -e "/tmp/AdGuardHome/update/AdGuardHome" ]; then
  182. echo -e "Failed to download core."
  183. rm -rf "/tmp/AdGuardHome/update" >/dev/null 2>&1
  184. exit 1
  185. else
  186. echo -e "download success start copy"
  187. if [ ! -z "$upxflag" ]; then
  188. echo -e "start upx may take a long time"
  189. doupx
  190. #maybe need chmod
  191. /tmp/upx-${upx_latest_ver}-${Arch}_linux/upx $upxflag /tmp/AdGuardHome/update/AdGuardHome/AdGuardHome
  192. rm -rf /tmp/upx-${upx_latest_ver}-${Arch}_linux
  193. fi
  194. echo -e "start copy"
  195. /etc/init.d/AdGuardHome stop
  196. rm "$binpath"
  197. mv -f /tmp/AdGuardHome/update/AdGuardHome/AdGuardHome "$binpath"
  198. if [ "$?" == "1" ]; then
  199. echo "mv failed maybe not enough space please use upx or change bin to /tmp/AdGuardHome"
  200. exit 1
  201. fi
  202. /etc/init.d/AdGuardHome start
  203. fi
  204. rm -rf "/tmp/AdGuardHome/update" >/dev/null 2>&1
  205. echo -e "Succeeded in updating core."
  206. uci set AdGuardHome.AdGuardHome.version="${latest_ver}"
  207. uci commit AdGuardHome
  208. echo -e "Local version: ${now_ver}, cloud version: ${latest_ver}.\n"
  209. }
  210. main(){
  211. check_if_already_running
  212. check_latest_version
  213. }
  214. main