Beispiel #1
0
    settings = c.parse()
    if settings == False:
        print >> sys.stderr, "Configuration file not found, Please give a valid configuration file"
        showhelp(1)
    if verbosity != False:
        settings["verbosity"] = verbosity
    if "verbosity" not in settings:
        settings["verbosity"] = 0

    #Pick the bot engine
    if ("protocol" not in settings) or (settings["protocol"] == "irc"):
        from bot import *
        #Set up the logging
        logger = Logger(settings["verbosity"])
        #Set up the Web Server
        r = Resource()
        r.putChild('', logReader(logger))
        webFactory = Site(r)
        reactor.listenTCP(8888, webFactory)
        #Set up the IRC Bot
        BotFactory = TwistedBotFactory(settings, config, logger, reactor)
        BotFactory.logger = logger
        reactor.connectTCP(
            settings["network"], 6667,
            TwistedBotFactory(settings, config, logger, reactor))
        reactor.suggestThreadPoolSize(10)
        tbot.protocol = settings["protocol"]
        reactor.run()
    elif (settings["protocol"] == "xmpp"):
        from jabberbot import *
        logger = Logger(2)
from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.web.resource import Resource


class AppendingReverseProxyResource(proxy.ReverseProxyResource):
    
    def __init__(self, host, port, prefix, postfix, reactor=reactor):
        super(AppendingReverseProxyResource, self).__init__(self, host, port, prefix, reactor=reactor)
        self.path += postfix
    

siteRoot = Resource()
site = server.Site(siteRoot)
port6001 = proxy.ReverseProxyResource('localhost', 6001, '')
port7000 = proxy.ReverseProxyResource('localhost', 7000, '')
port8000 = proxy.ReverseProxyResource('localhost', 8000, '')

siteRoot.putChild("ui", port8000)
siteRoot.putChild("slave", port7000)
siteRoot.putChild("api", port6001)

reactor.listenTCP(8080, site)
reactor.run()
 def setUp(self):
     self.pagename = b''
     self.resource = server.JsonAPIResource()
     self.root = Resource()
     self.root.putChild(self.pagename, self.resource)
Beispiel #4
0
 def __init__(self):
     self.resource = Resource()
     self.resource.putChild('send', SendMessage)
Beispiel #5
0
    def _listener_http(self, config, listener_config):
        port = listener_config["port"]
        bind_addresses = listener_config["bind_addresses"]
        tls = listener_config.get("tls", False)
        site_tag = listener_config.get("tag", port)

        if tls and config.no_tls:
            return

        resources = {}
        for res in listener_config["resources"]:
            for name in res["names"]:
                if name == "client":
                    client_resource = ClientRestResource(self)
                    if res["compress"]:
                        client_resource = gz_wrap(client_resource)

                    resources.update({
                        "/_matrix/client/api/v1":
                        client_resource,
                        "/_matrix/client/r0":
                        client_resource,
                        "/_matrix/client/unstable":
                        client_resource,
                        "/_matrix/client/v2_alpha":
                        client_resource,
                        "/_matrix/client/versions":
                        client_resource,
                    })

                if name == "federation":
                    resources.update({
                        FEDERATION_PREFIX:
                        TransportLayerServer(self),
                    })

                if name in ["static", "client"]:
                    resources.update({
                        STATIC_PREFIX:
                        File(
                            os.path.join(os.path.dirname(synapse.__file__),
                                         "static")),
                    })

                if name in ["media", "federation", "client"]:
                    media_repo = MediaRepositoryResource(self)
                    resources.update({
                        MEDIA_PREFIX:
                        media_repo,
                        LEGACY_MEDIA_PREFIX:
                        media_repo,
                        CONTENT_REPO_PREFIX:
                        ContentRepoResource(self, self.config.uploads_path),
                    })

                if name in ["keys", "federation"]:
                    resources.update({
                        SERVER_KEY_PREFIX:
                        LocalKey(self),
                        SERVER_KEY_V2_PREFIX:
                        KeyApiV2Resource(self),
                    })

                if name == "webclient":
                    resources[
                        WEB_CLIENT_PREFIX] = build_resource_for_web_client(
                            self)

                if name == "metrics" and self.get_config().enable_metrics:
                    resources[METRICS_PREFIX] = MetricsResource(self)

        if WEB_CLIENT_PREFIX in resources:
            root_resource = RootRedirect(WEB_CLIENT_PREFIX)
        else:
            root_resource = Resource()

        root_resource = create_resource_tree(resources, root_resource)

        if tls:
            for address in bind_addresses:
                reactor.listenSSL(port,
                                  SynapseSite(
                                      "synapse.access.https.%s" % (site_tag, ),
                                      site_tag,
                                      listener_config,
                                      root_resource,
                                  ),
                                  self.tls_server_context_factory,
                                  interface=address)
        else:
            for address in bind_addresses:
                reactor.listenTCP(port,
                                  SynapseSite(
                                      "synapse.access.http.%s" % (site_tag, ),
                                      site_tag,
                                      listener_config,
                                      root_resource,
                                  ),
                                  interface=address)
        logger.info("Synapse now listening on port %d", port)
Beispiel #6
0
def start_webserver(options, protocol=wsl.ServerProtocol, disableLogging=False):
    """
    Starts the web-server with the given protocol. Options must be an object
    with the following members:
        options.host : the interface for the web-server to listen on
        options.port : port number for the web-server to listen on
        options.timeout : timeout for reaping process on idle in seconds
        options.content : root for web-pages to serve.
    """
    from twisted.internet import reactor
    from twisted.web.server import Site
    from twisted.web.static import File
    import sys

    if not disableLogging:
        # redirect twisted logs to python standard logging.
        observer = log.PythonLoggingObserver()
        observer.start()
        # log.startLogging(sys.stdout)
        # Set logging level.
        if (options.debug):
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.ERROR)

    contextFactory = None

    use_SSL = False
    if options.sslKey and options.sslCert:
      use_SSL = True
      wsProtocol = "wss"
      from twisted.internet import ssl
      contextFactory = ssl.DefaultOpenSSLContextFactory(options.sslKey, options.sslCert)
    else:
      wsProtocol = "ws"

    # Create default or custom ServerProtocol
    wslinkServer = protocol()

    # create a wslink-over-WebSocket transport server factory
    transport_factory = wsl.TimeoutWebSocketServerFactory(\
           url        = "%s://%s:%d" % (wsProtocol, options.host, options.port),    \
           timeout    = options.timeout )
    transport_factory.protocol = wsl.WslinkWebSocketServerProtocol
    transport_factory.setServerProtocol(wslinkServer)

    root = Resource()

    # Do we serve static content or just websocket ?
    if len(options.content) > 0:
        # Static HTTP + WebSocket
        root = File(options.content)

    # Handle possibly complex ws endpoint
    if not options.nows:
        wsResource = WebSocketResource(transport_factory)
        handle_complex_resource_path(options.ws, root, wsResource)

    if options.uploadPath != None :
        from wslink.upload import UploadPage
        uploadResource = UploadPage(options.uploadPath)
        root.putChild("upload", uploadResource)

    if len(options.fsEndpoints) > 3:
        for fsResourceInfo in options.fsEndpoints.split('|'):
            infoSplit = fsResourceInfo.split('=')
            handle_complex_resource_path(infoSplit[0], root, File(infoSplit[1]))

    site = Site(root)

    if use_SSL:
      reactor.listenSSL(options.port, site, contextFactory)
    else:
      reactor.listenTCP(options.port, site)

    # flush ready line
    sys.stdout.flush()

    # Work around to force the output buffer to be flushed
    # This allow the process launcher to parse the output and
    # wait for "Start factory" to know that the WebServer
    # is running.
    if options.forceFlush :
        for i in range(200):
            log.msg("+"*80, logLevel=logging.CRITICAL)

    # Initialize testing: checks if we're doing a test and sets it up
    # testing.initialize(options, reactor, stop_webserver)

    reactor.callWhenRunning(print_ready)

    # Start the reactor
    if options.nosignalhandlers:
        reactor.run(installSignalHandlers=0)
    else:
        reactor.run()
