Пример #1
0
def makeService(config):
    """
    Create a punjab service to run
    """
    from twisted.web import  server, resource, static
    from twisted.application import internet

    import httpb

    serviceCollection = PunjabService()

    if config['html_dir']:
        r = static.File(config['html_dir'])
    else:
        print "The html directory is needed."
        return

    if config['white_list']:
        httpb.HttpbService.white_list = config['white_list'].split(',')

    if config['black_list']:
        httpb.HttpbService.black_list = config['black_list'].split(',')

    if config['httpb']:
        b = httpb.HttpbService(config['verbose'], config['polling'])
        if config['httpb'] == '':
            r.putChild('http-bind', resource.IResource(b))
        else:
            r.putChild(config['httpb'], resource.IResource(b))

    if config['site_log_file']:
        site  = server.Site(r, logPath=config['site_log_file'])
    else:
        site  = server.Site(r)

    if config['ssl']:
        from OpenSSL import SSL
        from punjab.ssl import OpenSSLContextFactoryChaining
        ssl_context = OpenSSLContextFactoryChaining(config['ssl_privkey'],
                                                       config['ssl_cert'],
                                                       SSL.SSLv23_METHOD,)
        sm = internet.SSLServer(int(config['port']),
                                site,
                                ssl_context,
                                backlog = int(config['verbose']))
        sm.setServiceParent(serviceCollection)
    else:
        sm = internet.TCPServer(int(config['port']), site)

        sm.setServiceParent(serviceCollection)

    serviceCollection.httpb = b

    return serviceCollection
    def getChild(self, path, request):
        """See twisted.web.Resource.getChild.
        """
        self.restat()

        if not self.isdir():
            return self.childNotFound

        if path:
            fpath = self.child(path)
        else:
            fpath = self.childSearchPreauth(*self.indexNames)
            if fpath is None:
                return self.directoryListing()

        if not fpath.exists():
            fpath = fpath.siblingExtensionSearch(*self.ignoredExts)
            if fpath is None:
                return self.childNotFound

        if platformType == "win32":
            # don't want .RPY to be different than .rpy, since that would allow
            # source disclosure.
            processor = InsensitiveDict(self.processors).get(
                fpath.splitext()[1])
        else:
            processor = self.processors.get(fpath.splitext()[1])
        if processor:
            return resource.IResource(processor(fpath.path, self.registry))
        return self.createSimilarFile(fpath.path)
Пример #3
0
    def getChild(self, path, request):
        """
        If this L{File}"s path refers to a directory, return a L{File}
        referring to the file named C{path} in that directory.

        If C{path} is the empty string, return a L{DirectoryLister}
        instead.

        @param path: The current path segment.
        @type path: L{bytes}

        @param request: The incoming request.
        @type request: An that provides L{twisted.web.iweb.IRequest}.

        @return: A resource representing the requested file or
            directory, or L{NoResource} if the path cannot be
            accessed.
        @rtype: An object that provides L{resource.IResource}.
        """
        if isinstance(path, bytes):
            try:
                # Request calls urllib.unquote on each path segment,
                # leaving us with raw bytes.
                path = path.decode('utf-8')
            except UnicodeDecodeError:
                log.err(
                    None,
                    "Could not decode path segment as utf-8: %r" % (path, ))
                return self.childNotFound

        self.restat(reraise=False)

        if not self.isdir():
            return self.childNotFound

        if path:
            try:
                fpath = self.child(path)
            except filepath.InsecurePath:
                return self.childNotFound
        else:
            fpath = self.childSearchPreauth(*self.indexNames)
            if fpath is None:
                return self.directoryListing()

        if not fpath.exists():
            fpath = fpath.siblingExtensionSearch(*self.ignoredExts)
            if fpath is None:
                return self.childNotFound

        extension = fpath.splitext()[1]
        if platformType == "win32":
            # don't want .RPY to be different than .rpy, since that would allow
            # source disclosure.
            processor = InsensitiveDict(self.processors).get(extension)
        else:
            processor = self.processors.get(extension)
        if processor:
            return resource.IResource(processor(fpath.path, self.registry))
        return self.createSimilarFile(fpath.path)
