forked from dovela/CloudFlare_DNS_Record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudFlare_DDNS_Setter.sh
231 lines (207 loc) · 8.42 KB
/
CloudFlare_DDNS_Setter.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
#!/bin/bash
Green_font="\033[32m" && Red_font="\033[31m" && Font_suffix="\033[0m"
Info="${Green_font}[Info]${Font_suffix}"
Error="${Red_font}[Error]${Font_suffix}"
echo -e "${Green_font}
#=======================================
# Project: CloudFlare_DDNS_Setter
# Version: 1.0
# Author: nanqinlang
# Blog: https://sometimesnaive.org
# Github: https://github.com/nanqinlang
#=======================================
# Secondary editing by dovela
# Version: 2.0
#=======================================
${Font_suffix}"
file='/home/CloudFlare_DDNS'
ddns_conf='/home/CloudFlare_DDNS/config.conf'
check_root(){
[[ "`id -u`" != "0" ]] && echo -e "${Error} must be root user !" && exit 1
}
check_system(){
[[ -z "`cat /etc/issue | grep -E -i "debian"`" && -z "`cat /etc/issue | grep -E -i "ubuntu"`" && -z "`cat /etc/redhat-release | grep -E -i "CentOS"`" ]] && echo -e "${Error} only support Debian or Ubuntu or CentOS !" && exit 1
}
# 检查dns ip和本机ip是否不同,如果相同则直接退出脚本
check_ip_diff(){
dns_ip=`ping $domain -c 1 -W 1 | head -n 1 | awk '{print $3}' | sed 's/[()]//g'`
echo -e "${Info} Local IP: ${local_ip} / DNS IP: ${dns_ip}"
if [[ $dns_ip == $local_ip ]]; then
echo -e "${Info} no need to update record"
exit 0
fi
}
check_deps(){
if [[ ! -z "`cat /etc/issue | grep -E -i "debian"`" ]]; then
apt update -y
apt-get install -y openssl libssl-dev ca-certificates curl python-pip
elif
[[ ! -z "`cat /etc/issue | grep -E -i "ubuntu"`" ]]; then
apt update -y
apt-get install -y openssl libssl-dev ca-certificates curl python-pip
elif
[[ ! -z "`cat /etc/redhat-release | grep -E -i "CentOS"`" ]]; then
yum install -y epel-release
yum install -y openssl libssl-dev ca-certificates curl python-pip
else
echo -e "${Error} only support Debian or Ubuntu or CentOS !" && exit 1
fi
}
# 判断发行版版本
get_dist_name(){
if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
DISTRO='CentOS'
PM='yum'
elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
DISTRO='RHEL'
PM='yum'
elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
DISTRO='Aliyun'
PM='yum'
elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
DISTRO='Fedora'
PM='yum'
elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
DISTRO='Debian'
PM='apt'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
DISTRO='Ubuntu'
PM='apt'
elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
DISTRO='Raspbian'
PM='apt'
else
DISTRO='unknow'
fi
}
directory(){
[[ ! -d ${file} ]] && echo -e "${Error} can not found config directory, please check !" && exit 1
cd ${file}
}
define(){
[[ ! -f ${ddns_conf} ]] && echo -e "${Error} can not found config file, please check !" && exit 1
email=`cat ${ddns_conf} | grep "email" | awk -F "[ =]" '{print $2}'`
zone_id=`cat ${ddns_conf} | grep "zone_id" | awk -F "[ =]" '{print $2}'`
api_key=`cat ${ddns_conf} | grep "api_key" | awk -F "[ =]" '{print $2}'`
record_id=`cat ${ddns_conf} | grep "record_id" | awk -F "[ =]" '{print $2}'`
domain=`cat ${ddns_conf} | grep "domain" | awk -F "[ =]" '{print $2}'`
ttl=`cat ${ddns_conf} | grep "ttl" | awk -F "[ =]" '{print $2}'`
local_ip=`curl ipv4.ip.sb`
lightsail_switch=`cat ${ddns_conf} | grep "lightsail_switch" | awk -F "[ =]" '{print $2}'`
lightsail_ipname=`cat ${ddns_conf} | grep "lightsail_ipname" | awk -F "[ =]" '{print $2}'`
lightsail_instance=`cat ${ddns_conf} | grep "lightsail_instance" | awk -F "[ =]" '{print $2}'`
}
choose_service(){
echo -e "${Info} system dist: ${DISTRO}"
if [[ -z "$1" ]]; then
echo -e "${Info} if you want a automatic ddns, firstly you should get record_id"
echo -e "${Info} alternatively you can use this script to create a A record and get its id"
echo -e "${Info} now select required service:\n1.get domain record_id\n2.create a new domain A record\n3.configure lightsail if necessary"
read -p "(input 1~3 to select):" service
while [[ ! "${service}" =~ ^[1-3]$ ]]
do
echo -e "${Error} invalid input !"
read -p "(input 1~3 to select):" service
done
[[ "${service}" = "1" ]] && get_record_id
if [[ $DISTRO == "Ubuntu" ]]; then
sed -i '/CloudFlare_DDNS/d' /var/spool/cron/crontabs/root
echo -e '*/3 * * * * bash /home/CloudFlare_DDNS/CloudFlare_DDNS_Setter.sh --ddns' >> /var/spool/cron/crontabs/root
echo -e "${Info} create cron task for ${DISTRO} success"
elif [[ $DISTRO == "CentOS" ]]; then
sed -i '/CloudFlare_DDNS/d' /var/spool/cron/root
echo -e '*/3 * * * * bash /home/CloudFlare_DDNS/CloudFlare_DDNS_Setter.sh --ddns' >> /var/spool/cron/root
echo -e "${Info} create cron task for ${DISTRO} success"
else
echo -e "${Error} not support for ${DISTRO} yet" && exit 1
fi
[[ "${service}" = "2" ]] && create_record
[[ "${service}" = "3" ]] && Lightsail_conf
elif [[ "$1" == "--ddns" ]]; then
echo -e "${Info} start DDNS service"
if [[ $lightsail_switch == "true" ]]; then
echo -e "${Info} start Lightsail service check"
lightsail_change_ip
fi
echo -e "${Info} start checking if ip changed"
check_ip_diff
echo -e "${Info} now will start automatically ddns record updating service"
echo -e "${Info} now will start automatically ddns record updating service"
update_record
else
echo -e "${Error} invalid input !" && exit 1
fi
}
get_record(){
curl -X GET "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?type=A&name${domain}&order=name" \
-H "X-Auth-Email: ${email}" \
-H "X-Auth-Key: ${api_key}" \
-H "Content-Type: application/json"
}
update_record(){
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}" \
-H "X-Auth-Email: ${email}" \
-H "X-Auth-Key: ${api_key}" \
-H "Content-Type: application/json" \
--data '{"type":"A", "name":"'${domain}'", "content":"'${local_ip}'", "ttl":'${ttl}', "proxied":false}'
}
create_record(){
curl -X POST "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records" \
-H "X-Auth-Email: ${email}" \
-H "X-Auth-Key: ${api_key}" \
-H "Content-Type: application/json" \
--data '{"type":"A", "name":"'${domain}'", "content":"'${local_ip}'", "ttl":'${ttl}', "proxied":false}'
}
get_record_id(){
records_text=`get_record`
record_id=`echo -e "${records_text}" | awk -F "\"" '{print $6F}'`
if [[ ${#record_id} -ne 32 ]]; then
echo -e "${Error} check if your A record is correct !" && exit 1
fi
sed -i '/record_id/d' ${ddns_conf}
echo -e "record_id=${record_id}" >> ${ddns_conf}
echo -e "${Info} get record id 【${record_id}】 success"
}
Lightsail_conf(){
pip install awscli --upgrade
clear
echo -e '''
===============================
北美: us-east-1 弗吉尼亚州
us-east-2 俄亥俄州
us-west-2 俄勒冈州
ca-central-1 加拿大
欧洲: eu-west-1 爱尔兰
eu-west-2 英国
eu-west-3 法国
eu-central-1 德国
亚洲: ap-northeast-1 日本
ap-northeast-2 韩国
ap-southeast-1 新加坡
ap-southeast-2 澳大利亚
ap-south-1 印度
===============================
'''
aws configure #输入AWSAccessKeyId和AWSSecretKey以及本机地域,第四项留空,Key由 https://console.aws.amazon.com/iam/home#/security_credential 申请
}
lightsail_change_ip(){
#检查本机ip是否被tcp阻断
tcp_status=`curl --silent https://ipcheck.need.sh/api_v2.php?ip=${local_ip}` | awk -F '[:}]' '{print $21}'
if [[ $tcp_status == "false" ]]; then
# 删除现有静态IP
aws lightsail release-static-ip --static-ip-name ${lightsail_ipname} >/dev/null 2>&1
# 创建新IP
aws lightsail allocate-static-ip --static-ip-name ${lightsail_ipname} >/dev/null 2>&1
# 绑定IP
aws lightsail attach-static-ip --static-ip-name ${lightsail_ipname} --instance-name ${lightsail_instance} >/dev/null 2>&1
#待机15s以确保ip更换完毕
sleep 15s
fi
}
check_root
check_system
get_dist_name
[[ "$1" = "install" ]] && check_deps && exit 0
directory
define
choose_service $1