forked from shuuzhoou/doubi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dowsdns.sh
632 lines (623 loc) · 22.7 KB
/
dowsdns.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================
# System Required: CentOS/Debian/Ubuntu
# Description: DowsDNS
# Version: 1.0.7
# Author: Toyo
# Blog: https://doub.io/dowsdns-jc3/
#=================================================
sh_ver="1.0.7"
file="/usr/local/dowsDNS"
dowsdns_conf="/usr/local/dowsDNS/conf/config.json"
dowsdns_data="/usr/local/dowsDNS/conf/hosts_repository_config.json"
dowsdns_wrcd="/usr/local/dowsDNS/data/wrcd.json"
dowsdns_log="/tmp/dowsdns.log"
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}" && Error="${Red_font_prefix}[错误]${Font_color_suffix}" && Tip="${Green_font_prefix}[注意]${Font_color_suffix}"
#检查系统
check_sys(){
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -q -E -i "debian"; then
release="debian"
elif cat /etc/issue | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -q -E -i "debian"; then
release="debian"
elif cat /proc/version | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
fi
bit=`uname -m`
}
check_installed_status(){
[[ ! -e ${file} ]] && echo -e "${Error} DowsDNS 没有安装,请检查 !" && exit 1
}
check_pid(){
PID=`ps -ef| grep "python start.py"| grep -v grep| grep -v ".sh"| grep -v "init.d"| grep -v "service"| awk '{print $2}'`
}
Download_dowsdns(){
cd "/usr/local"
wget -N --no-check-certificate "https://softs.fun/%E7%A7%91%E5%AD%A6%E4%B8%8A%E7%BD%91/PC/dowsDNS/Linux%2BMac/dowsDNS.zip"
[[ ! -e "dowsDNS.zip" ]] && echo -e "${Error} DowsDNS 下载失败 !" && exit 1
unzip dowsDNS.zip && rm -rf dowsDNS.zip
[[ ! -e "dowsDNS-master" ]] && echo -e "${Error} DowsDNS 解压失败 !" && exit 1
mv dowsDNS-master dowsDNS
[[ ! -e "dowsDNS" ]] && echo -e "${Error} DowsDNS 文件夹重命名失败 !" && rm -rf dowsDNS-master && exit 1
}
Service_dowsdns(){
if [[ ${release} = "centos" ]]; then
if ! wget --no-check-certificate "https://softs.fun/Bash/other/dowsdns_centos" -O /etc/init.d/dowsdns; then
echo -e "${Error} DowsDNS 服务管理脚本下载失败 !" && exit 1
fi
chmod +x /etc/init.d/dowsdns
chkconfig --add dowsdns
chkconfig dowsdns on
else
if ! wget --no-check-certificate "https://softs.fun/Bash/other/dowsdns_debian" -O /etc/init.d/dowsdns; then
echo -e "${Error} DowsDNS 服务管理脚本下载失败 !" && exit 1
fi
chmod +x /etc/init.d/dowsdns
update-rc.d -f dowsdns defaults
fi
echo -e "${Info} DowsDNS 服务管理脚本下载完成 !"
}
Installation_dependency(){
python_status=$(python --help)
if [[ ${release} == "centos" ]]; then
yum update
if [[ -z ${python_status} ]]; then
yum install -y python unzip
else
yum install -y unzip
fi
else
apt-get update
if [[ -z ${python_status} ]]; then
apt-get install -y python unzip
else
apt-get install -y unzip
fi
fi
}
Write_config(){
cat > ${dowsdns_conf}<<-EOF
{
"Remote_dns_server":"${dd_remote_dns_server}",
"Remote_dns_port":${dd_remote_dns_port},
"Rpz_json_path":"./data/rpz.json",
"Local_dns_server":"${dd_local_dns_server}",
"Local_dns_port":${dd_local_dns_port},
"sni_proxy_on":${dd_sni_proxy_on},
"Public_Server":${public_server},
"sni_proxy_ip":"${dd_sni_proxy_ip}"
}
EOF
}
Read_config(){
[[ ! -e ${dowsdns_conf} ]] && echo -e "${Error} DowsDNS 配置文件不存在 !" && exit 1
remote_dns_server=`cat ${dowsdns_conf}|grep "Remote_dns_server"|awk -F ":" '{print $NF}'|sed -r 's/.*\"(.+)\".*/\1/'`
remote_dns_port=`cat ${dowsdns_conf}|grep "Remote_dns_port"|sed -r 's/.*:(.+),.*/\1/'`
local_dns_server=`cat ${dowsdns_conf}|grep "Local_dns_server"|awk -F ":" '{print $NF}'|sed -r 's/.*\"(.+)\".*/\1/'`
local_dns_port=`cat ${dowsdns_conf}|grep "Local_dns_port"|sed -r 's/.*:(.+),.*/\1/'`
sni_proxy_ip=`cat ${dowsdns_conf}|grep "sni_proxy_ip"|awk -F ":" '{print $NF}'|sed -r 's/.*\"(.+)\".*/\1/'`
}
Read_wrcd(){
[[ ! -e ${dowsdns_wrcd} ]] && echo -e "${Error} DowsDNS 泛域名解析 配置文件不存在 !" && exit 1
wrcd_json=$(cat -n ${dowsdns_wrcd}|sed '$d;1d;s/\"//g;s/,//g')
wrcd_json_num=$(echo -e "${wrcd_json}"|wc -l)
wrcd_json_num=$(expr $wrcd_json_num + 1)
echo -e "当前DowsDNS 泛域名解析配置(不要问我为什么是从 2 开始):\n"
echo -e "${wrcd_json}\n"
}
Set_remote_dns_server(){
echo "请输入 DowsDNS 远程(上游)DNS解析服务器IP"
stty erase '^H' && read -p "(默认: 114.114.114.114):" dd_remote_dns_server
[[ -z "${dd_remote_dns_server}" ]] && dd_remote_dns_server="114.114.114.114"
echo
}
Set_remote_dns_port(){
while true
do
echo -e "请输入 DowsDNS 远程(上游)DNS解析服务器端口 [1-65535]"
stty erase '^H' && read -p "(默认: 53):" dd_remote_dns_port
[[ -z "$dd_remote_dns_port" ]] && dd_remote_dns_port="53"
expr ${dd_remote_dns_port} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${dd_remote_dns_port} -ge 1 ]] && [[ ${dd_remote_dns_port} -le 65535 ]]; then
echo
break
else
echo "输入错误, 请输入正确的端口。"
fi
else
echo "输入错误, 请输入正确的端口。"
fi
done
}
Set_remote_dns(){
echo -e "请选择并输入 DowsDNS 的远程(上游)DNS解析服务器
说明:即一些DowsDNS没有指定的域名都由上游DNS解析,比如百度啥的。
${Green_font_prefix}1.${Font_color_suffix} 114.114.114.114 53
${Green_font_prefix}2.${Font_color_suffix} 8.8.8.8 53
${Green_font_prefix}3.${Font_color_suffix} 208.67.222.222 53
${Green_font_prefix}4.${Font_color_suffix} 208.67.222.222 5353
${Green_font_prefix}5.${Font_color_suffix} 自定义输入" && echo
stty erase '^H' && read -p "(默认: 1. 114.114.114.114 53):" dd_remote_dns
[[ -z "${dd_remote_dns}" ]] && dd_remote_dns="1"
if [[ ${dd_remote_dns} == "1" ]]; then
dd_remote_dns_server="114.114.114.114"
dd_remote_dns_port="53"
elif [[ ${dd_remote_dns} == "2" ]]; then
dd_remote_dns_server="8.8.8.8"
dd_remote_dns_port="53"
elif [[ ${dd_remote_dns} == "3" ]]; then
dd_remote_dns_server="208.67.222.222"
dd_remote_dns_port="53"
elif [[ ${dd_remote_dns} == "4" ]]; then
dd_remote_dns_server="208.67.222.222"
dd_remote_dns_port="5353"
elif [[ ${dd_remote_dns} == "5" ]]; then
echo
Set_remote_dns_server
Set_remote_dns_port
else
dd_remote_dns_server="114.114.114.114"
dd_remote_dns_port="53"
fi
echo && echo " ================================================"
echo -e " 远程(上游)DNS解析服务器 IP :\t ${Red_background_prefix} ${dd_remote_dns_server} ${Font_color_suffix}
远程(上游)DNS解析服务器 端口 :\t ${Red_background_prefix} ${dd_remote_dns_port} ${Font_color_suffix}"
echo " ================================================" && echo
}
Set_local_dns_server(){
echo -e "请选择并输入 DowsDNS 的本地监听方式
${Green_font_prefix}1.${Font_color_suffix} 127.0.0.1 (只允许本地和局域网设备访问)
${Green_font_prefix}2.${Font_color_suffix} 0.0.0.0 (允许外网访问)" && echo
stty erase '^H' && read -p "(默认: 2. 0.0.0.0):" dd_local_dns_server
[[ -z "${dd_local_dns_server}" ]] && dd_local_dns_server="2"
if [[ ${dd_local_dns_server} == "1" ]]; then
dd_local_dns_server="127.0.0.1"
public_server="false"
elif [[ ${dd_local_dns_server} == "2" ]]; then
dd_local_dns_server="0.0.0.0"
public_server="true"
else
dd_local_dns_server="0.0.0.0"
public_server="true"
fi
echo && echo " ================================================"
echo -e " 本地监听方式: ${Red_background_prefix} ${dd_local_dns_server} ${Font_color_suffix}"
echo " ================================================" && echo
}
Set_local_dns_port(){
while true
do
echo -e "请输入 DowsDNS 监听端口 [1-65535]
注意:大部分设备是不支持设置 非53端口的DNS服务器的,所以非必须请直接回车默认使用 53端口。" && echo
stty erase '^H' && read -p "(默认: 53):" dd_local_dns_port
[[ -z "$dd_local_dns_port" ]] && dd_local_dns_port="53"
expr ${dd_local_dns_port} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${dd_local_dns_port} -ge 1 ]] && [[ ${dd_local_dns_port} -le 65535 ]]; then
echo && echo " ================================================"
echo -e " 监听端口 : ${Red_background_prefix} ${dd_local_dns_port} ${Font_color_suffix}"
echo " ================================================" && echo
break
else
echo "输入错误, 请输入正确的端口。"
fi
else
echo "输入错误, 请输入正确的端口。"
fi
done
}
Set_sni_proxy_on(){
echo "是否开启 DowsDNS SNI代理功能?[y/N]
注意:开启此功能后,任何自定义设置的 hosts或泛域名解析(包括DowsDNS自带的),都指向设置的SNI代理IP,如果你没有SNI代理IP,请输入 N !"
stty erase '^H' && read -p "(默认: N 关闭):" dd_sni_proxy_on
[[ -z "${dd_sni_proxy_on}" ]] && dd_sni_proxy_on="n"
if [[ ${dd_sni_proxy_on} == [Yy] ]]; then
dd_sni_proxy_on="true"
else
dd_sni_proxy_on="false"
fi
echo && echo " ================================================"
echo -e " SNI代理开关 : ${Red_background_prefix} ${dd_sni_proxy_on} ${Font_color_suffix}"
echo " ================================================" && echo
}
Set_sni_proxy_ip(){
ddd_sni_proxy_ip=$(wget --no-check-certificate -t2 -T4 -qO- "https://raw.githubusercontent.com/dowsnature/dowsDNS/master/conf/config.json"|grep "sni_proxy_ip"|awk -F ":" '{print $NF}'|sed -r 's/.*\"(.+)\".*/\1/')
[[ -z ${ddd_sni_proxy_ip} ]] && ddd_sni_proxy_ip="219.76.4.3"
echo "请输入 DowsDNS SNI代理 IP(如果没有就直接回车)"
stty erase '^H' && read -p "(默认: ${ddd_sni_proxy_ip}):" dd_sni_proxy_ip
[[ -z "${dd_sni_proxy_ip}" ]] && dd_sni_proxy_ip="${ddd_sni_proxy_ip}"
echo && echo " ================================================"
echo -e " SNI代理 IP : ${Red_background_prefix} ${dd_sni_proxy_ip} ${Font_color_suffix}"
echo " ================================================" && echo
}
Set_conf(){
Set_remote_dns
Set_local_dns_server
Set_local_dns_port
Set_sni_proxy_on
Set_sni_proxy_ip
}
Set_dowsdns_basis(){
check_installed_status
Set_conf
Read_config
Del_iptables
Write_config
Add_iptables
Save_iptables
Restart_dowsdns
}
Set_wrcd_name(){
echo "请输入 DowsDNS 要添加/修改的域名(子域名或泛域名)
注意:假如你想要 youtube.com 及其二级域名全部指向 指定的IP,那么你需要添加 *.youtube.com 和 youtube.com 这两个域名解析才有效。
这意味着 *.youtube.com 仅代表如 www.youtube.com xxx.youtube.com 这样的二级域名,而不能代表一级域名(顶级域名) youtube.com !"
stty erase '^H' && read -p "(默认回车取消):" wrcd_name
[[ -z "${wrcd_name}" ]] && echo "已取消..." && exit 0
echo
}
Set_wrcd_name_1(){
echo "检测到当前添加的域名为 泛域名,是否自动添加 上级域名(如顶级域名,就是上面示例说的 youtube.com) [Y/n]"
stty erase '^H' && read -p "(默认: Y 添加):" wrcd_name_1
[[ -z "${wrcd_name_1}" ]] && wrcd_name_1="y"
if [[ ${wrcd_name_1} == [Yy] ]]; then
wrcd_name_1=$(echo -e "${wrcd_name}"|cut -c 3-100)
echo -e "检测到 上级域名为 : ${Red_font_prefix}${wrcd_name_1}${Font_color_suffix}"
else
wrcd_name_1=""
echo "已取消...继续..."
fi
echo
}
Set_wrcd_ip(){
echo "请输入 DowsDNS 刚才添加/修改的域名要指向的IP
注意:如果你开启了 SNI代理功能(config.json),那么你这里设置的自定义泛域名解析都会被 SNI代理功能的SNI代理IP设置所覆盖,也就是统一指向 SNI代理的IP,这里的IP设置就没意义了。"
stty erase '^H' && read -p "(默认回车取消):" wrcd_ip
[[ -z "${wrcd_ip}" ]] && echo "已取消..." && exit 0
echo
}
Set_dowsdns_wrcd(){
check_installed_status
echo && echo -e "你要做什么?
${Green_font_prefix}0.${Font_color_suffix} 查看 泛域名解析列表
${Green_font_prefix}1.${Font_color_suffix} 添加 泛域名解析
${Green_font_prefix}2.${Font_color_suffix} 删除 泛域名解析
${Green_font_prefix}3.${Font_color_suffix} 修改 泛域名解析" && echo
stty erase '^H' && read -p "(默认: 取消):" wrcd_modify
[[ -z "${wrcd_modify}" ]] && echo "已取消..." && exit 1
if [[ ${wrcd_modify} == "0" ]]; then
Read_wrcd
elif [[ ${wrcd_modify} == "1" ]]; then
Add_wrcd
elif [[ ${wrcd_modify} == "2" ]]; then
Del_wrcd
elif [[ ${wrcd_modify} == "3" ]]; then
Modify_wrcd
else
echo -e "${Error} 请输入正确的数字 [0-3]" && exit 1
fi
}
Add_wrcd(){
while true
do
Set_wrcd_name
[[ $(echo -e "${wrcd_name}"|cut -c 1-2) == "*." ]] && Set_wrcd_name_1
Set_wrcd_ip
sed -i "2 i \"${wrcd_name}\":\"${wrcd_ip}\"," ${dowsdns_wrcd}
if [[ $? == "0" ]]; then
echo -e "${Info} 添加泛域名解析 成功 [${wrcd_name} : ${wrcd_ip}]"
else
echo -e "${Error} 添加泛域名解析 失败!" && exit 0
fi
if [[ ! -z ${wrcd_name_1} ]]; then
sed -i "2 i \"${wrcd_name_1}\":\"${wrcd_ip}\"," ${dowsdns_wrcd}
if [[ $? == "0" ]]; then
echo -e "${Info} 添加泛域名解析 成功 [${wrcd_name_1} : ${wrcd_ip}]"
else
echo -e "${Error} 添加泛域名解析 失败!" && exit 0
fi
fi
echo && echo "是否继续添加 泛域名解析?[Y/n]"
stty erase '^H' && read -p "(默认: Y 继续添加):" wrcd_add_1
[[ -z "${wrcd_add_1}" ]] && wrcd_add_1="y"
if [[ ${wrcd_add_1} == [Yy] ]]; then
continue
else
break
fi
done
echo -e "${Info} 重启 dowsDNS中..."
Restart_dowsdns
}
Del_wrcd(){
while true
do
Read_wrcd
echo "请根据上面的列表选择你要删除的 泛域名解析 序号数字 [ 2-${wrcd_json_num} ]"
stty erase '^H' && read -p "(默认回车取消):" del_wrcd_num
[[ -z "$del_wrcd_num" ]] && echo "已取消..." && exit 0
expr ${del_wrcd_num} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${del_wrcd_num} -ge 2 ]] && [[ ${del_wrcd_num} -le ${wrcd_json_num} ]]; then
wrcd_text=$(cat ${dowsdns_wrcd}|sed -n "${del_wrcd_num}p")
wrcd_name=$(echo -e "${wrcd_text}"|sed 's/\"//g;s/,//g'|awk -F ":" '{print $1}')
wrcd_ip=$(echo -e "${wrcd_text}"|sed 's/\"//g;s/,//g'|awk -F ":" '{print $2}')
del_wrcd_determine=$(echo ${wrcd_text:((${#wrcd_text} - 1))})
if [[ ${del_wrcd_num} == ${wrcd_json_num} ]]; then
del_wrcd_determine_num=$(expr $del_wrcd_num - 1)
sed -i "${del_wrcd_determine_num}s/,//g" ${dowsdns_wrcd}
fi
sed -i "${del_wrcd_num}d" ${dowsdns_wrcd}
if [[ $? == "0" ]]; then
echo -e "${Info} 删除泛域名解析 成功 [${wrcd_name} : ${wrcd_ip}]"
else
echo -e "${Error} 删除泛域名解析 失败!" && exit 0
fi
echo && echo "是否继续删除 泛域名解析?[Y/n]"
stty erase '^H' && read -p "(默认: Y 继续删除):" wrcd_del_1
[[ -z "${wrcd_del_1}" ]] && wrcd_del_1="y"
if [[ ${wrcd_del_1} == [Yy] ]]; then
continue
else
break
fi
else
echo "输入错误, 请输入正确的数字。"
fi
else
echo "输入错误, 请输入正确的数字。"
fi
done
echo -e "${Info} 重启 dowsDNS中..."
Restart_dowsdns
}
Modify_wrcd(){
while true
do
Read_wrcd
echo "请根据上面的列表选择你要修改的 泛域名解析 序号数字 [ 2-${wrcd_json_num} ]"
stty erase '^H' && read -p "(默认回车取消):" modify_wrcd_num
[[ -z "$modify_wrcd_num" ]] && echo "已取消..." && exit 0
expr ${modify_wrcd_num} + 0 &>/dev/null
if [[ $? -eq 0 ]]; then
if [[ ${modify_wrcd_num} -ge 2 ]] && [[ ${modify_wrcd_num} -le ${wrcd_json_num} ]]; then
wrcd_name_now=$(cat ${dowsdns_wrcd}|sed -n "${modify_wrcd_num}p"|sed 's/\"//g;s/,//g'|awk -F ":" '{print $1}')
wrcd_ip_now=$(cat ${dowsdns_wrcd}|sed -n "${modify_wrcd_num}p"|sed 's/\"//g;s/,//g'|awk -F ":" '{print $2}')
echo
Set_wrcd_name
Set_wrcd_ip
sed -i "${modify_wrcd_num}d" ${dowsdns_wrcd}
sed -i "${modify_wrcd_num} i \"${wrcd_name}\":\"${wrcd_ip}\"," ${dowsdns_wrcd}
#sed -i "s/\"${wrcd_name_now_1}\":\"${wrcd_ip_now}\"/\"${wrcd_name_1}\":\"${wrcd_ip}\"/g" ${dowsdns_wrcd}
if [[ $? == "0" ]]; then
echo -e "${Info} 修改泛域名解析 成功 [旧 ${wrcd_name_now} : ${wrcd_ip_now} , 新 ${wrcd_name} : ${wrcd_ip}]"
else
echo -e "${Error} 修改泛域名解析 失败!" && exit 0
fi
break
else
echo "输入错误, 请输入正确的数字。"
fi
else
echo "输入错误, 请输入正确的数字。"
fi
done
echo -e "${Info} 重启 dowsDNS中..."
Restart_dowsdns
}
Install_dowsdns(){
[[ -e ${file} ]] && echo -e "${Error} 检测到 DowsDNS 已安装 !" && exit 1
check_sys
echo -e "${Info} 开始设置 用户配置..."
Set_conf
echo -e "${Info} 开始安装/配置 依赖..."
Installation_dependency
echo -e "${Info} 开始下载/安装..."
Download_dowsdns
echo -e "${Info} 开始下载/安装 服务脚本(init)..."
Service_dowsdns
echo -e "${Info} 开始写入 配置文件..."
Write_config
echo -e "${Info} 开始设置 iptables防火墙..."
Set_iptables
echo -e "${Info} 开始添加 iptables防火墙规则..."
Add_iptables
echo -e "${Info} 开始保存 iptables防火墙规则..."
Save_iptables
echo -e "${Info} 所有步骤 安装完毕,开始启动..."
Start_dowsdns
}
Start_dowsdns(){
check_installed_status
check_pid
[[ ! -z ${PID} ]] && echo -e "${Error} DowsDNS 正在运行,请检查 !" && exit 1
/etc/init.d/dowsdns start
}
Stop_dowsdns(){
check_installed_status
check_pid
[[ -z ${PID} ]] && echo -e "${Error} DowsDNS 没有运行,请检查 !" && exit 1
/etc/init.d/dowsdns stop
}
Restart_dowsdns(){
check_installed_status
check_pid
[[ ! -z ${PID} ]] && /etc/init.d/dowsdns stop
/etc/init.d/dowsdns start
}
Update_dowsdns(){
check_installed_status
check_sys
cd ${file}
python update.py
}
Uninstall_dowsdns(){
check_installed_status
echo "确定要卸载 DowsDNS ? (y/N)"
echo
stty erase '^H' && read -p "(默认: n):" unyn
[[ -z ${unyn} ]] && unyn="n"
if [[ ${unyn} == [Yy] ]]; then
check_pid
[[ ! -z $PID ]] && kill -9 ${PID}
Read_config
Del_iptables
rm -rf ${file} && rm -rf /etc/init.d/dowsdns
if [[ ${release} = "centos" ]]; then
chkconfig --del dowsdns
else
update-rc.d -f dowsdns remove
fi
echo && echo "DowsDNS 卸载完成 !" && echo
else
echo && echo "卸载已取消..." && echo
fi
}
View_dowsdns(){
check_installed_status
Read_config
if [[ ${local_dns_server} == "127.0.0.1" ]]; then
ip="${local_dns_server} "
else
ip=`wget -qO- -t1 -T2 members.3322.org/dyndns/getip`
[[ -z ${ip} ]] && ip="VPS_IP"
fi
clear && echo "————————————————" && echo
echo -e " 请在你的设备中设置DNS服务器为:
IP : ${Green_font_prefix}${ip}${Font_color_suffix} ,端口 : ${Green_font_prefix}${local_dns_port}${Font_color_suffix}
注意:如果设备中没有 DNS端口设置选项,那么就只能使用默认的 53 端口"
echo && echo "————————————————"
}
Add_iptables(){
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${dd_local_dns_port} -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${dd_local_dns_port} -j ACCEPT
}
Del_iptables(){
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${local_dns_port} -j ACCEPT
iptables -D INPUT -m state --state NEW -m udp -p udp --dport ${local_dns_port} -j ACCEPT
}
Save_iptables(){
if [[ ${release} == "centos" ]]; then
service iptables save
else
iptables-save > /etc/iptables.up.rules
fi
}
Set_iptables(){
if [[ ${release} == "centos" ]]; then
service iptables save
chkconfig --level 2345 iptables on
elif [[ ${release} == "debian" ]]; then
iptables-save > /etc/iptables.up.rules
cat > /etc/network/if-pre-up.d/iptables<<-EOF
#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules
EOF
chmod +x /etc/network/if-pre-up.d/iptables
elif [[ ${release} == "ubuntu" ]]; then
iptables-save > /etc/iptables.up.rules
echo -e "\npre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-save > /etc/iptables.up.rules" >> /etc/network/interfaces
chmod +x /etc/network/interfaces
fi
}
View_Log(){
[[ ! -e ${dowsdns_log} ]] && echo -e "${Error} dowsDNS 日志文件不存在 !" && exit 1
echo && echo -e "${Tip} 按 ${Red_font_prefix}Ctrl+C${Font_color_suffix} 终止查看日志" && echo
tail -f ${dowsdns_log}
}
Update_Shell(){
echo -e "当前版本为 [ ${sh_ver} ],开始检测最新版本..."
sh_new_ver=$(wget --no-check-certificate -qO- softs.fun/Bash/dowsdns.sh|grep 'sh_ver="'|awk -F "=" '{print $NF}'|sed 's/\"//g'|head -1)
[[ -z ${sh_new_ver} ]] && echo -e "${Error} 检测最新版本失败 !" && exit 0
if [[ ${sh_new_ver} != ${sh_ver} ]]; then
echo -e "发现新版本[ ${sh_new_ver} ],是否更新?[Y/n]"
stty erase '^H' && read -p "(默认: y):" yn
[[ -z "${yn}" ]] && yn="y"
if [[ ${yn} == [Yy] ]]; then
wget -N --no-check-certificate "https://softs.fun/Bash/dowsdns.sh" && chmod +x dowsdns.sh
echo -e "脚本已更新为最新版本[ ${sh_new_ver} ] !"
else
echo && echo " 已取消..." && echo
fi
else
echo -e "当前已是最新版本[ ${sh_new_ver} ] !"
fi
}
echo && echo -e " DowsDNS 一键安装管理脚本 ${Red_font_prefix}[v${sh_ver}]${Font_color_suffix}
-- Toyo | doub.io/dowsdns-jc3 --
${Green_font_prefix} 0.${Font_color_suffix} 升级脚本
————————————
${Green_font_prefix} 1.${Font_color_suffix} 安装 DowsDNS
${Green_font_prefix} 2.${Font_color_suffix} 升级 DowsDNS
${Green_font_prefix} 3.${Font_color_suffix} 卸载 DowsDNS
————————————
${Green_font_prefix} 4.${Font_color_suffix} 启动 DowsDNS
${Green_font_prefix} 5.${Font_color_suffix} 停止 DowsDNS
${Green_font_prefix} 6.${Font_color_suffix} 重启 DowsDNS
————————————
${Green_font_prefix} 7.${Font_color_suffix} 设置 DowsDNS 基础配置
${Green_font_prefix} 8.${Font_color_suffix} 设置 DowsDNS 泛域名解析配置
${Green_font_prefix} 9.${Font_color_suffix} 查看 DowsDNS 信息
${Green_font_prefix}10.${Font_color_suffix} 查看 DowsDNS 日志
————————————" && echo
if [[ -e ${file} ]]; then
check_pid
if [[ ! -z "${PID}" ]]; then
echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} 并 ${Green_font_prefix}已启动${Font_color_suffix}"
else
echo -e " 当前状态: ${Green_font_prefix}已安装${Font_color_suffix} 但 ${Red_font_prefix}未启动${Font_color_suffix}"
fi
else
echo -e " 当前状态: ${Red_font_prefix}未安装${Font_color_suffix}"
fi
echo
stty erase '^H' && read -p " 请输入数字 [0-9]:" num
case "$num" in
0)
Update_Shell
;;
1)
Install_dowsdns
;;
2)
Update_dowsdns
;;
3)
Uninstall_dowsdns
;;
4)
Start_dowsdns
;;
5)
Stop_dowsdns
;;
6)
Restart_dowsdns
;;
7)
Set_dowsdns_basis
;;
8)
Set_dowsdns_wrcd
;;
9)
View_dowsdns
;;
10)
View_Log
;;
*)
echo "请输入正确数字 [0-10]"
;;
esac