This repository was archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyadhu
executable file
·235 lines (202 loc) · 5.06 KB
/
yadhu
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
CONFIG="/etc/yadhu"
YELLOW="\033[1;33m"
RED="\033[0;31m"
ENDCOLOR="\033[0m"
if [ $USER != root ]; then
echo -e $RED"Error: must be run as root"
echo -e $YELLOW"Exiting..."$ENDCOLOR
exit 0
fi
function get_json(){
ret=$(echo $1 | grep -Po '"'$2'":.*?[^\\]",?')
ret=$(echo $ret | cut -f2- -d':' | tr -d '",')
echo $ret
}
function gen_conftemplate(){
cat > Yadhu.conf <<EOF
# Please generate a pair of AppKey and AppSecrect
# at https://eu.api.ovh.com/createApp/
AppKey=
AppSecrect=
# Domain name.
Domain=
# Subdomain list separated by commas. Leave empty to change all subdomains.
Subdomains=
# Web to get the public ip
Web=http://checkip.dyndns.com
# Pattern to parse public ip
WebPatt="([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)"
# Check Interval in seconds
Check=18000
# Consumer key. Don't edit, controlled by Yadhu
__ck=
EOF
}
function set_ck(){
sed -i "s/__ck=.*/__ck=\"$1\"/" $CONFIG
__ck=$1
}
function request_ck(){
res=$(curl -s -X POST \
-H "X-Ovh-Application: $AppKey" \
-H "Content-type: application/json" \
-d '{"accessRules": [
{"method": "GET", "path": "/domain/zone/*"},
{"method": "PUT", "path": "/domain/zone/*"}
]}' \
https://eu.api.ovh.com/1.0/auth/credential)
ck=$(get_json "$res" "consumerKey")
if [ -z $ck ]; then
err=$(get_json "$ret" "message")
logger -t "Yadhu" -s "Error: $err"
exit 1
fi
url=$(get_json "$res" "validationUrl")
echo "Please to authenticate Yadhu visit:"
echo "$url"
read -n1 -p "After press any key to continue or ctrl-c to cancel."
set_ck "$ck"
}
function get_sign(){
AS=$1
CK=$2
Method=$3
Body=$4
TS=$5
Data=$6
ret='$1$'$(echo -n "$AS+$CK+$Method+$Body+$Data+$TS" | sha1sum)
ret=$(echo $ret | cut -d' ' -f1)
echo $ret
}
function get_ids(){
zone="https://eu.api.ovh.com/1.0/domain/zone/$Domain/dynHost/record"
if [ -z "$Subdomains" ]; then
TS=$(date +%s)
Sig=$(get_sign $AppSecrect $__ck GET $zone $TS)
ret=$(curl -s -X GET -H "X-Ovh-Application:$AppKey" \
-H "X-Ovh-Timestamp:$TS" \
-H "X-Ovh-Signature:$Sig" \
-H "X-Ovh-Consumer:$__ck" \
$zone)
err=$(get_json "$ret" "message")
if [ -n "$err" ]; then
logger -t "Yadhu" -s "Error: $err"
exit 1
fi
ids="$ids,$ret"
else
subd=$(echo $Subdomains | tr -s ',' ' ')
ids=""
for sd in $subd; do
Body="$zone?subDomain=$sd"
TS=$(date +%s)
Sig=$(get_sign $AppSecrect $__ck GET $Body $TS)
ret=$(curl -s -X GET -H "X-Ovh-Application:$AppKey" \
-H "X-Ovh-Timestamp:$TS" \
-H "X-Ovh-Signature:$Sig" \
-H "X-Ovh-Consumer:$__ck" \
$Body)
err=$(get_json "$ret" "message")
if [ -n "$err" ]; then
logger -t "Yadhu" -s "Error: $err"
exit 1
fi
ids="$ids,$ret"
done
fi
echo $(echo -n $ids | tr -d '[]' | tr ',' ' ')
}
function get_dh_info(){
TS=$(date +%s)
Sig=$(get_sign $AppSecrect $__ck GET $1 $TS)
ret=$(curl -s -X GET -H "Content-Type:application/json" \
-H "X-Ovh-Application:$AppKey" \
-H "X-Ovh-Timestamp:$TS" \
-H "X-Ovh-Signature:$Sig" \
-H "X-Ovh-Consumer:$__ck" \
$1)
err=$(get_json "$ret" "message")
if [ -n "$err" ]; then
logger -t "Yadhu" -s "Error: $err"
exit 1
fi
echo "$ret"
}
function set_dh(){
zone="https://eu.api.ovh.com/1.0/domain/zone/$Domain/dynHost/record/"
IP=$(curl -s "$Web" | grep -E -o "$WebPatt")
for id in $1; do
Body=$zone$id
reg="$(get_dh_info $Body)"
domain_url=$(get_json "$reg" "subDomain")\.$(get_json "$reg" "zone")
domain_cip=$(get_json "$reg" "ip")
if [ "$IP" == "$domain_cip" ]; then
logger -t "Yadhu" -s "Skiping $domain_url due IP is Already setted. ($IP)"
continue
fi
Data='{"ip":"'$IP'"}'
TS=$(date +%s)
Sig=$(get_sign $AppSecrect $__ck PUT $Body $TS $Data)
ret=$(curl -s -X PUT -H "Content-Type:application/json" \
-H "X-Ovh-Application:$AppKey" \
-H "X-Ovh-Timestamp:$TS" \
-H "X-Ovh-Signature:$Sig" \
-H "X-Ovh-Consumer:$__ck" \
-d "$Data" \
$Body)
err=$(get_json "$ret" "message")
if [ -n "$err" ]; then
logger -t "Yadhu" -s "Error: $err"
exit 1
fi
logger -t "Yadhu" -s "$domain_url updated with IP: $IP"
done
}
function finish(){
[[ $pid ]] && kill $pid
rm /var/run/yadhu.pid
}
if [ "$1" == "-d" ]; then
trap finish EXIT
fi
# Generate config template if needed
if [ ! -e "${CONFIG}" ]; then
if [ "$1" != "-d" ]; then
gen_conftemplate
logger -t "Yadhu" -s "Config template generated, please modify with propper values."
else
logger -t "Yadhu" -s "Error: Config file not found."
fi
exit 1
fi
# LOAD THE CONFIG FILE
source $CONFIG
if [ -z "$AppKey" -o -z "$AppSecrect" ]; then
logger -t "Yadhu" -s "Please generate a pair of AppKey and AppSecrect at:"
logger -t "Yadhu" -s "\"https://eu.api.ovh.com/createApp/\" and setup the config file."
exit 1
fi
if [ -z "$__ck" ]; then
if [ "$1" != "-d" ]; then
request_ck
else
logger -t "Yadhu" -s "Error: Consumer Key not set."
exit 1
fi
fi
if [ -z "$Domain" ]; then
logger -t "Yadhu" -s "Error: Please set a domain name at the config file."
exit 1
fi
while true; do
ids=""
ids=$(get_ids)
set_dh "$ids"
[ "$1" == "-d" ] || break;
pid=
sleep "$Check"s & pid=$!
wait
pid=
done
exit 0