-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnsomatic
More file actions
170 lines (151 loc) · 4.86 KB
/
dnsomatic
File metadata and controls
170 lines (151 loc) · 4.86 KB
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
#!/bin/sh
#shellcheck disable=SC2018
#shellcheck disable=SC2019
#shellcheck source=/dev/null
#DNS-O-MATIC updater by maghuro 2020
APPNAME="DNSOMatic"
APPNAMEnorm="$(echo $APPNAME | tr 'A-Z' 'a-z')"
CFGdir="/jffs/addons/$APPNAMEnorm.d"
CFG="${CFGdir}/settings.cfg"
SCRIPTPATH="$(cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P)"
readonly CRIT="\\e[41m"
readonly ERR="\\e[31m"
readonly WARN="\\e[33m"
readonly PASS="\\e[32m"
#set -x
[ -f "/opt/bin/$APPNAMEnorm" ] || ln -s "$SCRIPTPATH/$0" "/opt/bin/$APPNAMEnorm" >/dev/null 2>&1
if [ ! -f "$CFG" ]; then
if [ ! -d "$CFGdir" ]; then
mkdir "$CFGdir"
chmod 777 "$CFGdir"
fi
printf "" > "$CFG"
fi
. "$CFG"
# $1 = message to print, $2 = log level \\\ Thanks JackYaz
Print_Output(){
logger -t "$APPNAME" "$1"
printf "\\e[1m$2%s: $1\\e[0m\\n" "$APPNAME"
}
#set vars / $1 = var name, $2 = value
setvar(){
grep -q "$1=" < "$CFG" || printf "%s=\"\"\n" "$1" >> "$CFG"
sed -i "s/^$1=.*$/$1=\"$(echo "$2" | tr -d " ")\"/" "$CFG"
. "$CFG"
}
if [ "$1" = "config" ]; then
cat "$CFG"
exit 0
fi
if [ "$1" = "set" ]; then
if [ -n "$2" ] && [ -n "$3" ]; then
var="$(echo $2 | tr 'a-z' 'A-Z')"
setvar "$var" "$3"
Print_Output "Setting \"$(echo "$var" | tr -d " ")=$(echo "$3" | tr -d " ")\"..."
exit 0
else
echo "Missing args"
exit 1
fi
fi
if [ "$1" = "cron" ]; then
if [ -z "$2" ] && [ -z "$RUNEACH" ]; then
Print_Output "Please use \"$APPNAMEnorm cron X\" where X is the number in minutes for cronjob" "$ERR"
exit 1
elif [ -n "$2" ]; then
setvar RUNEACH "$2"
fi
cru a "$APPNAME" "*/$RUNEACH * * * * $0"
Print_Output "Creating cronjob to run every $RUNEACH minutes..." "$WARN"
#shift 2
cru a ${APPNAME}_first "$(date -D '%s' +'%M %H %d %m %a' -d $(($(date +%s)+200))) $0"
exit 0
fi
if [ "$ABUSE" = "1" ]; then
setvar ABUSE 2
Print_Output "Last update was abusive, skipping this one..."
exit 0
elif [ "$ABUSE" = "2" ]; then
setvar ABUSE 0
Print_Output "Last update was abusive, skipping this one also..."
exit 0
fi
missing=0
for vars in USERNAME PASSWORD HOSTNAME; do
if ! grep -q "$vars=" < "$CFG"; then
missing=1
Print_Output "Please use \"$APPNAMEnorm set $vars your_$(echo $vars | tr 'A-Z' 'a-z')\" before first run" "$CRIT"
fi
done
[ "$missing" = 1 ] && exit 1
#check if curl (for the sake of...) and jq exists
FAILOVER=0
for app in curl jq; do
if ! (which "$app" >/dev/null 2>&1); then
Print_Output "$app not found, installing..." "$CRIT"
opkg install "$app" >/dev/null 2>&1
if ! (which "$app" >/dev/null 2>&1); then
Print_Output "Can't download $app, exiting as it is required..." "$CRIT"
which jq >/dev/null 2>&1 || FAILOVER=1
fi
fi
done
if [ "$1" = "init" ]; then
Print_Output "Waiting 30 sec for first check..." "$WARN"
sleep 30s
fi
if [ "$1" = "force" ]; then
setvar CURRENTIP ""
setvar CURRENTCOUNTRY ""
fi
which curl >/dev/null 2>&1 || exit 1
if [ -n "$2" ]; then
NEWIP="$2"
NEWCOUNTRY="$(echo "$3" | tr 'a-z' 'A-Z')"
elif [ "$FAILOVER" != 1 ]; then
AIRVPN="$(curl -fs4 --retry 1 https://airvpn.org/api/whatismyip/)"
NEWIP="$(jq -n "$AIRVPN" | jq -j .ip)"
NEWCOUNTRY="$(jq -n "$AIRVPN" | jq -j .geo.code | tr 'a-z' 'A-Z')"
fi
[ -z "$NEWCOUNTRY" ] && NEWCOUNTRY="EARTH"
if [ -z "$NEWIP" ] || [ "$NEWIP" = "null" ]; then
Print_Output "Can't retrieve IP Address from main source, trying failover..." "$ERR"
NEWIP="$(curl -fs4 -- retry 1 https://api4.my-ip.io/ip)" #failover
NEWCOUNTRY="FAILOVER"
fi
if [ -z "$NEWIP" ] || [ "$NEWIP" = "null" ]; then
setvar CURRENTIP ""
setvar CURRENTCOUNTRY ""
Print_Output "Can't retrieve IP Address, stopping..." "$ERR"
exit 1
else
if [ "$NEWIP" != "$CURRENTIP" ]; then
UPDATE="$(curl -fs4 --retry 8 -u "$USERNAME":"$PASSWORD" https://updates.dnsomatic.com/nic/update?hostname="$HOSTNAME"\&wildcard=NOCHG\&mx=NOCHG\&backmx=NOCHG\&myip="$NEWIP")"
#if you need to curl another url to update another service, you can do it here, for example:
#curl -fs4 --retry 8 "https://link-ip.nextdns.io/confid/presharedkey" -o /dev/null #NextDNS linked IP
####
if [ "$UPDATE" = "good $NEWIP" ] || [ "$UPDATE" = "nochg $NEWIP" ]; then
if [ "$1" = "force" ]; then
Print_Output "Forcing IP update to $NEWIP/$NEWCOUNTRY" "$PASS"
elif [ -z "$CURRENTIP" ]; then
Print_Output "Updating IP to $NEWIP/$NEWCOUNTRY" "$PASS"
else
Print_Output "Updating IP from $CURRENTIP/$CURRENTCOUNTRY to $NEWIP/$NEWCOUNTRY" "$PASS"
fi
setvar CURRENTIP "$NEWIP"
setvar CURRENTCOUNTRY "$NEWCOUNTRY"
elif [ "$UPDATE" = "abuse" ]; then
setvar CURRENTIP ""
setvar CURRENTCOUNTRY ""
setvar ABUSE 1
Print_Output "Abusing! Nothing updated. Next two crons will be skipped and next update forced" "$ERR"
else
setvar CURRENTIP ""
setvar CURRENTCOUNTRY ""
Print_Output "Error while updating: \"$UPDATE\". Next update will be forced" "$ERR"
fi
else
[ "$PRINTNOTH" = 1 ] && Print_Output "Nothing to update. Current IP: $CURRENTIP/$CURRENTCOUNTRY" "$PASS"
fi
fi
exit 0