def _getController(controlAddr="127.0.0.1", controlPort=9051, passphrase=None, incorrectPasswordMsg="", printError=True): """ Custom handler for establishing a stem connection (... needs an overhaul). """ controller = None try: chroot = util.torTools.getConn().getPathPrefix() controller = Controller.from_port(controlAddr, controlPort) try: controller.authenticate(password = passphrase, chroot_path = chroot) except stem.connection.MissingPassword: try: passphrase = getpass.getpass("Controller password: "******"Unable to authenticate: password incorrect": # provide a warning that the provided password didn't work, then try # again prompting for the user to enter it print incorrectPasswordMsg return _getController(controlAddr, controlPort) elif printError: print exc return None
def handleKey(self, key): isKeystrokeConsumed = True if key in (ord('n'), ord('N')) and torTools.getConn().isNewnymAvailable(): self.sendNewnym() elif key in (ord('r'), ord('R')) and not self._isTorConnected: controller = None allowPortConnection, allowSocketConnection, _ = starter.allowConnectionTypes( ) if os.path.exists(CONFIG["startup.interface.socket"] ) and allowSocketConnection: try: # TODO: um... what about passwords? controller = Controller.from_socket_file( CONFIG["startup.interface.socket"]) controller.authenticate() except (IOError, stem.SocketError), exc: controller = None if not allowPortConnection: cli.popups.showMsg("Unable to reconnect (%s)" % exc, 3) elif not allowPortConnection: cli.popups.showMsg( "Unable to reconnect (socket '%s' doesn't exist)" % CONFIG["startup.interface.socket"], 3) if not controller and allowPortConnection: # TODO: This has diverged from starter.py's connection, for instance it # doesn't account for relative cookie paths or multiple authentication # methods. We can't use the starter.py's connection function directly # due to password prompts, but we could certainly make this mess more # manageable. try: ctlAddr, ctlPort = CONFIG[ "startup.interface.ipAddress"], CONFIG[ "startup.interface.port"] controller = Controller.from_port(ctlAddr, ctlPort) try: controller.authenticate() except stem.connection.MissingPassword: controller.authenticate( authValue) # already got the password above except Exception, exc: controller = None
def handleKey(self, key): isKeystrokeConsumed = True if key in (ord('n'), ord('N')) and torTools.getConn().isNewnymAvailable(): self.sendNewnym() elif key in (ord('r'), ord('R')) and not self._isTorConnected: controller = None allowPortConnection, allowSocketConnection, _ = starter.allowConnectionTypes() if os.path.exists(self._config["startup.interface.socket"]) and allowSocketConnection: try: # TODO: um... what about passwords? controller = Controller.from_socket_file(self._config["startup.interface.socket"]) controller.authenticate() except (IOError, stem.SocketError), exc: controller = None if not allowPortConnection: cli.popups.showMsg("Unable to reconnect (%s)" % exc, 3) elif not allowPortConnection: cli.popups.showMsg("Unable to reconnect (socket '%s' doesn't exist)" % self._config["startup.interface.socket"], 3) if not controller and allowPortConnection: # TODO: This has diverged from starter.py's connection, for instance it # doesn't account for relative cookie paths or multiple authentication # methods. We can't use the starter.py's connection function directly # due to password prompts, but we could certainly make this mess more # manageable. try: ctlAddr, ctlPort = self._config["startup.interface.ipAddress"], self._config["startup.interface.port"] controller = Controller.from_port(ctlAddr, ctlPort) try: controller.authenticate() except stem.connection.MissingPassword: controller.authenticate(authValue) # already got the password above except Exception, exc: controller = None
# By default attempts to connect using the control socket if it exists. This # skips attempting to connect by socket or port if the user has given # arguments for connecting to the other. controller = None allowPortConnection, allowSocketConnection, allowDetachedStart = allowConnectionTypes() socketPath = param["startup.interface.socket"] if os.path.exists(socketPath) and allowSocketConnection: try: # TODO: um... what about passwords? # https://trac.torproject.org/6881 controller = Controller.from_socket_file(socketPath) controller.authenticate() except IOError, exc: if not allowPortConnection: print "Unable to use socket '%s': %s" % (socketPath, exc) elif not allowPortConnection: print "Socket '%s' doesn't exist" % socketPath if not controller and allowPortConnection: # sets up stem connection, prompting for the passphrase if necessary and # sending problems to stdout if they arise authPassword = config.get("startup.controlPassword", CONFIG["startup.controlPassword"]) incorrectPasswordMsg = "Password found in '%s' was incorrect" % configPath controller = _getController(controlAddr, controlPort, authPassword, incorrectPasswordMsg, not allowDetachedStart) # removing references to the controller password so the memory can be freed # (unfortunately python does allow for direct access to the memory so this