-
Notifications
You must be signed in to change notification settings - Fork 0
/
adfreetime-auto-update
executable file
·80 lines (72 loc) · 1.9 KB
/
adfreetime-auto-update
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
#!/bin/sh
#
# Ad-free time! update script
#
# This script automatically sends your current IP address to the Ad-free time! api.
# It can be used to update your IP adress via cron.
#
# Initial author of unblock-us script:
# Timo Schlueter ([email protected], www.timo.in, twitter.com/tmuuh)
#
# Autor of Ad-free time! script:
# Hans Becker
#
# Version: 1.3
# Date: 2015-06-21
#
# change into directory of script (because that's where adfreetime-username.txt is expected to be)
cd "$(dirname "$0")"
readonly userEMail=$(head -n 1 adfreetime-username.txt)
readonly logFile="/tmp/af.log"
readonly ipFile="/tmp/af-last-ip.txt"
readonly apiUrl="https://login.adfreetime.com/cron.php?email=$userEMail"
readonly curlOptions="--silent --show-error --fail --location --max-time 30"
# of you're on ddwrt add --insecure option to curlOptions
log() {
echo "$(date) $1" >> "$logFile"
echo $1
}
# get public id
readonly publicIp=$(curl $curlOptions http://checkip.dyndns.org/ | grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>')
exitCode=$?
if [ $exitCode -ne 0 ]; then
log "Getting public ip failed"
exit $exitCode
fi
# check if ip has changed
if [ -f "$ipFile" ]; then
readonly oldIp=$(head -n 1 "$ipFile")
if [ "$publicIp" == "$oldIp" ]; then
log "IP ($publicIp) hasn't change since the last execution."
exit 0
fi
fi
# Check if email is set.
if [ -z "$userEMail" ]; then
log "No email set."
exit 2
else
# Call the api
response=$(curl $curlOptions $apiUrl)
exitCode=$?
if [ $exitCode -ne 0 ]; then
log "API call failed"
exit $exitCode
fi
# Check response from api
case "$response" in
*is\ activated*)
log "IP ($publicIp) was already activated."
;;
*Success*)
log "New IP ($publicIp) was successfully activated."
;;
*)
log "Unknown response:"
log "$response"
exit 1
;;
esac
fi
printf "%s" "$publicIp" > "$ipFile"
exit 0