示例#1
0
def get_tor_config():
    tor_config = TorConfig()
    if config.tor.control_port is None:
        config.tor.control_port = int(randomFreePort())
    if config.tor.socks_port is None:
        config.tor.socks_port = int(randomFreePort())

    tor_config.ControlPort = config.tor.control_port
    tor_config.SocksPort = config.tor.socks_port

    if config.tor.data_dir:
        data_dir = os.path.expanduser(config.tor.data_dir)
        # We only use the Tor data dir specified in the config file if
        # 1. It is not locked (i.e. another process is using it)
        # 2. We have write permissions to it
        data_dir_usable = is_tor_data_dir_usable(data_dir)
        try:
            mkdir_p(data_dir)
        except OSError as ose:
            if ose.errno == errno.EACCESS:
                data_dir_usable = False
            else:
                raise
        if data_dir_usable:
            tor_config.DataDirectory = data_dir

    if config.tor.bridges:
        tor_config.UseBridges = 1
        if config.advanced.obfsproxy_binary:
            tor_config.ClientTransportPlugin = (
                'obfs2,obfs3 exec %s managed' %
                config.advanced.obfsproxy_binary
            )
        bridges = []
        with open(config.tor.bridges) as f:
            for bridge in f:
                if 'obfs' in bridge:
                    if config.advanced.obfsproxy_binary:
                        bridges.append(bridge.strip())
                else:
                    bridges.append(bridge.strip())
        tor_config.Bridge = bridges

    if config.tor.torrc:
        for i in config.tor.torrc.keys():
            setattr(tor_config, i, config.tor.torrc[i])

    if os.geteuid() == 0:
        tor_config.User = pwd.getpwuid(os.geteuid()).pw_name

    tor_config.save()
    log.debug("Setting control port as %s" % tor_config.ControlPort)
    log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)
    return tor_config
示例#2
0
    def startTor(self):
        """ Starts Tor
        Launches a Tor with :param: socks_port :param: control_port
        :param: tor_binary set in ooniprobe.conf
        """
        log.msg("Starting Tor...")

        from txtorcon import TorConfig

        tor_config = TorConfig()
        if config.tor.control_port is None:
            config.tor.control_port = int(randomFreePort())
        if config.tor.socks_port is None:
            config.tor.socks_port = int(randomFreePort())

        tor_config.ControlPort = config.tor.control_port
        tor_config.SocksPort = config.tor.socks_port

        if config.tor.data_dir:
            data_dir = os.path.expanduser(config.tor.data_dir)

            if not os.path.exists(data_dir):
                log.msg("%s does not exist. Creating it." % data_dir)
                os.makedirs(data_dir)
            tor_config.DataDirectory = data_dir

        if config.tor.bridges:
            tor_config.UseBridges = 1
            if config.advanced.obfsproxy_binary:
                tor_config.ClientTransportPlugin = (
                    'obfs2,obfs3 exec %s managed' %
                    config.advanced.obfsproxy_binary
                )
            bridges = []
            with open(config.tor.bridges) as f:
                for bridge in f:
                    if 'obfs' in bridge:
                        if config.advanced.obfsproxy_binary:
                            bridges.append(bridge.strip())
                    else:
                        bridges.append(bridge.strip())
            tor_config.Bridge = bridges

        if config.tor.torrc:
            for i in config.tor.torrc.keys():
                setattr(tor_config, i, config.tor.torrc[i])

        if os.geteuid() == 0:
            tor_config.User = pwd.getpwuid(os.geteuid()).pw_name

        tor_config.save()
        log.debug("Setting control port as %s" % tor_config.ControlPort)
        log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)
        return start_tor(tor_config)
