Пример #1
0
def setupCollector(tor_process_protocol):
    def setup_complete(port):
        print("Exposed collector Tor hidden service on httpo://%s"
              % port.onion_uri)

    tempfile.tempdir = os.path.join(_repo_dir, 'tmp')
    if not os.path.isdir(tempfile.gettempdir()):
        os.makedirs(tempfile.gettempdir())
    _temp_dir = tempfile.mkdtemp()

    if config.main.tor_datadir is None:
        log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
        log.msg("Creating tmp directory in current directory for datadir.")
        log.debug("Using %s" % _temp_dir)
        datadir = _temp_dir
    else:
        datadir = config.main.tor_datadir

    torconfig = TorConfig(tor_process_protocol.tor_protocol)
    public_port = 80
    # XXX there is currently a bug in txtorcon that prevents data_dir from
    # being passed properly. Details on the bug can be found here:
    # https://github.com/meejah/txtorcon/pull/22
    hs_endpoint = TCPHiddenServiceEndpoint(reactor, torconfig, public_port,
                                           data_dir=datadir)
    hidden_service = hs_endpoint.listen(reportingBackend)
    hidden_service.addCallback(setup_complete)
    hidden_service.addErrback(txSetupFailed)
Пример #2
0
def startTor():
    def updates(prog, tag, summary):
        print("%d%%: %s" % (prog, summary))

    tempfile.tempdir = os.path.join(_repo_dir, 'tmp')
    if not os.path.isdir(tempfile.gettempdir()):
        os.makedirs(tempfile.gettempdir())
    _temp_dir = tempfile.mkdtemp()

    torconfig = TorConfig()
    torconfig.SocksPort = config.main.socks_port
    if config.main.tor2webmode:
        torconfig.Tor2webMode = 1
        torconfig.CircuitBuildTimeout = 60
    if config.main.tor_datadir is None:
        log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
        log.msg("Creating tmp directory in current directory for datadir.")
        log.debug("Using %s" % _temp_dir)
        datadir = _temp_dir
    else:
        datadir = config.main.tor_datadir
    torconfig.DataDirectory = datadir
    torconfig.save()
    if config.main.tor_binary is not None:
        d = launch_tor(torconfig, reactor,
                       tor_binary=config.main.tor_binary,
                       progress_updates=updates)
    else:
        d = launch_tor(torconfig, reactor, progress_updates=updates)
    d.addCallback(setupCollector, datadir)
    if ooniBouncer:
        d.addCallback(setupBouncer, datadir)
    d.addErrback(txSetupFailed)
Пример #3
0
def closeReport(report_id):
    report_dir = FilePath(config.main.report_dir)
    archive_dir = FilePath(config.main.archive_dir)
    report_path = report_dir.child(report_id)
    report_metadata_path = report_dir.child(report_id + METADATA_EXT)

    if not report_path.exists() or \
            not report_metadata_path.exists():
        raise e.ReportNotFound

    with report_metadata_path.open('r') as f:
        try:
            report_details = json.load(f)
        except ValueError:
            log.warn("Found corrupt metadata deleting %s and %s" %
                     (report_path.path, report_metadata_path.path))
            report_path.remove()
            report_metadata_path.remove()
            return

    dst_path = report_file_path(archive_dir,
                                report_details,
                                report_id)
    if report_path.getsize() > 0:
        report_path.moveTo(dst_path)
    else:
        # We currently just remove empty reports.
        # XXX Maybe in the future we want to keep track of how often this
        # happens.
        log.warn("Removing empty report %s" % report_path.path)
        report_path.remove()
    report_metadata_path.remove()
Пример #4
0
def startTor(torconfig):
    def updates(prog, tag, summary):
        print("%d%%: %s" % (prog, summary))

    if config.main.socks_port:
        torconfig.SocksPort = config.main.socks_port
    if config.main.control_port:
        torconfig.ControlPort = config.main.control_port
    if config.main.tor2webmode:
        torconfig.Tor2webMode = 1
        torconfig.CircuitBuildTimeout = 60
    if config.main.tor_datadir is None:
        temporary_data_dir = tempfile.mkdtemp()
        log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
        log.warn("Using %s" % temporary_data_dir)
        torconfig.DataDirectory = temporary_data_dir
    else:
        if os.path.exists(config.main.tor_datadir):
            torconfig.DataDirectory = os.path.abspath(config.main.tor_datadir)
        else:
            raise Exception

    tor_log_file = os.path.join(torconfig.DataDirectory, "tor.log")
    torconfig.Log = ["notice stdout", "notice file %s" % tor_log_file]
    torconfig.save()
    if not hasattr(torconfig, 'ControlPort'):
        control_port = int(randomFreePort())
        torconfig.ControlPort = control_port
        config.main.control_port = control_port

    if not hasattr(torconfig, 'SocksPort'):
        socks_port = int(randomFreePort())
        torconfig.SocksPort = socks_port
        config.main.socks_port = socks_port

    torconfig.save()

    if config.main.tor_binary is not None:
        d = launch_tor(torconfig,
                       reactor,
                       tor_binary=config.main.tor_binary,
                       progress_updates=updates)
    else:
        d = launch_tor(torconfig, reactor, progress_updates=updates)
    return d
