Automatically get localclient credentials if attempting to connect to a

localhost
This commit is contained in:
Andrew Resch 2009-01-28 02:31:51 +00:00
parent 464073e596
commit 44ffcd4499
2 changed files with 25 additions and 15 deletions

View File

@ -124,30 +124,32 @@ def get_torrent_info(filename):
"info_hash": info_hash "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 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: tuple, with the username and password to login as
:returns: a localhost uri containing authentication information or None if the information is not available
""" """
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") auth_file = deluge.configmanager.get_config_dir("auth")
if os.path.exists(auth_file): if os.path.exists(auth_file):
for line in open(auth_file): for line in open(auth_file):
if line.startswith("#"):
# This is a comment line
continue
try: try:
username, password = line.strip().split(":") lsplit = line.split(":")
except ValueError: 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 continue
if username == "localclient": if username == "localclient":
# We use '127.0.0.1' in place of 'localhost' just incase this isn't defined properly return (username, password)
hostname = u.hostname.replace("localhost", "127.0.0.1")
return u.scheme + "://" + username + ":" + password + "@" + hostname + ":" + str(u.port)
return None return None

View File

@ -35,6 +35,7 @@ import deluge.ui.gtkui.common as common
import deluge.configmanager import deluge.configmanager
from deluge.ui.client import client from deluge.ui.client import client
import deluge.ui.client import deluge.ui.client
import deluge.ui.common
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
from deluge.log import LOG as log from deluge.log import LOG as log
@ -271,6 +272,9 @@ class ConnectionManager(component.Component):
client.daemon.info().addCallback(on_info) client.daemon.info().addCallback(on_info)
continue 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 # Create a new Client instance
c = deluge.ui.client.Client() c = deluge.ui.client.Client()
d = c.connect(host, port, user, password) d = c.connect(host, port, user, password)
@ -377,6 +381,10 @@ class ConnectionManager(component.Component):
port = model[row][HOSTLIST_COL_PORT] port = model[row][HOSTLIST_COL_PORT]
user = model[row][HOSTLIST_COL_USER] user = model[row][HOSTLIST_COL_USER]
password = model[row][HOSTLIST_COL_PASS] 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) client.connect(host, port, user, password).addCallback(self.__on_connected, host_id)
self.connection_manager.response(gtk.RESPONSE_OK) self.connection_manager.response(gtk.RESPONSE_OK)