Beispiel #7
0
   def create_resource(self, path_config):
      """
      Creates child resource to be added to the parent.

      :param path_config: Configuration for the new child resource.
      :type path_config: dict

      :returns: Resource -- the new child resource
      """
      ## websocket_echo
      ## websocket_testee
      ## s3mirror
      ## websocket_stdio
      ##

      ## WAMP-WebSocket resource
      ##
      if path_config['type'] == 'websocket':

         ws_factory = CrossbarWampWebSocketServerFactory(self.session_factory, self.config.extra.cbdir, path_config, self._templates)

         ## FIXME: Site.start/stopFactory should start/stop factories wrapped as Resources
         ws_factory.startFactory()

         return WebSocketResource(ws_factory)


      ## Static file hierarchy resource
      ##
      elif path_config['type'] == 'static':

         static_options = path_config.get('options', {})

         if 'directory' in path_config:

            static_dir = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))

         elif 'package' in path_config:

            if not 'resource' in path_config:
               raise ApplicationError("crossbar.error.invalid_configuration", "missing resource")

            try:
               mod = importlib.import_module(path_config['package'])
            except ImportError as e:
               emsg = "ERROR: could not import resource '{}' from package '{}' - {}".format(path_config['resource'], path_config['package'], e)
               log.msg(emsg)
               raise ApplicationError("crossbar.error.invalid_configuration", emsg)
            else:
               try:
                  static_dir = os.path.abspath(pkg_resources.resource_filename(path_config['package'], path_config['resource']))
               except Exception as e:
                  emsg = "ERROR: could not import resource '{}' from package '{}' - {}".format(path_config['resource'], path_config['package'], e)
                  log.msg(emsg)
                  raise ApplicationError("crossbar.error.invalid_configuration", emsg)

         else:

            raise ApplicationError("crossbar.error.invalid_configuration", "missing web spec")

         static_dir = static_dir.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770

         ## create resource for file system hierarchy
         ##
         if static_options.get('enable_directory_listing', False):
            static_resource = File(static_dir)
         else:
            static_resource = FileNoListing(static_dir)

         ## set extra MIME types
         ##
         static_resource.contentTypes.update(EXTRA_MIME_TYPES)
         if 'mime_types' in static_options:
            static_resource.contentTypes.update(static_options['mime_types'])
         patchFileContentTypes(static_resource)

         ## render 404 page on any concrete path not found
         ##
         static_resource.childNotFound = Resource404(self._templates, static_dir)

         return static_resource


      ## WSGI resource
      ##
      elif path_config['type'] == 'wsgi':

         if not _HAS_WSGI:
            raise ApplicationError("crossbar.error.invalid_configuration", "WSGI unsupported")

         wsgi_options = path_config.get('options', {})

         if not 'module' in path_config:
            raise ApplicationError("crossbar.error.invalid_configuration", "missing module")

         if not 'object' in path_config:
            raise ApplicationError("crossbar.error.invalid_configuration", "missing object")

         try:
            mod = importlib.import_module(path_config['module'])
         except ImportError as e:
            raise ApplicationError("crossbar.error.invalid_configuration", "module import failed - {}".format(e))
         else:
            if not path_config['object'] in mod.__dict__:
               raise ApplicationError("crossbar.error.invalid_configuration", "object not in module")
            else:
               app = getattr(mod, path_config['object'])

         ## create a Twisted Web WSGI resource from the user's WSGI application object
         try:
            wsgi_resource = WSGIResource(reactor, reactor.getThreadPool(), app)
         except Exception as e:
            raise ApplicationError("crossbar.error.invalid_configuration", "could not instantiate WSGI resource: {}".format(e))
         else:
            return wsgi_resource


      ## Redirecting resource
      ##
      elif path_config['type'] == 'redirect':
         redirect_url = path_config['url'].encode('ascii', 'ignore')
         return RedirectResource(redirect_url)


      ## JSON value resource
      ##
      elif path_config['type'] == 'json':
         value = path_config['value']

         return JsonResource(value)


      ## CGI script resource
      ##
      elif path_config['type'] == 'cgi':

         cgi_processor = path_config['processor']
         cgi_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
         cgi_directory = cgi_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770

         return CgiDirectory(cgi_directory, cgi_processor, Resource404(self._templates, cgi_directory))


      ## WAMP-Longpoll transport resource
      ##
      elif path_config['type'] == 'longpoll':

         path_options = path_config.get('options', {})

         lp_resource = WampLongPollResource(self.session_factory,
            timeout = path_options.get('request_timeout', 10),
            killAfter = path_options.get('session_timeout', 30),
            queueLimitBytes = path_options.get('queue_limit_bytes', 128 * 1024),
            queueLimitMessages = path_options.get('queue_limit_messages', 100),
            debug = path_options.get('debug', False),
            debug_transport_id = path_options.get('debug_transport_id', None)
         )
         lp_resource._templates = self._templates

         return lp_resource


      ## Pusher resource
      ##
      elif path_config['type'] == 'pusher':

         ## create a vanilla session: the pusher will use this to inject events
         ##
         pusher_session_config = ComponentConfig(realm = path_config['realm'], extra = None)
         pusher_session = ApplicationSession(pusher_session_config)

         ## add the pushing session to the router
         ##
         self.session_factory.add(pusher_session, authrole = path_config.get('role', 'anonymous'))

         ## now create the pusher Twisted Web resource
         ##
         return PusherResource(path_config.get('options', {}), pusher_session)


      ## Schema Docs resource
      ##
      elif path_config['type'] == 'schemadoc':

         realm = path_config['realm']

         if not realm in self.realm_to_id:
            raise ApplicationError("crossbar.error.no_such_object", "No realm with URI '{}' configured".format(realm))

         realm_id = self.realm_to_id[realm]

         realm_schemas = self.realms[realm_id].session._schemas

         return SchemaDocResource(self._templates, realm, realm_schemas)


      ## Nested subpath resource
      ##
      elif path_config['type'] == 'path':

         nested_paths = path_config.get('paths', {})

         if '/' in nested_paths:
            nested_resource = self.create_resource(nested_paths['/'])
         else:
            nested_resource = Resource()

         ## nest subpaths under the current entry
         ##
         self.add_paths(nested_resource, nested_paths)

         return nested_resource

      else:
         raise ApplicationError("crossbar.error.invalid_configuration", "invalid Web path type '{}'".format(path_config['type']))
def registerEndpoints(args):
    root = Resource()
    root.putChild(b'', IndexResource())
    root.putChild(b'metrics', VMWareMetricsResource(args))
    root.putChild(b'healthz', HealthzResource())
    return root
