2023-03-04 22:32:26 -06:00
|
|
|
import http.server
|
2023-05-08 13:19:11 -05:00
|
|
|
import os
|
2023-08-01 20:46:41 -05:00
|
|
|
import re
|
2023-03-04 22:32:26 -06:00
|
|
|
import socketserver
|
2023-08-01 20:46:41 -05:00
|
|
|
import traceback
|
|
|
|
import urllib.request
|
2023-03-04 22:32:26 -06:00
|
|
|
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
|
|
|
|
|
2023-03-04 22:32:26 -06:00
|
|
|
PORT = 8000
|
|
|
|
|
|
|
|
|
|
|
|
class serveRA(http.server.SimpleHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
|
|
query = parse_qs(urlparse(self.path).query)
|
|
|
|
if 'grpName' in query:
|
|
|
|
grpName = query['grpName'][0]
|
2023-05-08 13:19:11 -05:00
|
|
|
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)
|
2023-05-08 13:20:16 -05:00
|
|
|
return
|
2023-03-04 22:32:26 -06:00
|
|
|
try:
|
2023-05-08 13:19:11 -05:00
|
|
|
file = open(grpfile, 'rb')
|
2023-08-01 20:46:41 -05:00
|
|
|
firstline = file.readline().decode('utf-8')
|
2023-08-01 22:02:19 -05:00
|
|
|
file.seek(0)
|
2023-08-01 20:46:41 -05:00
|
|
|
if firstline.startswith('remotelist='):
|
|
|
|
remote = firstline.split('=')[1].strip()
|
|
|
|
response = requests.get(remote, headers={'Accept': 'text/html,*/*'})
|
2023-08-02 22:21:26 -05:00
|
|
|
print(f"remote list came back with status code {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
|
|
responsetext = response.text
|
|
|
|
config = file.read().decode('utf-8')
|
2023-08-02 22:29:50 -05:00
|
|
|
confgrps = re.findall(r"permissions/(.+)=(.+)", config, flags=re.M)
|
|
|
|
baseperm = re.findall(r"permissions=(.+)", config, flags=re.M)
|
|
|
|
responsetext = re.sub(r"Group=(.+):(.+)", fr'Group=\1:{baseperm[0]}', responsetext, flags=re.M)
|
2023-08-02 22:21:26 -05:00
|
|
|
for congrp in confgrps:
|
|
|
|
responsetext = re.sub(rf"^Group=({congrp[0]}):(.+)", rf"Group=\1:{congrp[1]}", responsetext, flags=re.M)
|
|
|
|
self.wfile.write(responsetext.encode('utf-8'))
|
|
|
|
backupfile = open(f"./wlgrps/backup-{grpName}.cfg", 'wb')
|
|
|
|
backupfile.write(responsetext.encode('utf-8'))
|
|
|
|
backupfile.close()
|
|
|
|
else:
|
|
|
|
backupfile = open(f"./wlgrps/backup-{grpName}.cfg", 'rb')
|
|
|
|
self.copyfile(backupfile, self.wfile)
|
|
|
|
backupfile.close()
|
2023-08-01 20:46:41 -05:00
|
|
|
else:
|
|
|
|
self.copyfile(file, self.wfile)
|
2023-05-08 13:19:11 -05:00
|
|
|
file.close()
|
2023-03-04 22:32:26 -06:00
|
|
|
except:
|
2023-08-13 17:08:10 -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
|
|
|
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-type", "text")
|
|
|
|
self.end_headers()
|
|
|
|
else:
|
|
|
|
self.send_response(404)
|
2023-03-04 22:32:26 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def startServer():
|
|
|
|
handler = serveRA
|
|
|
|
|
|
|
|
with socketserver.TCPServer(("", PORT), handler) as httpd:
|
|
|
|
print('starting server!')
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
|
|
|
|
|
|
startServer()
|