forked from myanonamouse/seedboxapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.sh
More file actions
executable file
·119 lines (102 loc) · 3.37 KB
/
wrapper.sh
File metadata and controls
executable file
·119 lines (102 loc) · 3.37 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
#!/bin/sh
if [ "x$DEBUG" != "x" ]
then
set -x
fi
# The directory to store the two state files - /config is a docker standard
CACHEFILE=/tmp/MAM.ip
COOKIEFILE=/config/MAM.cookies
# HELPER: Ensure mam_id exists in the cookie file
ensure_mam_id() {
if [ "x$mam_id" == "x" ]; then
echo "Error: mam_id environment variable is missing."
exit 1
fi
# Check if mam_id is missing from the file
grep mam_id "${COOKIEFILE}" > /dev/null 2>/dev/null
if [ $? -ne 0 ]; then
# Append the mam_id manually so curl can use it next time
# Expiry set to year 2038 to prevent issues
printf ".myanonamouse.net\tTRUE\t/\tFALSE\t2147483647\tmam_id\t%s\n" "${mam_id}" >> "${COOKIEFILE}"
fi
}
if [ "x$interval" == "x" ]
then
echo Running with default interval of 1 minute
SLEEPTIME=60
else
if [ "$interval" -lt "1" ]
then
echo Cannot set interval to less than 1 minute
echo " => Running with default interval of 60 seconds"
SLEEPTIME=60
else
echo Running with an interval of $interval minute\(s\)
SLEEPTIME=`expr $interval \* 60`
fi
fi
# 1. PREPARE SESSION
# Ensure file exists and has ID before we start
touch ${COOKIEFILE}
ensure_mam_id
# 2. VALIDATE SESSION
# We use the file for both input (-b) and output (-c)
curl -s -b ${COOKIEFILE} -c ${COOKIEFILE} https://t.myanonamouse.net/json/dynamicSeedbox.php > /tmp/MAM.output
# CRITICAL FIX: Restore mam_id if curl wiped it
ensure_mam_id
grep '"Success":true' /tmp/MAM.output > /dev/null 2>/dev/null
if [ $? -ne 0 ]
then
echo "Login failed. Response: `cat /tmp/MAM.output`"
echo "Please check your mam_id."
exit 1
else
echo "Session is valid."
fi
OLDIP=`cat $CACHEFILE 2>/dev/null`
while [ $PPID -ne 1 ]
do
OLDIP=`cat $CACHEFILE 2>/dev/null`
NEWIP=`curl -s ip4.me/api/ | md5sum - | awk '{print $1}'`
if [ "x$DEBUG" != "x" ]
then
echo Current IP: `curl -s ip4.me/api/`
fi
# Check to see if the IP address has changed
if [ "${OLDIP}" != "${NEWIP}" ]
then
echo New IP detected
curl -s -b $COOKIEFILE -c $COOKIEFILE https://t.myanonamouse.net/json/dynamicSeedbox.php > /tmp/MAM.output
# CRITICAL FIX: Restore mam_id if curl wiped it
ensure_mam_id
grep -E 'No Session Cookie|Invalid session' /tmp/MAM.output > /dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo response: `cat /tmp/MAM.output`
echo Current cookie file is invalid.
# We don't exit here anymore, we just loop and let ensure_mam_id fix it next time
fi
# If that command worked, update the CACHEFILE
grep '"Success":true' /tmp/MAM.output > /dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo Response: \"`cat /tmp/MAM.output`\"
echo $NEWIP > $CACHEFILE
OLDPID=$NEWIP
else
grep 'Last change too recent' /tmp/MAM.output > /dev/null 2>/dev/null
if [ $? -eq 0 ]
then
echo Last update too recent - sleeping
else
echo response: `cat /tmp/MAM.output`
echo Invalid response
fi
fi
else
echo "No IP change detected: `date`"
fi
sleep $SLEEPTIME
# Empty the IP file if it has not been rotated for more than 30 days
find $CACHEFILE -mtime +30 -delete
done