示例#3
0
    def startTor(self):
        """ Starts Tor
        Launches a Tor with :param: socks_port :param: control_port
        :param: tor_binary set in ooniprobe.conf
        """
        log.msg("Starting Tor...")

        from txtorcon import TorConfig

        tor_config = TorConfig()
        if config.tor.control_port is None:
            config.tor.control_port = int(randomFreePort())
        if config.tor.socks_port is None:
            config.tor.socks_port = int(randomFreePort())

        tor_config.ControlPort = config.tor.control_port
        tor_config.SocksPort = config.tor.socks_port

        if config.tor.data_dir:
            data_dir = os.path.expanduser(config.tor.data_dir)

            if not os.path.exists(data_dir):
                log.msg("%s does not exist. Creating it." % data_dir)
                os.makedirs(data_dir)
            tor_config.DataDirectory = data_dir

        if config.tor.bridges:
            tor_config.UseBridges = 1
            if config.advanced.obfsproxy_binary:
                tor_config.ClientTransportPlugin = (
                    'obfs2,obfs3 exec %s managed' %
                    config.advanced.obfsproxy_binary)
            bridges = []
            with open(config.tor.bridges) as f:
                for bridge in f:
                    if 'obfs' in bridge:
                        if config.advanced.obfsproxy_binary:
                            bridges.append(bridge.strip())
                    else:
                        bridges.append(bridge.strip())
            tor_config.Bridge = bridges

        if config.tor.torrc:
            for i in config.tor.torrc.keys():
                setattr(tor_config, i, config.tor.torrc[i])

        if os.geteuid() == 0:
            tor_config.User = pwd.getpwuid(os.geteuid()).pw_name

        tor_config.save()
        log.debug("Setting control port as %s" % tor_config.ControlPort)
        log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)
        return start_tor(tor_config)
示例#4
0
    def test_full_tor_connection(self):
        config = txtorcon.TorConfig()
        config.ControlPort = net.randomFreePort()
        config.SocksPort = net.randomFreePort()
        config.DataDirectory = self.tor_datadir
        log.msg("Connecting to tor %s" % (onion.tor_details['version']))

        config.log = ['notice stdout', 'notice file %s' % self.tor_logfile]
        config.save()

        def updates(prog, tag, summary):
            log.msg("Progress is at: %s%%" % (prog))
            self.report['tor_progress'] = int(prog)
            self.report['tor_progress_tag'] = tag
            self.report['tor_progress_summary'] = summary

        d = txtorcon.launch_tor(config,
                                reactor,
                                tor_binary=onion.find_tor_binary(),
                                timeout=self.timeout,
                                progress_updates=updates)

        @d.addCallback
        def setup_complete(proto):
            try:
                proto.transport.signalProcess('TERM')
            except error.ProcessExitedAlready:
                proto.transport.loseConnection()
            log.msg("Successfully connected to Tor")
            self.report['success'] = True

        @d.addErrback
        def setup_failed(failure):
            log.msg("Failed to connect to Tor")
            self.report['success'] = False
            self.report['error'] = 'timeout-reached'
            return

        @d.addCallback
        def write_log(_):
            with open(self.tor_logfile) as f:
                self.report['tor_log'] = f.read()
            os.remove(self.tor_logfile)
            try:
                shutil.rmtree(self.tor_datadir)
            except:
                pass

        return d
示例#5
0
    def test_full_tor_connection(self):
        config = txtorcon.TorConfig()
        config.ControlPort = net.randomFreePort()
        config.SocksPort = net.randomFreePort()
        config.DataDirectory = self.tor_datadir
        log.msg(
            "Connecting to tor %s" %
            (onion.tor_details['version']))

        config.log = ['notice stdout', 'notice file %s' % self.tor_logfile]
        config.save()

        def updates(prog, tag, summary):
            log.msg("Progress is at: %s%%" % (prog))
            self.report['tor_progress'] = int(prog)
            self.report['tor_progress_tag'] = tag
            self.report['tor_progress_summary'] = summary

        d = txtorcon.launch_tor(config, reactor, tor_binary=onion.find_tor_binary(),
                                timeout=self.timeout,
                                progress_updates=updates)

        @d.addCallback
        def setup_complete(proto):
            try:
                proto.transport.signalProcess('TERM')
            except error.ProcessExitedAlready:
                proto.transport.loseConnection()
            log.msg("Successfully connected to Tor")
            self.report['success'] = True

        @d.addErrback
        def setup_failed(failure):
            log.msg("Failed to connect to Tor")
            self.report['success'] = False
            self.report['error'] = 'timeout-reached'
            return

        @d.addCallback
        def write_log(_):
            with open(self.tor_logfile) as f:
                self.report['tor_log'] = f.read()
            os.remove(self.tor_logfile)
            try:
                shutil.rmtree(self.tor_datadir)
            except:
                pass

        return d
