2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
#--depends-on permissions
|
2018-10-05 22:16:34 +00:00
|
|
|
#--require-config tls-api-key
|
|
|
|
#--require-config tls-api-certificate
|
|
|
|
|
2019-08-13 11:58:23 +00:00
|
|
|
import http.server, json, socket, ssl, threading, uuid, urllib.parse
|
2018-10-12 17:07:23 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2019-09-10 12:38:25 +00:00
|
|
|
class Response(object):
|
|
|
|
def __init__(self, compact=False):
|
|
|
|
self._compact = compact
|
|
|
|
self._headers = {}
|
|
|
|
self._data = b""
|
|
|
|
self.code = 200
|
|
|
|
self.content_type = "text/plain"
|
|
|
|
def write(self, data):
|
|
|
|
self._data += data
|
|
|
|
def write_text(self, data):
|
|
|
|
self._data += data.encode("utf8")
|
|
|
|
def write_json(self, obj):
|
|
|
|
if self._compact:
|
2019-09-10 14:08:06 +00:00
|
|
|
data = json.dumps(obj, separators=(",", ":"))
|
2019-09-10 12:38:25 +00:00
|
|
|
else:
|
2019-09-10 12:39:59 +00:00
|
|
|
data = json.dumps(obj, sort_keys=True, indent=4)
|
2019-09-10 12:38:25 +00:00
|
|
|
self._data += data.encode("utf8")
|
|
|
|
|
|
|
|
def set_header(self, key: str, value: str):
|
|
|
|
self._headers[key] = value
|
|
|
|
def get_headers(self):
|
|
|
|
headers = {}
|
|
|
|
has_content_type = False
|
|
|
|
for key, value in self._headers.items():
|
|
|
|
if key.lower() == "content-type":
|
|
|
|
has_content_type = True
|
|
|
|
headers[key] = value
|
|
|
|
if not has_content_type:
|
|
|
|
headers["Content-Type"] = self.content_type
|
2019-09-11 10:00:55 +00:00
|
|
|
headers["Content-Length"] = len(self._data)
|
2019-09-10 12:38:25 +00:00
|
|
|
return headers
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
|
|
|
|
2018-10-04 15:01:13 +00:00
|
|
|
_bot = None
|
|
|
|
_events = None
|
2018-11-06 17:22:50 +00:00
|
|
|
_log = None
|
2018-10-04 15:01:13 +00:00
|
|
|
class Handler(http.server.BaseHTTPRequestHandler):
|
2018-10-04 16:10:05 +00:00
|
|
|
timeout = 10
|
2019-02-08 21:52:24 +00:00
|
|
|
|
|
|
|
def _path_data(self):
|
|
|
|
path = urllib.parse.urlparse(self.path).path
|
2018-10-05 21:49:06 +00:00
|
|
|
_, _, endpoint = path[1:].partition("/")
|
|
|
|
endpoint, _, args = endpoint.partition("/")
|
|
|
|
args = list(filter(None, args.split("/")))
|
2019-02-08 21:56:58 +00:00
|
|
|
return path, endpoint, args
|
2019-02-08 21:52:24 +00:00
|
|
|
|
|
|
|
def _url_params(self):
|
|
|
|
parsed = urllib.parse.urlparse(self.path)
|
2019-02-08 21:55:42 +00:00
|
|
|
query = urllib.parse.parse_qs(parsed.query)
|
2019-02-08 21:52:24 +00:00
|
|
|
return dict([(k, v[0]) for k, v in query.items()])
|
|
|
|
|
|
|
|
def _body(self):
|
|
|
|
content_length = int(self.headers.get("content-length", 0))
|
|
|
|
return self.rfile.read(content_length)
|
|
|
|
|
2019-09-10 12:38:25 +00:00
|
|
|
def _respond(self, response):
|
|
|
|
self.send_response(response.code)
|
|
|
|
for key, value in response.get_headers().items():
|
2019-02-08 22:04:39 +00:00
|
|
|
self.send_header(key, value)
|
|
|
|
self.end_headers()
|
2019-09-10 12:38:25 +00:00
|
|
|
self.wfile.write(response.get_data())
|
2019-02-08 22:04:39 +00:00
|
|
|
|
2019-09-10 12:55:06 +00:00
|
|
|
def _key_settings(self, key):
|
|
|
|
return _bot.get_setting("api-key-%s" % key, {})
|
|
|
|
def _minify_setting(self):
|
|
|
|
return _bot.get_setting("rest-api-minify", False)
|
2019-03-11 12:16:56 +00:00
|
|
|
|
2019-09-10 14:40:01 +00:00
|
|
|
def url_for(self, headers, route, endpoint, get_params={}):
|
2019-09-10 14:59:54 +00:00
|
|
|
if "Host" in headers:
|
2019-09-10 15:39:44 +00:00
|
|
|
host, _, port = headers["Host"].partition(":")
|
|
|
|
if not port:
|
|
|
|
port = _bot.config.get("api-port", "5000")
|
|
|
|
|
2019-09-10 14:40:01 +00:00
|
|
|
get_params_str = ""
|
|
|
|
if get_params:
|
2019-09-10 15:29:35 +00:00
|
|
|
get_params_str = "?%s" % urllib.parse.urlencode(get_params)
|
2019-09-10 15:41:16 +00:00
|
|
|
return "%s:%s/%s/%s%s" % (host, port, route, endpoint,
|
2019-09-10 15:39:44 +00:00
|
|
|
get_params_str)
|
2019-09-10 14:59:54 +00:00
|
|
|
else:
|
|
|
|
return None
|
2019-09-10 14:40:01 +00:00
|
|
|
def _url_for(self, headers):
|
|
|
|
return lambda route, endpoint, get_params={}: self.url_for(
|
|
|
|
headers, route, endpoint, get_params)
|
|
|
|
|
2019-09-10 12:55:06 +00:00
|
|
|
def _handle(self, method, path, endpoint, args):
|
2018-12-08 09:00:12 +00:00
|
|
|
headers = utils.CaseInsensitiveDict(dict(self.headers.items()))
|
2019-02-08 21:52:24 +00:00
|
|
|
params = self._url_params()
|
|
|
|
data = self._body()
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2019-09-10 12:55:06 +00:00
|
|
|
response = Response(compact=self._minify_setting())
|
2019-09-10 12:38:25 +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:
|
2019-09-10 12:45:14 +00:00
|
|
|
response.code = 200
|
2018-10-05 21:49:06 +00:00
|
|
|
hook = hooks[0]
|
|
|
|
authenticated = hook.get_kwarg("authenticated", True)
|
|
|
|
key = params.get("key", None)
|
2019-09-10 12:55:06 +00:00
|
|
|
key_setting = self._key_settings(key)
|
2018-11-12 18:18:07 +00:00
|
|
|
permissions = key_setting.get("permissions", [])
|
2018-11-10 21:54:08 +00:00
|
|
|
|
2019-01-23 22:08:26 +00:00
|
|
|
if key_setting:
|
2019-01-23 22:23:21 +00:00
|
|
|
_log.debug("[HTTP] %s from API key %s (%s)",
|
2019-01-23 22:10:32 +00:00
|
|
|
[method, key, key_setting["comment"]])
|
2019-01-23 22:08:26 +00:00
|
|
|
|
2018-11-11 08:53:37 +00:00
|
|
|
if not authenticated or path in permissions or "*" in permissions:
|
2018-10-05 21:49:06 +00:00
|
|
|
if path.startswith("/api/"):
|
2018-10-06 08:24:43 +00:00
|
|
|
event_response = None
|
2018-10-06 08:22:11 +00:00
|
|
|
try:
|
2019-09-10 12:55:06 +00:00
|
|
|
event_response = _events.on("api").on(method).on(
|
2019-06-26 10:04:41 +00:00
|
|
|
endpoint).call_for_result_unsafe(params=params,
|
2019-09-10 12:38:25 +00:00
|
|
|
path=args, data=data, headers=headers,
|
2019-09-10 14:40:01 +00:00
|
|
|
response=response, url_for=self._url_for(headers))
|
2018-11-06 17:22:50 +00:00
|
|
|
except Exception as e:
|
|
|
|
_log.error("failed to call API endpoint \"%s\"",
|
|
|
|
[path], exc_info=True)
|
2019-09-10 12:38:25 +00:00
|
|
|
response.code = 500
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-11-06 14:09:13 +00:00
|
|
|
if not event_response == None:
|
2019-09-10 12:38:25 +00:00
|
|
|
response.write_json(event_response)
|
|
|
|
response.content_type = "application/json"
|
2018-11-10 21:54:08 +00:00
|
|
|
else:
|
2019-09-10 12:38:25 +00:00
|
|
|
response.code = 401
|
2019-09-10 12:55:06 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def _handle_wrap(self, method):
|
|
|
|
path, endpoint, args = self._path_data()
|
|
|
|
_log.debug("[HTTP] starting _handle for %s from %s:%d: %s",
|
|
|
|
[method, self.client_address[0], self.client_address[1], path])
|
2019-02-08 22:04:39 +00:00
|
|
|
|
2019-09-10 12:55:06 +00:00
|
|
|
response = _bot.trigger(lambda: self._handle(method, path, endpoint,
|
|
|
|
args))
|
2019-09-10 12:38:25 +00:00
|
|
|
self._respond(response)
|
2019-02-08 22:04:39 +00:00
|
|
|
|
2019-03-13 13:33:53 +00:00
|
|
|
_log.debug("[HTTP] finishing _handle for %s from %s:%d (%d)",
|
2019-09-10 12:45:55 +00:00
|
|
|
[method, self.client_address[0], self.client_address[1],
|
|
|
|
response.code])
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-10-05 21:49:06 +00:00
|
|
|
def do_GET(self):
|
2019-09-10 12:55:06 +00:00
|
|
|
self._handle_wrap("GET")
|
2018-10-05 21:49:06 +00:00
|
|
|
|
|
|
|
def do_POST(self):
|
2019-09-10 12:55:06 +00:00
|
|
|
self._handle_wrap("POST")
|
2018-10-05 21:49:06 +00:00
|
|
|
|
2018-11-14 23:01:22 +00:00
|
|
|
def log_message(self, format, *args):
|
2019-03-11 12:16:56 +00:00
|
|
|
return
|
2018-11-14 23:01:22 +00:00
|
|
|
|
2019-08-13 11:58:23 +00:00
|
|
|
class BitBotIPv6HTTPd(http.server.HTTPServer):
|
|
|
|
address_family = socket.AF_INET6
|
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("botset",
|
|
|
|
utils.BoolSetting("rest-api", "Enable/disable REST API"))
|
|
|
|
@utils.export("botset",
|
2019-09-10 14:00:47 +00:00
|
|
|
utils.BoolSetting("rest-api-minify", "Enable/disable REST API minifying"))
|
2018-10-12 17:07:23 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def on_load(self):
|
2018-10-04 15:01:13 +00:00
|
|
|
global _bot
|
2018-10-12 17:07:23 +00:00
|
|
|
_bot = self.bot
|
2018-10-04 15:01:13 +00:00
|
|
|
|
|
|
|
global _events
|
2018-10-12 17:07:23 +00:00
|
|
|
_events = self.events
|
2018-10-04 15:01:13 +00:00
|
|
|
|
2018-11-06 17:22:50 +00:00
|
|
|
global _log
|
|
|
|
_log = self.log
|
|
|
|
|
2018-12-08 08:56:47 +00:00
|
|
|
self.httpd = None
|
2018-10-12 17:07:23 +00:00
|
|
|
if self.bot.get_setting("rest-api", False):
|
2019-03-13 16:52:51 +00:00
|
|
|
port = int(self.bot.config.get("api-port", "5000"))
|
2019-08-13 11:58:23 +00:00
|
|
|
self.httpd = BitBotIPv6HTTPd(("", port), Handler)
|
2019-02-10 12:38:53 +00:00
|
|
|
|
2019-02-10 13:19:30 +00:00
|
|
|
self.httpd.socket = utils.security.ssl_wrap(self.httpd.socket,
|
2019-02-10 12:38:53 +00:00
|
|
|
cert=self.bot.config["tls-api-certificate"],
|
|
|
|
key=self.bot.config["tls-api-key"],
|
2019-02-10 13:26:35 +00:00
|
|
|
server_side=True, verify=False)
|
2019-02-10 12:38:53 +00:00
|
|
|
|
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):
|
2018-12-08 08:56:47 +00:00
|
|
|
if self.httpd:
|
|
|
|
self.httpd.shutdown()
|
2018-10-04 16:09:35 +00:00
|
|
|
|
2019-02-01 12:02:02 +00:00
|
|
|
@utils.hook("received.command.apikey", private_only=True, min_args=1)
|
2018-10-04 16:09:35 +00:00
|
|
|
def api_key(self, event):
|
|
|
|
"""
|
|
|
|
:help: Generate a new API key
|
2018-11-12 20:44:46 +00:00
|
|
|
:usage: <comment> [endpoint [endpoint ...]]
|
2018-10-04 16:09:35 +00:00
|
|
|
:permission: api-key
|
|
|
|
:prefix: APIKey
|
|
|
|
"""
|
2018-11-12 17:59:40 +00:00
|
|
|
api_key = uuid.uuid4().hex
|
|
|
|
comment = event["args_split"][0]
|
|
|
|
self.bot.set_setting("api-key-%s" % api_key, {
|
|
|
|
"comment": comment,
|
2018-11-12 22:20:46 +00:00
|
|
|
"permissions": event["args_split"][1:]
|
2018-11-12 17:59:40 +00:00
|
|
|
})
|
|
|
|
event["stdout"].write("New API key ('%s'): %s" % (comment, api_key))
|