Пример #5
0
def startTor(torconfig):
    def updates(prog, tag, summary):
        print("%d%%: %s" % (prog, summary))

    if config.main.socks_port:
        torconfig.SocksPort = config.main.socks_port
    if config.main.control_port:
        torconfig.ControlPort = config.main.control_port
    if config.main.tor2webmode:
        torconfig.Tor2webMode = 1
        torconfig.CircuitBuildTimeout = 60
    if config.main.tor_datadir is None:
        temporary_data_dir = tempfile.mkdtemp()
        log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
        log.warn("Using %s" % temporary_data_dir)
        torconfig.DataDirectory = temporary_data_dir
    else:
        if os.path.exists(config.main.tor_datadir):
            torconfig.DataDirectory = os.path.abspath(config.main.tor_datadir)
        else:
            raise Exception

    tor_log_file = os.path.join(torconfig.DataDirectory, "tor.log")
    torconfig.Log = ["notice stdout", "notice file %s" % tor_log_file]
    torconfig.save()
    if not hasattr(torconfig, 'ControlPort'):
        control_port = int(randomFreePort())
        torconfig.ControlPort = control_port
        config.main.control_port = control_port

    if not hasattr(torconfig, 'SocksPort'):
        socks_port = int(randomFreePort())
        torconfig.SocksPort = socks_port
        config.main.socks_port = socks_port

    torconfig.save()

    if config.main.tor_binary is not None:
        d = launch_tor(torconfig, reactor,
                       tor_binary=config.main.tor_binary,
                       progress_updates=updates)
    else:
        d = launch_tor(torconfig, reactor, progress_updates=updates)
    return d
Пример #6
0
def _configTor():
    torconfig = TorConfig()

    if config.main.socks_port is None:
        config.main.socks_port = int(randomFreePort())
    torconfig.SocksPort = config.main.socks_port

    if config.main.control_port is None:
        config.main.control_port = int(randomFreePort())
    torconfig.ControlPort = config.main.control_port

    if config.main.tor2webmode is True:
        torconfig.Tor2webMode = 1
        torconfig.CircuitBuildTimeout = 60
    if config.main.tor_datadir is None:
        temporary_data_dir = tempfile.mkdtemp()
        log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
        log.warn("Using %s" % temporary_data_dir)
        torconfig.DataDirectory = temporary_data_dir
        uid = -1
        gid = -1
        if config.main.uid is not None:
            uid = config.main.uid
        if config.main.gid is not None:
            gid = config.main.gid
        os.chown(temporary_data_dir, uid, gid)
    else:
        if os.path.exists(config.main.tor_datadir):
            torconfig.DataDirectory = os.path.abspath(config.main.tor_datadir)
        else:
            raise Exception("Could not find tor datadir")

    if config.main.uid is not None:
        try:
            user = pwd.getpwuid(config.main.uid)[0]
        except KeyError:
            raise Exception("Invalid user ID")
        torconfig.User = user

    tor_log_file = os.path.join(torconfig.DataDirectory, "tor.log")
    torconfig.Log = ["notice stdout", "notice file %s" % tor_log_file]
    torconfig.save()
    return torconfig
Пример #7
0
def _configTor():
    torconfig = TorConfig()

    if config.main.socks_port is None:
        config.main.socks_port = int(randomFreePort())
    torconfig.SocksPort = config.main.socks_port

    if config.main.control_port is None:
        config.main.control_port = int(randomFreePort())
    torconfig.ControlPort = config.main.control_port

    if config.main.tor2webmode is True:
        torconfig.Tor2webMode = 1
        torconfig.CircuitBuildTimeout = 60
    if config.main.tor_datadir is None:
        temporary_data_dir = tempfile.mkdtemp()
        log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
        log.warn("Using %s" % temporary_data_dir)
        torconfig.DataDirectory = temporary_data_dir
        uid = -1
        gid = -1
        if config.main.uid is not None:
            uid = config.main.uid
        if config.main.gid is not None:
            gid = config.main.gid
        os.chown(temporary_data_dir, uid, gid)
    else:
        if os.path.exists(config.main.tor_datadir):
            torconfig.DataDirectory = os.path.abspath(config.main.tor_datadir)
        else:
            raise Exception("Could not find tor datadir")

    if config.main.uid is not None:
        try:
            user = pwd.getpwuid(config.main.uid)[0]
        except KeyError:
            raise Exception("Invalid user ID")
        torconfig.User = user

    tor_log_file = os.path.join(torconfig.DataDirectory, "tor.log")
    torconfig.Log = ["notice stdout", "notice file %s" % tor_log_file]
    torconfig.save()
    return torconfig