示例#6
0
 def test_check_tor_silly_listener(self):
     self.conf.advanced.start_tor = False
     self.conf.tor.socks_port = net.randomFreePort()
     self.conf.tor.control_port = None
     self.run_silly_server()
     try:
         yield self.conf.check_tor()
     except errors.ConfigFileIncoherent:
         pass
示例#7
0
 def test_check_tor_silly_listener(self):
     self.conf.advanced.start_tor = False
     self.conf.tor.socks_port = net.randomFreePort()
     self.conf.tor.control_port = None
     self.run_silly_server()
     try:
         yield self.conf.check_tor()
     except errors.ConfigFileIncoherent:
         pass
示例#8
0
    def test_check_tor_correct(self):
        """
        This test has been disabled because there is a strange concatenation of
        conditions that make it not possible to run it on travis.
        The tests need to be run as root on travis so that the ones that use
        scapy will work properly. When running tor as root, though, it will by
        default drop privileges to a lesser priviledged user (on debian based
        systems debian-tor). The problem is that the datadir will have already
        been created with the privileges of root, hence it will fail to use it
        as a datadir and fail.
        txtorcon addressed this issue in https://github.com/meejah/txtorcon/issues/26
        by chmodding the datadir with what is set as User.
        So we could either:

            1) Set User to root so that tor has access to that directory, but
            this will not work because then it will not be happy that
            /var/run/tor has more lax permissions (also debian-tor can read it)
            so it will fail. We could disable the control port, hence not
            needing to use /var/run/tor, but this is not possible due to:
            https://github.com/meejah/txtorcon/issues/80

            2) We set the User to be the owner of /var/run/tor, but this does
            not exist on all systems, so it would only work for travis.

        For the time being I am just going to disable this test and wait for
        one of the above bugs to have a better fix.
        """
        self.skipTest("See comment in the code")
        self.conf.advanced.start_tor = False
        self.conf.tor.socks_port = net.randomFreePort()
        self.conf.tor.control_port = net.randomFreePort()
        self.tor_process = yield self.run_tor()
        yield self.conf.check_incoherences(self.configuration)
        self.tor_process.transport.signalProcess('TERM')

        d = defer.Deferred()
        reactor.callLater(10, d.callback, None)
        yield d
示例#9
0
    def test_check_tor_correct(self):
        """
        This test has been disabled because there is a strange concatenation of
        conditions that make it not possible to run it on travis.
        The tests need to be run as root on travis so that the ones that use
        scapy will work properly. When running tor as root, though, it will by
        default drop privileges to a lesser priviledged user (on debian based
        systems debian-tor). The problem is that the datadir will have already
        been created with the privileges of root, hence it will fail to use it
        as a datadir and fail.
        txtorcon addressed this issue in https://github.com/meejah/txtorcon/issues/26
        by chmodding the datadir with what is set as User.
        So we could either:

            1) Set User to root so that tor has access to that directory, but
            this will not work because then it will not be happy that
            /var/run/tor has more lax permissions (also debian-tor can read it)
            so it will fail. We could disable the control port, hence not
            needing to use /var/run/tor, but this is not possible due to:
            https://github.com/meejah/txtorcon/issues/80

            2) We set the User to be the owner of /var/run/tor, but this does
            not exist on all systems, so it would only work for travis.

        For the time being I am just going to disable this test and wait for
        one of the above bugs to have a better fix.
        """
        self.skipTest("See comment in the code")
        self.conf.advanced.start_tor = False
        self.conf.tor.socks_port = net.randomFreePort()
        self.conf.tor.control_port = net.randomFreePort()
        self.tor_process = yield self.run_tor()
        yield self.conf.check_incoherences(self.configuration)
        self.tor_process.transport.signalProcess('TERM')

        d = defer.Deferred()
        reactor.callLater(10, d.callback, None)
        yield d