Beispiel #9
0
def start_webserver(options,
                    protocol=vtk_wamp.ServerProtocol,
                    disableLogging=False):
    """
    Starts the web-server with the given protocol. Options must be an object
    with the following members:
        options.host : the interface for the web-server to listen on
        options.port : port number for the web-server to listen on
        options.timeout : timeout for reaping process on idle in seconds
        options.content : root for web-pages to serve.
    """
    from twisted.internet import reactor
    from twisted.web.server import Site
    from twisted.web.static import File
    import sys

    if not disableLogging:
        log.startLogging(sys.stdout)

    contextFactory = None

    use_SSL = False
    if options.sslKey and options.sslCert:
        use_SSL = True
        wsProtocol = "wss"
        from twisted.internet import ssl
        contextFactory = ssl.DefaultOpenSSLContextFactory(
            options.sslKey, options.sslCert)
    else:
        wsProtocol = "ws"

    # Create WAMP router factory
    from autobahn.twisted.wamp import RouterFactory
    router_factory = RouterFactory()

    # create a user DB
    authdb = vtk_wamp.AuthDb()

    # create a WAMP router session factory
    from autobahn.twisted.wamp import RouterSessionFactory
    session_factory = RouterSessionFactory(router_factory)
    session_factory.session = vtk_wamp.CustomWampCraRouterSession
    session_factory.authdb = authdb

    # Create ApplicationSession and register protocols
    appSession = protocol(types.ComponentConfig(realm="vtkweb"))
    appSession.setAuthDB(authdb)
    session_factory.add(appSession)

    # create a WAMP-over-WebSocket transport server factory
    transport_factory = vtk_wamp.TimeoutWampWebSocketServerFactory(session_factory, \
           url        = "%s://%s:%d" % (wsProtocol, options.host, options.port),    \
           debug      = options.debug,                                              \
           debug_wamp = options.debug,                                              \
           timeout    = options.timeout )

    root = Resource()

    # Do we serve static content or just websocket ?
    if len(options.content) > 0:
        # Static HTTP + WebSocket
        root = File(options.content)

    # Handle possibly complex ws endpoint
    if not options.nows:
        wsResource = WebSocketResource(transport_factory)
        handle_complex_resource_path(options.ws, root, wsResource)

    # Handle possibly complex lp endpoint
    if not options.nolp:
        lpResource = WampLongPollResource(session_factory)
        handle_complex_resource_path(options.lp, root, lpResource)

    if options.uploadPath != None:
        from upload import UploadPage
        uploadResource = UploadPage(options.uploadPath)
        root.putChild("upload", uploadResource)

    site = Site(root)

    if use_SSL:
        reactor.listenSSL(options.port, site, contextFactory)
    else:
        reactor.listenTCP(options.port, site)

    # Work around to force the output buffer to be flushed
    # This allow the process launcher to parse the output and
    # wait for "Start factory" to know that the WebServer
    # is running.
    if options.forceFlush:
        for i in range(200):
            log.msg("+" * 80, logLevel=logging.CRITICAL)

    # Initialize testing: checks if we're doing a test and sets it up
    testing.initialize(options, reactor, stop_webserver)

    # Start the reactor
    if options.nosignalhandlers:
        reactor.run(installSignalHandlers=0)
    else:
        reactor.run()

    # Give the testing module a chance to finalize, if necessary
    testing.finalize()
Beispiel #10
0
 def setUp(self):
     self.pagename = ''
     self.indexResource = server.IndexResource()
     self.root = Resource()
     self.root.putChild(self.pagename, self.indexResource)
Beispiel #11
0
 def setUp(self):
     self.pagename = 'howto.html'
     self.howtoResource = server.HowtoResource()
     self.root = Resource()
     self.root.putChild(self.pagename, self.howtoResource)
Beispiel #12
0
    def __init__(self, reactor, top, note_dirty, read_only_dbs, writable_db,
                 http_endpoint, ws_endpoint, root_cap, title):
        self.__http_port = http_endpoint
        self.__ws_port = ws_endpoint

        self.__ws_protocol = txws.WebSocketFactory(
            OurStreamFactory(top, root_cap))

        # Roots of resource trees
        # - appRoot is everything stateful/authority-bearing
        # - serverRoot is the HTTP '/' and static resources are placed there
        serverRoot = _make_static(staticResourcePath)
        if root_cap is None:
            appRoot = serverRoot
            self.__visit_path = '/'
        else:
            serverRoot = _make_static(staticResourcePath)
            appRoot = _SlashedResource()
            serverRoot.putChild(root_cap, appRoot)
            self.__visit_path = '/' + urllib.quote(root_cap, safe='') + '/'

        # UI entry point
        appRoot.putChild('', _RadioIndexHtmlResource(title))

        # Exported radio control objects
        appRoot.putChild('radio', BlockResource(top, note_dirty, notDeletable))

        # Frequency DB
        appRoot.putChild('dbs', shinysdr.db.DatabasesResource(read_only_dbs))
        appRoot.putChild('wdb', shinysdr.db.DatabaseResource(writable_db))

        # Debug graph
        appRoot.putChild('flow-graph', FlowgraphVizResource(reactor, top))

        # Construct explicit resources for merge.
        test = _reify(serverRoot, 'test')
        jasmine = _reify(test, 'jasmine')
        for name in ['jasmine.css', 'jasmine.js', 'jasmine-html.js']:
            jasmine.putChild(
                name,
                static.File(
                    os.path.join(os.path.dirname(__file__),
                                 'deps/jasmine/lib/jasmine-core/', name)))

        client = _reify(serverRoot, 'client')
        client.putChild(
            'openlayers',
            static.File(
                os.path.join(os.path.dirname(__file__), 'deps/openlayers')))
        client.putChild(
            'require.js',
            static.File(
                os.path.join(os.path.dirname(__file__), 'deps/require.js')))

        # Plugin resources
        load_list_css = []
        load_list_js = []
        plugin_resources = Resource()
        client.putChild('plugins', plugin_resources)
        for resource_def in getPlugins(IClientResourceDef, shinysdr.plugins):
            # Add the plugin's resource to static serving
            plugin_resources.putChild(resource_def.key, resource_def.resource)
            plugin_resource_url = '/client/plugins/' + urllib.quote(
                resource_def.key, safe='') + '/'
            # Tell the client to load the plugins
            # TODO constrain path values to be relative
            if resource_def.load_css_path is not None:
                load_list_css.append(plugin_resource_url +
                                     resource_def.load_cs_path)
            if resource_def.load_js_path is not None:
                # TODO constrain value to be in the directory
                load_list_js.append(plugin_resource_url +
                                    resource_def.load_js_path)

        # Client plugin list
        client.putChild(
            'plugin-index.json',
            static.Data(
                _json_encoder_for_values.encode({
                    u'css': load_list_css,
                    u'js': load_list_js,
                }).encode('utf-8'), 'application/json'))

        self.__site = server.Site(serverRoot)
        self.__ws_port_obj = None
        self.__http_port_obj = None
