def launch_tor_service(self, logfile='/dev/null'):
        """Launch Tor service and return the process."""
        self.log_file = logfile
        self.tmp_tor_data_dir = ut.clone_dir_with_timestap(
            cm.get_tor_data_path(self.tbb_version))

        self.torrc_dict.update({
            'DataDirectory': self.tmp_tor_data_dir,
            'Log': ['INFO file %s' % logfile]
        })

        wl_log.debug("Tor config: %s" % self.torrc_dict)
        try:
            self.tor_process = stem.process.launch_tor_with_config(
                config=self.torrc_dict,
                init_msg_handler=self.tor_log_handler,
                tor_cmd=cm.get_tor_bin_path(self.tbb_version),
                timeout=270)
            self.controller = Controller.from_port()
            self.controller.authenticate()
            return self.tor_process

        except stem.SocketError as exc:
            wl_log.critical("Unable to connect to tor on port %s: %s" %
                            (cm.SOCKS_PORT, exc))
            sys.exit(1)
        except:
            # most of the time this is due to another instance of
            # tor running on the system
            wl_log.critical("Error launching Tor", exc_info=True)
            sys.exit(1)

        wl_log.info("Tor running at port {0} & controller port {1}.".format(
            cm.SOCKS_PORT, cm.CONTROLLER_PORT))
        return self.tor_process
 def init_tbb_profile(self, version):
     profile_directory = cm.get_tbb_profile_path(version)
     self.prof_dir_path = clone_dir_with_timestap(profile_directory)
     if self.capture_screen and self.page_url:
         self.add_canvas_permission()
     try:
         tbb_profile = webdriver.FirefoxProfile(self.prof_dir_path)
     except Exception:
         wl_log.error("Error creating the TB profile", exc_info=True)
     else:
         return tbb_profile
    def launch_tor_service(self, logfile='/dev/null'):
        """Launch Tor service and return the process."""
        self.log_file = logfile
        self.tmp_tor_data_dir = ut.clone_dir_with_timestap(self.tor_data_path)

        self.torrc_dict.update({'ControlPort': str(cm.REFACTOR_CONTROL_PORT),
                                'DataDirectory': self.tmp_tor_data_dir,
                                'Log': ['INFO file %s' % logfile]})

        print("Tor config: %s" % self.torrc_dict)
        # the following may raise, make sure it's handled
        self.tor_process = stem.process.launch_tor_with_config(
            config=self.torrc_dict,
            init_msg_handler=self.tor_log_handler,
            tor_cmd=self.tor_binary_path,
            timeout=270
        )
        self.controller = Controller.from_port(port=cm.REFACTOR_CONTROL_PORT)
        self.controller.authenticate()
        print("Tor running at port {0} & controller port {1}."
              .format(cm.DEFAULT_SOCKS_PORT, cm.REFACTOR_CONTROL_PORT))
        return self.tor_process