示例#10
0
def startTor():
    """ Starts Tor
    Launches a Tor with :param: socks_port :param: control_port
    :param: tor_binary set in ooniprobe.conf
    """

    @defer.inlineCallbacks
    def state_complete(state):
        config.tor_state = state
        log.msg("Successfully bootstrapped Tor")
        log.debug("We now have the following circuits: ")
        for circuit in state.circuits.values():
            log.debug(" * %s" % circuit)

        socks_port = yield state.protocol.get_conf("SocksPort")
        control_port = yield state.protocol.get_conf("ControlPort")
        client_ip = yield state.protocol.get_info("address")

        config.tor.socks_port = int(socks_port.values()[0])
        config.tor.control_port = int(control_port.values()[0])

        config.probe_ip = client_ip.values()[0]

        log.debug("Obtained our IP address from a Tor Relay %s" % config.privacy.client_ip)

    def setup_failed(failure):
        log.exception(failure)
        raise UnableToStartTor

    def setup_complete(proto):
        """
        Called when we read from stdout that Tor has reached 100%.
        """
        log.debug("Building a TorState")
        state = TorState(proto.tor_protocol)
        state.post_bootstrap.addCallback(state_complete)
        state.post_bootstrap.addErrback(setup_failed)
        return state.post_bootstrap

    def updates(prog, tag, summary):
        log.debug("%d%%: %s" % (prog, summary))

    tor_config = TorConfig()
    if config.tor.control_port:
        tor_config.ControlPort = config.tor.control_port
    else:
        control_port = int(randomFreePort())
        tor_config.ControlPort = control_port
        config.tor.control_port = control_port

    if config.tor.socks_port:
        tor_config.SocksPort = config.tor.socks_port
    else:
        socks_port = int(randomFreePort())
        tor_config.SocksPort = socks_port
        config.tor.socks_port = socks_port

    if config.tor.data_dir:
        data_dir = os.path.expanduser(config.tor.data_dir)

        if not os.path.exists(data_dir):
            log.msg("%s does not exist. Creating it." % data_dir)
            os.makedirs(data_dir)
        tor_config.DataDirectory = data_dir

    tor_config.save()

    log.debug("Setting control port as %s" % tor_config.ControlPort)
    log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)

    d = launch_tor(tor_config, reactor, tor_binary=config.advanced.tor_binary, progress_updates=updates)
    d.addCallback(setup_complete)
    d.addErrback(setup_failed)
    return d
