173 lines
4.5 KiB
Python
173 lines
4.5 KiB
Python
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
import igmirror
|
|
import sys
|
|
import os
|
|
import re
|
|
|
|
class MyServer(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
path = self.path.strip('/')
|
|
parts = path.split('/')
|
|
|
|
if len(parts) == 1:
|
|
|
|
self.send_response(200)
|
|
self.send_header("Content-type", "text/html")
|
|
self.end_headers()
|
|
|
|
# list accounts (plain text)
|
|
if parts[0] == 'list':
|
|
accounts = os.listdir('./db/accounts')
|
|
for acc in sorted(set(accounts)):
|
|
self.wfile.write(bytes(acc+'\n', "utf-8"))
|
|
|
|
# lists accounts on a pretty HTML + CSS
|
|
# making sure there is no XSS possible on account names
|
|
elif parts[0] == 'mirrors':
|
|
html = """<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>{instance} - Mirrors</title>
|
|
</head>
|
|
<body>
|
|
<style type="text/css">
|
|
body {
|
|
font-family: Arial;
|
|
}
|
|
div#content {
|
|
max-width: 40em;
|
|
margin: auto;
|
|
}
|
|
div#content > div:nth-child(1) {
|
|
display: flex;
|
|
margin: 2em 1em;
|
|
border-bottom: 3px solid #dbdbdb;
|
|
padding-bottom: 2em;
|
|
}
|
|
div#content > div:nth-child(1) > h3 {
|
|
margin: 0;
|
|
width: 100%;
|
|
}
|
|
div#content > div:nth-child(1) > span {
|
|
min-width: 6em;
|
|
}
|
|
div.item {
|
|
padding: 1em;
|
|
box-shadow: 0px 2px .2em #acacac;
|
|
margin-bottom: 1em;
|
|
}
|
|
div.item > h3.name {
|
|
font-weight: initial;
|
|
}
|
|
div.item > div.links {
|
|
display: flex;
|
|
height: 2.5em;
|
|
margin-top: 2em;
|
|
}
|
|
div.item > div.links > div {
|
|
display: flex;
|
|
width: 100%;
|
|
}
|
|
div.item > div.links > div > a {
|
|
margin: auto;
|
|
text-decoration: none;
|
|
}
|
|
div.item > div.links > div:nth-child(1) {
|
|
background: #ffdade;
|
|
}
|
|
div.item > div.links > div:nth-child(1) > a {
|
|
font-weight: bold;
|
|
color: #ce0680;
|
|
}
|
|
div.item > div.links > div:nth-child(2) {
|
|
background: #f2f2f2;
|
|
}
|
|
div.item > div.links > div:nth-child(2) > a {
|
|
color: #367280;
|
|
font-style: italic;
|
|
}
|
|
</style>
|
|
<div id="content">
|
|
<div>
|
|
<h3>{instance}</h3>
|
|
<span>{item_count} accounts</span>
|
|
</div>
|
|
{mirrors}
|
|
</div>
|
|
</body>
|
|
</html>
|
|
"""
|
|
html = igmirror.pixelfed_htmlfill_mirrors(html)
|
|
self.wfile.write(bytes(html, "utf-8"))
|
|
|
|
return
|
|
|
|
if len(parts) == 2:
|
|
|
|
self.send_response(200)
|
|
self.send_header("Content-type", "application/json")
|
|
self.end_headers()
|
|
|
|
# a wilcard select all accounts
|
|
what = parts[0]
|
|
action = parts[1].lower()
|
|
if what == '*':
|
|
if False:
|
|
pass
|
|
elif action == 'update':
|
|
igmirror.update_allaccounts_async()
|
|
elif action == 'login':
|
|
igmirror.pixelfed_loginall_async()
|
|
elif action == 'logout':
|
|
igmirror.pixelfed_logoutall_async()
|
|
else:
|
|
# make sure account name contains only safe characters
|
|
# i think IG usernames can only have this characters:
|
|
accname = re.sub(r'[^a-zA-Z0-9_\.]+', '', what)
|
|
if False:
|
|
pass
|
|
elif action == 'add':
|
|
igmirror.add_igaccount(accname)
|
|
elif action == 'update':
|
|
igmirror.update_igaccount_async(accname)
|
|
elif action == 'login':
|
|
igmirror.pixelfed_login(accname, True)
|
|
elif action == 'logout':
|
|
igmirror.pixelfed_logout(accname)
|
|
elif action == 'nuke':
|
|
igmirror.delete_statuses(accname)
|
|
|
|
self.wfile.write(bytes('{"status": "ok"}', "utf-8"))
|
|
return
|
|
|
|
self.send_response(400)
|
|
self.send_header("Content-type", "application/json")
|
|
self.end_headers()
|
|
self.wfile.write(bytes('{"status": "parameters are not correct"}', "utf-8"))
|
|
|
|
if __name__ == "__main__":
|
|
addr = '0.0.0.0'
|
|
port = 8080
|
|
|
|
if len(sys.argv) > 1:
|
|
arg = sys.argv[1].strip()
|
|
if ':' in arg:
|
|
ps = arg.split(':')
|
|
addr = ps[0]
|
|
port = int(ps[1])
|
|
else:
|
|
port = int(arg)
|
|
|
|
webServer = HTTPServer((addr, port), MyServer)
|
|
print("Server started http://%s:%s" % (addr, port))
|
|
|
|
try:
|
|
webServer.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
|
|
webServer.server_close()
|
|
print("Server stopped.")
|