(reactor, control_protocol, socks_config=None)
Internal helper.

    This uses an already-configured SOCKS endpoint from the attached
    Tor, or creates a new TCP one (and configures Tor with it). If
    socks_config is non-None, it is a SOCKSPort line and will either
    be used if it already exists or will be created.
Internal helper.
Internal helper .
def _create_socks_endpoint(reactor, control_protocol, socks_config=None):
    """
    Internal helper.

    This uses an already-configured SOCKS endpoint from the attached
    Tor, or creates a new TCP one (and configures Tor with it). If
    socks_config is non-None, it is a SOCKSPort line and will either
    be used if it already exists or will be created.
    """
    socks_ports = yield control_protocol.get_conf('SOCKSPort')
    if socks_ports:
        socks_ports = list(socks_ports.values())[0]
        if not isinstance(socks_ports, list):
            socks_ports = [socks_ports]
        # see TorConfig for more fun-times regarding *PortLines, including
        # the __*Port things...
        if socks_ports == ['DEFAULT']:
            default = yield control_protocol.get_conf_single('__SocksPort')
            socks_ports = [default]
    else:
        # return from get_conf was an empty dict; we want a list
        socks_ports = []

    # everything in the SocksPort list can include "options" after the
    # initial value. We don't care about those, but do need to strip
    # them.
    socks_ports = [port.split()[0] for port in socks_ports]

    # could check platform? but why would you have unix ports on a
    # platform that doesn't?
    unix_ports = set([p for p in socks_ports if p.startswith('unix:')])
    tcp_ports = set(socks_ports) - unix_ports

    socks_endpoint = None
    for p in list(unix_ports) + list(tcp_ports):  # prefer unix-ports
        if socks_config and p != socks_config:
            continue
        try:
            socks_endpoint = _endpoint_from_socksport_line(reactor, p)
        except Exception as e:
            log.err(
                Failure(),
                "failed to process SOCKS port '{}': {}".format(p, e)
            )

    # if we still don't have an endpoint, nothing worked (or there
    # were no SOCKSPort lines at all) so we add config to tor
    if socks_endpoint is None:
        if socks_config is None:
            # is a unix-socket in /tmp on a supported platform better than
            # this?
            port = yield available_tcp_port(reactor)
            socks_config = str(port)
        socks_ports.append(socks_config)

        # NOTE! We must set all the ports in one command or we'll
        # destroy pre-existing config
        args = []
        for p in socks_ports:
            args.append('SOCKSPort')
            args.append(p)
        yield control_protocol.set_conf(*args)
        socks_endpoint = _endpoint_from_socksport_line(reactor, socks_config)

    assert socks_endpoint is not None
    defer.returnValue(socks_endpoint)
def _create_socks_endpoint ( reactor , control_protocol , socks_config = None ) : socks_ports = yield control_protocol . get_conf ( 'SOCKSPort' ) if socks_ports : socks_ports = list ( socks_ports . values ( ) ) [ 0 ] if not isinstance ( socks_ports , list ) : socks_ports = [ socks_ports ] # see TorConfig for more fun-times regarding *PortLines, including # the __*Port things... if socks_ports == [ 'DEFAULT' ] : default = yield control_protocol . get_conf_single ( '__SocksPort' ) socks_ports = [ default ] else : # return from get_conf was an empty dict; we want a list socks_ports = [ ] # everything in the SocksPort list can include "options" after the # initial value. We don't care about those, but do need to strip # them. socks_ports = [ port . split ( ) [ 0 ] for port in socks_ports ] # could check platform? but why would you have unix ports on a # platform that doesn't? unix_ports = set ( [ p for p in socks_ports if p . startswith ( 'unix:' ) ] ) tcp_ports = set ( socks_ports ) - unix_ports socks_endpoint = None for p in list ( unix_ports ) + list ( tcp_ports ) : # prefer unix-ports if socks_config and p != socks_config : continue try : socks_endpoint = _endpoint_from_socksport_line ( reactor , p ) except Exception as e : log . err ( Failure ( ) , "failed to process SOCKS port '{}': {}" . format ( p , e ) ) # if we still don't have an endpoint, nothing worked (or there # were no SOCKSPort lines at all) so we add config to tor if socks_endpoint is None : if socks_config is None : # is a unix-socket in /tmp on a supported platform better than # this? port = yield available_tcp_port ( reactor ) socks_config = str ( port ) socks_ports . append ( socks_config ) # NOTE! We must set all the ports in one command or we'll # destroy pre-existing config args = [ ] for p in socks_ports : args . append ( 'SOCKSPort' ) args . append ( p ) yield control_protocol . set_conf ( * args ) socks_endpoint = _endpoint_from_socksport_line ( reactor , socks_config ) assert socks_endpoint is not None defer . returnValue ( socks_endpoint )