DiscordWhitelist/simpleFileServer.py

98 lines
3.6 KiB
Python
Raw Normal View History

import http.server
import os
2023-08-01 20:46:41 -05:00
import re
import socketserver
2023-08-01 20:46:41 -05:00
import traceback
2023-08-22 19:45:33 -05:00
from time import sleep
from urllib.parse import urlparse
from urllib.parse import parse_qs
from sys import stderr
2023-08-01 20:46:41 -05:00
import requests
PORT = 8000
class serveRA(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
query = parse_qs(urlparse(self.path).query)
2024-10-01 18:27:04 -05:00
if "grpName" in query:
2023-08-13 17:11:45 -05:00
self.send_response(200)
self.send_header("Content-type", "text")
self.end_headers()
2024-10-01 18:27:04 -05:00
grpName = query["grpName"][0]
grpfile = "./"
if os.path.exists(f"./wlgrps/{grpName}.cfg"):
grpfile += f"wlgrps/{grpName}.cfg"
elif os.path.exists(f"./extgrps/{grpName}.cfg"):
grpfile += f"extgrps/{grpName}.cfg"
else:
print("could not find admins file!", file=stderr)
return
try:
2024-10-01 18:27:04 -05:00
file = open(grpfile, "rb")
firstline = file.readline().decode("utf-8")
2023-08-01 22:02:19 -05:00
file.seek(0)
2024-10-01 18:27:04 -05:00
if firstline.startswith("remotelist="):
remote = firstline.split("=")[1].strip()
response = requests.get(remote, headers={"Accept": "text/html,*/*"})
print(
f"remote list came back with status code {response.status_code}"
)
2023-08-02 22:21:26 -05:00
if response.status_code == 200:
responsetext = response.text
2024-10-01 18:27:04 -05:00
config = file.read().decode("utf-8")
confgrps = re.findall(
r"permissions/(.+)=(.+)", config, flags=re.M
)
2023-08-02 22:29:50 -05:00
baseperm = re.findall(r"permissions=(.+)", config, flags=re.M)
2024-10-01 18:27:04 -05:00
responsetext = re.sub(
r"Group=(.+):(.+)",
rf"Group=\1:{baseperm[0]}",
responsetext,
flags=re.M,
)
2023-08-02 22:21:26 -05:00
for congrp in confgrps:
2024-10-01 18:27:04 -05:00
responsetext = re.sub(
rf"^Group=({congrp[0]}):(.+)",
rf"Group=\1:{congrp[1]}",
responsetext,
flags=re.M,
)
self.wfile.write(responsetext.encode("utf-8"))
with open(f"./wlgrps/backup-{grpName}.cfg", "wb") as backupfile:
backupfile.write(responsetext.encode("utf-8"))
2023-09-07 19:00:12 -05:00
backupfile.close()
2023-08-02 22:21:26 -05:00
else:
2024-10-01 18:27:04 -05:00
backupfile = open(f"./wlgrps/backup-{grpName}.cfg", "rb")
2023-08-02 22:21:26 -05:00
self.copyfile(backupfile, self.wfile)
backupfile.close()
2023-08-01 20:46:41 -05:00
else:
self.copyfile(file, self.wfile)
file.close()
except:
2024-10-01 18:27:04 -05:00
print("failed to serve file!", file=stderr)
2023-08-02 22:25:15 -05:00
print(traceback.format_exc(), file=stderr)
2023-08-13 17:08:10 -05:00
else:
2024-10-01 18:27:04 -05:00
self.send_error(
400, "please specify the group to pull from", "no grpName specified!"
)
return
def startServer():
handler = serveRA
2023-08-23 21:42:22 -05:00
while True:
print("trying to start...")
2023-08-22 19:45:33 -05:00
try:
2023-09-07 19:00:12 -05:00
with socketserver.ThreadingTCPServer(("", PORT), handler) as httpd:
2024-10-01 18:27:04 -05:00
print("starting server!")
2023-08-22 19:45:33 -05:00
httpd.serve_forever()
except:
print("server did not start trying again!")
sleep(5)
startServer()