-
Notifications
You must be signed in to change notification settings - Fork 7
/
parental_control.sh
executable file
·216 lines (188 loc) · 4.43 KB
/
parental_control.sh
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
SET_BASE="0"
ADD_RULE="1"
ADD_GROUP="2"
CLEAN_RULE="3"
CLEAN_GROUP="4"
SET_RULE="5"
SET_GROUP="6"
UPDATE_URL=""
UPDATE_TIME=""
UPDATE_EN="0"
str_action_num()
{
case $1 in
"DROP")
echo 0
;;
"ACCEPT")
echo 1
;;
"POLICY_DROP")
echo 2
;;
"POLICY_ACCEPT")
echo 3
;;
esac
}
url_to_app_feature()
{
case "$1" in
\[*\]) echo "$1"|sed -e "s/\[\|\|\]//g"| tr ',' ' '
;;
*) echo "tcp;;;$1;;"
;;
esac
}
config_apply()
{
test -z "$1" && return 1
if [ -e "/dev/parental_control" ];then
#[ "$DEBUG" = "1" ] && echo "config json str=$1"
echo "$1" >/dev/parental_control
fi
}
load_base_config()
{
local drop_anonymous src_dev
config_get drop_anonymous "global" "drop_anonymous" "0"
config_get src_dev "global" "src_dev"
config_get UPDATE_TIME "global" "update_time"
config_get UPDATE_URL "global" "update_url"
config_get UPDATE_EN "global" "auto_update" "0"
json_init
json_add_int "op" $SET_BASE
json_add_object "data"
json_add_int "drop_anonymous" $drop_anonymous
json_add_string "src_dev" "$src_dev"
json_str=`json_dump`
config_apply "$json_str"
json_cleanup
}
load_rule()
{
json_init
json_add_int "op" $ADD_RULE
json_add_object "data"
json_add_array "rules"
load_rule_cb(){
local config=$1
local action apps action_str blacklist
config_get action_str "$config" "action"
config_get apps "$config" "apps"
config_get blacklist "$config" "blacklist"
action="$(str_action_num $action_str)"
json_add_object ""
json_add_string "id" "$config"
json_add_int "action" $action
[ -n "$apps" ] && {
json_add_array "apps"
for app in $apps;do
json_add_int "" $app
done
json_select ..
}
[ -n "$blacklist" ] && {
json_add_array "blacklist"
for item in $blacklist;do
for i in $(url_to_app_feature $item);do
json_add_string "" "$i"
done
done
json_select ..
}
json_select ..
}
config_foreach load_rule_cb rule
json_str=`json_dump`
config_apply "$json_str"
json_cleanup
}
load_group()
{
json_init
json_add_int "op" $ADD_GROUP
json_add_object "data"
json_add_array "groups"
load_group_cb(){
local config=$1
local rule macs
config_get rule "$config" "default_rule"
config_get macs "$config" "macs"
json_add_object ""
json_add_string "id" "$config"
json_add_string "rule" $rule
[ -n "$macs" ] && {
json_add_array "macs"
for mac in $macs;do
json_add_string "" $mac
done
json_select ..
}
json_select ..
}
config_foreach load_group_cb group
json_str=`json_dump`
config_apply "$json_str"
json_cleanup
}
clean_rule()
{
json_init
json_add_int "op" $CLEAN_RULE
json_add_object "data"
json_str=`json_dump`
config_apply "$json_str"
json_cleanup
}
clean_group()
{
json_init
json_add_int "op" $CLEAN_GROUP
json_add_object "data"
json_str=`json_dump`
config_apply "$json_str"
json_cleanup
}
set_group_rule()
{
local config=$1
local rule=$2
local rule macs
json_init
json_add_int "op" $SET_GROUP
json_add_object "data"
json_add_array "groups"
config_get macs "$config" "macs"
json_add_object ""
json_add_string "id" "$config"
json_add_string "rule" $rule
[ -n "$macs" ] && {
json_add_array "macs"
for mac in $macs;do
json_add_string "" $mac
done
json_select ..
}
json_select ..
json_str=`json_dump`
config_apply "$json_str"
json_cleanup
}
update_feature_lib()
{
local url="$1"
local cur_v="$(cat /etc/parental_control/app_feature.cfg |head -n1|tr -cd '[0-9]')"
local new_v=""
curl -LSs --connect-timeout 30 -m 30 "$url" -o /tmp/app_feature.cfg && {
new_v="$(cat /tmp/app_feature.cfg |head -n1|tr -cd '[0-9]')"
[ "$new_v" -gt "$cur_v" ] && {
logger -t 'parental_control' "update app featere library from $cur_v to $new_v"
mv /tmp/app_feature.cfg /etc/parental_control/app_feature.cfg
sleep 3 && /etc/init.d/parental_control restart &
return
}
}
}