-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhop.py
128 lines (111 loc) · 4.37 KB
/
hop.py
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
#!/usr/bin/env python
# Abusive IP Grabber from MageMojo HOP 1.1
########## GLOBAL NEEDED THINGS | DO NOT CHANGE ##########
import os
import sys
import os.path
from os import path
import ipaddress
import socket
import argparse
import subprocess
import re
wlist=[] #declare list
# Some Pretty Colors for logs
NC='\033[0m' # No Color
RED='\033[0;31m'
GREEN='\033[0;32m'
# Make this file if non-existant
if str(path.exists("/srv/.nginx/white.list")) == "False":
touchit="touch /srv/.nginx/white.list"
os.system(touchit)
# Check if update needed
gitcopy = "wget -q -O /srv/.nginx/hop.check https://raw.githubusercontent.com/magemojo/mmstrace/master/hop.py"
os.system(gitcopy)
hopnow = open("/srv/.nginx/hop.py", "r").readlines()[1]
hopnew = open("/srv/.nginx/hop.check", "r").readlines()[1]
if hopnew == hopnow:
print("Update not needed")
else:
if "MageMojo" in hopnew:
if "MageMojo" in hopnow:
print("Update Needed!")
getit = "wget -q https://raw.githubusercontent.com/magemojo/mmstrace/master/hopupdate.py"
os.system(getit)
#runit = "python3 ./hopupdate.py"
runit = subprocess.Popen("python3 /srv/.nginx/hopupdate.py")
os.system(runit)
sys.exit()
# Get UUID
parser = argparse.ArgumentParser()
parser.add_argument('-u','--uuid', action='store', dest='uuid', type=str, help="uuid of the instance")
args = parser.parse_args()
if args.uuid:
# Put white.list in a pythonlist if not empty
filesize = os.path.getsize("/srv/.nginx/white.list")
if filesize == 0:
print("No whitelist found or empty")
else:
with open("/srv/.nginx/white.list") as w:
contents = w.readlines()
for line in contents:
line = line.replace("\n", "")
wlist.append(line)
# URL
uuid = str(args.uuid)
url = "https://magemojo.com/mojo_scripts/banlist.php?uuid={" + uuid + "}"
#print(url)
# GET IP list
getlist = "cd /tmp/;curl " + url + " -o \'#1.list\'"
os.system(getlist)
# Check file exists or is not empty
filesize = os.path.getsize("/tmp/" + uuid + ".list")
if filesize == 0:
print(RED + " Something is wrong. Source list not found or empty" + NC)
else:
# Test if file was a timeout error
pattern = re.compile("[A-Za-z]+")
badfile = "false"
# if letters found in file
for line in open("/tmp/" + uuid + ".list"):
if "error" in line:
print("Can't use file. It has an error: " + line)
badfile = "true"
elif "cloudfront" in line:
print("Can't use file. It has an error: " + line)
badfile = "true"
if badfile == "false":
# Build awk to add deny nginx format
do = "awk \'{print \"deny \"$0\";\"}\' /tmp/" + uuid + ".list"
# Make sure to ignore whitelisted IPs
for ip in wlist:
try:
# Check if real IP
if '/24' in ip:
ipaddress.ip_network(ip)
else:
socket.inet_aton(ip)
do = do + " | grep -v " + str(ip)
except socket.error:
print(RED + ip + " is not a valid IP in white.list" + NC) # Not legal
# Tell it where to put them
do = do + " | uniq > /srv/.nginx/server_level/" + uuid + ".conf"
# Run it
os.system(do)
#print(do) ## debug
# Reload nginx to apply changes
doreload = "/usr/share/stratus/cli nginx.update"
nginxupdate = subprocess.run(["/usr/share/stratus/cli","nginx.update"], check=True)
if "returncode=0" in str(nginxupdate):
print(nginxupdate)
print(GREEN + " List good. Reloaded nginx config" + NC)
else:
print(nginxupdate)
mv = "mv /srv/.nginx/server_level/" + uuid + ".conf /srv/.nginx/server_level/" + uuid + ".failed"
print(RED + " Nginx failed to reload: reverted" + NC)
nginxupdate = subprocess.run(["/usr/share/stratus/cli","nginx.update"], check=True)
# Clean up tmp file
rm = "rm /tmp/" + uuid + ".list"
os.system(rm)
else:
print(RED + " UUID was not specified. Can not continue" + NC)