-
Notifications
You must be signed in to change notification settings - Fork 3
/
ddns.sh
176 lines (158 loc) · 7.29 KB
/
ddns.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
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
CLOUDFLARE_API_TOKEN=
CLOUDFLARE_API_KEY=
CLOUDFLARE_RECORD_NAME=
CLOUDFLARE_RECORD_TYPE=
CLOUDFLARE_USER_MAIL=
CLOUDFLARE_ZONE_NAME=
OUTBOUND_INTERFACE=
SOCKS_ADDR=
SOCKS_PORT=
TELEGRAM_BOT_ID=
TELEGRAM_CHAT_ID=
FORCE_UPDATE=false
while getopts k:n:t:u:z:i:a:p:b:c:fr: opts; do
case ${opts} in
t) CLOUDFLARE_API_TOKEN=${OPTARG} ;;
k) CLOUDFLARE_API_KEY=${OPTARG} ;;
n) CLOUDFLARE_RECORD_NAME=${OPTARG} ;;
r) CLOUDFLARE_RECORD_TYPE=${OPTARG} ;;
u) CLOUDFLARE_USER_MAIL=${OPTARG} ;;
z) CLOUDFLARE_ZONE_NAME=${OPTARG} ;;
i) OUTBOUND_INTERFACE=${OPTARG} ;;
a) SOCKS_ADDR=${OPTARG} ;;
p) SOCKS_PORT=${OPTARG} ;;
b) TELEGRAM_BOT_ID=${OPTARG} ;;
c) TELEGRAM_CHAT_ID=${OPTARG} ;;
f) FORCE_UPDATE=true ;;
*);;
esac
done
if [ "$CLOUDFLARE_API_TOKEN" = "" ] && [ "$CLOUDFLARE_API_KEY" = "" ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: CLOUDFLARE_API_TOKEN or CLOUDFLARE_API_KEY is required.\n"
exit 2
fi
if [ "$CLOUDFLARE_API_TOKEN" != "" ] && [ "$CLOUDFLARE_API_KEY" != "" ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: CLOUDFLARE_API_TOKEN and CLOUDFLARE_API_KEY both set, CLOUDFLARE_API_KEY will be ignored.\n"
fi
if [ "$CLOUDFLARE_RECORD_NAME" = "" ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: CLOUDFLARE_RECORD_NAME is required.\n"
exit 2
fi
if [ "$CLOUDFLARE_RECORD_TYPE" = "A" ]; then
IP_API="https://ipv4.icanhazip.com"
elif [ "$CLOUDFLARE_RECORD_TYPE" = "AAAA" ]; then
IP_API="https://ipv6.icanhazip.com"
else
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Invalid record type, CLOUDFLARE_RECORD_TYPE can only be A or AAAA.\n"
exit 2
fi
if [ "$CLOUDFLARE_USER_MAIL" = "" ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: CLOUDFLARE_USER_MAIL is required.\n"
exit 2
fi
if [ "$CLOUDFLARE_ZONE_NAME" = "" ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: CLOUDFLARE_ZONE_NAME is required.\n"
exit 2
fi
if [ "$OUTBOUND_INTERFACE" != "" ]; then
CURL_INTERFACE="--interface $OUTBOUND_INTERFACE"
else
CURL_INTERFACE=""
fi
if [ "$SOCKS_ADDR" != "" ]; then
if [ "$SOCKS_PORT" != "" ] && [ "$SOCKS_PORT" -gt 0 ] && [ "$SOCKS_PORT" -lt 65536 ]; then
CURL_PROXY="-x socks5h://$SOCKS_ADDR:$SOCKS_PORT"
else
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Invalid socks server prot, it must be in 1-65535."
fi
else
CURL_PROXY=""
fi
PUBLIC_IP=`curl $CURL_INTERFACE -s $IP_API`
PUBLIC_IP_FILE=$HOME/.IP::$CLOUDFLARE_RECORD_TYPE::$CLOUDFLARE_RECORD_NAME.ddns
if [ -f $PUBLIC_IP_FILE ]; then
OLD_PUBLIC_IP=`cat $PUBLIC_IP_FILE`
else
OLD_PUBLIC_IP=""
fi
if [ "$PUBLIC_IP" = "$OLD_PUBLIC_IP" ] && [ "$FORCE_UPDATE" = false ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Public IP not changed, you can use -f to update anyway.\n"
exit 0
fi
CLOUDFLARE_ID_FILE=$HOME/.ID::$CLOUDFLARE_RECORD_TYPE::$CLOUDFLARE_RECORD_NAME.ddns
if [ -f $CLOUDFLARE_ID_FILE ] && [ $(wc -l $CLOUDFLARE_ID_FILE | cut -d " " -f 1) == 4 ] \
&& [ "$(sed -n '3,1p' "$CLOUDFLARE_ID_FILE")" == "$CLOUDFLARE_ZONE_NAME" ] \
&& [ "$(sed -n '4,1p' "$CLOUDFLARE_ID_FILE")" == "$CLOUDFLARE_RECORD_NAME" ]; then
CLOUDFLARE_ZONE_ID=$(sed -n '1,1p' "$CLOUDFLARE_ID_FILE")
CLOUDFLARE_RECORD_ID=$(sed -n '2,1p' "$CLOUDFLARE_ID_FILE")
else
if [ "$CLOUDFLARE_API_TOKEN" != "" ]; then
CLOUDFLARE_API_KEY=$CLOUDFLARE_API_TOKEN
CLOUDFLARE_ZONE_ID=$(curl $CURL_INTERFACE $CURL_PROXY -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CLOUDFLARE_ZONE_NAME" -H "X-Auth-Email: $CLOUDFLARE_USER_MAIL" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
CLOUDFLARE_RECORD_ID=$(curl $CURL_INTERFACE $CURL_PROXY -s -X GET "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records?name=$CLOUDFLARE_RECORD_NAME&type=$CLOUDFLARE_RECORD_TYPE" -H "X-Auth-Email: $CLOUDFLARE_USER_MAIL" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
printf "$CLOUDFLARE_ZONE_ID\n" > $CLOUDFLARE_ID_FILE
printf "$CLOUDFLARE_RECORD_ID\n" >> $CLOUDFLARE_ID_FILE
printf "$CLOUDFLARE_ZONE_NAME\n" >> $CLOUDFLARE_ID_FILE
printf "$CLOUDFLARE_RECORD_NAME" >> $CLOUDFLARE_ID_FILE
else
CLOUDFLARE_API_KEY=$CLOUDFLARE_API_TOKEN
CLOUDFLARE_ZONE_ID=$(curl $CURL_INTERFACE $CURL_PROXY -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$CLOUDFLARE_ZONE_NAME" -H "X-Auth-Email: $CLOUDFLARE_USER_MAIL" -H "X-Auth-Key: $CLOUDFLARE_API_KEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
CLOUDFLARE_RECORD_ID=$(curl $CURL_INTERFACE $CURL_PROXY -s -X GET "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records?name=$CLOUDFLARE_RECORD_NAME&type=$CLOUDFLARE_RECORD_TYPE" -H "X-Auth-Email: $CLOUDFLARE_USER_MAIL" -H "X-Auth-Key: $CLOUDFLARE_API_KEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
printf "$CLOUDFLARE_ZONE_ID\n" > $CLOUDFLARE_ID_FILE
printf "$CLOUDFLARE_RECORD_ID\n" >> $CLOUDFLARE_ID_FILE
printf "$CLOUDFLARE_ZONE_NAME\n" >> $CLOUDFLARE_ID_FILE
printf "$CLOUDFLARE_RECORD_NAME" >> $CLOUDFLARE_ID_FILE
fi
fi
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Updating $CLOUDFLARE_RECORD_NAME to $PUBLIC_IP...\n"
if [ "$CLOUDFLARE_API_TOKEN" != "" ]; then
CLOUDFLARE_API_RESPONSE=$(curl $CURL_INTERFACE $CURL_PROXY -o /dev/null -s -w "%{http_code}\n" -X PUT "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/$CLOUDFLARE_RECORD_ID" \
-H "X-Auth-Email: $CLOUDFLARE_USER_MAIL" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"type\":\"$CLOUDFLARE_RECORD_TYPE\",\"name\":\"$CLOUDFLARE_RECORD_NAME\",\"content\":\"$PUBLIC_IP\", \"ttl\":120}")
else
CLOUDFLARE_API_RESPONSE=$(curl $CURL_INTERFACE $CURL_PROXY -o /dev/null -s -w "%{http_code}\n" -X PUT "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/$CLOUDFLARE_RECORD_ID" \
-H "X-Auth-Email: $CLOUDFLARE_USER_MAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
--data "{\"type\":\"$CLOUDFLARE_RECORD_TYPE\",\"name\":\"$CLOUDFLARE_RECORD_NAME\",\"content\":\"$PUBLIC_IP\", \"ttl\":120}")
fi
if [ "$CLOUDFLARE_API_RESPONSE" != 200 ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Failed to update record.\n"
exit 1
else
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: $CLOUDFLARE_RECORD_NAME successfully updated to $PUBLIC_IP.\n"
printf $PUBLIC_IP > $PUBLIC_IP_FILE
if [ "$TELEGRAM_BOT_ID" != "" ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Reporting to Telegram...\n"
TELEGRAM_API_RESPONSE=`curl $CURL_INTERFACE $CURL_PROXY -o /dev/null -s -w "%{http_code}\n" "https://api.telegram.org/bot$TELEGRAM_BOT_ID/sendMessage?chat_id=$TELEGRAM_CHAT_ID&parse_mode=HTML&text=<b>DDNS%20Notification:</b>%0A$CLOUDFLARE_RECORD_TYPE%20type%20$CLOUDFLARE_RECORD_NAME%20successfully%20updated%20to%20$PUBLIC_IP"`
if [ "$TELEGRAM_API_RESPONSE" != 200 ]; then
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Report failed.\n"
exit 2
else
LOG_TIME=`date --rfc-3339 sec`
printf "$LOG_TIME: Reported successfully.\n"
exit 0
fi
else
exit 0
fi
fi