Beispiel #13
0
def createService(sname, config):
    global monitor_threads, monitors, servicename

    try:
        application = connexion.FlaskApp(__name__,
                                         specification_dir='swagger/')
        flask_app = application.app
        flask_app.url_map.strict_slashes = False
        anchore_engine.subsys.metrics.init_flask_metrics(
            flask_app, servicename=servicename)
        application.add_api('swagger.yaml', validate_responses=False)
    except Exception as err:
        traceback.print_exc()
        raise err

    try:
        myconfig = config['services'][sname]
        servicename = sname
    except Exception as err:
        raise err

    try:
        kick_timer = int(myconfig['cycle_timer_seconds'])
    except:
        kick_timer = 1

    doapi = False
    try:
        if myconfig['listen'] and myconfig['port'] and myconfig[
                'endpoint_hostname']:
            doapi = True
    except:
        doapi = False

    kwargs = {}
    kwargs['kick_timer'] = kick_timer
    kwargs['monitors'] = monitors
    kwargs['monitor_threads'] = monitor_threads
    kwargs['servicename'] = servicename

    if doapi:
        # start up flask service

        flask_site = WSGIResource(reactor,
                                  reactor.getThreadPool(),
                                  application=flask_app)
        realroot = Resource()
        realroot.putChild(
            b"v1",
            anchore_engine.services.common.getAuthResource(
                flask_site, sname, config))
        realroot.putChild(b"health",
                          anchore_engine.services.common.HealthResource())
        # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask
        root = rewrite.RewriterResource(realroot, default_version_rewrite)
        ret_svc = anchore_engine.services.common.createServiceAPI(
            root, sname, config)

        # start up the monitor as a looping call
        lc = LoopingCall(anchore_engine.services.common.monitor, **kwargs)
        lc.start(1)
    else:
        # start up the monitor as a timer service
        svc = internet.TimerService(1, anchore_engine.services.common.monitor,
                                    **kwargs)
        svc.setName(sname)
        ret_svc = svc

    return (ret_svc)
Beispiel #14
0
 def test_putChild(self):
     """
     L{WebSocketsResource.putChild} raises C{RuntimeError} when called.
     """
     self.assertRaises(RuntimeError, self.resource.putChild, b"foo",
                       Resource())

class PageResource(Resource):
    def render_GET(self, request):
        request.setHeader("Access-Control-Allow-Origin", "*")
        request.setHeader("Content-Type", "application/json")
        return " Hello World!"


if __name__ == "__main__":
    source = {
        "candb": MySQLDB(),
        "graph": Xlore(),
        "en_parser": stanford_parser.Parser()
    }
    root = Resource()
    #root.putChild("show", PageResource())
    root.putChild("show", File("./web_ui"))
    root.putChild("linking", LinkingResource(source))
    search = Resource()
    root.putChild("search", search)
    search.putChild("items", SearchResource(source))

    from twisted.internet import reactor

    reactor.listenTCP(5656, server.Site(root))
    try:
        reactor.run()
    except:
        print "excpet"
        reactor.stop()
Beispiel #16
0
    def __init__(self, log, host, port, backlog, coordinator, eventengine, database):
        '''
        Initialize the web interface.
        @param port: the port on which the web server should listen
        @param coordinator: an instance of the network coordinator in order to interact with it
        @param eventengine: an instance of the event engine in order to interact with it
        @param database: an instance of the database layer in order to interact with it
        '''
        self.host = host # web server interface
        self.port = port # web server listening port
        self.backlog = backlog # size of the listen queue
        self.coordinator = coordinator
        self.eventengine = eventengine
        self.db = database
        
        self.log = log

        root = Resource()
        site = Site(root)
        
        # Main page
        root.putChild("", Root())
        
        # Room management
        root.putChild("location_add", Location_add(self.db))
        root.putChild("location_added", Location_added(self.db))
        root.putChild("locations", Locations(self.db))
        root.putChild("location_del", Location_del(self.db))
        root.putChild("location_edit", Location_edit(self.db))
        root.putChild("location_edited", Location_edited(self.db))
        
        # Plugin management
        root.putChild("plugin_add", Plugin_add(self.db))
        root.putChild("plugin_add_do", Plugin_add_do(self.coordinator, self.db))
        root.putChild("plugin_status", Plugin_status(self.coordinator, self.db))
        root.putChild("plugins", Plugins(self.db))
        root.putChild("plugin_del", Plugin_del(self.db))
        root.putChild("plugin_edit", Plugin_edit(self.db))
        root.putChild("plugin_edited", Plugin_edited(self.db))
        
        # Device management
        root.putChild("device_add", Device_add(self.db))
        root.putChild("device_save", Device_save(self.db))
        root.putChild("device_list", Device_list(self.db))
        root.putChild("device_man", Device_management(self.db))
        root.putChild("device_del", Device_del(self.db))
        root.putChild("device_edit", Device_edit(self.db))
        root.putChild("history", History(self.db))
        root.putChild("control_type", Control_type(self.db))

        # Events
        root.putChild("event_create", Event_create(self.db))
        root.putChild("event_value_by_id", Event_value_by_id(self.db))
        root.putChild("event_getvalue", Event_getvalue(self.db))
        root.putChild("event_save", Event_save(self.eventengine, self.db))
        root.putChild("event_control_values_by_id", Event_control_values_by_id(self.db))
        root.putChild("event_control_types_by_id", Event_control_types_by_id(self.db))
        root.putChild("events", Events(self.db))
        root.putChild("event_del", Event_del(self.eventengine, self.db))

        root.putChild("css", File(os.path.join(houseagent.template_dir, 'css')))
        root.putChild("js", File(os.path.join(houseagent.template_dir, 'js')))
        root.putChild("images", File(os.path.join(houseagent.template_dir, 'images')))


        root.putChild("graphdata", GraphData())
        root.putChild("create_graph", CreateGraph(self.db))
        
        root.putChild("control", Control(self.db))        
        root.putChild("control_onoff", Control_onoff(self.coordinator))
        root.putChild("control_dimmer", Control_dimmer(self.coordinator))
        root.putChild("control_stat", Control_stat(self.coordinator))

        # Load plugin pages
        self.load_pages(root)
        
        try:
            reactor.listenTCP(self.port, site, self.backlog, self.host)
        except CannotListenError,e:
            log.critical("--> %s" % e)
            sys.exit(1)
Beispiel #17
0
    def create_resource_tree(self, web_client, redirect_root_to_web_client):
        """Create the resource tree for this Home Server.

        This in unduly complicated because Twisted does not support putting
        child resources more than 1 level deep at a time.

        Args:
            web_client (bool): True to enable the web client.
            redirect_root_to_web_client (bool): True to redirect '/' to the
            location of the web client. This does nothing if web_client is not
            True.
        """
        # list containing (path_str, Resource) e.g:
        # [ ("/aaa/bbb/cc", Resource1), ("/aaa/dummy", Resource2) ]
        desired_tree = [
            (CLIENT_PREFIX, self.get_resource_for_client()),
            (FEDERATION_PREFIX, self.get_resource_for_federation()),
            (CONTENT_REPO_PREFIX, self.get_resource_for_content_repo())
        ]
        if web_client:
            logger.info("Adding the web client.")
            desired_tree.append((WEB_CLIENT_PREFIX,
                                self.get_resource_for_web_client()))

        if web_client and redirect_root_to_web_client:
            self.root_resource = RootRedirect(WEB_CLIENT_PREFIX)
        else:
            self.root_resource = Resource()

        # ideally we'd just use getChild and putChild but getChild doesn't work
        # unless you give it a Request object IN ADDITION to the name :/ So
        # instead, we'll store a copy of this mapping so we can actually add
        # extra resources to existing nodes. See self._resource_id for the key.
        resource_mappings = {}
        for (full_path, resource) in desired_tree:
            logging.info("Attaching %s to path %s", resource, full_path)
            last_resource = self.root_resource
            for path_seg in full_path.split('/')[1:-1]:
                if not path_seg in last_resource.listNames():
                    # resource doesn't exist, so make a "dummy resource"
                    child_resource = Resource()
                    last_resource.putChild(path_seg, child_resource)
                    res_id = self._resource_id(last_resource, path_seg)
                    resource_mappings[res_id] = child_resource
                    last_resource = child_resource
                else:
                    # we have an existing Resource, use that instead.
                    res_id = self._resource_id(last_resource, path_seg)
                    last_resource = resource_mappings[res_id]

            # ===========================
            # now attach the actual desired resource
            last_path_seg = full_path.split('/')[-1]

            # if there is already a resource here, thieve its children and
            # replace it
            res_id = self._resource_id(last_resource, last_path_seg)
            if res_id in resource_mappings:
                # there is a dummy resource at this path already, which needs
                # to be replaced with the desired resource.
                existing_dummy_resource = resource_mappings[res_id]
                for child_name in existing_dummy_resource.listNames():
                    child_res_id = self._resource_id(existing_dummy_resource,
                                                     child_name)
                    child_resource = resource_mappings[child_res_id]
                    # steal the children
                    resource.putChild(child_name, child_resource)

            # finally, insert the desired resource in the right place
            last_resource.putChild(last_path_seg, resource)
            res_id = self._resource_id(last_resource, last_path_seg)
            resource_mappings[res_id] = resource

        return self.root_resource