Пример #4
0
def makeService(config):
    # finger on port 79
    s = service.MultiService()
    f = FingerService(config['file'])
    h = internet.TCPServer(1079, IFingerFactory(f))
    h.setServiceParent(s)

    # website on port 8000
    r = resource.IResource(f)
    r.templateDirectory = config['templates']
    site = server.Site(r)
    j = internet.TCPServer(8000, site)
    j.setServiceParent(s)

    # ssl on port 443
    #    if config.get('ssl'):
    #        k = internet.SSLServer(443, site, ServerContextFactory())
    #        k.setServiceParent(s)

    # irc fingerbot
    if config.has_key('ircnick'):
        i = IIRCClientFactory(f)
        i.nickname = config['ircnick']
        ircserver = config['ircserver']
        b = internet.TCPClient(ircserver, 6667, i)
        b.setServiceParent(s)

    # Pespective Broker on port 8889
    if config.has_key('pbport'):
        m = internet.TCPServer(int(config['pbport']),
                               pb.PBServerFactory(IPerspectiveFinger(f)))
        m.setServiceParent(s)

    return s
Пример #5
0
 def setUp(self):
     self.destroyDb()
     settings = pokernetworkconfig.Config([])
     settings.doc = libxml2.parseMemory(settings_xml, len(settings_xml))
     settings.header = settings.doc.xpathNewContext()
     self.service = pokerservice.PokerService(settings)
     if verbose >= 0: self.service.verbose = verbose
     site = server.Site(resource.IResource(self.service))
     self.p = reactor.listenTCP(0, site, interface="127.0.0.1")
     self.port = self.p.getHost().port
     self.cookie = None
Пример #6
0
def get_script_response(resource_path, request):
    try:
        res = utils.exec_cached_script(resource_path)
        # If the script exports an `app` variable, load it as a WSGI resource
        if "app" in res:
            return WSGIResource(reactor, reactor.getThreadPool(), res["app"])
        elif "get_resource" in res:
            return resource.IResource(res["get_resource"](request))
        raise Exception("Script does not export `app` variable or `get_resource` function.")
    except:
        # Catch all exceptions and log them
        logger.exception("Unahandled exception in exec'd file '{}'".format(resource_path))
    return SimpleResource(500)
Пример #7
0
        def _getChild(request):
            headers = {}
            resource_path = request_path

            route_descriptor, route_match = self.routes.get_descriptor(*request_parts)
            if route_descriptor is not None:
                headers = route_descriptor.get("headers", {})

                # Forward case
                if "forward" in route_descriptor:
                    # Recreate the URL
                    if route_descriptor.get("recreate_url", True):
                        fscheme, fnetloc, _, _, _, _ = urllib.parse.urlparse(route_descriptor["forward"])
                        url = urllib.parse.urlunparse((fscheme, fnetloc, request.uri.decode("UTF-8"), "", "", ""))
                    else:
                        url = route_descriptor["forward"]

                    replace = route_descriptor.get("replace", [])
                    return ForwardResource(url, headers=headers, replace=replace)
                # Code case
                elif "code" in route_descriptor:
                    code = route_descriptor.get("code", 200)
                    body = route_descriptor.get("body", "")
                    return SimpleResource(request_path, code, headers=headers, body=body)
                # Path case
                elif "path" in route_descriptor:
                    resource_path = route_descriptor["path"]
                    # Replace regex groups in the route path
                    for i, group in enumerate(re.search(route_descriptor["route"], route_match).groups()):
                        if group is not None:
                            resource_path = resource_path.replace("${}".format(i + 1), group)

                # Security: Ensure the absolute resource_path is within the wwwroot!
                # Prepend the resource_path with the wwwroot and canonicalize
                resource_path = os.path.abspath(os.path.join(self.filesroot, resource_path.lstrip("/")))
                if resource_path.startswith(self.filesroot):
                    if os.path.exists(resource_path):
                        # Security: Don't show the soruce of python files!
                        if os.path.splitext(resource_path)[1].lower() == ".py":
                            try:
                                res = exec_cached_script(resource_path)
                                return resource.IResource(res["get_resource"](request))
                            except Exception:
                                logger.exception("Unahandled exception in exec'd file '{}'".format(resource_path))
                        else:
                            with open(resource_path, "r") as f:
                                data = f.read()
                            return SimpleResource(request_path, 200, headers=headers, body=data)

            # Default handling, 404 here
            return super().getChild(name, request)
