AdGuardHome 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=95
  4. STOP=01
  5. CONFIGURATION=AdGuardHome
  6. CRON_FILE=/etc/crontabs/root
  7. EXTRA_COMMANDS="do_redirect testbackup test_crontab force_reload isrunning"
  8. EXTRA_HELP=" do_redirect 0 or 1\
  9. testbackup backup or restore\
  10. test_crontab
  11. force_reload
  12. isrunning"
  13. set_forward_dnsmasq()
  14. {
  15. local PORT="$1"
  16. addr="127.0.0.1#$PORT"
  17. OLD_SERVER="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
  18. echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1
  19. if [ $? -eq 0 ]; then
  20. return
  21. fi
  22. uci delete dhcp.@dnsmasq[0].server 2>/dev/null
  23. uci add_list dhcp.@dnsmasq[0].server=$addr
  24. for server in $OLD_SERVER; do
  25. if [ "$server" = "$addr" ]; then
  26. continue
  27. fi
  28. uci add_list dhcp.@dnsmasq[0].server=$server
  29. done
  30. uci delete dhcp.@dnsmasq[0].resolvfile 2>/dev/null
  31. uci set dhcp.@dnsmasq[0].noresolv=1
  32. }
  33. stop_forward_dnsmasq()
  34. {
  35. local OLD_PORT="$1"
  36. addr="127.0.0.1#$OLD_PORT"
  37. OLD_SERVER="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
  38. echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1
  39. if [ $? -ne 0 ]; then
  40. return
  41. fi
  42. uci del_list dhcp.@dnsmasq[0].server=$addr 2>/dev/null
  43. addrlist="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
  44. if [ -z "$addrlist" ] ; then
  45. uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 2>/dev/null
  46. uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null
  47. fi
  48. }
  49. set_iptable()
  50. {
  51. local ipv6_server=$1
  52. local tcp_server=$2
  53. uci -q batch <<-EOF >/dev/null 2>&1
  54. delete firewall.AdGuardHome
  55. set firewall.AdGuardHome=include
  56. set firewall.AdGuardHome.type=script
  57. set firewall.AdGuardHome.path=/usr/share/AdGuardHome/firewall.start
  58. set firewall.AdGuardHome.reload=1
  59. commit firewall
  60. EOF
  61. IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`"
  62. for IP in $IPS
  63. do
  64. if [ "$tcp_server" == "1" ]; then
  65. iptables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  66. fi
  67. iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  68. done
  69. if [ "$ipv6_server" == 0 ]; then
  70. return
  71. fi
  72. IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
  73. for IP in $IPS
  74. do
  75. if [ "$tcp_server" == "1" ]; then
  76. ip6tables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  77. fi
  78. ip6tables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
  79. done
  80. }
  81. clear_iptable()
  82. {
  83. uci -q batch <<-EOF >/dev/null 2>&1
  84. delete firewall.AdGuardHome
  85. commit firewall
  86. EOF
  87. local OLD_PORT="$1"
  88. local ipv6_server=$2
  89. IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`"
  90. for IP in $IPS
  91. do
  92. iptables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  93. iptables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  94. done
  95. if [ "$ipv6_server" == 0 ]; then
  96. return
  97. fi
  98. echo "warn ip6tables nat mod is needed"
  99. IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
  100. for IP in $IPS
  101. do
  102. ip6tables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  103. ip6tables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
  104. done
  105. }
  106. service_triggers() {
  107. procd_add_reload_trigger "$CONFIGURATION"
  108. [ "$(uci get AdGuardHome.AdGuardHome.redirect)" == "redirect" ] && procd_add_reload_trigger firewall
  109. }
  110. isrunning(){
  111. config_load "${CONFIGURATION}"
  112. _isrunning
  113. local r=$?
  114. ([ "$r" == "0" ] && echo "running") || ([ "$r" == "1" ] && echo "not run" ) || echo "no bin"
  115. return $r
  116. }
  117. _isrunning(){
  118. config_get binpath $CONFIGURATION binpath "/usr/bin/AdGuardHome/AdGuardHome"
  119. [ ! -f "$binpath" ] && return 2
  120. pgrep $binpath 2>&1 >/dev/null && return 0
  121. return 1
  122. }
  123. force_reload(){
  124. config_load "${CONFIGURATION}"
  125. _isrunning && procd_send_signal "$CONFIGURATION" || start
  126. }
  127. get_tz()
  128. {
  129. SET_TZ=""
  130. if [ -e "/etc/localtime" ]; then
  131. return
  132. fi
  133. for tzfile in /etc/TZ /var/etc/TZ
  134. do
  135. if [ ! -e "$tzfile" ]; then
  136. continue
  137. fi
  138. tz="`cat $tzfile 2>/dev/null`"
  139. done
  140. if [ -z "$tz" ]; then
  141. return
  142. fi
  143. SET_TZ=$tz
  144. }
  145. rm_port53()
  146. {
  147. local AdGuardHome_PORT=$(config_editor "dns.port" "" "$configpath" "1")
  148. dnsmasq_port=$(uci get dhcp.@dnsmasq[0].port 2>/dev/null)
  149. if [ -z "$dnsmasq_port" ]; then
  150. dnsmasq_port="53"
  151. fi
  152. if [ "$dnsmasq_port" == "$AdGuardHome_PORT" ]; then
  153. if [ "$dnsmasq_port" == "53" ]; then
  154. dnsmasq_port="1745"
  155. fi
  156. elif [ "$dnsmasq_port" == "53" ]; then
  157. return
  158. fi
  159. config_editor "dns.port" "$dnsmasq_port" "$configpath"
  160. uci set dhcp.@dnsmasq[0].port="53"
  161. }
  162. use_port53()
  163. {
  164. local AdGuardHome_PORT=$(config_editor "dns.port" "" "$configpath" "1")
  165. dnsmasq_port=$(uci get dhcp.@dnsmasq[0].port 2>/dev/null)
  166. if [ -z "$dnsmasq_port" ]; then
  167. dnsmasq_port="53"
  168. fi
  169. if [ "$dnsmasq_port" == "$AdGuardHome_PORT" ]; then
  170. if [ "$dnsmasq_port" == "53" ]; then
  171. AdGuardHome_PORT="1745"
  172. fi
  173. elif [ "$AdGuardHome_PORT" == "53" ]; then
  174. return
  175. fi
  176. config_editor "dns.port" "53" "$configpath"
  177. uci set dhcp.@dnsmasq[0].port="$AdGuardHome_PORT"
  178. }
  179. do_redirect()
  180. {
  181. config_load "${CONFIGURATION}"
  182. _do_redirect $1
  183. }
  184. _do_redirect()
  185. {
  186. local section="$CONFIGURATION"
  187. args=""
  188. ipv6_server=1
  189. tcp_server=0
  190. enabled=$1
  191. if [ "$enabled" == "1" ]; then
  192. echo -n "1">/var/run/AdGredir
  193. else
  194. echo -n "0">/var/run/AdGredir
  195. fi
  196. config_get configpath $CONFIGURATION configpath "/etc/AdGuardHome.yaml"
  197. AdGuardHome_PORT=$(config_editor "dns.port" "" "$configpath" "1")
  198. if [ -z "$AdGuardHome_PORT" ]; then
  199. AdGuardHome_PORT="0"
  200. fi
  201. config_get "redirect" "$section" "redirect" "none"
  202. config_get "old_redirect" "$section" "old_redirect" "none"
  203. config_get "old_port" "$section" "old_port" "0"
  204. config_get "old_enabled" "$section" "old_enabled" "0"
  205. uci get dhcp.@dnsmasq[0].port >/dev/null 2>&1 || uci set dhcp.@dnsmasq[0].port="53" >/dev/null 2>&1
  206. if [ "$old_enabled" = "1" -a "$old_redirect" == "exchange" ]; then
  207. AdGuardHome_PORT=$(uci get dhcp.@dnsmasq[0].port 2>/dev/null)
  208. fi
  209. if [ "$old_redirect" != "$redirect" ] || [ "$old_port" != "$AdGuardHome_PORT" ] || [ "$old_enabled" = "1" -a "$enabled" = "0" ]; then
  210. if [ "$old_redirect" != "none" ]; then
  211. if [ "$old_redirect" == "redirect" -a "$old_port" != "0" ]; then
  212. clear_iptable "$old_port" "$ipv6_server"
  213. elif [ "$old_redirect" == "dnsmasq-upstream" ]; then
  214. stop_forward_dnsmasq "$old_port"
  215. elif [ "$old_redirect" == "exchange" ]; then
  216. rm_port53
  217. fi
  218. fi
  219. elif [ "$old_enabled" = "1" -a "$enabled" = "1" ]; then
  220. if [ "$old_redirect" == "redirect" -a "$old_port" != "0" ]; then
  221. clear_iptable "$old_port" "$ipv6_server"
  222. fi
  223. fi
  224. uci delete AdGuardHome.@AdGuardHome[0].old_redirect 2>/dev/null
  225. uci delete AdGuardHome.@AdGuardHome[0].old_port 2>/dev/null
  226. uci delete AdGuardHome.@AdGuardHome[0].old_enabled 2>/dev/null
  227. uci add_list AdGuardHome.@AdGuardHome[0].old_redirect="$redirect" 2>/dev/null
  228. uci add_list AdGuardHome.@AdGuardHome[0].old_port="$AdGuardHome_PORT" 2>/dev/null
  229. uci add_list AdGuardHome.@AdGuardHome[0].old_enabled="$enabled" 2>/dev/null
  230. uci commit AdGuardHome
  231. if [ "$enabled" != "0" ] && [ "$AdGuardHome_PORT" != "0" ] ; then
  232. if [ "$redirect" = "redirect" ]; then
  233. set_iptable $ipv6_server $tcp_server
  234. elif [ "$redirect" = "dnsmasq-upstream" ]; then
  235. set_forward_dnsmasq "$AdGuardHome_PORT"
  236. elif [ "$redirect" == "exchange" -a "$(uci get dhcp.@dnsmasq[0].port 2>/dev/null)" == "53" ]; then
  237. use_port53
  238. fi
  239. fi
  240. [ -n $(uci changes dhcp) ] && uci commit dhcp && /etc/init.d/dnsmasq restart
  241. }
  242. get_filesystem()
  243. {
  244. # print out path filesystem
  245. echo $1 | awk '
  246. BEGIN{
  247. while (("mount"| getline ret) > 0)
  248. {
  249. split(ret,d);
  250. fs[d[3]]=d[5];
  251. m=index(d[1],":")
  252. if (m==0)
  253. {
  254. pt[d[3]]=d[1]
  255. }else{
  256. pt[d[3]]=substr(d[1],m+1)
  257. }}}{
  258. split($0,d,"/");
  259. if ("/" in fs)
  260. {
  261. result1=fs["/"];
  262. }
  263. if ("/" in pt)
  264. {
  265. result2=pt["/"];
  266. }
  267. for (i=2;i<=length(d);i++)
  268. {
  269. p[i]=p[i-1]"/"d[i];
  270. if (p[i] in fs)
  271. {
  272. result1=fs[p[i]];
  273. result2=pt[p[i]];
  274. }
  275. }
  276. if (result2 in fs){
  277. result=fs[result2]}
  278. else{
  279. result=result1}
  280. print(result);}'
  281. }
  282. config_editor()
  283. {
  284. awk -v yaml="$1" -v value="$2" -v file="$3" -v ro="$4" '
  285. BEGIN{split(yaml,part,"\.");s="";i=1;l=length(part);}
  286. {
  287. if (match($0,s""part[i]":"))
  288. {
  289. if (i==l)
  290. {
  291. split($0,t,": ");
  292. if (ro==""){
  293. system("sed -i '\''"FNR"c \\"t[1]": "value"'\'' "file);
  294. }else{
  295. print(t[2]);
  296. }
  297. exit;
  298. }
  299. s=s"[- ]{2}";
  300. i++;
  301. }
  302. }' $3
  303. }
  304. boot_service() {
  305. rm /var/run/AdGserverdis >/dev/null 2>&1
  306. config_load "${CONFIGURATION}"
  307. config_get waitonboot $CONFIGURATION waitonboot "0"
  308. config_get_bool enabled $CONFIGURATION enabled 0
  309. config_get binpath $CONFIGURATION binpath "/usr/bin/AdGuardHome/AdGuardHome"
  310. [ -f "$binpath" ] && start_service
  311. if [ "$enabled" == "1" ] && [ "$waitonboot" == "1" ]; then
  312. procd_open_instance "waitnet"
  313. procd_set_param command "/usr/share/AdGuardHome/waitnet.sh"
  314. procd_close_instance
  315. echo "no net start pinging"
  316. fi
  317. }
  318. testbackup(){
  319. config_load "${CONFIGURATION}"
  320. if [ "$1" == "backup" ]; then
  321. backup
  322. elif [ "$1" == "restore" ]; then
  323. restore
  324. fi
  325. }
  326. restore()
  327. {
  328. config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
  329. config_get backupwdpath $CONFIGURATION backupwdpath "/usr/bin/AdGuardHome"
  330. cp -u -r -f $backupwdpath/data $workdir
  331. }
  332. backup() {
  333. config_get backupwdpath $CONFIGURATION backupwdpath "/usr/bin/AdGuardHome"
  334. mkdir -p $backupwdpath/data
  335. config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
  336. config_get backupfile $CONFIGURATION backupfile ""
  337. for one in $backupfile;
  338. do
  339. while :
  340. do
  341. if [ -d "$backupwdpath/data/$one" ]; then
  342. cpret=$(cp -u -r -f $workdir/data/$one $backupwdpath/data 2>&1)
  343. else
  344. cpret=$(cp -u -r -f $workdir/data/$one $backupwdpath/data/$one 2>&1)
  345. fi
  346. echo "$cpret"
  347. echo "$cpret" | grep "no space left on device"
  348. if [ "$?" == "0" ]; then
  349. echo "磁盘已满,删除log重试中"
  350. del_querylog && continue
  351. rm -f -r $backupwdpath/data/filters
  352. rm -f -r $workdir/data/filters && continue
  353. echo "backup failed"
  354. fi
  355. break
  356. done
  357. done
  358. }
  359. start_service() {
  360. # Reading config
  361. rm /var/run/AdGserverdis >/dev/null 2>&1
  362. config_load "${CONFIGURATION}"
  363. # update password
  364. config_get hashpass $CONFIGURATION hashpass ""
  365. config_get configpath $CONFIGURATION configpath "/etc/AdGuardHome.yaml"
  366. if [ -n "$hashpass" ]; then
  367. config_editor "users.password" "$hashpass" "$configpath"
  368. uci set $CONFIGURATION.$CONFIGURATION.hashpass=""
  369. fi
  370. local enabled
  371. config_get_bool enabled $CONFIGURATION enabled 0
  372. # update crontab
  373. do_crontab
  374. if [ "$enabled" == "0" ]; then
  375. _do_redirect 0
  376. return
  377. fi
  378. #what need to do before reload
  379. config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
  380. config_get backupfile $CONFIGURATION backupfile ""
  381. mkdir -p $workdir/data
  382. if [ -n "$backupfile" ] && [ ! -d "$workdir/data" ]; then
  383. restore
  384. fi
  385. # for jffs2 data-stk-oo not support
  386. local cwdfs=$(get_filesystem $workdir)
  387. echo "workdir is a $cwdfs filesystem"
  388. if [ "$cwdfs" == "jffs2" ]; then
  389. echo "fs error ln db to tmp $workdir $cwdfs"
  390. logger "AdGuardHome" "warning db redirect to tmp"
  391. touch $workdir/data/stats.db
  392. if [ ! -L $workdir/data/stats.db ]; then
  393. mv -f $workdir/data/stats.db /tmp/stats.db 2>/dev/null
  394. ln -s /tmp/stats.db $workdir/data/stats.db 2>/dev/null
  395. fi
  396. touch $workdir/data/sessions.db
  397. if [ ! -L $workdir/data/sessions.db ]; then
  398. mv -f $workdir/data/sessions.db /tmp/sessions.db 2>/dev/null
  399. ln -s /tmp/sessions.db $workdir/data/sessions.db 2>/dev/null
  400. fi
  401. fi
  402. local ADDITIONAL_ARGS=""
  403. config_get binpath $CONFIGURATION binpath "/usr/bin/AdGuardHome/AdGuardHome"
  404. mkdir -p ${binpath%/*}
  405. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -c $configpath"
  406. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -w $workdir"
  407. config_get httpport $CONFIGURATION httpport 3000
  408. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -p $httpport"
  409. # hack to save config file when upgrade system
  410. config_get upprotect $CONFIGURATION upprotect ""
  411. eval upprotect=${upprotect// /\\\\n}
  412. echo -e "$upprotect">/lib/upgrade/keep.d/luci-app-adguardhome
  413. config_get logfile $CONFIGURATION logfile ""
  414. if [ -n "$logfile" ]; then
  415. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -l $logfile"
  416. fi
  417. if [ ! -f "$binpath" ]; then
  418. _do_redirect 0
  419. /usr/share/AdGuardHome/update_core.sh 2>&1 >/tmp/AdGuardHome_update.log &
  420. exit 0
  421. fi
  422. config_get_bool verbose $CONFIGURATION verbose 0
  423. if [ "$verbose" -eq 1 ]; then
  424. ADDITIONAL_ARGS="$ADDITIONAL_ARGS -v"
  425. fi
  426. procd_open_instance
  427. get_tz
  428. if [ -n "$SET_TZ" ]; then
  429. procd_set_param env TZ="$SET_TZ"
  430. fi
  431. procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
  432. procd_set_param limits core="unlimited" nofile="65535 65535"
  433. procd_set_param stderr 1
  434. procd_set_param command $binpath $ADDITIONAL_ARGS
  435. procd_set_param file "$configpath" "/etc/hosts" "/etc/config/AdGuardHome"
  436. procd_close_instance
  437. if [ -f "$configpath" ]; then
  438. _do_redirect 1
  439. else
  440. _do_redirect 0
  441. config_get "redirect" "AdGuardHome" "redirect" "none"
  442. if [ "$redirect" != "none" ]; then
  443. procd_open_instance "waitconfig"
  444. procd_set_param command "/usr/share/AdGuardHome/watchconfig.sh"
  445. procd_close_instance
  446. echo "no config start watching"
  447. fi
  448. fi
  449. echo "AdGuardHome service enabled"
  450. echo "luci enable switch=$enabled"
  451. (sleep 10 && [ -z "$(pgrep $binpath)" ] && logger "AdGuardHome" "no process in 10s cancel redirect" && _do_redirect 0 )&
  452. }
  453. reload_service()
  454. {
  455. rm /var/run/AdGlucitest >/dev/null 2>&1
  456. echo "AdGuardHome reloading"
  457. start
  458. }
  459. del_querylog(){
  460. local btarget=$(ls $backupwdpath/data | grep -F "querylog.json" | sort -r | head -n 1)
  461. local wtarget=$(ls $workdir/data | grep -F "querylog.json" | sort -r | head -n 1)
  462. if [ "$btarget"x == "$wtarget"x ]; then
  463. [ -z "$btarget" ] && return 1
  464. rm -f $workdir/data/$wtarget
  465. rm -f $backupwdpath/data/$btarget
  466. return 0
  467. fi
  468. if [ "$btarget" \> "$wtarget" ]; then
  469. rm -f $backupwdpath/data/$btarget
  470. return 0
  471. else
  472. rm -f $workdir/data/$wtarget
  473. return 0
  474. fi
  475. }
  476. stop_service()
  477. {
  478. config_load "${CONFIGURATION}"
  479. _do_redirect 0
  480. do_crontab
  481. if [ "$1" != "nobackup" ]; then
  482. config_get backupfile $CONFIGURATION backupfile "0"
  483. if [ -n "$backupfile" ]; then
  484. backup
  485. fi
  486. fi
  487. echo "AdGuardHome service disabled"
  488. touch /var/run/AdGserverdis
  489. }
  490. boot() {
  491. rc_procd boot_service "$@"
  492. if eval "type service_started" 2>/dev/null >/dev/null; then
  493. service_started
  494. fi
  495. }
  496. test_crontab(){
  497. config_load "${CONFIGURATION}"
  498. do_crontab
  499. }
  500. do_crontab(){
  501. config_get_bool enabled $CONFIGURATION enabled 0
  502. config_get crontab $CONFIGURATION crontab ""
  503. local findstr default cronenable replace commit
  504. local cronreload=0
  505. local commit=0
  506. findstr="/usr/share/AdGuardHome/update_core.sh"
  507. default="30 3 * * * /usr/share/AdGuardHome/update_core.sh 2>&1"
  508. [ "$enabled" == "0" ] || [ "${crontab//autoupdate/}" == "$crontab" ] && cronenable=0 || cronenable=1
  509. crontab_editor
  510. config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
  511. config_get lastworkdir $CONFIGURATION lastworkdir "/usr/bin/AdGuardHome"
  512. findstr="/usr/share/AdGuardHome/tailto.sh [0-9]* \$(uci get AdGuardHome.AdGuardHome.workdir)/data/querylog.json"
  513. default="0 * * * * /usr/share/AdGuardHome/tailto.sh 2000 \$(uci get AdGuardHome.AdGuardHome.workdir)/data/querylog.json"
  514. [ "$enabled" == "0" ] || [ "${crontab//cutquerylog/}" == "$crontab" ] && cronenable=0 || cronenable=1
  515. crontab_editor
  516. config_get logfile $CONFIGURATION logfile ""
  517. config_get lastlogfile $CONFIGURATION lastlogfile ""
  518. findstr="/usr/share/AdGuardHome/tailto.sh [0-9]* \$(uci get AdGuardHome.AdGuardHome.logfile)"
  519. default="30 3 * * * /usr/share/AdGuardHome/tailto.sh 2000 \$(uci get AdGuardHome.AdGuardHome.logfile)"
  520. [ "$logfile" == "syslog" ] || [ "$logfile" == "" ] || [ "$enabled" == "0" ] || [ "${crontab//cutruntimelog/}" == "$crontab" ] && cronenable=0 || cronenable=1
  521. crontab_editor
  522. findstr="/usr/share/AdGuardHome/addhost.sh"
  523. default="0 * * * * /usr/share/AdGuardHome/addhost.sh"
  524. [ "$enabled" == "0" ] || [ "${crontab//autohost/}" == "$crontab" ] && cronenable=0 || cronenable=1
  525. crontab_editor
  526. [ "$cronenable" == "0" ] && /usr/share/AdGuardHome/addhost.sh "del" "noreload" || /usr/share/AdGuardHome/addhost.sh "" "noreload"
  527. findstr="/usr/share/AdGuardHome/gfw2adg.sh"
  528. default="30 3 * * * /usr/share/AdGuardHome/gfw2adg.sh"
  529. [ "$enabled" == "0" ] || [ "${crontab//autogfw/}" == "$crontab" ] && cronenable=0 || cronenable=1
  530. crontab_editor
  531. [ "$cronreload" -gt 0 ] && /etc/init.d/cron restart
  532. }
  533. crontab_editor(){
  534. #usage input:
  535. #findstr=
  536. #default=
  537. #cronenable=
  538. #replace="${last//\//\\/}/${now//\//\\/}"
  539. #output:cronreload:if >1 please /etc/init.d/cron restart manual
  540. local testline reload
  541. local line="$(grep "$findstr" $CRON_FILE)"
  542. [ -n "$replace" ] && [ -n "$line" ] && eval testline="\${line//$replace}" && [ "$testline" != "$line" ] && line="$testline" && reload="1" && replace=""
  543. if [ "${line:0:1}" != "#" ]; then
  544. if [ $cronenable -eq 1 ]; then
  545. [ -z "$line" ] && line="$default" && reload="1"
  546. if [ -n "$reload" ]; then
  547. sed -i "\,$findstr,d" $CRON_FILE
  548. echo "$line" >> $CRON_FILE
  549. cronreload=$((cronreload+1))
  550. fi
  551. elif [ -n "$line" ]; then
  552. sed -i "\,$findstr,d" $CRON_FILE
  553. echo "#$line" >> $CRON_FILE
  554. cronreload=$((cronreload+1))
  555. fi
  556. else
  557. if [ $cronenable -eq 1 ]; then
  558. sed -i "\,$findstr,d" $CRON_FILE
  559. echo "${line:1}" >> $CRON_FILE
  560. cronreload=$((cronreload+1))
  561. elif [ -z "$reload" ]; then
  562. sed -i "\,$findstr,d" $CRON_FILE
  563. echo "$line" >> $CRON_FILE
  564. fi
  565. fi
  566. }