-
Notifications
You must be signed in to change notification settings - Fork 7
/
update.sh
74 lines (63 loc) · 2.96 KB
/
update.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
#!/bin/bash
set -u
# settings
# Login information of freenom.com
# Open DNS management page in your browser.
# URL vs settings:
# https://my.freenom.com/clientarea.php?managedns={freenom_domain_name}&domainid={freenom_domain_id}
source $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/config
BASE_URL="https://my.freenom.com"
CAREA_URL="$BASE_URL/clientarea.php"
LOGIN_URL="$BASE_URL/dologin.php"
LOGOUT_URL="$BASE_URL/logout.php"
MANAGED_URL="$CAREA_URL?managedns=$freenom_domain_name&domainid=$freenom_domain_id"
GET_IP_URL="https://api.ipify.org/"
LOGGER_SCRIPT=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/log.sh
SYNOLOGY_EVENT_SCRIPT=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/synology_event.sh
# get current ip address
current_ip="$(curl -s "$GET_IP_URL")"
if [ -z "$current_ip" ]; then
$LOGGER_SCRIPT continue "Could not get current IP address."
$SYNOLOGY_EVENT_SCRIPT "{'%DDNS_HOST_NAME%':'$freenom_domain_name','%EXTERNAL_IP%':'$current_ip','%DDNS_NAME%':'freenom','%FAIL_REASON%':'Could not get current IP address.'}"
exit 1
fi
cookie_file=$(mktemp)
cleanup() {
$LOGGER_SCRIPT continue "Cleanup"
rm -f "$cookie_file"
}
trap cleanup INT EXIT TERM
$LOGGER_SCRIPT continue "Login"
loginResultUrl=$(curl --compressed -kLs -o /dev/null -c "$cookie_file" \
-F "username=$freenom_email" -F "password=$freenom_passwd" \
-e "$CAREA_URL" \
-w %{url_effective} \
"$LOGIN_URL" 2>&1)
if [ ! -s "$cookie_file" ]; then
$LOGGER_SCRIPT continue "Login failed: empty cookie."
$SYNOLOGY_EVENT_SCRIPT "{'%DDNS_HOST_NAME%':'$freenom_domain_name','%EXTERNAL_IP%':'$current_ip','%DDNS_NAME%':'freenom','%FAIL_REASON%':'Login failed: empty cookie.'}"
exit 1
fi
if echo "$loginResultUrl" | grep "/clientarea.php?incorrect=true"; then
$LOGGER_SCRIPT continue "Login failed."
$SYNOLOGY_EVENT_SCRIPT "{'%DDNS_HOST_NAME%':'$freenom_domain_name','%EXTERNAL_IP%':'$current_ip','%DDNS_NAME%':'freenom','%FAIL_REASON%':'Login failed.'}"
exit 1
fi
$LOGGER_SCRIPT continue "Update $current_ip to domain($freenom_domain_name)"
updateResult=$(curl --compressed -k -L -b "$cookie_file" \
-e "$MANAGED_URL" \
-F "dnsaction=modify" \
-F "records[0][line]=" \
-F "records[0][type]=A" \
-F "records[0][name]=" \
-F "records[0][ttl]=14440" \
-F "records[0][value]=$current_ip" \
"$MANAGED_URL" 2>&1)
if ! echo "$updateResult" | grep -q "name=\"records\[0\]\[value\]\" value=\"$current_ip\""; then
$LOGGER_SCRIPT continue "Update failed." 1>&2
$SYNOLOGY_EVENT_SCRIPT "{'%DDNS_HOST_NAME%':'$freenom_domain_name','%EXTERNAL_IP%':'$current_ip','%DDNS_NAME%':'freenom','%FAIL_REASON%':'Update failed.'}"
exit 1
fi
$LOGGER_SCRIPT continue "Logout"
curl --compressed -k -b "$cookie_file" "$LOGOUT_URL" > /dev/null 2>&1
exit 0