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
·120 lines (107 loc) · 3.08 KB
/
wrapper.sh
File metadata and controls
executable file
·120 lines (107 loc) · 3.08 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
#!/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
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
grep mam_id ${COOKIEFILE} > /dev/null 2>/dev/null
if [ $? -ne 0 ]
then
if [ "x$mam_id" == "x" ]
then
echo no mam_id, and no existing session.
exit 1
fi
echo No existing session, creating new cookie file using mam_id from environment
curl -s -b mam_id=${mam_id} -c ${COOKIEFILE} https://t.myanonamouse.net/json/dynamicSeedbox.php > /tmp/MAM.output
grep '"Success":true' /tmp/MAM.output > /dev/null 2>/dev/null
if [ $? -ne 0 ]
then
echo mam_id passed on command line is invalid
exit 1
else
grep mam_id ${COOKIEFILE} > /dev/null 2>/dev/null
if [ $? -ne 0 ]
then
echo Command successful, but failed to create cookie file.
exit 1
else
echo New session created.
fi
fi
else
curl -s -b ${COOKIEFILE} -c ${COOKIEFILE} https://t.myanonamouse.net/json/dynamicSeedbox.php > /tmp/MAM.output
grep '"Success":true' /tmp/MAM.output > /dev/null 2>/dev/null
if [ $? -ne 0 ]
then
echo response: `cat /tmp/MAM.output`
echo Current cookie file is invalid. Please delete it, set the mam_id, and restart the container.
exit 1
else
echo Session is valid
fi
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
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. Please delete it, set the mam_id, and restart the container.
exit 1
fi
# If that command worked, and we therefore got the success message
# from MAM, update the CACHEFILE for the next execution
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
exit 1
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, this will enforce session freshness.
find $CACHEFILE -mtime +30 -delete
done