Пример #8
0
        def startTor(self, torconfig):
            def updates(prog, tag, summary):
                print("%d%%: %s" % (prog, summary))

            torconfig.SocksPort = config.main.socks_port
            if config.main.tor2webmode:
                torconfig.Tor2webMode = 1
                torconfig.CircuitBuildTimeout = 60
            if config.main.tor_datadir is None:
                self.temporary_data_dir = tempfile.mkdtemp()
                log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
                log.warn("Using %s" % self.temporary_data_dir)
                torconfig.DataDirectory = self.temporary_data_dir
            else:
                torconfig.DataDirectory = config.main.tor_datadir
            torconfig.save()
            if config.main.tor_binary is not None:
                d = launch_tor(torconfig, reactor,
                               tor_binary=config.main.tor_binary,
                               progress_updates=updates)
            else:
                d = launch_tor(torconfig, reactor, progress_updates=updates)
            return d
Пример #9
0
 def startTor(self, torconfig):
     def updates(prog, tag, summary):
         print("%d%%: %s" % (prog, summary))
     
     torconfig.SocksPort = config.main.socks_port
     if config.main.tor2webmode:
         torconfig.Tor2webMode = 1
         torconfig.CircuitBuildTimeout = 60
     if config.main.tor_datadir is None:
         self.temporary_data_dir = tempfile.mkdtemp()
         log.warn("Option 'tor_datadir' in oonib.conf is unspecified!")
         log.warn("Using %s" % self.temporary_data_dir)
         torconfig.DataDirectory = self.temporary_data_dir
     else:
         torconfig.DataDirectory = config.main.tor_datadir
     torconfig.save()
     if config.main.tor_binary is not None:
         d = launch_tor(torconfig, reactor,
                        tor_binary=config.main.tor_binary,
                        progress_updates=updates)
     else:
         d = launch_tor(torconfig, reactor, progress_updates=updates)
     return d
Пример #10
0
                                                         self.listen_port,
                                                         interface='127.0.0.1')
        d = self.tcp_endpoint.listen(self.protocolfactory)
        d.addCallback(self._add_attributes).addErrback(self._retry_local_port)
        return d
    TCPHiddenServiceEndpoint._create_listener =  create_listener

class OBaseRunner(object):
    pass

if platformType == "win32":
    from twisted.scripts._twistw import WindowsApplicationRunner

    OBaseRunner = WindowsApplicationRunner
    # XXX Currently we don't support windows for starting a Tor Hidden Service
    log.warn(
        "Apologies! We don't support starting a Tor Hidden Service on Windows.")

else:
    from twisted.scripts._twistd_unix import UnixApplicationRunner
    class OBaseRunner(UnixApplicationRunner):
        temporary_data_dir = None

        def txSetupFailed(self, failure):
            log.err("Setup failed")
            log.exception(failure)

        def setupHSEndpoint(self, tor_process_protocol, torconfig, endpoint):
            endpointName = endpoint.settings['name']

            def setup_complete(port):
                if LooseVersion(txtorcon_version) >= LooseVersion('0.10.0'):
Пример #11
0
        self.tcp_endpoint = TCP4ServerEndpoint(self.reactor, self.listen_port,
                interface='127.0.0.1')
        d = self.tcp_endpoint.listen(self.protocolfactory)
        d.addCallback(self._add_attributes).addErrback(self._retry_local_port)
        return d
    TCPHiddenServiceEndpoint._create_listener =  create_listener

class OBaseRunner(object):
    pass

if platformType == "win32":
    from twisted.scripts._twistw import WindowsApplicationRunner

    OBaseRunner = WindowsApplicationRunner
    # XXX Currently we don't support windows for starting a Tor Hidden Service
    log.warn(
        "Apologies! We don't support starting a Tor Hidden Service on Windows.")

else:
    from twisted.scripts._twistd_unix import UnixApplicationRunner
    class OBaseRunner(UnixApplicationRunner):
        temporary_data_dir = None

        def txSetupFailed(self, failure):
            log.err("Setup failed")
            log.exception(failure)

        def setupHSEndpoint(self, tor_process_protocol, torconfig, endpoint):
            endpointName = endpoint.settings['name']
            def setup_complete(port):
                print("Exposed %s Tor hidden service on httpo://%s" % (endpointName,
                    port.onion_uri))