Пример #8
0
    def setUp(self):
        # set up punjab
        html_dir = "./html"
        if not os.path.exists(html_dir):
            os.mkdir(html_dir)  # create directory in _trial_temp
        self.root = static.File(
            html_dir)  # make _trial_temp/html the root html directory
        self.rid = random.randint(0, 10000000)
        self.hbs = HttpbService(1)
        self.b = resource.IResource(self.hbs)
        self.root.putChild('xmpp-bosh', self.b)

        self.site = server.Site(self.root)

        self.p = reactor.listenTCP(0, self.site, interface="127.0.0.1")
        self.port = self.p.getHost().port

        # set up proxy

        self.proxy = httpb_client.Proxy(self.getURL())
        self.sid = None
        self.keys = httpb_client.Keys()

        # set up dummy xmpp server

        self.server_service = xmppserver.XMPPServerService()
        self.server_factory = xmppserver.IXMPPServerFactory(
            self.server_service)
        self.server = reactor.listenTCP(0,
                                        self.server_factory,
                                        interface="127.0.0.1")
        self.server_port = self.server.socket.getsockname()[1]

        # Hook the server's buildProtocol to make the protocol instance
        # accessible to tests.
        buildProtocol = self.server_factory.buildProtocol
        d1 = defer.Deferred()

        def _rememberProtocolInstance(addr):
            self.server_protocol = buildProtocol(addr)
            # keeping this around because we may want to wrap this specific to tests
            # self.server_protocol = protocol.wrappedProtocol
            d1.callback(None)
            return self.server_protocol

        self.server_factory.buildProtocol = _rememberProtocolInstance
Пример #9
0
def makeService(config):
    # finger on port 79
    s = service.MultiService()
    f = FingerService(config["file"])
    h = strports.service("tcp:1079", IFingerFactory(f))
    h.setServiceParent(s)

    # website on port 8000
    r = resource.IResource(f)
    r.templateDirectory = config["templates"]
    site = server.Site(r)
    j = strports.service("tcp:8000", site)
    j.setServiceParent(s)

    # ssl on port 443
    #    if config.get('ssl'):
    #        k = strports.service(
    #            "ssl:port=443:certKey=cert.pem:privateKey=key.pem", site
    #        )
    #        k.setServiceParent(s)

    # irc fingerbot
    if "ircnick" in config:
        i = IIRCClientFactory(f)
        i.nickname = config["ircnick"]
        ircserver = config["ircserver"]
        b = internet.ClientService(
            endpoints.HostnameEndpoint(reactor, ircserver, 6667), i)
        b.setServiceParent(s)

    # Pespective Broker on port 8889
    if "pbport" in config:
        m = internet.StreamServerEndpointService(
            endpoints.TCP4ServerEndpoint(reactor, int(config["pbport"])),
            pb.PBServerFactory(IPerspectiveFinger(f)),
        )
        m.setServiceParent(s)

    return s
Пример #10
0
    def getChild(self, path, request):
        """
        If this L{File}'s path refers to a directory, return a L{File}
        referring to the file named C{path} in that directory.

        If C{path} is the empty string, return a L{DirectoryLister} instead.
        """
        self.restat(reraise=False)

        if not self.isdir():
            return self.childNotFound

        if path:
            try:
                fpath = self.child(path)
            except filepath.InsecurePath:
                return self.childNotFound
        else:
            fpath = self.childSearchPreauth(*self.indexNames)
            if fpath is None:
                return self.directoryListing()

        if not fpath.exists():
            fpath = fpath.siblingExtensionSearch(*self.ignoredExts)
            if fpath is None:
                return self.childNotFound

        if platformType == "win32":
            # don't want .RPY to be different than .rpy, since that would allow
            # source disclosure.
            processor = InsensitiveDict(self.processors).get(
                fpath.splitext()[1])
        else:
            processor = self.processors.get(fpath.splitext()[1])
        if processor:
            return resource.IResource(processor(fpath.path, self.registry))
        return self.createSimilarFile(fpath.path)
