summaryrefslogtreecommitdiffstats
path: root/net/adblock/files/adblock.init
blob: 5cd8672111b401d31714336907477908aeb8d082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/sh /etc/rc.common
#

START=90
EXTRA_COMMANDS="toggle stats cfgup envchk query"
EXTRA_HELP="	toggle	Toggle adblock 'on' or 'off'
	stats	Update adblock statistics
	cfgup	Update adblock configuration file
	envchk	Check/Set adblock environment
	query	<DOMAIN> Query active blocklists for specific domain"

adb_debug=0
adb_pid="${$}"
adb_script="/usr/bin/adblock-update.sh"
adb_helper="/usr/bin/adblock-helper.sh"
adb_pidfile="/var/run/adblock.pid"
bg_parm="&"

if [ $((adb_debug)) -eq 0 ]
then
    exec 2>/dev/null
fi

if [ -r "${adb_pidfile}" ]
then
    logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1
    exit 255
fi

. "${adb_helper}"
f_envload

if [ "${adb_restricted}" = "1" ]
then
    adb_uci="$(which true)"
fi

boot()
{
    return 0
}

start()
{
    if [ -t 1 ]
    then
        unset bg_parm
    fi
    eval "${adb_script}" ${bg_parm}
    return 0
}

restart()
{
    stop
    start
}

reload()
{
    reload="true"
    stop
    start
}

stop()
{
    f_rmdns
    f_rmuhttpd
    config_foreach f_rmconfig source
    if [ -z "${reload}" ]
    then
        f_rmfirewall
    fi
    if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "$(${adb_uci} -q changes adblock)" ]
    then
        "${adb_uci}" -q commit adblock
        f_log "all adblock related services stopped"
    fi
    return 0
}

toggle()
{
    if [ -d "${adb_dnshidedir}" ]
    then
        list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
        list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
        if [ -n "${list_dns}" ]
        then
            source="${adb_dnsdir}/${adb_dnsprefix}"
            target="${adb_dnshidedir}"
            pos="off"
        elif [ -n "${list_dnshide}" ]
        then
            source="${adb_dnshidedir}/${adb_dnsprefix}"
            target="${adb_dnsdir}"
            pos="on"
        fi
        if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ]
        then
            mv -f "${source}"* "${target}"
            /etc/init.d/dnsmasq restart
            "${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}"
            "${adb_uci}" -q commit "adblock"
            f_log "adblock toggle switched '${pos}'"
        fi
    fi
    return 0
}

stats()
{
    f_statistics
    "${adb_uci}" -q commit "adblock"
    return 0
}

cfgup()
{
    stop
    cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock"
    rc=$?
    if [ $((rc)) -eq 0 ]
    then
        f_log "default adblock configuration applied, please check the settings in '/etc/config/adblock'"
    else
        f_log "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'"
    fi
    return 0
}

query()
{
    domain="${1}"
    tld="${domain#*.}"
    list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
    if [ -z "${list_dns}" ]
    then
        f_log "no active blocklists found, please start adblock first"
    elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
    then
        f_log "invalid domain query input, please submit a specific (sub-)domain, i.e. 'www.abc.xyz'"
    else
        while [ "${domain}" != "${tld}" ]
        do
            search="${domain//./\.}"
            result="$(grep -Hm 1 "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk -F ':|/' '{print "   "$4"\t: "$6}')"
            count="$(grep -hc "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk '{sum += $1} END {printf sum}')"
            printf "%s\n" "=> distinct results for domain '${domain}' (overall ${count})"
            if [ -z "${result}" ]
            then
                printf "%s\n" "   no matches in active blocklists"
            else
                printf "%s\n" "${result}"
            fi
            domain="${tld}"
            tld="${domain#*.}"
        done
    fi
    return 0
}

envchk()
{
    adb_loglevel=0
    f_envcheck
    rm -f "${adb_tmpfile}"
    rm -rf "${adb_tmpdir}"
    adb_loglevel=1
    f_log "adblock environment check finished successfully"
    return 0
}