Beispiel #18
0
    def __init__(self, sydent):
        self.sydent = sydent

        root = Resource()
        matrix = Resource()
        identity = Resource()
        api = Resource()
        v1 = self.sydent.servlets.v1
        v2 = self.sydent.servlets.v2

        validate = Resource()
        email = Resource()
        msisdn = Resource()
        emailReqCode = self.sydent.servlets.emailRequestCode
        emailValCode = self.sydent.servlets.emailValidate
        msisdnReqCode = self.sydent.servlets.msisdnRequestCode
        msisdnValCode = self.sydent.servlets.msisdnValidate
        getValidated3pid = self.sydent.servlets.getValidated3pid

        lookup = self.sydent.servlets.lookup
        bulk_lookup = self.sydent.servlets.bulk_lookup

        hash_details = self.sydent.servlets.hash_details
        lookup_v2 = self.sydent.servlets.lookup_v2

        threepid_v1 = Resource()
        threepid_v2 = Resource()
        bind = self.sydent.servlets.threepidBind
        unbind = self.sydent.servlets.threepidUnbind

        pubkey = Resource()
        ephemeralPubkey = Resource()

        pk_ed25519 = self.sydent.servlets.pubkey_ed25519

        root.putChild(b'_matrix', matrix)
        matrix.putChild(b'identity', identity)
        identity.putChild(b'api', api)
        identity.putChild(b'v2', v2)
        api.putChild(b'v1', v1)

        validate.putChild(b'email', email)
        validate.putChild(b'msisdn', msisdn)

        v1.putChild(b'validate', validate)

        v1.putChild(b'lookup', lookup)
        v1.putChild(b'bulk_lookup', bulk_lookup)

        v1.putChild(b'pubkey', pubkey)
        pubkey.putChild(b'isvalid', self.sydent.servlets.pubkeyIsValid)
        pubkey.putChild(b'ed25519:0', pk_ed25519)
        pubkey.putChild(b'ephemeral', ephemeralPubkey)
        ephemeralPubkey.putChild(b'isvalid',
                                 self.sydent.servlets.ephemeralPubkeyIsValid)

        threepid_v2.putChild(b'getValidated3pid', getValidated3pid)
        threepid_v2.putChild(b'bind', bind)
        threepid_v2.putChild(b'unbind', unbind)

        threepid_v1.putChild(b'getValidated3pid', getValidated3pid)
        threepid_v1.putChild(b'unbind', unbind)
        if self.sydent.enable_v1_associations:
            threepid_v1.putChild(b'bind', bind)

        v1.putChild(b'3pid', threepid_v1)

        email.putChild(b'requestToken', emailReqCode)
        email.putChild(b'submitToken', emailValCode)

        msisdn.putChild(b'requestToken', msisdnReqCode)
        msisdn.putChild(b'submitToken', msisdnValCode)

        v1.putChild(b'store-invite', self.sydent.servlets.storeInviteServlet)

        v1.putChild(b'sign-ed25519',
                    self.sydent.servlets.blindlySignStuffServlet)

        # v2
        # note v2 loses the /api so goes on 'identity' not 'api'
        identity.putChild(b'v2', v2)

        # v2 exclusive APIs
        v2.putChild(b'terms', self.sydent.servlets.termsServlet)
        account = self.sydent.servlets.accountServlet
        v2.putChild(b'account', account)
        account.putChild(b'register', self.sydent.servlets.registerServlet)
        account.putChild(b'logout', self.sydent.servlets.logoutServlet)

        # v2 versions of existing APIs
        v2.putChild(b'validate', validate)
        v2.putChild(b'pubkey', pubkey)
        v2.putChild(b'3pid', threepid_v2)
        v2.putChild(b'store-invite', self.sydent.servlets.storeInviteServlet)
        v2.putChild(b'sign-ed25519',
                    self.sydent.servlets.blindlySignStuffServlet)
        v2.putChild(b'lookup', lookup_v2)
        v2.putChild(b'hash_details', hash_details)

        self.factory = Site(root)
        self.factory.displayTracebacks = False
Beispiel #19
0
 def test_wrapResourceWeb(self):
     from twisted.web.resource import IResource, Resource
     root = Resource()
     wrapped = wrapResource(root, [self.checker])
     self.assertTrue(IResource.providedBy(wrapped))
