-
Notifications
You must be signed in to change notification settings - Fork 62
/
chinadns-ng.init
171 lines (146 loc) · 3.83 KB
/
chinadns-ng.init
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
#!/bin/sh /etc/rc.common
#
# Copyright (C) 2019 - 2020, pexcn <i@pexcn.me>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#
START=90
USE_PROCD=1
#PROCD_DEBUG=1
append_param() {
local section="$1"
local option="$2"
local switch="$3"
local default="$4"
local _loctmp
config_get _loctmp "$section" "$option"
[ -n "$_loctmp" -o -n "$default" ] || return 0
procd_append_param command "$switch" "${_loctmp:-$default}"
}
append_bool() {
local section="$1"
local option="$2"
local value="$3"
local default="$4"
local _loctmp
config_get_bool _loctmp "$section" "$option" "$default"
[ "$_loctmp" = 1 ] || return 0
procd_append_param command "$value"
}
append_param_if_not_eq() {
local section="$1"
local option="$2"
local switch="$3"
local compare="$4"
local _loctmp
config_get _loctmp "$section" "$option"
[ -n "$_loctmp" ] || return 0
[ "$_loctmp" != "$compare" ] && {
procd_append_param command "$switch" "$_loctmp"
}
}
get_cpu_cores() {
grep -c ^processor /proc/cpuinfo
}
is_exist_ipset_by_name() {
ipset list -n $1 &>/dev/null
}
destroy_ipset_by_name() {
ipset flush $1 2>/dev/null
ipset destroy $1 2>/dev/null
}
create_chnroute_ipset() {
local enable
config_get_bool enable $1 enable
[ "$enable" = 1 ] || return 0
create_ipv4_ipset
create_ipv6_ipset
}
create_ipv4_ipset() {
local ipset_name
config_get ipset_name "$1" "ipset_name_4" "chnroute"
if is_exist_ipset_by_name $ipset_name; then
ipset flush $ipset_name
else
ipset create $ipset_name hash:net hashsize 64 family inet
fi
# import chnroute
ipset restore <<- EOF
$(cat /etc/chinadns-ng/chnroute.txt | sed "s/^/add $ipset_name /")
EOF
}
create_ipv6_ipset() {
local ipset_name
config_get ipset_name "$1" "ipset_name_6" "chnroute6"
if is_exist_ipset_by_name $ipset_name; then
ipset flush $ipset_name
else
ipset create $ipset_name hash:net hashsize 64 family inet6
fi
# import chnroute6
ipset restore <<- EOF
$(cat /etc/chinadns-ng/chnroute6.txt | sed "s/^/add $ipset_name /")
EOF
}
destroy_chnroute_ipset() {
# force destroy, even though does not exist or config file changed
destroy_ipv4_ipset
destroy_ipv6_ipset
}
destroy_ipv4_ipset() {
local ipset_name
config_get ipset_name "$1" "ipset_name_4" "chnroute"
destroy_ipset_by_name $ipset_name
}
destroy_ipv6_ipset() {
local ipset_name
config_get ipset_name "$1" "ipset_name_6" "chnroute6"
destroy_ipset_by_name $ipset_name
}
start_chinadns_ng() {
local enable
config_get_bool enable $1 enable
[ "$enable" = 1 ] || return 0
local reuse_port
config_get_bool reuse_port $1 reuse_port
local instances=$(get_cpu_cores)
[ "$reuse_port" = 1 ] || instances=1
for i in $(seq 1 $instances)
do
procd_open_instance chinadns-ng-$i
procd_set_param respawn
procd_set_param stderr 1
procd_set_param nice -5
procd_set_param limits nofile="65535 65535"
procd_set_param command /usr/bin/chinadns-ng
append_param $1 bind_addr "-b"
append_param $1 bind_port "-l"
append_param $1 china_dns "-c"
append_param $1 trust_dns "-t"
append_param_if_not_eq $1 ipset_name_4 "-4" "chnroute"
append_param_if_not_eq $1 ipset_name_6 "-6" "chnroute6"
append_param $1 gfwlist_file "-g"
append_param $1 chnlist_file "-m"
append_param_if_not_eq $1 timeout_sec "-o" "3"
append_param_if_not_eq $1 repeat_times "-p" "1"
append_bool $1 chnlist_first "-M"
append_bool $1 fair_mode "-f"
append_bool $1 reuse_port "-r"
append_bool $1 noip_as_chnip "-n"
procd_close_instance
done
}
start_service() {
config_load chinadns-ng
config_foreach create_chnroute_ipset chinadns-ng
config_foreach start_chinadns_ng chinadns-ng
}
stop_service() {
config_load chinadns-ng
config_foreach destroy_chnroute_ipset chinadns-ng
}
reload_service() {
# reference: https://github.com/pexcn/openwrt-chinadns-ng/issues/13
echo "Unimplemented, use \"restart\" instead."
}