Ejemplo n.º 1
0
def proxy(data):
    recved = len(data)

    idx = data.find("\r\n")
    if idx <= 0:
        return

    line, rest = data[:idx], data[idx:]
    if line.startswith("CONNECT"):
        parts = line.split(None)
        netloc = parts[1]
        remote = parse_address(netloc, 80)

        reply_msg = "%s 200 OK\r\n\r\n" % parts[2]
        return {"remote": remote, "reply": reply_msg, "data": ""}

    parser = HttpParser()
    parsed = parser.execute(data, recved)
    if parsed != recved:
        return {
            'close': 'HTTP/1.0 502 Gateway Error\r\n\r\nError parsing request'
        }

    if not parser.get_url():
        return

    parsed_url = urlparse.urlparse(parser.get_url())

    is_ssl = parsed_url.scheme == "https"
    remote = parse_address(parsed_url.netloc, 80)

    return {"remote": remote, "ssl": is_ssl}
Ejemplo n.º 2
0
def proxy(data):
    recved = len(data)

    idx = data.find("\r\n")
    if idx <= 0:
        return

    line, rest = data[:idx], data[idx:]
    if line.startswith("CONNECT"):
        parts = line.split(None)
        netloc = parts[1]
        remote = parse_address(netloc, 80)

        reply_msg = "%s 200 OK\r\n\r\n" % parts[2]
        return {"remote": remote, 
                "reply": reply_msg,
                "data": ""}


    parser = HttpParser()
    parsed = parser.execute(data, recved)
    if parsed != recved:
        return  { 'close':'HTTP/1.0 502 Gateway Error\r\n\r\nError parsing request'}

    if not parser.get_url():
        return

    parsed_url = urlparse.urlparse(parser.get_url())

    is_ssl = parsed_url.scheme == "https"
    remote = parse_address(parsed_url.netloc, 80)

    return {"remote": remote, 
            "ssl": is_ssl}
Ejemplo n.º 3
0
    def load_routes(self):
        """ load route from configuration file """
        fname = os.path.join(self.get_spooldir(), 'routes')
        if not os.path.exists(fname):
            return

        # do we need to relad routes ?
        mtime = os.stat(fname).st_mtime
        if self.rmtime == mtime:
            return
        self.rmtime = mtime

        # build rules
        with open(fname, 'r') as f:
            routes_conf = json.load(f)
            for name, conf in routes_conf.items():
                host = conf.get('host', '(.*)')
                routes = conf.get('routes', {})
                self.hosts.append((re.compile(host), name))
                _routes = []
                for (route, route_conf) in routes.items():
                    if 'remote' in route_conf:

                        # build base_uri
                        is_ssl = 'ssl' in route_conf
                        remote = parse_address(route_conf.get('remote'), 80)
                        host = get_host(remote, is_ssl=is_ssl)

                        route_conf.update(
                            dict(host=host,
                                 base_uri=base_uri(host, is_ssl=is_ssl),
                                 remote=remote,
                                 listen=self.address,
                                 listen_ssl=self.is_listen_ssl()))

                        if ROUTE_RE.match(route):
                            spec = re.compile(route)
                        else:
                            spec = re.compile("%s(.*)" % route)
                        _routes.append((route, spec, route_conf))

                _routes.sort()
                _routes.reverse()
                self.routes[name] = _routes
            self.hosts.sort()
            self.hosts.reverse()
Ejemplo n.º 4
0
    def load_routes(self):
        """ load route from configuration file """
        fname = os.path.join(self.get_spooldir(), 'routes')
        if not os.path.exists(fname):
            return

        # do we need to relad routes ?
        mtime = os.stat(fname).st_mtime
        if self.rmtime == mtime:
            return
        self.rmtime = mtime
        
        # build rules
        with open(fname, 'r') as f:
            routes_conf = json.load(f)
            for name, conf in routes_conf.items():
                host = conf.get('host', '(.*)')
                routes = conf.get('routes', {})
                self.hosts.append((re.compile(host), name))
                _routes = []
                for (route, route_conf) in routes.items():
                    if 'remote' in route_conf:
                        
                        # build base_uri
                        is_ssl = 'ssl' in route_conf
                        remote = parse_address(route_conf.get('remote'), 80)
                        host = get_host(remote, is_ssl=is_ssl)
                       
                        route_conf.update(dict(
                            host = host,
                            base_uri =  base_uri(host, is_ssl=is_ssl),
                            remote = remote,
                            listen = self.address,
                            listen_ssl = self.is_listen_ssl()))

                        if ROUTE_RE.match(route):
                            spec = re.compile(route)
                        else:
                            spec = re.compile("%s(.*)" % route)
                        _routes.append((route, spec, route_conf))
                
                _routes.sort()
                _routes.reverse()
                self.routes[name] = _routes
            self.hosts.sort()
            self.hosts.reverse()
Ejemplo n.º 5
0
def rewrite_request(req):
    try:
        while True:
            parser = HttpStream(req)
            headers = parser.headers()

            parsed_url = urlparse.urlparse(parser.url())

            is_ssl = parsed_url.scheme == "https"

            host = get_host(parse_address(parsed_url.netloc, 80),
                            is_ssl=is_ssl)
            headers['Host'] = host
            headers['Connection'] = 'close'

            if 'Proxy-Connection' in headers:
                del headers['Proxy-Connection']

            location = urlparse.urlunparse(
                ('', '', parsed_url.path, parsed_url.params, parsed_url.query,
                 parsed_url.fragment))

            httpver = "HTTP/%s" % ".".join(map(str, parser.version()))

            new_headers = [
                "%s %s %s\r\n" % (parser.method(), location, httpver)
            ]

            new_headers.extend(["%s: %s\r\n" % (hname, hvalue) \
                    for hname, hvalue in headers.items()])

            req.writeall(bytes("".join(new_headers) + "\r\n"))
            body = parser.body_file()
            send_body(req, body, parser.is_chunked())

    except (socket.error, NoMoreData, ParserError):
        pass
Ejemplo n.º 6
0
def rewrite_request(req):
    try:
        while True:
            parser = HttpStream(req)
            headers = parser.headers()

            parsed_url = urlparse.urlparse(parser.url())

            is_ssl = parsed_url.scheme == "https"

            host = get_host(parse_address(parsed_url.netloc, 80),
                is_ssl=is_ssl)
            headers['Host'] = host
            headers['Connection'] = 'close'

            if 'Proxy-Connection' in headers:
                del headers['Proxy-Connection']


            location = urlparse.urlunparse(('', '', parsed_url.path,
                parsed_url.params, parsed_url.query, parsed_url.fragment))

            httpver = "HTTP/%s" % ".".join(map(str, 
                        parser.version()))

            new_headers = ["%s %s %s\r\n" % (parser.method(), location, 
                httpver)]

            new_headers.extend(["%s: %s\r\n" % (hname, hvalue) \
                    for hname, hvalue in headers.items()])

            req.writeall(bytes("".join(new_headers) + "\r\n"))
            body = parser.body_file()
            send_body(req, body, parser.is_chunked())

    except (socket.error, NoMoreData, ParserError):
            pass