Beispiel #20
0
    def register_resources(self, args: Namespace) -> None:
        from hathor.conf import HathorSettings
        from hathor.debug_resources import (
            DebugCrashResource,
            DebugLogResource,
            DebugMessAroundResource,
            DebugPrintResource,
            DebugRaiseResource,
            DebugRejectResource,
        )
        from hathor.mining.ws import MiningWebsocketFactory
        from hathor.p2p.resources import AddPeersResource, MiningInfoResource, MiningResource, StatusResource
        from hathor.profiler import get_cpu_profiler
        from hathor.profiler.resources import CPUProfilerResource, ProfilerResource
        from hathor.prometheus import PrometheusMetricsExporter
        from hathor.transaction.resources import (
            BlockAtHeightResource,
            CreateTxResource,
            DashboardTransactionResource,
            DecodeTxResource,
            GetBlockTemplateResource,
            GraphvizFullResource,
            GraphvizNeighboursResource,
            MempoolResource,
            PushTxResource,
            SubmitBlockResource,
            TransactionAccWeightResource,
            TransactionResource,
            TxParentsResource,
            ValidateAddressResource,
        )
        from hathor.version_resource import VersionResource
        from hathor.wallet.resources import (
            AddressResource,
            BalanceResource,
            HistoryResource,
            LockWalletResource,
            SendTokensResource,
            SignTxResource,
            StateWalletResource,
            UnlockWalletResource,
        )
        from hathor.wallet.resources.nano_contracts import (
            NanoContractDecodeResource,
            NanoContractExecuteResource,
            NanoContractMatchValueResource,
        )
        from hathor.wallet.resources.thin_wallet import (
            AddressBalanceResource,
            AddressHistoryResource,
            AddressSearchResource,
            SendTokensResource as SendTokensThinResource,
            TokenHistoryResource,
            TokenResource,
        )
        from hathor.websocket import HathorAdminWebsocketFactory, WebsocketStatsResource

        settings = HathorSettings()
        cpu = get_cpu_profiler()

        if args.prometheus:
            kwargs: Dict[str, Any] = {'metrics': self.manager.metrics}

            if args.data:
                kwargs['path'] = os.path.join(args.data, 'prometheus')
            else:
                raise ValueError('To run prometheus exporter you must have a data path')

            prometheus = PrometheusMetricsExporter(**kwargs)
            prometheus.start()

        if args.status:
            # TODO get this from a file. How should we do with the factory?
            root = Resource()
            wallet_resource = Resource()
            root.putChild(b'wallet', wallet_resource)
            thin_wallet_resource = Resource()
            root.putChild(b'thin_wallet', thin_wallet_resource)
            contracts_resource = Resource()
            wallet_resource.putChild(b'nano-contract', contracts_resource)
            p2p_resource = Resource()
            root.putChild(b'p2p', p2p_resource)
            graphviz = Resource()
            # XXX: reach the resource through /graphviz/ too, previously it was a leaf so this wasn't a problem
            graphviz.putChild(b'', graphviz)
            for fmt in ['dot', 'pdf', 'png', 'jpg']:
                bfmt = fmt.encode('ascii')
                graphviz.putChild(b'full.' + bfmt, GraphvizFullResource(self.manager, format=fmt))
                graphviz.putChild(b'neighbours.' + bfmt, GraphvizNeighboursResource(self.manager, format=fmt))

            resources = [
                (b'status', StatusResource(self.manager), root),
                (b'version', VersionResource(self.manager), root),
                (b'create_tx', CreateTxResource(self.manager), root),
                (b'decode_tx', DecodeTxResource(self.manager), root),
                (b'validate_address', ValidateAddressResource(self.manager), root),
                (b'push_tx',
                    PushTxResource(self.manager, args.max_output_script_size, args.allow_non_standard_script),
                    root),
                (b'graphviz', graphviz, root),
                (b'transaction', TransactionResource(self.manager), root),
                (b'block_at_height', BlockAtHeightResource(self.manager), root),
                (b'transaction_acc_weight', TransactionAccWeightResource(self.manager), root),
                (b'dashboard_tx', DashboardTransactionResource(self.manager), root),
                (b'profiler', ProfilerResource(self.manager), root),
                (b'top', CPUProfilerResource(self.manager, cpu), root),
                (b'mempool', MempoolResource(self.manager), root),
                # mining
                (b'mining', MiningResource(self.manager), root),
                (b'getmininginfo', MiningInfoResource(self.manager), root),
                (b'get_block_template', GetBlockTemplateResource(self.manager), root),
                (b'submit_block', SubmitBlockResource(self.manager), root),
                (b'tx_parents', TxParentsResource(self.manager), root),
                # /thin_wallet
                (b'address_history', AddressHistoryResource(self.manager), thin_wallet_resource),
                (b'address_balance', AddressBalanceResource(self.manager), thin_wallet_resource),
                (b'address_search', AddressSearchResource(self.manager), thin_wallet_resource),
                (b'send_tokens', SendTokensThinResource(self.manager), thin_wallet_resource),
                (b'token', TokenResource(self.manager), thin_wallet_resource),
                (b'token_history', TokenHistoryResource(self.manager), thin_wallet_resource),
                # /wallet/nano-contract
                (b'match-value', NanoContractMatchValueResource(self.manager), contracts_resource),
                (b'decode', NanoContractDecodeResource(self.manager), contracts_resource),
                (b'execute', NanoContractExecuteResource(self.manager), contracts_resource),
                # /p2p
                (b'peers', AddPeersResource(self.manager), p2p_resource),
            ]

            if args.enable_debug_api:
                debug_resource = Resource()
                root.putChild(b'_debug', debug_resource)
                resources.extend([
                    (b'log', DebugLogResource(), debug_resource),
                    (b'raise', DebugRaiseResource(), debug_resource),
                    (b'reject', DebugRejectResource(), debug_resource),
                    (b'print', DebugPrintResource(), debug_resource),
                ])
            if args.enable_crash_api:
                crash_resource = Resource()
                root.putChild(b'_crash', crash_resource)
                resources.extend([
                    (b'exit', DebugCrashResource(), crash_resource),
                    (b'mess_around', DebugMessAroundResource(self.manager), crash_resource),
                ])

            for url_path, resource, parent in resources:
                parent.putChild(url_path, resource)

            if self.manager.stratum_factory is not None:
                from hathor.stratum.resources import MiningStatsResource
                root.putChild(b'miners', MiningStatsResource(self.manager))

            with_wallet_api = bool(self.wallet and args.wallet_enable_api)
            if with_wallet_api:
                wallet_resources = (
                    # /wallet
                    (b'balance', BalanceResource(self.manager), wallet_resource),
                    (b'history', HistoryResource(self.manager), wallet_resource),
                    (b'address', AddressResource(self.manager), wallet_resource),
                    (b'send_tokens', SendTokensResource(self.manager), wallet_resource),
                    (b'sign_tx', SignTxResource(self.manager), wallet_resource),
                    (b'unlock', UnlockWalletResource(self.manager), wallet_resource),
                    (b'lock', LockWalletResource(self.manager), wallet_resource),
                    (b'state', StateWalletResource(self.manager), wallet_resource),
                )
                for url_path, resource, parent in wallet_resources:
                    parent.putChild(url_path, resource)

            # Websocket resource
            assert self.manager.tx_storage.indexes is not None
            ws_factory = HathorAdminWebsocketFactory(metrics=self.manager.metrics,
                                                     address_index=self.manager.tx_storage.indexes.addresses)
            ws_factory.start()
            root.putChild(b'ws', WebSocketResource(ws_factory))

            # Mining websocket resource
            mining_ws_factory = MiningWebsocketFactory(self.manager)
            root.putChild(b'mining_ws', WebSocketResource(mining_ws_factory))

            ws_factory.subscribe(self.manager.pubsub)

            # Websocket stats resource
            root.putChild(b'websocket_stats', WebsocketStatsResource(ws_factory))

            real_root = Resource()
            real_root.putChild(settings.API_VERSION_PREFIX.encode('ascii'), root)

            from hathor.profiler.site import SiteProfiler
            status_server = SiteProfiler(real_root)
            reactor.listenTCP(args.status, status_server)
            self.log.info('with status', listen=args.status, with_wallet_api=with_wallet_api)

            # Set websocket factory in metrics
            self.manager.metrics.websocket_factory = ws_factory
Beispiel #21
0
 def setUp(self):
     self.reactor = MemoryReactor()
     self.site = Site(Resource())
