123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- #!/bin/sh /etc/rc.common
-
- USE_PROCD=1
-
- START=95
- STOP=01
- CONFIGURATION=AdGuardHome
- EXTRA_COMMANDS="do_redirect"
- EXTRA_HELP=" do_redirect 0 or 1"
- set_forward_dnsmasq()
- {
- local PORT="$1"
- addr="127.0.0.1#$PORT"
- OLD_SERVER="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
- echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1
- if [ $? -eq 0 ]; then
- return
- fi
- uci delete dhcp.@dnsmasq[0].server 2>/dev/null
- uci add_list dhcp.@dnsmasq[0].server=$addr
- for server in $OLD_SERVER; do
- if [ "$server" = "$addr" ]; then
- continue
- fi
- uci add_list dhcp.@dnsmasq[0].server=$server
- done
- uci delete dhcp.@dnsmasq[0].resolvfile 2>/dev/null
- uci set dhcp.@dnsmasq[0].noresolv=1
- uci commit dhcp
- /etc/init.d/dnsmasq restart
- }
- stop_forward_dnsmasq()
- {
- local OLD_PORT="$1"
- addr="127.0.0.1#$OLD_PORT"
- OLD_SERVER="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
- echo $OLD_SERVER | grep "^$addr" >/dev/null 2>&1
- if [ $? -ne 0 ]; then
- return
- fi
- uci del_list dhcp.@dnsmasq[0].server=$addr 2>/dev/null
- addrlist="`uci get dhcp.@dnsmasq[0].server 2>/dev/null`"
- if [ -z "$addrlist" ] ; then
- uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto 2>/dev/null
- uci delete dhcp.@dnsmasq[0].noresolv 2>/dev/null
- fi
- uci commit dhcp
- /etc/init.d/dnsmasq restart
- }
- set_iptable()
- {
- local ipv6_server=$1
- local tcp_server=$2
- IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`"
- for IP in $IPS
- do
- if [ "$tcp_server" == "1" ]; then
- iptables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
- fi
- iptables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
- done
- if [ "$ipv6_server" == 0 ]; then
- return
- fi
- IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
- for IP in $IPS
- do
- if [ "$tcp_server" == "1" ]; then
- ip6tables -t nat -A PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
- fi
- ip6tables -t nat -A PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $AdGuardHome_PORT >/dev/null 2>&1
- done
- }
- clear_iptable()
- {
- local OLD_PORT="$1"
- local ipv6_server=$2
- IPS="`ifconfig | grep "inet addr" | grep -v ":127" | grep "Bcast" | awk '{print $2}' | awk -F : '{print $2}'`"
- for IP in $IPS
- do
- iptables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
- iptables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
- done
- if [ "$ipv6_server" == 0 ]; then
- return
- fi
- IPS="`ifconfig | grep "inet6 addr" | grep -v " fe80::" | grep -v " ::1" | grep "Global" | awk '{print $3}'`"
- for IP in $IPS
- do
- ip6tables -t nat -D PREROUTING -p udp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
- ip6tables -t nat -D PREROUTING -p tcp -d $IP --dport 53 -j REDIRECT --to-ports $OLD_PORT >/dev/null 2>&1
- done
-
- }
- service_triggers() {
- procd_add_reload_trigger firewall
- procd_add_reload_trigger "$CONFIGURATION"
- }
- get_tz()
- {
- SET_TZ=""
- if [ -e "/etc/localtime" ]; then
- return
- fi
-
- for tzfile in /etc/TZ /var/etc/TZ
- do
- if [ ! -e "$tzfile" ]; then
- continue
- fi
-
- tz="`cat $tzfile 2>/dev/null`"
- done
-
- if [ -z "$tz" ]; then
- return
- fi
-
- SET_TZ=$tz
- }
- do_redirect()
- {
- config_load "${CONFIGURATION}"
- local section="$CONFIGURATION"
- args=""
- ipv6_server=0
- tcp_server=0
- enabled=$1
- config_get configpath $CONFIGURATION configpath "/etc/AdGuardHome.yaml"
- AdGuardHome_PORT=$(awk '/ port:/{printf($2)}' $configpath)
- if [ -z "$AdGuardHome_PORT" ]; then
- AdGuardHome_PORT="0"
- fi
- config_get "redirect" "$section" "redirect" "none"
- config_get "old_redirect" "$section" "old_redirect" "none"
- config_get "old_port" "$section" "old_port" "0"
- config_get "old_enabled" "$section" "old_enabled" "0"
- if [ "$old_redirect" != "$redirect" ] || [ "$old_port" != "$AdGuardHome_PORT" ] || [ "$old_enabled" = "1" -a "$enabled" = "0" ]; then
- if [ "$old_redirect" != "none" ]; then
- if [ "$old_port" != "0" ]; then
- clear_iptable "$old_port" "$ipv6_server"
- fi
- if [ "$old_redirect" == "dnsmasq-upstream" ]; then
- stop_forward_dnsmasq "$old_port"
- fi
- fi
- fi
-
- uci delete AdGuardHome.@AdGuardHome[0].old_redirect 2>/dev/null
- uci delete AdGuardHome.@AdGuardHome[0].old_port 2>/dev/null
- uci delete AdGuardHome.@AdGuardHome[0].old_enabled 2>/dev/null
- uci add_list AdGuardHome.@AdGuardHome[0].old_redirect="$redirect" 2>/dev/null
- uci add_list AdGuardHome.@AdGuardHome[0].old_port="$AdGuardHome_PORT" 2>/dev/null
- uci add_list AdGuardHome.@AdGuardHome[0].old_enabled="$enabled" 2>/dev/null
- uci commit AdGuardHome
- [ "$enabled" -gt 0 ] || return 1
- if [ "$AdGuardHome_PORT" == "0" ]; then
- return 1
- fi
- if [ "$redirect" = "redirect" ]; then
- set_iptable $ipv6_server $tcp_server
- elif [ "$redirect" = "dnsmasq-upstream" ]; then
- set_forward_dnsmasq "$AdGuardHome_PORT"
- fi
- #procd_open_instance "AdGuardHome"
- #get_tz
- #if [ ! -z "$SET_TZ" ]; then
- # procd_set_param env TZ="$SET_TZ"
- #fi
- #procd_set_param file "$AdGuardHome_CONF"
- #procd_close_instance
- }
- get_filesystem()
- {
- # print out path filesystem
- echo $1 | awk '
- BEGIN{
- while (("mount"| getline ret) > 0)
- {
- split(ret,d);
- fs[d[3]]=d[5];
- m=index(d[1],":")
- if (m==0)
- {
- pt[d[3]]=d[1]
- }else{
- pt[d[3]]=substr(d[1],m+1)
- }}}{
- split($0,d,"/");
- if ("/" in fs)
- {
- result1=fs["/"];
- }
- if ("/" in pt)
- {
- result2=pt["/"];
- }
- for (i=2;i<=length(d);i++)
- {
- p[i]=p[i-1]"/"d[i];
- if (p[i] in fs)
- {
- result1=fs[p[i]];
- result2=pt[p[i]];
- }
- }
- if (result2 in fs){
- result=fs[result2]}
- else{
- result=result1}
- print(result);}'
- }
- config_editor()
- {
- awk -v yaml="$1" -v value="$2" -v file="$3" '
- BEGIN{split(yaml,part,"\.");s="";i=1;l=length(part);}
- {
- if (match($0,s""part[i]))
- {
- if (i==l)
- {
- split($0,t,":");
- system("sed -i '\''"FNR"c \\"t[1]": "value"'\'' "file);
- exit;
- }
- s=s"[- ]{2}";
- i++;
- }
- }' $3
- }
- start_service() {
- # Reading config
- config_load "${CONFIGURATION}"
- config_get hashpass $CONFIGURATION hashpass ""
- if [ ! -z "$hashpass" ]; then
- config_editor "users.password" "$hashpass" "$configpath"
- uci set $CONFIGURATION.$CONFIGURATION.hashpass=""
- fi
- local enabled
- config_get_bool enabled $CONFIGURATION enabled 0
-
- if [ "$enabled" == "1" ]; then
- local ADDITIONAL_ARGS=""
- config_get configpath $CONFIGURATION configpath "/etc/AdGuardHome.yaml"
- config_get binpath $CONFIGURATION binpath "/usr/bin/AdGuardHome/AdGuardHome"
-
- echo -e "$configpath\n$binpath">/lib/upgrade/keep.d/luci-app-adguardhome
- mkdir -p ${binpath%/*}
- ADDITIONAL_ARGS="$ADDITIONAL_ARGS -c $configpath"
-
- config_get httpport $CONFIGURATION httpport 3000
- ADDITIONAL_ARGS="$ADDITIONAL_ARGS -p $httpport"
-
- config_get workdir $CONFIGURATION workdir "/usr/bin/AdGuardHome"
- ADDITIONAL_ARGS="$ADDITIONAL_ARGS -w $workdir"
- mkdir -p $workdir/data
- echo -e "$configpath\n$binpath">/lib/upgrade/keep.d/luci-app-adguardhome
- # hack to save config file when upgrade system
- config_get keepdb $CONFIGURATION keepdb 0
- if [ "$keepdb" -eq 1 ]; then
- echo -e "$workdir/data/sessions.db\n$workdir/data/stats.db\n$workdir/data/querylog.json">>/lib/upgrade/keep.d/luci-app-adguardhome
- fi
- config_get logfile $CONFIGURATION logfile ""
- if [ ! -z "$logfile" ]; then
- ADDITIONAL_ARGS="$ADDITIONAL_ARGS -l $logfile"
- fi
-
- if [ ! -f "$binpath" ]; then
- do_redirect 0
- touch /var/run/update_core
- sh /usr/share/AdGuardHome/update_core.sh >/tmp/AdGuardHome_update.log
- rm /var/run/update_core
- exit 0
- fi
- config_get_bool verbose $CONFIGURATION verbose 0
- if [ "$verbose" -eq 1 ]; then
- ADDITIONAL_ARGS="$ADDITIONAL_ARGS -v"
- fi
-
- # for overlay data-stk-oo not suppport
- local cwdfs=$(get_filesystem $workdir)
- echo "workdir is a $cwdfs filesystem"
- if [ "$cwdfs" == "jffs2" ]; then
- echo "fs error ln db to tmp $workdir $cwdfs"
- logger "AdGuardHome" "warning db redirect to tmp"
- touch $workdir/data/stats.db
- if [ ! -L $workdir/data/stats.db ]; then
- mv -f $workdir/data/stats.db /tmp/stats.db
- ln -s /tmp/stats.db $workdir/data/stats.db
- fi
- touch $workdir/data/sessions.db
- if [ ! -L $workdir/data/sessions.db ]; then
- mv -f $workdir/data/sessions.db /tmp/sessions.db
- ln -s /tmp/sessions.db $workdir/data/sessions.db
- fi
- fi
- procd_open_instance
- get_tz
- if [ ! -z "$SET_TZ" ]; then
- procd_set_param env TZ="$SET_TZ"
- fi
- procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
- procd_set_param limits core="unlimited"
- procd_set_param stderr 1
- # pass config to script on start
- procd_set_param command $binpath $ADDITIONAL_ARGS
- procd_set_param file "$configpath"
- procd_set_param file "/etc/config/AdGuardHome"
- procd_close_instance
- if [ -f "$configpath" ]; then
- do_redirect 1
- else
- do_redirect 0
- config_get "redirect" "AdGuardHome" "redirect" "none"
- if [ "$redirect" != "none" ]; then
- procd_open_instance "waitconfig"
- procd_set_param command "/usr/share/AdGuardHome/watchconfig.sh"
- procd_close_instance
- fi
- fi
- echo "AdGuardHome turn on"
- fi
- }
- reload_service()
- {
- config_load "${CONFIGURATION}"
- echo "AdGuardHome reloading"
- config_get_bool enabled $CONFIGURATION enabled 0
- procd_send_signal $CONFIGURATION
- start
- echo "enabled=$enabled"
- }
- stop_service()
- {
- config_load "${CONFIGURATION}"
- do_redirect 0
- procd_send_signal $CONFIGURATION
- echo "AdGuardHome turn off"
- echo "enabled=$enabled"
- }
|