Beispiel #1
0
    def auto_configure_tor(self, session, hostport=None):
        need_raw = [ConnBroker.OUTGOING_RAW]
        hostport = hostport or ('127.0.0.1', 9050)
        try:
            with ConnBroker.context(need=need_raw) as context:
                tor = socket.create_connection(hostport, timeout=10)
        except IOError:
            return  _('Failed to connect to Tor on %s:%s. Is it installed?'
                      ) % hostport

        # If that succeeded, we might have Tor!
        old_proto = session.config.sys.proxy.protocol
        session.config.sys.proxy.protocol = 'tor'
        session.config.sys.proxy.host = hostport[0]
        session.config.sys.proxy.port = hostport[1]
        session.config.sys.proxy.fallback = True

        # Configure connection broker, revert settings while we test
        ConnBroker.configure()
        session.config.sys.proxy.protocol = old_proto

        # Test it...
        need_tor = [ConnBroker.OUTGOING_HTTPS]
        try:
            with ConnBroker.context(need=need_tor) as context:
                motd = urlopen(MOTD_URL_TOR_ONLY_NO_MARS,
                               data=None, timeout=10).read()
                assert(motd.strip().endswith('}'))
            session.config.sys.proxy.protocol = 'tor'
            message = _('Successfully configured and enabled Tor!')
        except (IOError, AssertionError):
            ConnBroker.configure()
            message = _('Failed to configure Tor on %s:%s. Is the network down?'
                        ) % hostport
        return message