Beispiel #22
0
class DummyChannel:
    class TCP:
        port = 80
        disconnected = False

        def __init__(self, peer=None):
            if peer is None:
                peer = IPv4Address("TCP", "192.168.1.1", 12344)
            self._peer = peer
            self.written = BytesIO()
            self.producers = []

        def getPeer(self):
            return self._peer

        def write(self, data):
            if not isinstance(data, bytes):
                raise TypeError(
                    f"Can only write bytes to a transport, not {data!r}")
            self.written.write(data)

        def writeSequence(self, iovec):
            for data in iovec:
                self.write(data)

        def getHost(self):
            return IPv4Address("TCP", "10.0.0.1", self.port)

        def registerProducer(self, producer, streaming):
            self.producers.append((producer, streaming))

        def unregisterProducer(self):
            pass

        def loseConnection(self):
            self.disconnected = True

    @implementer(ISSLTransport)
    class SSL(TCP):
        def abortConnection(self):
            # ITCPTransport.abortConnection
            pass

        def getTcpKeepAlive(self):
            # ITCPTransport.getTcpKeepAlive
            pass

        def getTcpNoDelay(self):
            # ITCPTransport.getTcpNoDelay
            pass

        def loseWriteConnection(self):
            # ITCPTransport.loseWriteConnection
            pass

        def setTcpKeepAlive(self, enabled):
            # ITCPTransport.setTcpKeepAlive
            pass

        def setTcpNoDelay(self, enabled):
            # ITCPTransport.setTcpNoDelay
            pass

        def getPeerCertificate(self):
            # ISSLTransport.getPeerCertificate
            pass

    site = Site(Resource())

    def __init__(self, peer=None):
        self.transport = self.TCP(peer)

    def requestDone(self, request):
        pass

    def writeHeaders(self, version, code, reason, headers):
        response_line = version + b" " + code + b" " + reason + b"\r\n"
        headerSequence = [response_line]
        headerSequence.extend(name + b": " + value + b"\r\n"
                              for name, value in headers)
        headerSequence.append(b"\r\n")
        self.transport.writeSequence(headerSequence)

    def getPeer(self):
        return self.transport.getPeer()

    def getHost(self):
        return self.transport.getHost()

    def registerProducer(self, producer, streaming):
        self.transport.registerProducer(producer, streaming)

    def unregisterProducer(self):
        self.transport.unregisterProducer()

    def write(self, data):
        self.transport.write(data)

    def writeSequence(self, iovec):
        self.transport.writeSequence(iovec)

    def loseConnection(self):
        self.transport.loseConnection()

    def endRequest(self):
        pass

    def isSecure(self):
        return isinstance(self.transport, self.SSL)

    def abortConnection(self):
        # ITCPTransport.abortConnection
        pass

    def getTcpKeepAlive(self):
        # ITCPTransport.getTcpKeepAlive
        pass

    def getTcpNoDelay(self):
        # ITCPTransport.getTcpNoDelay
        pass

    def loseWriteConnection(self):
        # ITCPTransport.loseWriteConnection
        pass

    def setTcpKeepAlive(self):
        # ITCPTransport.setTcpKeepAlive
        pass

    def setTcpNoDelay(self):
        # ITCPTransport.setTcpNoDelay
        pass

    def getPeerCertificate(self):
        # ISSLTransport.getPeerCertificate
        pass
Beispiel #23
0
    def render_GET(self, request):
        r = ('Welcome to Scrapy Book. Try: '
                '<a href="properties/index_00000.html">properties</a> '
                '<a href="images">images</a>, '
                '<a href="dynamic">dynamic</a>, '
                '<a href="benchmark/">benchmark</a> '
                '<a href="maps/api/geocode/json?sensor=false&address=Camden%20Town%2C%20London">maps</a>')
        return r.encode('utf-8')

if __name__ == '__main__':

    path = os.path.dirname(os.path.abspath(__file__))

    os.chdir(path)
    
    root = Resource()

    root.model = Model()
    root.putChild(b'', Home())
    benchmark = Resource()
    root.putChild(b'benchmark', benchmark)
    benchmark.putChild(b'', Benchmark())
    benchmark.putChild(b'api', Api())
    benchmark.putChild(b'index', Index())
    benchmark.putChild(b'detail', Detail())
    root.putChild(b'properties', Properties(root.model))
    root.putChild(b'maps', Maps(root.model))
    root.putChild(b'images', File('images'))
    root.putChild(b'static', File('static'))
    root.putChild(b'dynamic', Dynamic())
Beispiel #24
0
def create_root(config, settings_module):
    from scrapy.settings import Settings
    from .specmanager import SpecManager
    from .authmanager import AuthManager
    from .projectspec import create_project_resource
    from slyd.api import APIResource
    from slyd.bot import create_bot_resource
    from slyd.projects import create_projects_manager_resource

    from slyd.splash.ferry import (FerryServerProtocol, FerryServerFactory,
                                   create_ferry_resource)
    from slyd.splash.proxy import ProxyResource

    root = Resource()
    static = Resource()
    for file_name in listdir(config['docroot']):
        file_path = join(config['docroot'], file_name)
        if isfile(file_path):
            static.putChild(file_name, File(file_path))
    static.putChild('main.html', File(join(config['docroot'], 'index.html')))

    root.putChild('static', static)
    root.putChild('assets', File(join(config['docroot'], 'assets')))
    root.putChild('fonts', File(join(config['docroot'], 'assets', 'fonts')))
    root.putChild('', File(join(config['docroot'], 'index.html')))

    settings = Settings()
    settings.setmodule(settings_module)
    spec_manager = SpecManager(settings)

    # add server capabilities at /server_capabilities
    capabilities = Capabilities(spec_manager)
    root.putChild('server_capabilities', capabilities)

    # add projects manager at /projects
    projects = create_projects_manager_resource(spec_manager)
    root.putChild('projects', projects)

    # # add json api routes
    root.putChild('api', APIResource(spec_manager))

    # add crawler at /projects/PROJECT_ID/bot
    projects.putChild('bot', create_bot_resource(spec_manager))

    # add project spec at /projects/PROJECT_ID/spec
    spec = create_project_resource(spec_manager)
    projects.putChild('spec', spec)

    # add websockets for communicating with splash
    factory = FerryServerFactory("ws://127.0.0.1:%s" % config['port'],
                                 debug=False,
                                 assets=config['docroot'])
    factory.protocol = FerryServerProtocol
    factory.setProtocolOptions(allowHixie76=True)
    websocket = create_ferry_resource(spec_manager, factory)
    root.putChild("ws", websocket)

    root.putChild('proxy', ProxyResource())

    auth_manager = AuthManager(settings)
    return auth_manager.protectResource(root)
