AdGuardHome 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=95
  4. STOP=01
  5. CONFIGURATION=AdGuardHome
  6. EXTRA_COMMANDS="do_redirect"
  7. EXTRA_HELP=" do_redirect 0 or 1"
  8. set_forward_dnsmasq()
  9. {
  10. local PORT="$1"
  11. addr="127.0.0.1#$PORT"
  12. OLD_SERVER="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
  13. echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1
  14. if [ $? -eq 0 ]; then
  15. return
  16. fi
  17. uci delete dhcp.@dnsmasq[0].server 2>/dev/null
  18. uci add_list dhcp.@dnsmasq[0].server=$addr
  19. for server in $OLD_SERVER; do
  20. if [ "$server" = "$addr" ]; then
  21. continue
  22. fi
  23. uci add_list dhcp.@dnsmasq[0].server=$server
  24. done
  25. uci delete dhcp.@dnsmasq[0].resolvfile 2>/dev/null
  26. uci set dhcp.@dnsmasq[0].noresolv=1
  27. uci commit dhcp
  28. /etc/init.d/dnsmasq restart
  29. }
  30. stop_forward_dnsmasq()
  31. {
  32. local OLD_PORT="$1"
  33. addr="127.0.0.1#$OLD_PORT"
  34. OLD_SERVER="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
  35. echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1
  36. if [ $? -ne 0 ]; then
  37. return
  38. fi
  39. uci del_list dhcp.@dnsmasq[0].server=$addr 2>/dev/null
  40. addrlist="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
  41. if [ -z "$addrlist" ] ; then
  42. uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 2>/dev/null
  43. uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null
  44. fi
  45. uci commit dhcp
  46. /etc/init.d/dnsmasq restart
  47. }
  48. set_iptable()
  49. {
  50. local ipv6_server=$1
  51. local tcp_server=$2
  52. IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`"
  53. for IP in $IPS
  54. do
  55. if [ "$tcp_server" == "1" ]; then
  56. iptables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  57. fi
  58. iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  59. done
  60. if [ "$ipv6_server" == 0 ]; then
  61. return
  62. fi
  63. IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
  64. for IP in $IPS
  65. do
  66. if [ "$tcp_server" == "1" ]; then
  67. ip6tables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  68. fi
  69. ip6tables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  70. done
  71. }
  72. clear_iptable()
  73. {
  74. local OLD_PORT="$1"
  75. local ipv6_server=$2
  76. IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`"
  77. for IP in $IPS
  78. do
  79. iptables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  80. iptables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  81. done
  82. if [ "$ipv6_server" == 0 ]; then
  83. return
  84. fi
  85. IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
  86. for IP in $IPS
  87. do
  88. ip6tables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  89. ip6tables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  90. done
  91. }
  92. service_triggers() {
  93. procd_add_reload_trigger firewall
  94. procd_add_reload_trigger "$CONFIGURATION"
  95. }
  96. get_tz()
  97. {
  98. SET_TZ=""
  99. if [ -e "/etc/localtime" ]; then
  100. return
  101. fi
  102. for tzfile in /etc/TZ /var/etc/TZ
  103. do
  104. if [ ! -e "$tzfile" ]; then
  105. continue
  106. fi
  107. tz="`cat $tzfile 2>/dev/null`"
  108. done
  109. if [ -z "$tz" ]; then
  110. return
  111. fi
  112. SET_TZ=$tz
  113. }
  114. do_redirect()
  115. {
  116. config_load "${CONFIGURATION}"
  117. local section="$CONFIGURATION"
  118. args=""
  119. ipv6_server=0
  120. tcp_server=0
  121. enabled=$1
  122. config_get configpath $CONFIGURATION configpath "/etc/AdGuardHome.yaml"
  123. AdGuardHome_PORT=$(awk '/ port:/{printf($2)}' $configpath)
  124. if [ -z "$AdGuardHome_PORT" ]; then
  125. AdGuardHome_PORT="0"
  126. fi
  127. config_get "redirect" "$section" "redirect" "none"
  128. config_get "old_redirect" "$section" "old_redirect" "none"
  129. config_get "old_port" "$section" "old_port" "0"
  130. config_get "old_enabled" "$section" "old_enabled" "0"
  131. if [ "$old_redirect" != "$redirect" ] || [ "$old_port" != "$AdGuardHome_PORT" ] || [ "$old_enabled" = "1" -a "$enabled" = "0" ]; then
  132. if [ "$old_redirect" != "none" ]; then
  133. if [ "$old_port" != "0" ]; then
  134. clear_iptable "$old_port" "$ipv6_server"
  135. fi
  136. if [ "$old_redirect" == "dnsmasq-upstream" ]; then
  137. stop_forward_dnsmasq "$old_port"
  138. fi
  139. fi
  140. fi
  141. uci delete AdGuardHome.@AdGuardHome[0].old_redirect 2>/dev/null
  142. uci delete AdGuardHome.@AdGuardHome[0].old_port 2>/dev/null
  143. uci delete AdGuardHome.@AdGuardHome[0].old_enabled 2>/dev/null
  144. uci add_list AdGuardHome.@AdGuardHome[0].old_redirect="$redirect" 2>/dev/null
  145. uci add_list AdGuardHome.@AdGuardHome[0].old_port="$AdGuardHome_PORT" 2>/dev/null
  146. uci add_list AdGuardHome.@AdGuardHome[0].old_enabled="$enabled" 2>/dev/null
  147. uci commit AdGuardHome
  148. [ "$enabled" -gt 0 ] || return 1
  149. if [ "$AdGuardHome_PORT" == "0" ]; then
  150. return 1
  151. fi
  152. if [ "$redirect" = "redirect" ]; then
  153. set_iptable $ipv6_server $tcp_server
  154. elif [ "$redirect" = "dnsmasq-upstream" ]; then
  155. set_forward_dnsmasq "$AdGuardHome_PORT"
  156. fi
  157. #procd_open_instance "AdGuardHome"
  158. #get_tz
  159. #if [ ! -z "$SET_TZ" ]; then
  160. # procd_set_param env TZ="$SET_TZ"
  161. #fi
  162. #procd_set_param file "$AdGuardHome_CONF"
  163. #procd_close_instance
  164. }
  165. get_filesystem()
  166. {
  167. # print out path filesystem
  168. echo $1 | awk '
  169. BEGIN{
  170. while (("mount"| getline ret) > 0)
  171. {
  172. split(ret,d);
  173. fs[d[3]]=d[5];
  174. m=index(d[1],":")
  175. if (m==0)
  176. {
  177. pt[d[3]]=d[1]
  178. }else{
  179. pt[d[3]]=substr(d[1],m+1)
  180. }}}{
  181. split($0,d,"/");
  182. if ("/" in fs)
  183. {
  184. result1=fs["/"];
  185. }
  186. if ("/" in pt)
  187. {
  188. result2=pt["/"];
  189. }
  190. for (i=2;i<=length(d);i++)
  191. {
  192. p[i]=p[i-1]"/"d[i];
  193. if (p[i] in fs)
  194. {
  195. result1=fs[p[i]];
  196. result2=pt[p[i]];
  197. }
  198. }
  199. if (result2 in fs){
  200. result=fs[result2]}
  201. else{
  202. result=result1}
  203. print(result);}'
  204. }
  205. config_editor()
  206. {
  207. awk -v yaml="$1" -v value="$2" -v file="$3" '
  208. BEGIN{split(yaml,part,"\.");s="";i=1;l=length(part);}
  209. {
  210. if (match($0,s""part[i]))
  211. {
  212. if (i==l)
  213. {
  214. split($0,t,":");
  215. system("sed -i '\''"FNR"c \\"t[1]": "value"'\'' "file);
  216. exit;
  217. }
  218. s=s"[- ]{2}";
  219. i++;
  220. }
  221. }' $3
  222. }
  223. start_service() {
  224. # Reading config
  225. config_load "${CONFIGURATION}"
  226. config_get hashpass $CONFIGURATION hashpass ""
  227. if [ ! -z "$hashpass" ]; then
  228. config_editor "users.password" "$hashpass" "$configpath"
  229. uci set $CONFIGURATION.$CONFIGURATION.hashpass=""
  230. fi
  231. local enabled
  232. config_get_bool enabled $CONFIGURATION enabled 0
  233. if [ "$enabled" == "1" ]; then
  234. local ADDITIONAL_ARGS=""
  235. config_get configpath $CONFIGURATION configpath "/etc/AdGuardHome.yaml"
  236. config_get binpath $CONFIGURATION binpath "/usr/bin/AdGuardHome/AdGuardHome"
  237. echo -e "$configpath\n$binpath">/lib/upgrade/keep.d/luci-app-adguardhome
  238. mkdir -p ${binpath%/*}
  239. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -c $configpath"
  240. config_get httpport $CONFIGURATION httpport 3000
  241. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -p $httpport"
  242. config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
  243. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -w $workdir"
  244. mkdir -p $workdir/data
  245. echo -e "$configpath\n$binpath">/lib/upgrade/keep.d/luci-app-adguardhome
  246. # hack to save config file when upgrade system
  247. config_get keepdb $CONFIGURATION keepdb 0
  248. if [ "$keepdb" -eq 1 ]; then
  249. echo -e "$workdir/data/sessions.db\n$workdir/data/stats.db\n$workdir/data/querylog.json">>/lib/upgrade/keep.d/luci-app-adguardhome
  250. fi
  251. config_get logfile $CONFIGURATION logfile ""
  252. if [ ! -z "$logfile" ]; then
  253. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -l $logfile"
  254. fi
  255. if [ ! -f "$binpath" ]; then
  256. do_redirect 0
  257. touch /var/run/update_core
  258. sh /usr/share/AdGuardHome/update_core.sh >/tmp/AdGuardHome_update.log
  259. rm /var/run/update_core
  260. exit 0
  261. fi
  262. config_get_bool verbose $CONFIGURATION verbose 0
  263. if [ "$verbose" -eq 1 ]; then
  264. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -v"
  265. fi
  266. # for overlay data-stk-oo not suppport
  267. local cwdfs=$(get_filesystem $workdir)
  268. echo "workdir is a $cwdfs filesystem"
  269. if [ "$cwdfs" == "jffs2" ]; then
  270. echo "fs error ln db to tmp $workdir $cwdfs"
  271. logger "AdGuardHome" "warning db redirect to tmp"
  272. touch $workdir/data/stats.db
  273. if [ ! -L $workdir/data/stats.db ]; then
  274. mv -f $workdir/data/stats.db /tmp/stats.db
  275. ln -s /tmp/stats.db $workdir/data/stats.db
  276. fi
  277. touch $workdir/data/sessions.db
  278. if [ ! -L $workdir/data/sessions.db ]; then
  279. mv -f $workdir/data/sessions.db /tmp/sessions.db
  280. ln -s /tmp/sessions.db $workdir/data/sessions.db
  281. fi
  282. fi
  283. procd_open_instance
  284. get_tz
  285. if [ ! -z "$SET_TZ" ]; then
  286. procd_set_param env TZ="$SET_TZ"
  287. fi
  288. procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
  289. procd_set_param limits core="unlimited"
  290. procd_set_param stderr 1
  291. # pass config to script on start
  292. procd_set_param command $binpath $ADDITIONAL_ARGS
  293. procd_set_param file "$configpath"
  294. procd_set_param file "/etc/config/AdGuardHome"
  295. procd_close_instance
  296. if [ -f "$configpath" ]; then
  297. do_redirect 1
  298. else
  299. do_redirect 0
  300. config_get "redirect" "AdGuardHome" "redirect" "none"
  301. if [ "$redirect" != "none" ]; then
  302. procd_open_instance "waitconfig"
  303. procd_set_param command "/usr/share/AdGuardHome/watchconfig.sh"
  304. procd_close_instance
  305. fi
  306. fi
  307. echo "AdGuardHome turn on"
  308. fi
  309. }
  310. reload_service()
  311. {
  312. config_load "${CONFIGURATION}"
  313. echo "AdGuardHome reloading"
  314. config_get_bool enabled $CONFIGURATION enabled 0
  315. procd_send_signal $CONFIGURATION
  316. start
  317. echo "enabled=$enabled"
  318. }
  319. stop_service()
  320. {
  321. config_load "${CONFIGURATION}"
  322. do_redirect 0
  323. procd_send_signal $CONFIGURATION
  324. echo "AdGuardHome turn off"
  325. echo "enabled=$enabled"
  326. }