示例#11
0
    def startTor(self):
        """ Starts Tor
        Launches a Tor with :param: socks_port :param: control_port
        :param: tor_binary set in ooniprobe.conf
        """
        log.msg("Starting Tor...")

        @defer.inlineCallbacks
        def state_complete(state):
            config.tor_state = state
            log.msg("Successfully bootstrapped Tor")
            log.debug("We now have the following circuits: ")
            for circuit in state.circuits.values():
                log.debug(" * %s" % circuit)

            socks_port = yield state.protocol.get_conf("SocksPort")
            control_port = yield state.protocol.get_conf("ControlPort")

            config.tor.socks_port = int(socks_port.values()[0])
            config.tor.control_port = int(control_port.values()[0])

        def setup_failed(failure):
            log.exception(failure)
            raise errors.UnableToStartTor

        def setup_complete(proto):
            """
            Called when we read from stdout that Tor has reached 100%.
            """
            log.debug("Building a TorState")
            config.tor.protocol = proto
            state = TorState(proto.tor_protocol)
            state.post_bootstrap.addCallback(state_complete)
            state.post_bootstrap.addErrback(setup_failed)
            return state.post_bootstrap

        def updates(prog, tag, summary):
            log.msg("%d%%: %s" % (prog, summary))

        tor_config = TorConfig()
        if config.tor.control_port:
            tor_config.ControlPort = config.tor.control_port

        if config.tor.socks_port:
            tor_config.SocksPort = config.tor.socks_port

        if config.tor.data_dir:
            data_dir = os.path.expanduser(config.tor.data_dir)

            if not os.path.exists(data_dir):
                log.msg("%s does not exist. Creating it." % data_dir)
                os.makedirs(data_dir)
            tor_config.DataDirectory = data_dir

        if config.tor.bridges:
            tor_config.UseBridges = 1
            if config.advanced.obfsproxy_binary:
                tor_config.ClientTransportPlugin = (
                    'obfs2,obfs3 exec %s managed' %
                    config.advanced.obfsproxy_binary)
            bridges = []
            with open(config.tor.bridges) as f:
                for bridge in f:
                    if 'obfs' in bridge:
                        if config.advanced.obfsproxy_binary:
                            bridges.append(bridge.strip())
                    else:
                        bridges.append(bridge.strip())
            tor_config.Bridge = bridges

        if config.tor.torrc:
            for i in config.tor.torrc.keys():
                setattr(tor_config, i, config.tor.torrc[i])

        if os.geteuid() == 0:
            tor_config.User = pwd.getpwuid(os.geteuid()).pw_name

        tor_config.save()

        if not hasattr(tor_config, 'ControlPort'):
            control_port = int(randomFreePort())
            tor_config.ControlPort = control_port
            config.tor.control_port = control_port

        if not hasattr(tor_config, 'SocksPort'):
            socks_port = int(randomFreePort())
            tor_config.SocksPort = socks_port
            config.tor.socks_port = socks_port

        tor_config.save()
        log.debug("Setting control port as %s" % tor_config.ControlPort)
        log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)

        if config.advanced.tor_binary:
            d = launch_tor(tor_config,
                           reactor,
                           tor_binary=config.advanced.tor_binary,
                           progress_updates=updates)
        else:
            d = launch_tor(tor_config, reactor, progress_updates=updates)
        d.addCallback(setup_complete)
        d.addErrback(setup_failed)
        return d
示例#12
0
    def startTor(self):
        """ Starts Tor
        Launches a Tor with :param: socks_port :param: control_port
        :param: tor_binary set in ooniprobe.conf
        """
        @defer.inlineCallbacks
        def state_complete(state):
            config.tor_state = state
            log.msg("Successfully bootstrapped Tor")
            log.debug("We now have the following circuits: ")
            for circuit in state.circuits.values():
                log.debug(" * %s" % circuit)

            socks_port = yield state.protocol.get_conf("SocksPort")
            control_port = yield state.protocol.get_conf("ControlPort")

            config.tor.socks_port = int(socks_port.values()[0])
            config.tor.control_port = int(control_port.values()[0])

            log.debug("Obtained our IP address from a Tor Relay %s" % config.probe_ip)

        def setup_failed(failure):
            log.exception(failure)
            raise errors.UnableToStartTor

        def setup_complete(proto):
            """
            Called when we read from stdout that Tor has reached 100%.
            """
            log.debug("Building a TorState")
            state = TorState(proto.tor_protocol)
            state.post_bootstrap.addCallback(state_complete)
            state.post_bootstrap.addErrback(setup_failed)
            return state.post_bootstrap

        def updates(prog, tag, summary):
            log.debug("%d%%: %s" % (prog, summary))

        tor_config = TorConfig()
        if config.tor.control_port:
            tor_config.ControlPort = config.tor.control_port
        else:
            control_port = int(randomFreePort())
            tor_config.ControlPort = control_port
            config.tor.control_port = control_port

        if config.tor.socks_port:
            tor_config.SocksPort = config.tor.socks_port
        else:
            socks_port = int(randomFreePort())
            tor_config.SocksPort = socks_port
            config.tor.socks_port = socks_port

        if config.tor.data_dir:
            data_dir = os.path.expanduser(config.tor.data_dir)

            if not os.path.exists(data_dir):
                log.msg("%s does not exist. Creating it." % data_dir)
                os.makedirs(data_dir)
            tor_config.DataDirectory = data_dir

        if config.tor.bridges:
            tor_config.UseBridges = 1
            if config.advanced.obfsproxy_binary:
                tor_config.ClientTransportPlugin = \
                        'obfs2,obfs3 exec %s managed' % \
                        config.advanced.obfsproxy_binary
            bridges = []
            with open(config.tor.bridges) as f:
                for bridge in f:
                    if 'obfs' in bridge:
                        if config.advanced.obfsproxy_binary:
                            bridges.append(bridge.strip())
                    else:
                        bridges.append(bridge.strip())
            tor_config.Bridge = bridges

        tor_config.save()

        log.debug("Setting control port as %s" % tor_config.ControlPort)
        log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)

        if config.advanced.tor_binary:
            d = launch_tor(tor_config, reactor,
                           tor_binary=config.advanced.tor_binary,
                           progress_updates=updates)
        else:
            d = launch_tor(tor_config, reactor,
                           progress_updates=updates)

        d.addCallback(setup_complete)
        d.addErrback(setup_failed)
        return d
