From 44ffcd449975a730308e0cfc5bc967db48ad85e7 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Wed, 28 Jan 2009 02:31:51 +0000 Subject: [PATCH] Automatically get localclient credentials if attempting to connect to a localhost --- deluge/ui/common.py | 32 +++++++++++++++------------- deluge/ui/gtkui/connectionmanager.py | 8 +++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/deluge/ui/common.py b/deluge/ui/common.py index ccd89264a..1fc37bf63 100644 --- a/deluge/ui/common.py +++ b/deluge/ui/common.py @@ -124,30 +124,32 @@ def get_torrent_info(filename): "info_hash": info_hash } -def get_localhost_auth_uri(uri): +def get_localhost_auth(): """ Grabs the localclient auth line from the 'auth' file and creates a localhost uri - :param uri: the uri to add the authentication info to - :returns: a localhost uri containing authentication information or None if the information is not available + :returns: tuple, with the username and password to login as """ - u = urlparse.urlsplit(uri) - # If there is already a username in this URI, let's just return it since - # the user has provided credentials. - if u.username: - return uri - auth_file = deluge.configmanager.get_config_dir("auth") if os.path.exists(auth_file): - for line in open(auth_file): + if line.startswith("#"): + # This is a comment line + continue try: - username, password = line.strip().split(":") - except ValueError: + lsplit = line.split(":") + except Exception, e: + log.error("Your auth file is malformed: %s", e) + continue + + if len(lsplit) == 2: + username, password = lsplit + elif len(lsplit) == 3: + username, password, level = lsplit + else: + log.error("Your auth file is malformed: Incorrect number of fields!") continue if username == "localclient": - # We use '127.0.0.1' in place of 'localhost' just incase this isn't defined properly - hostname = u.hostname.replace("localhost", "127.0.0.1") - return u.scheme + "://" + username + ":" + password + "@" + hostname + ":" + str(u.port) + return (username, password) return None diff --git a/deluge/ui/gtkui/connectionmanager.py b/deluge/ui/gtkui/connectionmanager.py index 4d02c078c..d4d0c0830 100644 --- a/deluge/ui/gtkui/connectionmanager.py +++ b/deluge/ui/gtkui/connectionmanager.py @@ -35,6 +35,7 @@ import deluge.ui.gtkui.common as common import deluge.configmanager from deluge.ui.client import client import deluge.ui.client +import deluge.ui.common from deluge.configmanager import ConfigManager from deluge.log import LOG as log @@ -271,6 +272,9 @@ class ConnectionManager(component.Component): client.daemon.info().addCallback(on_info) continue + if host in ("127.0.0.1", "localhost") and not user: + # We need to get the localhost creds + user, password = deluge.ui.common.get_localhost_auth() # Create a new Client instance c = deluge.ui.client.Client() d = c.connect(host, port, user, password) @@ -377,6 +381,10 @@ class ConnectionManager(component.Component): port = model[row][HOSTLIST_COL_PORT] user = model[row][HOSTLIST_COL_USER] password = model[row][HOSTLIST_COL_PASS] + if not user and host in ("127.0.0.1", "localhost"): + # This is a localhost connection with no username, so lets try to + # get the localclient creds from the auth file. + user, password = deluge.ui.common.get_localhost_auth() client.connect(host, port, user, password).addCallback(self.__on_connected, host_id) self.connection_manager.response(gtk.RESPONSE_OK)