Beispiel #25
0
    def register_resources(self, args: Namespace) -> None:
        from hathor.conf import HathorSettings
        from hathor.p2p.resources import AddPeersResource, MiningInfoResource, MiningResource, StatusResource
        from hathor.prometheus import PrometheusMetricsExporter
        from hathor.resources import ProfilerResource
        from hathor.transaction.resources import (
            DashboardTransactionResource,
            DecodeTxResource,
            GraphvizLegacyResource,
            GraphvizFullResource,
            GraphvizNeighboursResource,
            PushTxResource,
            TipsHistogramResource,
            TipsResource,
            TransactionAccWeightResource,
            TransactionResource,
        )
        from hathor.version_resource import VersionResource
        from hathor.wallet.resources import (
            AddressResource,
            BalanceResource,
            HistoryResource,
            LockWalletResource,
            SendTokensResource,
            SignTxResource,
            StateWalletResource,
            UnlockWalletResource,
        )
        from hathor.wallet.resources.thin_wallet import (
            AddressHistoryResource, SendTokensResource as
            SendTokensThinResource, TokenHistoryResource, TokenResource)
        from hathor.wallet.resources.nano_contracts import (
            NanoContractDecodeResource,
            NanoContractExecuteResource,
            NanoContractMatchValueResource,
        )
        from hathor.websocket import HathorAdminWebsocketFactory, WebsocketStatsResource
        from hathor.stratum.resources import MiningStatsResource

        settings = HathorSettings()

        if args.prometheus:
            kwargs: Dict[str, Any] = {'metrics': self.manager.metrics}

            if args.data:
                kwargs['path'] = os.path.join(args.data, 'prometheus')
            else:
                raise ValueError(
                    'To run prometheus exporter you must have a data path')

            prometheus = PrometheusMetricsExporter(**kwargs)
            prometheus.start()

        if args.status:
            # TODO get this from a file. How should we do with the factory?
            root = Resource()
            wallet_resource = Resource()
            root.putChild(b'wallet', wallet_resource)
            thin_wallet_resource = Resource()
            root.putChild(b'thin_wallet', thin_wallet_resource)
            contracts_resource = Resource()
            wallet_resource.putChild(b'nano-contract', contracts_resource)
            p2p_resource = Resource()
            root.putChild(b'p2p', p2p_resource)
            graphviz = GraphvizLegacyResource(self.manager)
            # XXX: reach the resource through /graphviz/ too, previously it was a leaf so this wasn't a problem
            graphviz.putChild(b'', graphviz)
            for fmt in ['dot', 'pdf', 'png', 'jpg']:
                bfmt = fmt.encode('ascii')
                graphviz.putChild(
                    b'full.' + bfmt,
                    GraphvizFullResource(self.manager, format=fmt))
                graphviz.putChild(
                    b'neighbours.' + bfmt,
                    GraphvizNeighboursResource(self.manager, format=fmt))

            resources = (
                (b'status', StatusResource(self.manager), root),
                (b'version', VersionResource(self.manager), root),
                (b'mining', MiningResource(self.manager), root),
                (b'getmininginfo', MiningInfoResource(self.manager), root),
                (b'decode_tx', DecodeTxResource(self.manager), root),
                (b'push_tx', PushTxResource(self.manager), root),
                (b'graphviz', graphviz, root),
                (b'tips-histogram', TipsHistogramResource(self.manager), root),
                (b'tips', TipsResource(self.manager), root),
                (b'transaction', TransactionResource(self.manager), root),
                (b'transaction_acc_weight',
                 TransactionAccWeightResource(self.manager), root),
                (b'dashboard_tx', DashboardTransactionResource(self.manager),
                 root),
                (b'profiler', ProfilerResource(self.manager), root),
                # /thin_wallet
                (b'address_history', AddressHistoryResource(self.manager),
                 thin_wallet_resource),
                (b'send_tokens', SendTokensThinResource(self.manager),
                 thin_wallet_resource),
                (b'token', TokenResource(self.manager), thin_wallet_resource),
                (b'token_history', TokenHistoryResource(self.manager),
                 thin_wallet_resource),
                # /wallet/nano-contract
                (b'match-value', NanoContractMatchValueResource(self.manager),
                 contracts_resource),
                (b'decode', NanoContractDecodeResource(self.manager),
                 contracts_resource),
                (b'execute', NanoContractExecuteResource(self.manager),
                 contracts_resource),
                # /p2p
                (b'peers', AddPeersResource(self.manager), p2p_resource),
            )
            for url_path, resource, parent in resources:
                parent.putChild(url_path, resource)

            if self.manager.stratum_factory is not None:
                root.putChild(b'miners', MiningStatsResource(self.manager))

            if self.wallet and args.wallet_enable_api:
                wallet_resources = (
                    # /wallet
                    (b'balance', BalanceResource(self.manager), wallet_resource
                     ),
                    (b'history', HistoryResource(self.manager),
                     wallet_resource),
                    (b'address', AddressResource(self.manager),
                     wallet_resource),
                    (b'send_tokens', SendTokensResource(self.manager),
                     wallet_resource),
                    (b'sign_tx', SignTxResource(self.manager),
                     wallet_resource),
                    (b'unlock', UnlockWalletResource(self.manager),
                     wallet_resource),
                    (b'lock', LockWalletResource(self.manager),
                     wallet_resource),
                    (b'state', StateWalletResource(self.manager),
                     wallet_resource),
                )
                for url_path, resource, parent in wallet_resources:
                    parent.putChild(url_path, resource)

            # Websocket resource
            ws_factory = HathorAdminWebsocketFactory(
                metrics=self.manager.metrics,
                wallet_index=self.manager.tx_storage.wallet_index)
            ws_factory.start()
            resource = WebSocketResource(ws_factory)
            root.putChild(b"ws", resource)

            ws_factory.subscribe(self.manager.pubsub)

            # Websocket stats resource
            root.putChild(b'websocket_stats',
                          WebsocketStatsResource(ws_factory))

            real_root = Resource()
            real_root.putChild(settings.API_VERSION_PREFIX.encode('ascii'),
                               root)
            status_server = server.Site(real_root)
            reactor.listenTCP(args.status, status_server)

            # Set websocket factory in metrics
            self.manager.metrics.websocket_factory = ws_factory
Beispiel #26
0
def create_log_resources():
    logs = Resource()
    logs.putChild(b"v1", create_log_streaming_resource())
    return logs
Beispiel #27
0
            self.result = result[len(result) - 1]
            print("Hello : ", self.result)
            requests.write('''
                          <html><body>
                          <br>CDR : %s <br>
                          </body></html>
                          ''' % self.result)
            requests.finish()
        else:
            print("Empty")
            requests.write('''
                          <html><body>
                          <br>No Call Details Found<br>
                          </body></html>
                          ''')
            requests.finish()

    def err_db(self, error):
        """On database error"""
        print('error db')
        print(error)


if __name__ == "__main__":
    root = Resource()
    root.putChild("form1", FormPage())
    root.putChild("form2", NewPage())
    factory = Site(root)
    reactor.listenTCP(8880, factory)
    reactor.run()
Beispiel #28
0
    def __init__(self, log, host, port, backlog, coordinator, eventengine,
                 database):
        '''
        Initialize the web interface.
        @param port: the port on which the web server should listen
        @param coordinator: an instance of the network coordinator in order to interact with it
        @param eventengine: an instance of the event engine in order to interact with it
        @param database: an instance of the database layer in order to interact with it
        '''
        self.host = host  # web server interface
        self.port = port  # web server listening port
        self.backlog = backlog  # size of the listen queue
        self.coordinator = coordinator
        self.eventengine = eventengine
        self.db = database

        self.log = log

        root = Resource()
        site = Site(root)

        # Main page
        root.putChild("", Root())

        # Location management
        root.putChild('locations', Locations(self.db))
        root.putChild('locations_view', Locations_view())

        # Plugin management
        root.putChild('plugins', Plugins(self.db, self.coordinator))
        root.putChild('plugins_view', Plugins_view())

        # Device management
        root.putChild('devices', Devices(self.db))
        root.putChild('devices_view', Devices_view())

        # Device control
        root.putChild("control", Control(self.db))

        # Value management
        root.putChild('values', Values(self.db, self.coordinator))
        root.putChild('values_view', Values_view())
        root.putChild('history_types', HistoryTypes(self.db))
        root.putChild('history_periods', HistoryPeriods(self.db))
        root.putChild('control_types', ControlTypes(self.db))

        # Events
        root.putChild("event_create", Event_create(self.db))
        root.putChild("event_value_by_id", Event_value_by_id(self.db))
        root.putChild("event_getvalue", Event_getvalue(self.db))
        root.putChild("event_save", Event_save(self.eventengine, self.db))
        root.putChild("event_control_values_by_id",
                      Event_control_values_by_id(self.db))
        root.putChild("event_control_types_by_id",
                      Event_control_types_by_id(self.db))
        root.putChild("events", Events(self.db))
        root.putChild("event_del", Event_del(self.eventengine, self.db))

        # Graphing
        root.putChild("create_graph", CreateGraph(self.db))
        root.putChild("graph_latest", GraphLatest(self.db))
        root.putChild("graph_daily", GraphDaily(self.db))

        # Static files
        root.putChild("css", File(os.path.join(houseagent.template_dir,
                                               'css')))
        root.putChild("js", File(os.path.join(houseagent.template_dir, 'js')))
        root.putChild("images",
                      File(os.path.join(houseagent.template_dir, 'images')))

        # Load plugin pages
        self.load_pages(root)

        try:
            reactor.listenTCP(self.port, site, self.backlog, self.host)
        except CannotListenError, e:
            log.critical("--> %s" % e)
            sys.exit(1)
 def setUp(self):
     self.pagename = b''
     self.root = Resource()
Beispiel #30
0
def _create_vulnerable_tree():
    private = Resource()
    private.putChild(b"logs", create_log_resources())
    return private