示例#13
0
    def startTor(self):
        """ Starts Tor
        Launches a Tor with :param: socks_port :param: control_port
        :param: tor_binary set in ooniprobe.conf
        """
        @defer.inlineCallbacks
        def state_complete(state):
            config.tor_state = state
            log.msg("Successfully bootstrapped Tor")
            log.debug("We now have the following circuits: ")
            for circuit in state.circuits.values():
                log.debug(" * %s" % circuit)

            socks_port = yield state.protocol.get_conf("SocksPort")
            control_port = yield state.protocol.get_conf("ControlPort")

            config.tor.socks_port = int(socks_port.values()[0])
            config.tor.control_port = int(control_port.values()[0])

            log.debug("Obtained our IP address from a Tor Relay %s" %
                      config.probe_ip)

        def setup_failed(failure):
            log.exception(failure)
            raise errors.UnableToStartTor

        def setup_complete(proto):
            """
            Called when we read from stdout that Tor has reached 100%.
            """
            log.debug("Building a TorState")
            state = TorState(proto.tor_protocol)
            state.post_bootstrap.addCallback(state_complete)
            state.post_bootstrap.addErrback(setup_failed)
            return state.post_bootstrap

        def updates(prog, tag, summary):
            log.debug("%d%%: %s" % (prog, summary))

        tor_config = TorConfig()
        if config.tor.control_port:
            tor_config.ControlPort = config.tor.control_port
        else:
            control_port = int(randomFreePort())
            tor_config.ControlPort = control_port
            config.tor.control_port = control_port

        if config.tor.socks_port:
            tor_config.SocksPort = config.tor.socks_port
        else:
            socks_port = int(randomFreePort())
            tor_config.SocksPort = socks_port
            config.tor.socks_port = socks_port

        if config.tor.data_dir:
            data_dir = os.path.expanduser(config.tor.data_dir)

            if not os.path.exists(data_dir):
                log.msg("%s does not exist. Creating it." % data_dir)
                os.makedirs(data_dir)
            tor_config.DataDirectory = data_dir

        tor_config.save()

        log.debug("Setting control port as %s" % tor_config.ControlPort)
        log.debug("Setting SOCKS port as %s" % tor_config.SocksPort)

        if config.advanced.tor_binary:
            d = launch_tor(tor_config,
                           reactor,
                           tor_binary=config.advanced.tor_binary,
                           progress_updates=updates)
        else:
            d = launch_tor(tor_config, reactor, progress_updates=updates)

        d.addCallback(setup_complete)
        d.addErrback(setup_failed)
        return d