2021-02-07 00:21:58 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
2021-02-11 19:03:31 +00:00
|
|
|
from urllib.parse import urlparse
|
2021-02-07 00:21:58 +00:00
|
|
|
import igmirror
|
2021-02-11 19:03:31 +00:00
|
|
|
import json
|
2021-02-07 01:56:08 +00:00
|
|
|
import sys
|
2021-02-08 20:16:06 +00:00
|
|
|
import os
|
2021-02-07 00:21:58 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
class MyServer(BaseHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
2021-02-11 19:03:31 +00:00
|
|
|
_ = urlparse(self.path)
|
|
|
|
qs = _.query
|
|
|
|
path = _.path.strip().strip('/')
|
2021-02-07 00:21:58 +00:00
|
|
|
parts = path.split('/')
|
2021-02-10 18:07:03 +00:00
|
|
|
|
2021-02-08 20:16:06 +00:00
|
|
|
if len(parts) == 1:
|
2021-02-11 19:03:31 +00:00
|
|
|
response(self, 200, 'html')
|
|
|
|
action = parts[0].lower()
|
2021-02-10 18:07:03 +00:00
|
|
|
|
2021-02-11 19:03:31 +00:00
|
|
|
if False:
|
|
|
|
pass
|
2021-02-10 18:07:03 +00:00
|
|
|
|
|
|
|
# list accounts (plain text)
|
2021-02-11 19:03:31 +00:00
|
|
|
elif action == 'list':
|
2021-02-08 20:16:06 +00:00
|
|
|
accounts = os.listdir('./db/accounts')
|
|
|
|
for acc in sorted(set(accounts)):
|
2021-02-11 19:03:31 +00:00
|
|
|
echo(self, acc)
|
2021-02-11 19:16:27 +00:00
|
|
|
return 0
|
2021-02-10 18:07:03 +00:00
|
|
|
|
|
|
|
# lists accounts on a pretty HTML + CSS
|
|
|
|
# making sure there is no XSS possible on account names
|
2021-02-11 19:03:31 +00:00
|
|
|
elif action == 'mirrors':
|
2021-02-11 19:13:27 +00:00
|
|
|
html = igmirror.pixelfed_htmlfill_mirrors( igmirror.template('mirrors') )
|
2021-02-11 19:16:27 +00:00
|
|
|
return echo(self, html)
|
2021-02-10 18:07:03 +00:00
|
|
|
|
2021-02-11 19:16:27 +00:00
|
|
|
return echo(self, {'status': 'error', 'message': '1st parameter action not defined: {}'.format(action)})
|
2021-02-10 18:07:03 +00:00
|
|
|
|
|
|
|
if len(parts) == 2:
|
2021-02-11 19:03:31 +00:00
|
|
|
response(self, 200, 'json')
|
|
|
|
acc_id = parts[0]
|
2021-02-10 18:07:03 +00:00
|
|
|
action = parts[1].lower()
|
2021-02-11 19:03:31 +00:00
|
|
|
|
|
|
|
if acc_id == '*':
|
2021-02-10 18:07:03 +00:00
|
|
|
if False:
|
|
|
|
pass
|
2021-02-11 19:08:05 +00:00
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'update':
|
2021-02-07 19:13:47 +00:00
|
|
|
igmirror.update_allaccounts_async()
|
2021-02-11 19:08:05 +00:00
|
|
|
return echo(self, {'status': 'ok', 'message': 'All accounts update asyncronous started!'})
|
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'login':
|
|
|
|
igmirror.pixelfed_loginall_async()
|
2021-02-11 19:08:05 +00:00
|
|
|
return echo(self, {'status': 'ok', 'message': 'All accounts login asyncronous started!'})
|
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'logout':
|
|
|
|
igmirror.pixelfed_logoutall_async()
|
2021-02-11 19:08:05 +00:00
|
|
|
return echo(self, {'status': 'ok', 'message': 'All accounts logout asyncronous started!'})
|
|
|
|
|
2021-02-07 13:55:02 +00:00
|
|
|
else:
|
|
|
|
# make sure account name contains only safe characters
|
|
|
|
# i think IG usernames can only have this characters:
|
2021-02-10 18:07:03 +00:00
|
|
|
if False:
|
|
|
|
pass
|
2021-02-11 19:03:31 +00:00
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'add':
|
2021-02-11 19:03:31 +00:00
|
|
|
igmirror.add_igaccount(acc_id)
|
|
|
|
return echo(self, {'status': 'ok', 'message': 'New mirror account added to Pixelfed!'})
|
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'update':
|
2021-02-11 19:03:31 +00:00
|
|
|
igmirror.update_igaccount_async(acc_id)
|
|
|
|
return echo(self, {'status': 'ok', 'message': 'Account update process started asyncronously'})
|
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'login':
|
2021-02-11 19:03:31 +00:00
|
|
|
igmirror.pixelfed_login(acc_id, True)
|
|
|
|
return echo(self, {'status': 'ok', 'message': 'Account logged in to Pixelfed'})
|
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'logout':
|
2021-02-11 19:03:31 +00:00
|
|
|
igmirror.pixelfed_logout(acc_id)
|
|
|
|
return echo(self, {'status': 'ok', 'message': 'Account logged out of Pixelfed'})
|
|
|
|
|
2021-02-10 18:07:03 +00:00
|
|
|
elif action == 'nuke':
|
2021-02-11 19:03:31 +00:00
|
|
|
igmirror.delete_statuses(acc_id)
|
|
|
|
return echo(self, {'status': 'ok', 'message': 'Account nuked successfully'})
|
2021-02-10 18:07:03 +00:00
|
|
|
|
2021-02-11 19:03:31 +00:00
|
|
|
return echo(self, {'status': 'error', 'message': '2nd parameter action not configured: {}'.format(action)})
|
2021-02-10 18:07:03 +00:00
|
|
|
|
2021-02-11 19:03:31 +00:00
|
|
|
if len(parts) == 3:
|
|
|
|
response(self, 200, 'json')
|
|
|
|
acc_id = parts[0]
|
|
|
|
action = parts[1].lower()
|
|
|
|
key = parts[2].lower()
|
|
|
|
|
|
|
|
if False:
|
|
|
|
pass
|
|
|
|
|
|
|
|
elif action == 'cfg':
|
|
|
|
status, message = igmirror.account_config(acc_id, key, qs)
|
|
|
|
status = 'ok' if status else 'error'
|
|
|
|
return echo(self, {'status': status, 'message': message})
|
|
|
|
|
|
|
|
return echo(self, {'status': 'error', 'message': '2nd parameter action not configured: {}'.format(arg2)})
|
|
|
|
|
|
|
|
response(self, 400, 'json')
|
|
|
|
return echo(self, {'status': 'error', 'message': 'Parameters are not correct. Check out the API documentation'})
|
|
|
|
|
|
|
|
def response(obj, code, typ):
|
|
|
|
obj.send_response(code)
|
|
|
|
typ = typ.lower().strip()
|
|
|
|
if typ == 'html':
|
|
|
|
obj.send_header('Content-Type', 'text/html')
|
|
|
|
elif typ == 'json':
|
|
|
|
obj.send_header('Content-Type', 'application/json')
|
|
|
|
obj.end_headers()
|
|
|
|
|
|
|
|
def echo(obj, what, newline=True):
|
|
|
|
if type(what) is dict or type(what) is list:
|
|
|
|
what = json.dumps(what)
|
|
|
|
if newline:
|
|
|
|
what = what + '\n'
|
|
|
|
obj.wfile.write(bytes(what, "utf-8"))
|
2021-02-07 00:21:58 +00:00
|
|
|
|
2021-02-07 01:56:08 +00:00
|
|
|
if __name__ == "__main__":
|
2021-02-10 18:07:03 +00:00
|
|
|
addr = '0.0.0.0'
|
2021-02-07 01:56:08 +00:00
|
|
|
port = 8080
|
2021-02-10 18:16:49 +00:00
|
|
|
|
2021-02-07 01:56:08 +00:00
|
|
|
if len(sys.argv) > 1:
|
2021-02-10 18:16:49 +00:00
|
|
|
arg = sys.argv[1].strip()
|
|
|
|
if ':' in arg:
|
|
|
|
ps = arg.split(':')
|
|
|
|
addr = ps[0]
|
|
|
|
port = int(ps[1])
|
|
|
|
else:
|
|
|
|
port = int(arg)
|
2021-02-10 18:07:03 +00:00
|
|
|
|
|
|
|
webServer = HTTPServer((addr, port), MyServer)
|
|
|
|
print("Server started http://%s:%s" % (addr, port))
|
2021-02-07 00:21:58 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
webServer.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
webServer.server_close()
|
|
|
|
print("Server stopped.")
|