Пример #11
0
 def getChild(self, path, request):
     child = getattr(self._element, path)
     return resource.IResource(child)
Пример #12
0
        def _getChild(request):
            headers = {}
            resource_path = request_path

            route_descriptor, route_match = self.routes.get_descriptor(
                *request_parts)
            if route_descriptor is not None:
                headers = route_descriptor.get("headers", {})

                # Forward case
                if "forward" in route_descriptor:
                    # Recreate the URL
                    if route_descriptor.get("recreate_url", True):
                        fscheme, fnetloc, _, _, _, _ = urllib.parse.urlparse(
                            route_descriptor["forward"])
                        url = urllib.parse.urlunparse(
                            (fscheme, fnetloc, request.uri.decode("UTF-8"), "",
                             "", ""))
                    else:
                        url = route_descriptor["forward"]

                    replace = route_descriptor.get("replace", [])
                    return ForwardResource(url,
                                           headers=headers,
                                           replace=replace)
                # Code case
                elif "code" in route_descriptor:
                    code = route_descriptor.get("code", 200)
                    body = route_descriptor.get("body", "").encode("UTF-8")
                    return SimpleResource(request_path,
                                          code,
                                          headers=headers,
                                          body=body)
                # Path case
                elif "path" in route_descriptor:
                    resource_path = route_descriptor["path"]
                    # Replace regex groups in the route path
                    for i, group in enumerate(
                            re.search(route_descriptor["route"],
                                      route_match).groups()):
                        if group is not None:
                            resource_path = resource_path.replace(
                                "${}".format(i + 1), group)

                # Security: Ensure the absolute resource_path is within the wwwroot!
                # Prepend the resource_path with the wwwroot and canonicalize
                resource_path = os.path.abspath(
                    os.path.join(self.filesroot, resource_path.lstrip("/")))
                if resource_path.startswith(self.filesroot):
                    # First check if an exact match to the resource_path exists
                    if os.path.isfile(resource_path):
                        # Security: Don't show the soruce of python files!
                        if os.path.splitext(resource_path)[1].lower() == ".py":
                            try:
                                res = exec_cached_script(resource_path)
                                return resource.IResource(
                                    res["get_resource"](request))
                            except Exception:
                                logger.exception(
                                    "Unahandled exception in exec'd file '{}'".
                                    format(resource_path))
                        else:
                            with open(resource_path, "rb") as f:
                                data = f.read()
                            replace = route_descriptor.get("replace", [])
                            if len(replace):
                                data = data.decode()
                                for replace_descriptor in replace:
                                    replacement = replace_descriptor[
                                        "replacement"]
                                    replacement = replacement.replace(
                                        "{hostname}", host)
                                    replacement = replacement.replace(
                                        "{port}", str(port))
                                    replacement = replacement.replace(
                                        "{path}", path)
                                    replacement = replacement.replace(
                                        "{scheme}", scheme)
                                    data = re.sub(
                                        replace_descriptor["pattern"],
                                        replacement, data)
                                data = data.encode()
                            return SimpleResource(request_path,
                                                  200,
                                                  headers=headers,
                                                  body=data)
                    else:
                        # Then search for an `index.py` in each resource_path directory
                        current_dir = self.filesroot
                        search_dirs = [""] + resource_path.replace(
                            self.filesroot, "").strip("/").split("/")
                        for dir in search_dirs:
                            current_dir = os.path.join(current_dir, dir)
                            if os.path.isfile(
                                    os.path.join(current_dir, "index.py")):
                                resource_path = os.path.join(
                                    current_dir, "index.py")
                                try:
                                    res = exec_cached_script(resource_path)
                                    return resource.IResource(
                                        res["get_resource"](request))
                                except Exception:
                                    logger.exception(
                                        "Unahandled exception in exec'd file '{}'"
                                        .format(resource_path))
                                    break

                        logger.debug(
                            "File not found '{}'".format(resource_path))

            # Default handling, 404 here
            return SimpleResource(request_path, 404, body=b'Not Found')
