2018-10-05 22:16:34 +00:00
|
|
|
#--require-config tls-api-key
|
|
|
|
#--require-config tls-api-certificate
|
|
|
|
|
|
|
|
import http.server, json, ssl, threading, uuid, urllib.parse
|
2018-10-04 15:01:13 +00:00
|
|
|
import flask
|
2018-10-04 16:09:35 +00:00
|
|
|
from src import utils
|
2018-10-04 15:01:13 +00:00
|
|
|
|
|
|
|
_bot = None
|
|
|
|
_events = None
|
|
|
|
class Handler(http.server.BaseHTTPRequestHandler):
|
2018-10-04 16:10:05 +00:00
|
|
|
timeout = 10
|
2018-10-05 22:14:32 +00:00
|
|
|
def _handle(self, method, path, data="", params={}):
|
2018-10-05 21:49:06 +00:00
|
|
|
_, _, endpoint = path[1:].partition("/")
|
|
|
|
endpoint, _, args = endpoint.partition("/")
|
|
|
|
args = list(filter(None, args.split("/")))
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
response = ""
|
|
|
|
code = 404
|
2018-10-04 16:59:24 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
hooks = _events.on("api").on(method).on(endpoint).get_hooks()
|
|
|
|
if hooks:
|
|
|
|
hook = hooks[0]
|
|
|
|
authenticated = hook.get_kwarg("authenticated", True)
|
|
|
|
key = params.get("key", None)
|
|
|
|
if authenticated and (not key or not _bot.get_setting(
|
|
|
|
"api-key-%s" % key, False)):
|
|
|
|
code = 401
|
|
|
|
else:
|
|
|
|
if path.startswith("/api/"):
|
|
|
|
response = _events.on("api").on(method).on(endpoint
|
2018-10-05 22:14:32 +00:00
|
|
|
).call_for_result(params=params, path=args, data=data)
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
if response:
|
|
|
|
response = json.dumps(response, sort_keys=True,
|
|
|
|
indent=4)
|
|
|
|
code = 200
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
self.send_response(code)
|
|
|
|
self.send_header("Content-type", "application/json")
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(response.encode("utf8"))
|
|
|
|
def _safe_handle(self, method, path, params):
|
|
|
|
_bot.lock.acquire()
|
|
|
|
try:
|
|
|
|
self._handle(method, path, params)
|
2018-10-04 15:01:13 +00:00
|
|
|
except:
|
|
|
|
pass
|
2018-10-04 16:18:24 +00:00
|
|
|
finally:
|
|
|
|
_bot.lock.release()
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
def _decode_params(self, s):
|
|
|
|
params = urllib.parse.parse_qs(s)
|
|
|
|
return dict([(k, v[0]) for k, v in params.items()])
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
parsed = urllib.parse.urlparse(self.path)
|
|
|
|
get_params = self._decode_params(parsed.query)
|
2018-10-05 22:14:32 +00:00
|
|
|
self._handle("get", parsed.path, params=get_params)
|
2018-10-05 21:49:06 +00:00
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
parsed = urllib.parse.urlparse(self.path)
|
2018-10-05 22:14:32 +00:00
|
|
|
post_params = self._decode_params(parsed.query)
|
2018-10-05 21:49:06 +00:00
|
|
|
content_length = int(self.headers.get("content-length", 0))
|
|
|
|
post_body = self.rfile.read(content_length)
|
2018-10-05 22:14:32 +00:00
|
|
|
self._handle("post", parsed.path, data=post_body, params=post_params)
|
2018-10-05 21:49:06 +00:00
|
|
|
|
2018-10-04 16:09:52 +00:00
|
|
|
@utils.export("botset", {"setting": "rest-api",
|
|
|
|
"help": "Enable/disable REST API",
|
|
|
|
"validate": utils.bool_or_none})
|
2018-10-04 15:01:13 +00:00
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot, events, exports):
|
|
|
|
self.bot = bot
|
|
|
|
global _bot
|
|
|
|
_bot = bot
|
|
|
|
|
|
|
|
self.events = events
|
|
|
|
global _events
|
|
|
|
_events = events
|
|
|
|
|
|
|
|
if bot.get_setting("rest-api", False):
|
|
|
|
self.httpd = http.server.HTTPServer(("", 5000), Handler)
|
2018-10-05 22:16:34 +00:00
|
|
|
self.httpd.socket = ssl.wrap_socket(self.httpd.socket,
|
|
|
|
keyfile=self.bot.config["tls-api-key"],
|
|
|
|
certfile=self.bot.config["tls-api-certificate"],
|
|
|
|
server_side=True)
|
2018-10-04 15:01:13 +00:00
|
|
|
self.thread = threading.Thread(target=self.httpd.serve_forever)
|
|
|
|
self.thread.daemon = True
|
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self.httpd.shutdown()
|
2018-10-04 16:09:35 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.apikey", private_only=True)
|
|
|
|
def api_key(self, event):
|
|
|
|
"""
|
|
|
|
:help: Generate a new API key
|
|
|
|
:permission: api-key
|
|
|
|
:prefix: APIKey
|
|
|
|
"""
|
|
|
|
api_key = str(uuid.uuid4())
|
|
|
|
self.bot.set_setting("api-key-%s" % api_key, True)
|
|
|
|
event["stdout"].write(api_key)
|