示例#1
0
class BackendServer(SmartPlugin):
    ALLOW_MULTIINSTANCE = False
    PLUGIN_VERSION='1.3.5'

    def my_to_bool(self, value, attr='', default=False):
        try:
            result = self.to_bool(value)
        except:
            result = default
            self.logger.error("BackendServer: Invalid value '"+str(value)+"' configured for attribute "+attr+" in plugin.conf, using '"+str(result)+"' instead")
        return result

    def get_local_ip_address(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("10.10.10.10", 80))
        return s.getsockname()[0]

    def __init__(self, sh, port=None, threads=8, ip='', updates_allowed='True', user="******", password="", hashed_password="", language="", developer_mode="no", pypi_timeout=5):
        self.logger = logging.getLogger(__name__)
        self._user = user
        self._password = password
        self._hashed_password = hashed_password

        if self._password is not None and self._password != "" and self._hashed_password is not None and self._hashed_password != "":
            self.logger.warning("BackendServer: Both 'password' and 'hashed_password' given. Ignoring 'password' and using 'hashed_password'!")
            self._password = None

        if self._password is not None and self._password != "" and (self._hashed_password is None or self._hashed_password == ""):
            self.logger.warning("BackendServer: Giving plaintext password in configuration is insecure. Consider using 'hashed_password' instead!")
            self._hashed_password = None

        if (self._password is not None and self._password != "") or (self._hashed_password is not None and self._hashed_password != ""):
            self._basic_auth = True
        else:
            self._basic_auth = False
        self._sh = sh

        if self.is_int(port):
            self.port = int(port)
        else:
            self.port = 8383
            if port is not None:
                self.logger.error("BackendServer: Invalid value '"+str(port)+"' configured for attribute 'port' in plugin.conf, using '"+str(self.port)+"' instead")

        if self.is_int(threads):
            self.threads = int(threads)
        else:
            self.threads = 8
            self.logger.error("BackendServer: Invalid value '"+str(threads)+"' configured for attribute 'thread' in plugin.conf, using '"+str(self.threads)+"' instead")

        if ip == '':
            ip = self.get_local_ip_address()
            self.logger.debug("BackendServer: Using local ip address '{0}'".format(ip))
        else:
            pass
        #    if not self.is_ip(ip):
        #         self.logger.error("BackendServer: Invalid value '"+str(ip)+"' configured for attribute ip in plugin.conf, using '"+str('0.0.0.0')+"' instead")
        #         ip = '0.0.0.0'
        language = language.lower()
        if language != '':
            if not load_translation(language):
                self.logger.warning("BackendServer: Language '{0}' not found, using standard language instead".format(language))
        self.developer_mode =  self.my_to_bool(developer_mode, 'developer_mode', False)

        self.updates_allowed = self.my_to_bool(updates_allowed, 'updates_allowed', True)

        if self.is_int(pypi_timeout):
            self.pypi_timeout = int(pypi_timeout)
        else:
            self.pypi_timeout = 5
            if pypi_timeout is not None:
                self.logger.error("BackendServer: Invalid value '" + str(pypi_timeout) + "' configured for attribute 'pypi_timeout' in plugin.conf, using '" + str(self.pypi_timeout) + "' instead")

        current_dir = os.path.dirname(os.path.abspath(__file__))
        self.logger.debug("BackendServer running from '{}'".format(current_dir))

        config = {'global': {
            'engine.autoreload.on': False,
            'tools.staticdir.debug': True,
            'tools.trailing_slash.on': False
            },
            '/': {
                'tools.auth_basic.on': self._basic_auth,
                'tools.auth_basic.realm': 'earth',
                'tools.auth_basic.checkpassword': self.validate_password,
                'tools.staticdir.root': current_dir,
            },
            '/static': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': os.path.join(current_dir, 'static')
            }
        }
        from cherrypy._cpserver import Server
        self._server = Server()
        self._server.socket_host = ip
        self._server.socket_port = int(self.port)
        self._server.thread_pool = self.threads
        self._server.subscribe()

        self._cherrypy = cherrypy
        self._cherrypy.config.update(config)
        self._cherrypy.tree.mount(Backend(self, self.updates_allowed, language, self.developer_mode, self.pypi_timeout), '/', config = config)

    def run(self):
        self.logger.debug("BackendServer: rest run")
        self._server.start()
        #self._cherrypy.engine.start()
        self.logger.debug("BackendServer: engine started")
        #cherrypy.engine.block()
        self.alive = True

    def stop(self):
        self.logger.debug("BackendServer: shutting down")
        self._server.stop()
        #self._cherrypy.engine.exit()
        self.logger.debug("BackendServer: engine exited")
        self.alive = False

    def parse_item(self, item):
        pass

    def parse_logic(self, logic):
        pass

    def update_item(self, item, caller=None, source=None, dest=None):
        pass

    def validate_password(self, realm, username, password):
        if username != self._user or password is None or password == "":
            return False

        if self._hashed_password is not None:
            return Utils.check_hashed_password(password, self._hashed_password)
        elif self._password is not None:
            return password == self._password

        return False
示例#2
0
        return "OK"

    parts = path.split("/")
    assert parts[0] == ""
    assert parts[1] == ".well-known"
    assert parts[2] == "acme-challenge"
    token = parts[3]

    start_response("200 OK", [])
    return tokens.get(token, "")


from cherrypy._cpserver import Server

cherrypy.server.unsubscribe(
)  # don't start default http server that listens on some socket
cherrypy.config.update({"/": {}})

server = Server(exos_vr="ALL")
server._socket_host = "::"
server.socket_port = 80

server.thread_pool = 7
server.subscribe()

cherrypy.tree.graft(httpd_service, "/")

cherrypy.engine.start()
server.start()
cherrypy.engine.block()