Пример #13
0
class ServerContextFactory:
    def getContext(self):
        """
        Create an SSL context.

        This is a sample implementation that loads a certificate from a file
        called 'server.pem'.
        """
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_certificate_file('server.pem')
        ctx.use_privatekey_file('server.pem')
        return ctx


application = service.Application('finger', uid=1, gid=1)
f = FingerService('/etc/users')
serviceCollection = service.IServiceCollection(application)
f.setServiceParent(serviceCollection)
internet.TCPServer(79, IFingerFactory(f)).setServiceParent(serviceCollection)
site = server.Site(resource.IResource(f))
internet.TCPServer(8000, site).setServiceParent(serviceCollection)
internet.SSLServer(443, site,
                   ServerContextFactory()).setServiceParent(serviceCollection)
i = IIRCClientFactory(f)
i.nickname = 'fingerbot'
internet.TCPClient('irc.freenode.org', 6667,
                   i).setServiceParent(serviceCollection)
internet.TCPServer(8889, pb.PBServerFactory(
    IPerspectiveFinger(f))).setServiceParent(serviceCollection)
Пример #14
0
#!/bin/env python
from twisted.web import server, resource, static
from twisted.application import service, internet

from punjab.httpb import Httpb, HttpbService

root = static.File("./html")

# uncomment only one of the bosh lines, use_raw does no xml
# parsing/serialization but is potentially less reliable
#bosh = HttpbService(1, use_raw=True)
bosh = HttpbService(1)

# You can limit servers with a whitelist.
# The whitelist is a list of strings to match domain names.
# bosh.white_list = ['jabber.org', 'thetofu.com']
# or a black list
# bosh.block_list = ['jabber.org', '.thetofu.com']

root.putChild('http-bind', resource.IResource(bosh))

site = server.Site(root)

application = service.Application("punjab")
internet.TCPServer(5280, site).setServiceParent(application)
Пример #15
0
 def setUp(self):
     self.collection = self.collectionClass()
     self.resource = resource.IResource(self.collection)
Пример #16
0
    def set_user(self, user, status):
        self.users[user] = status


class AnotherBackend(FingerService):
    def get_user(self, user):
        try:
            user = pwd.getpwnam(user.decode()).pw_dir.encode()
        except Exception as e:
            print(e)
            user = b'Nothing'
        return defer.succeed(user)


f = AnotherBackend()

http_factory = server.Site(resource.IResource(f))
XML_factory = server.Site(IXMLResource(f))
pb_factory = pb.PBServerFactory(IPerspective(f))

reactor.listenTCP(8100, http_factory)
reactor.listenTCP(8101, IGetterFactory(f))
reactor.listenTCP(8102, ISetterFactory(f))
reactor.listenTCP(8103, XML_factory)
reactor.listenTCP(8104, pb_factory)

# e = endpoints.serverFromString(reactor, "ssl:port=443:certKey=cert.pem:privateKey=key.pem")
# e.listen(http_factory)
client_made()
reactor.run()
Пример #17
0

class _CorsEncoder(object):
    """
    @ivar _request: A reference to the originating request.

    @since: 12.3
    """
    def __init__(self, request):
        self._request = request

    def encode(self, data):
        return data

    def finish(self):
        return ""


root = JSON_RPC().customize(ExampleServer)
wrapped = resource.IResource(root, [CorsEncoderFactory()])
site = server.Site(wrapped)

# 8008 is the port you want to run under. Choose something >1024
PORT = 8008
print('Listening on port %d...' % PORT)
print(
    '*Be sure to use ClientRemote from client_remote.py rather than the standard Client.'
)
reactor.listenTCP(PORT, site)
reactor.run()