diff options
-rw-r--r-- | module/remote/WebSocketBackend.py | 10 | ||||
-rw-r--r-- | module/remote/json_converter.py | 39 | ||||
-rw-r--r-- | module/remote/wsbackend/ApiHandler.py | 109 | ||||
-rw-r--r-- | module/remote/wsbackend/Dispatcher.py | 24 | ||||
-rw-r--r-- | module/remote/wsbackend/EventHandler.py | 41 | ||||
-rw-r--r-- | module/remote/wsbackend/Server.py | 2 | ||||
-rw-r--r-- | module/utils/__init__.py | 6 | ||||
-rw-r--r-- | module/web/api_app.py | 35 | ||||
-rw-r--r-- | module/web/filters.py | 62 | ||||
-rw-r--r-- | module/web/webinterface.py | 22 |
10 files changed, 221 insertions, 129 deletions
diff --git a/module/remote/WebSocketBackend.py b/module/remote/WebSocketBackend.py index 7af927d8f..6e01dad87 100644 --- a/module/remote/WebSocketBackend.py +++ b/module/remote/WebSocketBackend.py @@ -21,7 +21,7 @@ import logging from module.remote.RemoteManager import BackendBase from mod_pywebsocket import util -def get_class_logger(o): +def get_class_logger(o=None): return logging.getLogger('log') # Monkey patch for our logger @@ -30,15 +30,17 @@ util.get_class_logger = get_class_logger class WebSocketBackend(BackendBase): def setup(self, host, port): - from wsbackend.Server import WebSocketServer, DefaultOptions - from wsbackend.Dispatcher import Dispatcher, ApiHandler + from wsbackend.Dispatcher import Dispatcher + from wsbackend.ApiHandler import ApiHandler + from wsbackend.EventHandler import EventHandler options = DefaultOptions() options.server_host = host options.port = port options.dispatcher = Dispatcher() - options.dispatcher.addHandler('/api', ApiHandler()) + options.dispatcher.addHandler('/api', ApiHandler(self.core.api)) + options.dispatcher.addHandler('/events', EventHandler(self.core.api)) self.server = WebSocketServer(options) diff --git a/module/remote/json_converter.py b/module/remote/json_converter.py new file mode 100644 index 000000000..57e85fd16 --- /dev/null +++ b/module/remote/json_converter.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from module.common.json_layer import json +from ttypes import BaseObject + +# json encoder that accepts TBase objects +class BaseEncoder(json.JSONEncoder): + + def default(self, o): + if isinstance(o, BaseObject): + ret = {"@class" : o.__class__.__name__} + for att in o.__slots__: + ret[att] = getattr(o, att) + return ret + + return json.JSONEncoder.default(self, o) + +class BaseDecoder(json.JSONDecoder): + + def __init__(self, *args, **kwargs): + json.JSONDecoder.__init__(self, *args, **kwargs) + self.object_hook = self.convertObject + + def convertObject(self, dct): + if '@class' in dct: + # TODO: convert + pass + + return dct + +def dumps(*args, **kwargs): + kwargs['cls'] = BaseEncoder + return json.dumps(*args, **kwargs) + + +def loads(*args, **kwargs): + kwargs['cls'] = BaseDecoder + return json.loads(*args, **kwargs)
\ No newline at end of file diff --git a/module/remote/wsbackend/ApiHandler.py b/module/remote/wsbackend/ApiHandler.py new file mode 100644 index 000000000..8f6bb3a0a --- /dev/null +++ b/module/remote/wsbackend/ApiHandler.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +############################################################################### +# Copyright(c) 2008-2012 pyLoad Team +# http://www.pyload.org +# +# This file is part of pyLoad. +# pyLoad is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Subjected to the terms and conditions in LICENSE +# +# @author: RaNaN +############################################################################### + +from mod_pywebsocket.util import get_class_logger +from mod_pywebsocket.msgutil import receive_message, send_message +from module.remote.json_converter import loads, dumps + +class ApiHandler: + """Provides access to the API. + + Send your request as json encoded string in the following manner: + ["function", [*args]] or ["function", {**kwargs}] + + the result will be: + + [code, result] + + Don't forget to login first. + Non json request will be ignored. + """ + + def __init__(self, api): + self.log = get_class_logger() + self.api = api + + def do_extra_handshake(self, req): + self.log.debug("WS Connected: %s" % req) + + def transfer_data(self, req): + + while True: + try: + line = receive_message(req) + self.handle_message(line, req) + except TypeError, e: # connection closed + print e + return + + def passive_closing_handshake(self, req): + self.log.debug("WS Closed: %s" % req) + + def handle_message(self, msg, req): + try: + o = loads(msg) + except ValueError, e: #invalid json object + self.log.debug("Invalid Request: %s" % e) + return + + if type(o) != list and len(o) > 2: + self.log.debug("Invalid Api call: %s" % o) + return self.send_result(req, 500, "Invalid Api call") + if len(o) == 1: # arguments ommited + o.append([]) + + func, args = o + if type(args) == list: + kwargs = {} + else: + args, kwargs = [], args + + if func == 'login': + user = self.api.checkAuth(*args, **kwargs) + if user: + req.api = self.api.withUserContext(user.uid) + return self.send_result(req, 200, True) + + else: + return self.send_result(req, 403, "Forbidden") + + elif func == 'logout': + req.api = None + return self.send_result(req, 200, True) + + else: + if not req.api: + return self.send_result(req, 403, "Forbidden") + + if not self.api.isAuthorized(func, req.api.user): + return self.send_result(req, 401, "Unauthorized") + + try: + result = getattr(req.api, func)(*args, **kwargs) + except AttributeError: + return self.send_result(req, 404, "Not Found") + except Exception, e: + return self.send_result(req, 500, str(e)) + + if result is None: result = True + + return self.send_result(req, 200, result) + + + def send_result(self, req, code, result): + return send_message(req, dumps([code, result]))
\ No newline at end of file diff --git a/module/remote/wsbackend/Dispatcher.py b/module/remote/wsbackend/Dispatcher.py index eb1c9a291..44cc7555e 100644 --- a/module/remote/wsbackend/Dispatcher.py +++ b/module/remote/wsbackend/Dispatcher.py @@ -18,7 +18,6 @@ from mod_pywebsocket import util from mod_pywebsocket.dispatch import Dispatcher as BaseDispatcher -from mod_pywebsocket.msgutil import receive_message, send_message class Dispatcher(BaseDispatcher): @@ -29,25 +28,4 @@ class Dispatcher(BaseDispatcher): self._source_warnings = [] def addHandler(self, path, handler): - self._handler_suite_map[path] = handler - - - -class ApiHandler: - - def do_extra_handshake(self, req): - pass - - def transfer_data(self, req): - - while True: - try: - line = receive_message(req) - except TypeError: # connection closed - return - - print "Got", line - send_message(req, "You send: %s" % line) - - def passive_closing_handshake(self, req): - print "Closed", req
\ No newline at end of file + self._handler_suite_map[path] = handler
\ No newline at end of file diff --git a/module/remote/wsbackend/EventHandler.py b/module/remote/wsbackend/EventHandler.py new file mode 100644 index 000000000..deab25a6c --- /dev/null +++ b/module/remote/wsbackend/EventHandler.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +############################################################################### +# Copyright(c) 2008-2012 pyLoad Team +# http://www.pyload.org +# +# This file is part of pyLoad. +# pyLoad is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Subjected to the terms and conditions in LICENSE +# +# @author: RaNaN +############################################################################### + +from mod_pywebsocket.msgutil import receive_message, send_message + +class EventHandler: + + def __init__(self, api): + self.api = api + + def do_extra_handshake(self, req): + pass + + def transfer_data(self, req): + + while True: + try: + line = receive_message(req) + except TypeError: # connection closed + return + + print "Got", line + send_message(req, "You send: %s" % line) + + def passive_closing_handshake(self, req): + print "Closed", req
\ No newline at end of file diff --git a/module/remote/wsbackend/Server.py b/module/remote/wsbackend/Server.py index 570753073..65f4fe5d5 100644 --- a/module/remote/wsbackend/Server.py +++ b/module/remote/wsbackend/Server.py @@ -32,6 +32,7 @@ # A copy of standalone.py with uneeded stuff removed # some logging methods removed +# Added api attribute to request import BaseHTTPServer import CGIHTTPServer @@ -129,6 +130,7 @@ class _StandaloneRequest(object): """ self._logger = logging.getLogger("log") + self.api = None self._request_handler = request_handler self.connection = _StandaloneConnection(request_handler) diff --git a/module/utils/__init__.py b/module/utils/__init__.py index 4ecc53a12..3adb695ee 100644 --- a/module/utils/__init__.py +++ b/module/utils/__init__.py @@ -192,12 +192,6 @@ def to_int(string, default=0): except ValueError: return default -def to_dict(obj): - ret = {"class" : obj.__class__.__name__} - for att in obj.__slots__: - ret[att] = getattr(obj, att) - return ret - def from_string(value, typ=None): """ cast value to given type, unicode for strings """ diff --git a/module/web/api_app.py b/module/web/api_app.py index b2d7fa5b6..c3237c743 100644 --- a/module/web/api_app.py +++ b/module/web/api_app.py @@ -10,19 +10,8 @@ from bottle import route, request, response, HTTPError from utils import set_session, get_user_api from webinterface import PYLOAD -from module.common.json_layer import json -from module.utils import remove_chars, to_dict -from module.lib.SafeEval import const_eval as literal_eval -from module.Api import BaseObject - -# json encoder that accepts TBase objects -class TBaseEncoder(json.JSONEncoder): - - def default(self, o): - if isinstance(o, BaseObject): - return to_dict(o) - return json.JSONEncoder.default(self, o) - +from module.remote.json_converter import loads, dumps +from module.utils import remove_chars def add_header(r): r.headers.replace("Content-type", "application/json") @@ -43,10 +32,10 @@ def call_api(func, args=""): api = get_user_api(s) if not api: - return HTTPError(403, json.dumps("Forbidden")) + return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): - return HTTPError(401, json.dumps("Unauthorized")) + return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} @@ -59,23 +48,23 @@ def call_api(func, args=""): return callApi(func, *args, **kwargs) except Exception, e: print_exc() - return HTTPError(500, json.dumps({"error": e.message, "traceback": format_exc()})) + return HTTPError(500, dumps({"error": e.message, "traceback": format_exc()})) # Better error codes on invalid input def callApi(func, *args, **kwargs): if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func - return HTTPError(404, json.dumps("Not Found")) + return HTTPError(404, dumps("Not Found")) # TODO: encoding - result = getattr(PYLOAD, func)(*[literal_eval(x) for x in args], - **dict([(x, literal_eval(y)) for x, y in kwargs.iteritems()])) + result = getattr(PYLOAD, func)(*[loads(x) for x in args], + **dict([(x, loads(y)) for x, y in kwargs.iteritems()])) # null is invalid json response if result is None: result = True - return json.dumps(result, cls=TBaseEncoder) + return dumps(result) #post -> username, password @@ -89,17 +78,17 @@ def login(): user = PYLOAD.checkAuth(username, password) if not user: - return json.dumps(False) + return dumps(False) s = set_session(request, user) # get the session id by dirty way, documentations seems wrong try: sid = s._headers["cookie_out"].split("=")[1].split(";")[0] - return json.dumps(sid) + return dumps(sid) except: print "Could not get session" - return json.dumps(True) + return dumps(True) @route("/api/logout") diff --git a/module/web/filters.py b/module/web/filters.py deleted file mode 100644 index 13b8345fc..000000000 --- a/module/web/filters.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import os -from os.path import abspath, commonprefix, join - -quotechar = "::/" - -try: - from os.path import relpath -except: - from posixpath import curdir, sep, pardir - def relpath(path, start=curdir): - """Return a relative version of a path""" - if not path: - raise ValueError("no path specified") - start_list = abspath(start).split(sep) - path_list = abspath(path).split(sep) - # Work out how much of the filepath is shared by start and path. - i = len(commonprefix([start_list, path_list])) - rel_list = [pardir] * (len(start_list)-i) + path_list[i:] - if not rel_list: - return curdir - return join(*rel_list) - - -def quotepath(path): - try: - return path.replace("../", quotechar) - except AttributeError: - return path - except: - return "" - -def unquotepath(path): - try: - return path.replace(quotechar, "../") - except AttributeError: - return path - except: - return "" - -def path_make_absolute(path): - p = os.path.abspath(path) - if p[-1] == os.path.sep: - return p - else: - return p + os.path.sep - -def path_make_relative(path): - p = relpath(path) - if p[-1] == os.path.sep: - return p - else: - return p + os.path.sep - -def truncate(value, n): - if (n - len(value)) < 3: - return value[:n]+"..." - return value - -def date(date, format): - return date
\ No newline at end of file diff --git a/module/web/webinterface.py b/module/web/webinterface.py index ae97406c7..196825bfb 100644 --- a/module/web/webinterface.py +++ b/module/web/webinterface.py @@ -30,7 +30,7 @@ PYLOAD_DIR = abspath(join(PROJECT_DIR, "..", "..")) sys.path.append(PYLOAD_DIR) from module import InitHomeDir -from module.utils import decode, format_size +from module.utils import format_size import bottle from bottle import run, app @@ -64,7 +64,7 @@ PREFIX = config.get('webinterface', 'prefix') if PREFIX: PREFIX = PREFIX.rstrip("/") - if not PREFIX.startswith("/"): + if PREFIX and not PREFIX.startswith("/"): PREFIX = "/" + PREFIX DEBUG = config.get("general", "debug_mode") or "-d" in sys.argv or "--debug" in sys.argv @@ -84,28 +84,26 @@ loader = PrefixLoader({ env = Environment(loader=loader, extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'], trim_blocks=True, auto_reload=False, bytecode_cache=bcc) -from filters import quotepath, path_make_relative, path_make_absolute, truncate, date +# Filter -env.filters["quotepath"] = quotepath -env.filters["truncate"] = truncate -env.filters["date"] = date -env.filters["path_make_relative"] = path_make_relative -env.filters["path_make_absolute"] = path_make_absolute -env.filters["decode"] = decode env.filters["type"] = lambda x: str(type(x)) env.filters["formatsize"] = format_size env.filters["getitem"] = lambda x, y: x.__getitem__(y) -if PREFIX: +if not PREFIX: env.filters["url"] = lambda x: x else: env.filters["url"] = lambda x: PREFIX + x if x.startswith("/") else x +# Locale + gettext.setpaths([join(os.sep, "usr", "share", "pyload", "locale"), None]) translation = gettext.translation("django", join(PYLOAD_DIR, "locale"), languages=[config.get("general", "language"), "en"],fallback=True) translation.install(True) env.install_gettext_translations(translation) +# Middlewares + from beaker.middleware import SessionMiddleware session_opts = { @@ -125,6 +123,9 @@ import pyload_app import cnl_app import api_app + +# Server Adapter + def run_simple(host="0.0.0.0", port="8000"): run(app=web, host=host, port=port, quiet=True) @@ -140,7 +141,6 @@ def run_threaded(host="0.0.0.0", port="8000", threads=6, cert="", key=""): CherryPyWSGIServer.ssl_certificate = cert CherryPyWSGIServer.ssl_private_key = key - # todo: threads configurable from utils import CherryPyWSGI CherryPyWSGI.numthreads = threads |