コード例 #1
0
ファイル: ports.py プロジェクト: mojoe70/proxy-verifier
def _get_available_port(queue):
    """
    Get the next available port from the port queue and return it.

    Since there can be a delay between when the queue is populated and when the
    port is requested, this checks the next port in the queue to see whether it
    is still not open. If it isn't, it is dropped from the queue and the next
    one is inspected. This process continues until either an available port is
    found, or, if the queue is exhausted, PortQueueSelectionError is raised.

    Returns:
        An available (i.e., non-open) port.

    Throws:
        PortQueueSelectionError if the port queue is exhausted.
    """

    if queue.qsize() == 0:
        host.WriteWarning("Port queue is empty.")
        raise PortQueueSelectionError(
            "Could not get a valid port because the queue is empty")

    port = queue.get()
    while PortOpen(port):
        host.WriteDebug('_get_available_port'
                        "Port was open but now is used: {}".format(port))
        if queue.qsize() == 0:
            host.WriteWarning("Port queue is empty.")
            raise PortQueueSelectionError(
                "Could not get a valid port because the queue is empty")
        port = queue.get()
    return port
コード例 #2
0
def setup_port_queue(amount: int = 1000):
    '''
    Populates `g_ports`  with all of the closed ports on the localhost
    '''
    global g_ports
    if g_ports is None:
        g_ports = Queue.LifoQueue()
    else:
        return
    try:
        # some docker setups don't have sbin setup correctly
        new_env = os.environ.copy()
        new_env['PATH'] = ':'.join(("/sbin", "/usr/sbin", new_env['PATH']))
        if platform.system() == 'Darwin':
            dmin = subprocess.check_output(
                ["sysctl", "net.inet.ip.portrange.first"],
                env=new_env).decode().split(":")[1].split()[0]
            dmax = subprocess.check_output(
                ["sysctl", "net.inet.ip.portrange.last"],
                env=new_env).decode().split(":")[1].split()[0]
        else:
            dmin, dmax = subprocess.check_output(
                ["sysctl", "net.ipv4.ip_local_port_range"],
                env=new_env).decode().split("=")[1].split()
        dmin = int(dmin)
        dmax = int(dmax)
    except (subprocess.CalledProcessError, OSError):
        host.WriteWarning(
            "Unable to call sysctrl!\n Tests may fail because of bad port selection!"
        )
        return

    rmin = dmin - 2000
    rmax = 65536 - dmax

    if rmax > amount:
        # fill in ports
        port = dmax + 1
        while port < 65536 and g_ports.qsize() < amount:
            # if port good:
            if not PortOpen(port):
                g_ports.put(port)
            port += 1
    if rmin > amount and g_ports.qsize() < amount:
        port = 2001
        while port < dmin and g_ports.qsize() < amount:
            # if port good:
            if not PortOpen(port):
                g_ports.put(port)
            port += 1
コード例 #3
0
ファイル: ports.py プロジェクト: xavierchi/trafficserver
def setup_port_queue(amount=1000):
    global g_ports
    if g_ports is None:
        g_ports = Queue.LifoQueue()
    else:
        return
    try:
        # some docker setups don't have sbin setup correctly
        new_env = os.environ.copy()
        new_env['PATH'] = "/sbin:/usr/sbin:" + new_env['PATH']
        dmin, dmax = subprocess.check_output(
            ["sysctl", "net.ipv4.ip_local_port_range"],
            env=new_env).decode().split("=")[1].split()
        dmin = int(dmin)
        dmax = int(dmax)
    except:
        host.WriteWarning(
            "Unable to call sysctrl!\n Tests may fail because of bad port selection!"
        )
        return

    rmin = dmin - 2000
    rmax = 65536 - dmax

    if rmax > amount:
        # fill in ports
        port = dmax + 1
        while port < 65536 and g_ports.qsize() < amount:
            # if port good:
            if not PortOpen(port):
                g_ports.put(port)
            port += 1
    if rmin > amount and g_ports.qsize() < amount:
        port = 2001
        while port < dmin and g_ports.qsize() < amount:
            # if port good:
            if not PortOpen(port):
                g_ports.put(port)
            port += 1
コード例 #4
0
ファイル: ports.py プロジェクト: mojoe70/proxy-verifier
def _setup_port_queue(amount=1000):
    """
    Build up the set of ports that the OS in theory will not use.
    """
    global g_ports
    if g_ports is None:
        host.WriteDebug('_setup_port_queue', "Populating the port queue.")
        g_ports = Queue.Queue()
    else:
        # The queue has already been populated.
        host.WriteDebug(
            '_setup_port_queue',
            "Queue was previously populated. Queue size: {}".format(
                g_ports.qsize()))
        return
    try:
        # Use sysctl to find the range of ports that the OS publishes it uses.
        # some docker setups don't have sbin setup correctly
        new_env = os.environ.copy()
        new_env['PATH'] = "/sbin:/usr/sbin:" + new_env['PATH']
        if 'Darwin' == platform.system():
            dmin = subprocess.check_output(
                ["sysctl", "net.inet.ip.portrange.first"],
                env=new_env).decode().split(":")[1].split()[0]
            dmax = subprocess.check_output(
                ["sysctl", "net.inet.ip.portrange.last"],
                env=new_env).decode().split(":")[1].split()[0]
        else:
            dmin, dmax = subprocess.check_output(
                ["sysctl", "net.ipv4.ip_local_port_range"],
                env=new_env).decode().split("=")[1].split()
        dmin = int(dmin)
        dmax = int(dmax)
    except Exception:
        host.WriteWarning(
            "Unable to call sysctrl!\n Tests may fail because of bad port selection!"
        )
        return

    rmin = dmin - 2000
    rmax = 65536 - dmax

    if rmax > amount:
        # Fill in ports, starting above the upper OS-usable port range.
        port = dmax + 1
        while port < 65536 and g_ports.qsize() < amount:
            if not PortOpen(port):
                host.WriteDebug(
                    '_setup_port_queue',
                    "Adding a possible port to connect to: {0}".format(port))
                g_ports.put(port)
            else:
                host.WriteDebug(
                    '_setup_port_queue',
                    "Rejecting a possible port to connect to: {0}".format(
                        port))
            port += 1
    if rmin > amount and g_ports.qsize() < amount:
        port = 2001
        # Fill in more ports, starting at 2001, well above well known ports,
        # and going up until the minimum port range used by the OS.
        while port < dmin and g_ports.qsize() < amount:
            if not PortOpen(port):
                host.WriteDebug(
                    '_setup_port_queue',
                    "Adding a possible port to connect to: {0}".format(port))
                g_ports.put(port)
            else:
                host.WriteDebug(
                    '_setup_port_queue',
                    "Rejecting a possible port to connect to: {0}".format(
                        port))
            port += 1