-
Notifications
You must be signed in to change notification settings - Fork 34
/
cf.sh
133 lines (115 loc) · 4.85 KB
/
cf.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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# Configuration
echo "https://github.com/BlueSkyXN/DNS-AUTO-Switch"
echo "IP+1不好吗:https://www.blueskyxn.com"
echo "BlueSkyXN:开始读取配置"
# Ping API
PING_API=http://IP:8080/ping
echo "BlueSkyXN:Ping·API读取成功"
#使用https://github.com/TorchPing/go-torch 自行搭建测试API 默认监听端口为8080 你也可以用域名反代 记得有/ping
# Original IP 常规IP
ORG_IP=1.1.1.1
# Failure IP 失败后IP
FAIL_IP=1.1.1.1
# Telegram Bot Token
TG_BOT_TOKEN=1254xxxx:AxxxxxxxxxxxxY
# Telegram Chat ID
TG_CHATID=13xxxx741
# API key, see https://www.cloudflare.com/a/account/my-account,
# incorrect api-key results in E_UNAUTH error
CFKEY=d02732xxxxxxxxxxxxxxxxx689d80
# Username, eg: user@example.com
CFUSER=xxxxxx@qq.com
# Zone name, eg: example.com
CFZONE_NAME=blueskyxn.com
# Hostname to update, eg: homeserver.example.com
CFRECORD_NAME=www.blueskyxn.com
# Record type, A(IPv4)|AAAA(IPv6), default IPv4
CFRECORD_TYPE=A
# Cloudflare TTL for record, between 120 and 86400 seconds
CFTTL=120
# If required settings are missing just exit
if [ "$CFKEY" = "" ]; then
echo "Missing api-key, get at: https://www.cloudflare.com/a/account/my-account"
echo "and save in ${0} or using the -k flag"
exit 2
fi
if [ "$CFUSER" = "" ]; then
echo "Missing username, probably your email-address"
echo "and save in ${0} or using the -u flag"
exit 2
fi
if [ "$CFRECORD_NAME" = "" ]; then
echo "Missing hostname, what host do you want to update?"
echo "save in ${0} or using the -h flag"
exit 2
fi
# If the hostname is not a FQDN
if [ "$CFRECORD_NAME" != "$CFZONE_NAME" ] && ! [ -z "${CFRECORD_NAME##*$CFZONE_NAME}" ]; then
CFRECORD_NAME="$CFRECORD_NAME.$CFZONE_NAME"
echo " => Hostname is not a FQDN, assuming $CFRECORD_NAME"
fi
# Get zone_identifier & record_identifier
ID_FILE=$HOME/.cf-id_$CFRECORD_NAME.txt
if [ -f $ID_FILE ] && [ $(wc -l $ID_FILE | cut -d " " -f 1) == 4 ] \
&& [ "$(sed -n '3,1p' "$ID_FILE")" == "$CFZONE_NAME" ] \
&& [ "$(sed -n '4,1p' "$ID_FILE")" == "$CFRECORD_NAME" ]; then
CFZONE_ID=$(sed -n '1,1p' "$ID_FILE")
CFRECORD_ID=$(sed -n '2,1p' "$ID_FILE")
else
echo "Updating zone_identifier & record_identifier"
CFZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
CFRECORD_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records?name=$CFRECORD_NAME" -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
echo "$CFZONE_ID" > $ID_FILE
echo "$CFRECORD_ID" >> $ID_FILE
echo "$CFZONE_NAME" >> $ID_FILE
echo "$CFRECORD_NAME" >> $ID_FILE
fi
# Get current and old WAN ip
PRESENT_IP_FILE=$HOME/.ip_$CFRECORD_NAME.txt
if [ -f $PRESENT_IP_FILE ]; then
OLD_PRESENT_IP=`cat $PRESENT_IP_FILE`
else
echo "No file, need IP,请前往root,建立.ip_域名.txt如www.blueskyxn.com,然后填入IP"
OLD_PRESENT_IP=""
fi
# Check service failure
CHECK=$(curl -s "$PING_API/$ORG_IP/22")
if [ "$(echo $CHECK | grep "\"status\":true")" != "" ]; then
if [ "$ORG_IP" = "$OLD_PRESENT_IP" ]; then
echo "No service failure found. No DNS record update required. "
exit 0
fi
echo "No service failure found. Updating DNS to $ORG_IP"
RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" \
-H "X-Auth-Email: $CFUSER" \
-H "X-Auth-Key: $CFKEY" \
-H "Content-Type: application/json" \
--data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$ORG_IP\", \"ttl\":$CFTTL}")
curl -s "https://api.telegram.org/bot$TG_BOT_TOKEN/sendMessage?chat_id=$TG_CHATID&text=No service failure found. Updating DNS record $CFZONE_NAME to $ORG_IP"
echo $ORG_IP > $PRESENT_IP_FILE
else
if [ "$FAIL_IP" = "$OLD_PRESENT_IP" ]; then
echo "Service failure found. No DNS record update required. "
exit 0
fi
echo "Service failure found. Updating DNS to $FAIL_IP"
RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CFZONE_ID/dns_records/$CFRECORD_ID" \
-H "X-Auth-Email: $CFUSER" \
-H "X-Auth-Key: $CFKEY" \
-H "Content-Type: application/json" \
--data "{\"id\":\"$CFZONE_ID\",\"type\":\"$CFRECORD_TYPE\",\"name\":\"$CFRECORD_NAME\",\"content\":\"$FAIL_IP\", \"ttl\":$CFTTL}")
curl -s "https://api.telegram.org/bot$TG_BOT_TOKEN/sendMessage?chat_id=$TG_CHATID&text=Service failure found. Updating DNS record $CFZONE_NAME to $FAIL_IP"
echo $FAIL_IP > $PRESENT_IP_FILE
fi
if [ "$RESPONSE" != "${RESPONSE%success*}" ] && [ "$(echo $RESPONSE | grep "\"success\":true")" != "" ]; then
echo "Updated succesfuly!"
exit
else
echo 'Something went wrong :('
echo "Response: $RESPONSE"
exit 1
fi