Пример #1
0
def frontend_handler(socket, address):
    hostname = ''
    hostheader = re.compile('host: ([^\(\);:,<>]+)', re.I)
    # Peek up to 512 bytes into data for the Host header
    for n in [128, 256, 512]:
        bytes = socket.recv(n, MSG_PEEK)
        if not bytes:
            socket.close()
            return
        for line in bytes.split('\r\n'):
            match = hostheader.match(line)
            if match:
                hostname = match.group(1)
        if hostname:
            break
    hostname = hostname.split(':')[0]
    if not hostname:
        logging.debug("!frontend: no hostname, closing")
        socket.close()
        return
    if hostname.startswith('_version.'):
        data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1}
               """.format(len(VERSION), VERSION).strip()
        socket.sendall(data)
        socket.close()
        logging.debug("version request")
        return
    if hostname.startswith('_backend.'):
        port = os.environ.get('DOTCLOUD_SERVER_BACKEND_PORT',
                              Tunnel.backend_port)
        data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1}
               """.format(len(str(port)), port).strip()
        socket.sendall(data)
        socket.close()
        return
    tunnel = Tunnel.get_by_hostname(hostname)
    if not tunnel:
        logging.debug("!frontend: no tunnel, closing ({0})".format(hostname))
        socket.close()
        return
    conn = tunnel.pop_proxy_backend(timeout=2)
    if not conn:
        logging.debug("!frontend: no backend, closing")
        socket.close()
        return
    protocol.send_message(conn, protocol.proxy_reply())
    pool = util.join_sockets(conn, socket)
    logging.debug("popped backend:\"{0}\" for frontend:\"{1}\"".format(
        tunnel.name, hostname))
    pool.waitall()
Пример #2
0
def frontend_handler(socket, address):
    hostname = ''
    hostheader = re.compile('host: ([^\(\);:,<>]+)', re.I)
    # Peek up to 512 bytes into data for the Host header
    for n in [128, 256, 512]:
        bytes = socket.recv(n, MSG_PEEK)
        if not bytes:
            socket.close()
            return
        for line in bytes.split('\r\n'):
            match = hostheader.match(line)
            if match:
                hostname = match.group(1)
        if hostname:
            break
    hostname = hostname.split(':')[0]
    if not hostname:
        logging.debug("!frontend: no hostname, closing")
        socket.close()
        return
    if hostname.startswith('_version.'):
        data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1}
               """.format(len(VERSION), VERSION).strip()
        socket.sendall(data)
        socket.close()
        logging.debug("version request")
        return
    if hostname.startswith('_backend.'):
        port = os.environ.get('DOTCLOUD_SERVER_BACKEND_PORT', Tunnel.backend_port)
        data = """HTTP/1.1 200 OK\r\nContent-Length: {0}\r\nConnection: close\r\n\r\n{1}
               """.format(len(str(port)), port).strip()
        socket.sendall(data)
        socket.close()
        return
    tunnel = Tunnel.get_by_hostname(hostname)
    if not tunnel:
        logging.debug("!frontend: no tunnel, closing ({0})".format(
            hostname))
        socket.close()
        return
    conn = tunnel.pop_proxy_backend(timeout=2)
    if not conn:
        logging.debug("!frontend: no backend, closing")
        socket.close()
        return
    protocol.send_message(conn, protocol.proxy_reply())
    pool = util.join_sockets(conn, socket)
    logging.debug("popped backend:\"{0}\" for frontend:\"{1}\"".format(
                tunnel.name, hostname))
    pool.waitall()