def __init__(self, config): self.config = config self.config_file = config['config_filename'] self.host = config['management']['host'] # hostname of the management interface self.port = config['management']['port'] # port to listen on self.srcdir = config['management']['srcdir'] # source directory of templates server_private_key = os.path.join(config['management']['basedir'], config['management']['ssl_key']) server_cert = os.path.join(config['management']['basedir'], config['management']['ssl_cert']) # allow the config to be readable by .rpy files registry = Registry() registry.setComponent(WebBoxMulti, self) self.root = FileNoDirectoryListings(self.srcdir, registry=registry) # make .rpy python cgi self.root.processors = {'.rpy': script.ResourceScript} self.root.ignoreExt('.rpy') # hook in the controller urls to /start /stop etc. self.root.putChild("new", WSGIResource(reactor, reactor.getThreadPool(), self.do_new)) admin = FileNoDirectoryListings(self.srcdir + os.sep + "admin", registry=registry) self.admin = admin # make .rpy python cgi self.admin.processors = {'.rpy': script.ResourceScript} self.admin.ignoreExt('.rpy') self.admin.putChild("start", WSGIResource(reactor, reactor.getThreadPool(), self.do_start)) self.admin.putChild("stop", WSGIResource(reactor, reactor.getThreadPool(), self.do_stop)) class MgmtRealm(object): implements(IRealm) def requestAvatar(self, avatarId, mind, *interfaces): if IResource in interfaces: return (IResource, admin, lambda: None) logging.debug("Error in requestAvatar.") realm = MgmtRealm() portal = Portal(realm, [FilePasswordDB(self.config['htdigest'])]) #FIXME insecure passwords credentialFactory = DigestCredentialFactory("md5", "webbox") # webbox is the "realm" of the htdigest adminAuth = HTTPAuthSessionWrapper(portal, [credentialFactory]) self.root.putChild("admin", adminAuth) self.site = Site(self.root) sslContext = ssl.DefaultOpenSSLContextFactory(server_private_key, server_cert) reactor.listenSSL(self.port, self.site, contextFactory=sslContext) self.url = "https://"+self.host+":"+str(self.port) # load a web browser once the server has started def on_start(arg): logging.debug("Listening on: "+self.url) try: import webbrowser #webbrowser.open(server_url) except Exception as e: pass # no web browser? no problem. def start_failed(arg): logging.debug("Startup failed: "+str(arg)) # calls the web browser opening function above when the reactor has finished starting up d = Deferred() d.addCallbacks(on_start, start_failed) reactor.callWhenRunning(d.callback, "Server startup") logging.debug("Starting WebBox management interface on: "+self.url) # add webboxes already configured self.webboxes = {} self.add_webboxes() reactor.run()
def __init__(self, config): self.config = config self.host = config['management']['host'] # hostname of the management interface self.port = config['management']['port'] # port to listen on self.srcdir = config['management']['srcdir'] # source directory of templates # TODO ssl port? or multiple ports? self.vhost = NameVirtualHost() # allow the config to be readable by .rpy files registry = Registry() registry.setComponent(WebBoxVHost, self) root = FileNoDirectoryListings(self.srcdir, registry=registry) # make .rpy python cgi root.processors = {'.rpy': script.ResourceScript} root.ignoreExt('.rpy') # hook in the vhost controller urls to /start /stop etc. root.putChild("start", WSGIResource(reactor, reactor.getThreadPool(), self.do_start)) root.putChild("stop", WSGIResource(reactor, reactor.getThreadPool(), self.do_stop)) class MgmtRealm(object): implements(IRealm) def requestAvatar(self, avatarId, mind, *interfaces): if IResource in interfaces: return (IResource, root, lambda: None) logging.debug("Error in requestAvatar.") realm = MgmtRealm() portal = Portal(realm, [FilePasswordDB(config['htdigest'])]) #FIXME insecure passwords credentialFactory = DigestCredentialFactory("md5", "webbox") # webbox is the "realm" of the htdigest rootAuth = HTTPAuthSessionWrapper(portal, [credentialFactory]) self.vhost.addHost(self.host, rootAuth) self.site = Site(self.vhost) reactor.listenTCP(self.port, self.site) # TODO ssl port? self.url = "http://"+self.host+":"+str(self.port) # load a web browser once the server has started def on_start(arg): logging.debug("Listening on: "+self.url) try: import webbrowser webbrowser.open(server_url) except Exception as e: pass # no web browser? no problem. def start_failed(arg): logging.debug("Startup failed: "+str(arg)) # calls the web browser opening function above when the reactor has finished starting up d = Deferred() d.addCallbacks(on_start, start_failed) reactor.callWhenRunning(d.callback, "Server startup") logging.debug("Starting WebBox management interface on: "+self.url) # add vhosts already configured self.webboxes = {} self.add_vhosts() reactor.run()