Since there's no default username for authentication, update hostlist to include the username.

Remove debug prints or extreme debugging. Minor code cleanup. Remove un-used imports.
This commit is contained in:
Pedro Algarvio 2010-12-21 18:20:21 +00:00
parent 86a1b801f5
commit e44cac0eaa
4 changed files with 43 additions and 60 deletions

View File

@ -104,7 +104,8 @@ class AuthManager(component.Component):
from hashlib import sha1 as sha_hash from hashlib import sha1 as sha_hash
except ImportError: except ImportError:
from sha import new as sha_hash from sha import new as sha_hash
return "localclient:" + sha_hash(str(random.random())).hexdigest() + ":" + str(AUTH_LEVEL_ADMIN) + "\n" return ("localclient:" + sha_hash(str(random.random())).hexdigest() +
":" + str(AUTH_LEVEL_ADMIN) + "\n")
def __load_auth_file(self): def __load_auth_file(self):
auth_file = configmanager.get_config_dir("auth") auth_file = configmanager.get_config_dir("auth")
@ -135,7 +136,9 @@ class AuthManager(component.Component):
continue continue
if len(lsplit) == 2: if len(lsplit) == 2:
username, password = lsplit username, password = lsplit
log.warning("Your auth entry for %s contains no auth level, using AUTH_LEVEL_DEFAULT(%s)..", username, AUTH_LEVEL_DEFAULT) log.warning("Your auth entry for %s contains no auth level, "
"using AUTH_LEVEL_DEFAULT(%s)..", username,
AUTH_LEVEL_DEFAULT)
level = AUTH_LEVEL_DEFAULT level = AUTH_LEVEL_DEFAULT
elif len(lsplit) == 3: elif len(lsplit) == 3:
username, password, level = lsplit username, password, level = lsplit

View File

@ -44,7 +44,6 @@ except ImportError:
import zlib import zlib
import deluge.common import deluge.common
import deluge.component as component
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.error import AuthenticationRequired from deluge.error import AuthenticationRequired
from deluge.event import known_events from deluge.event import known_events
@ -111,8 +110,10 @@ class DelugeRPCRequest(object):
:returns: a properly formated RPCRequest :returns: a properly formated RPCRequest
""" """
if self.request_id is None or self.method is None or self.args is None or self.kwargs is None: if self.request_id is None or self.method is None or self.args is None \
raise TypeError("You must set the properties of this object before calling format_message!") or self.kwargs is None:
raise TypeError("You must set the properties of this object "
"before calling format_message!")
return (self.request_id, self.method, self.args, self.kwargs) return (self.request_id, self.method, self.args, self.kwargs)
@ -163,7 +164,8 @@ class DelugeRPCProtocol(Protocol):
log.debug("Received invalid message: type is not tuple") log.debug("Received invalid message: type is not tuple")
return return
if len(request) < 3: if len(request) < 3:
log.debug("Received invalid message: number of items in response is %s", len(3)) log.debug("Received invalid message: number of items in "
"response is %s", len(3))
return return
message_type = request[0] message_type = request[0]
@ -192,7 +194,8 @@ class DelugeRPCProtocol(Protocol):
elif message_type == RPC_ERROR: elif message_type == RPC_ERROR:
# Create the DelugeRPCError to pass to the errback # Create the DelugeRPCError to pass to the errback
r = self.__rpc_requests[request_id] r = self.__rpc_requests[request_id]
e = DelugeRPCError(r.method, r.args, r.kwargs, request[2][0], request[2][1], request[2][2]) e = DelugeRPCError(r.method, r.args, r.kwargs, request[2][0],
request[2][1], request[2][2])
# Run the errbacks registered with this Deferred object # Run the errbacks registered with this Deferred object
d.errback(e) d.errback(e)
@ -281,7 +284,9 @@ class DaemonSSLProxy(DaemonProxy):
log.debug("sslproxy.connect()") log.debug("sslproxy.connect()")
self.host = host self.host = host
self.port = port self.port = port
self.__connector = reactor.connectSSL(self.host, self.port, self.__factory, ssl.ClientContextFactory()) self.__connector = reactor.connectSSL(self.host, self.port,
self.__factory,
ssl.ClientContextFactory())
self.connect_deferred = defer.Deferred() self.connect_deferred = defer.Deferred()
self.daemon_info_deferred = defer.Deferred() self.daemon_info_deferred = defer.Deferred()
@ -389,17 +394,13 @@ class DaemonSSLProxy(DaemonProxy):
""" """
try: try:
if error_data.check(AuthenticationRequired): if error_data.check(AuthenticationRequired):
print error_data.value.__dict__
return error_data return error_data
except: except:
pass pass
# print 1234567, error_data
# Get the DelugeRPCError object from the error_data # Get the DelugeRPCError object from the error_data
error = error_data.value error = error_data.value
# if error.exception_type == "AuthenticationRequired":
# return
# Create a delugerpcrequest to print out a nice RPCRequest string # Create a delugerpcrequest to print out a nice RPCRequest string
r = DelugeRPCRequest() r = DelugeRPCRequest()
r.method = error.method r.method = error.method
@ -423,7 +424,8 @@ class DaemonSSLProxy(DaemonProxy):
self.daemon_info_deferred.callback(daemon_info) self.daemon_info_deferred.callback(daemon_info)
def on_info_fail(reason): def on_info_fail(reason):
log.debug("Failed to get info from daemon: %s", reason) log.debug("Failed to get info from daemon")
log.exception(reason)
self.daemon_info_deferred.errback(reason) self.daemon_info_deferred.errback(reason)
self.call("daemon.info").addCallback(on_info).addErrback(on_info_fail) self.call("daemon.info").addCallback(on_info).addErrback(on_info_fail)
@ -431,7 +433,6 @@ class DaemonSSLProxy(DaemonProxy):
def __on_connect_fail(self, reason): def __on_connect_fail(self, reason):
log.debug("__on_connect_fail called") log.debug("__on_connect_fail called")
log.debug("connect_fail: %s", reason)
log.exception(reason) log.exception(reason)
self.daemon_info_deferred.errback(reason) self.daemon_info_deferred.errback(reason)
@ -574,13 +575,6 @@ class Client(object):
:returns: a Deferred object that will be called once the connection :returns: a Deferred object that will be called once the connection
has been established or fails has been established or fails
""" """
log.debug("real client connect")
# if not username and host in ("127.0.0.1", "localhost"):
# # No username was provided and it's the localhost, so we can try
# # to grab the credentials from the auth file.
# import common
# username, password = common.get_localhost_auth()
self._daemon_proxy = DaemonSSLProxy(dict(self.__event_handlers)) self._daemon_proxy = DaemonSSLProxy(dict(self.__event_handlers))
self._daemon_proxy.set_disconnect_callback(self.__on_disconnect) self._daemon_proxy.set_disconnect_callback(self.__on_disconnect)
d = self._daemon_proxy.connect(host, port) d = self._daemon_proxy.connect(host, port)
@ -604,35 +598,16 @@ class Client(object):
auth_deferred.errback(reason) auth_deferred.errback(reason)
def on_connected(daemon_version): def on_connected(daemon_version):
log.debug("Client.connect.on_connected: %s", daemon_version) log.debug("Client.connect.on_connected. Daemon version: %s",
print 1234, self._daemon_proxy daemon_version)
d = self._daemon_proxy.authenticate(username, password) d = self._daemon_proxy.authenticate(username, password)
print 1234, d
d.addCallback(on_authenticate, daemon_version) d.addCallback(on_authenticate, daemon_version)
d.addErrback(on_authenticate_fail) d.addErrback(on_authenticate_fail)
# return d
d.addCallback(on_connected) d.addCallback(on_connected)
return auth_deferred return auth_deferred
return d return d
# def authenticate(self, username="", password=""):
# if not self.connected():
# raise Exception("You first need to call connect")
# if not username and self._daemon_proxy.host in ("127.0.0.1", "localhost"):
# # No username was provided and it's the localhost, so we can try
# # to grab the credentials from the auth file.
# import common
# username, password = common.get_localhost_auth()
#
# def on_authenticate_fail(reason):
# log.debug("Failed to authenticate %s@%s:%s")
#
# d = self._daemon_proxy.authenticate(username, password)
# d.addErrback(on_authenticate_fail)
# return d
def disconnect(self): def disconnect(self):
""" """
Disconnects from the daemon. Disconnects from the daemon.

View File

@ -41,12 +41,10 @@ import logging
from twisted.internet import reactor from twisted.internet import reactor
import deluge.component as component import deluge.component as component
import deluge.common
import common import 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.error import AuthenticationRequired from deluge.error import AuthenticationRequired
from deluge.log import LOG as log from deluge.log import LOG as log
@ -58,7 +56,8 @@ DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 58846 DEFAULT_PORT = 58846
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
"hosts": [(hashlib.sha1(str(time.time())).hexdigest(), DEFAULT_HOST, DEFAULT_PORT, "", "")] "hosts": [(hashlib.sha1(str(time.time())).hexdigest(), DEFAULT_HOST,
DEFAULT_PORT, "localclient", "")]
} }
HOSTLIST_COL_ID = 0 HOSTLIST_COL_ID = 0
@ -145,7 +144,11 @@ class ConnectionManager(component.Component):
# Create status pixbufs # Create status pixbufs
if not HOSTLIST_PIXBUFS: if not HOSTLIST_PIXBUFS:
for stock_id in (gtk.STOCK_NO, gtk.STOCK_YES, gtk.STOCK_CONNECT): for stock_id in (gtk.STOCK_NO, gtk.STOCK_YES, gtk.STOCK_CONNECT):
HOSTLIST_PIXBUFS.append(self.connection_manager.render_icon(stock_id, gtk.ICON_SIZE_MENU)) HOSTLIST_PIXBUFS.append(
self.connection_manager.render_icon(
stock_id, gtk.ICON_SIZE_MENU
)
)
# Create the host list gtkliststore # Create the host list gtkliststore
# id-hash, hostname, port, status, username, password, version # id-hash, hostname, port, status, username, password, version
@ -176,7 +179,9 @@ class ConnectionManager(component.Component):
# Connect the signals to the handlers # Connect the signals to the handlers
self.glade.signal_autoconnect(self) self.glade.signal_autoconnect(self)
self.hostlist.get_selection().connect("changed", self.on_hostlist_selection_changed) self.hostlist.get_selection().connect(
"changed", self.on_hostlist_selection_changed
)
self.__update_list() self.__update_list()
@ -208,7 +213,8 @@ class ConnectionManager(component.Component):
# Check to see if there is already an entry for this host and return # Check to see if there is already an entry for this host and return
# if thats the case # if thats the case
for entry in self.liststore: for entry in self.liststore:
if [entry[HOSTLIST_COL_HOST], entry[HOSTLIST_COL_PORT], entry[HOSTLIST_COL_USER]] == [host, port, username]: if [entry[HOSTLIST_COL_HOST], entry[HOSTLIST_COL_PORT],
entry[HOSTLIST_COL_USER]] == [host, port, username]:
raise Exception("Host already in list!") raise Exception("Host already in list!")
# Host isn't in the list, so lets add it # Host isn't in the list, so lets add it
@ -417,8 +423,7 @@ class ConnectionManager(component.Component):
self.glade.get_widget("button_startdaemon").set_sensitive(False) self.glade.get_widget("button_startdaemon").set_sensitive(False)
# Make sure label is displayed correctly using mnemonics # Make sure label is displayed correctly using mnemonics
self.glade.get_widget("label_startdaemon").set_use_underline( self.glade.get_widget("label_startdaemon").set_use_underline(True)
True)
def start_daemon(self, port, config): def start_daemon(self, port, config):
""" """
@ -431,8 +436,9 @@ class ConnectionManager(component.Component):
if e.errno == 2: if e.errno == 2:
dialogs.ErrorDialog( dialogs.ErrorDialog(
_("Unable to start daemon!"), _("Unable to start daemon!"),
_("Deluge cannot find the 'deluged' executable, it is likely \ _("Deluge cannot find the 'deluged' executable, it is "
that you forgot to install the deluged package or it's not in your PATH.")).run() "likely that you forgot to install the deluged package "
"or it's not in your PATH.")).run()
else: else:
raise e raise e
except Exception, e: except Exception, e:
@ -465,9 +471,7 @@ that you forgot to install the deluged package or it's not in your PATH.")).run(
component.start() component.start()
def __on_connected_failed(self, reason, host_id, host, port, user): def __on_connected_failed(self, reason, host_id, host, port, user):
log.exception(reason.value) log.debug("Failed to connect: %s", reason)
# log.debug(reason.__dict__)
# log.debug(reason.value.__dict__)
if reason.check(AuthenticationRequired): if reason.check(AuthenticationRequired):
log.debug("PasswordRequired exception") log.debug("PasswordRequired exception")
dialog = dialogs.AuthenticationDialog(reason.value.message, dialog = dialogs.AuthenticationDialog(reason.value.message,
@ -499,7 +503,8 @@ that you forgot to install the deluged package or it's not in your PATH.")).run(
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 status == _("Offline") and self.glade.get_widget("chk_autostart").get_active() and\ if status == _("Offline") and \
self.glade.get_widget("chk_autostart").get_active() and \
host in ("127.0.0.1", "localhost"): host in ("127.0.0.1", "localhost"):
# We need to start this localhost # We need to start this localhost
self.start_daemon(port, deluge.configmanager.get_config_dir()) self.start_daemon(port, deluge.configmanager.get_config_dir())
@ -515,7 +520,6 @@ that you forgot to install the deluged package or it's not in your PATH.")).run(
do_retry_connect(try_counter) do_retry_connect(try_counter)
return result return result
def do_retry_connect(try_counter): def do_retry_connect(try_counter):
log.debug("user: %s pass: %s", user, password)
d = client.connect(host, port, user, password) d = client.connect(host, port, user, password)
d.addCallback(self.__on_connected, host_id) d.addCallback(self.__on_connected, host_id)
d.addErrback(on_connect_fail, try_counter) d.addErrback(on_connect_fail, try_counter)
@ -548,7 +552,8 @@ that you forgot to install the deluged package or it's not in your PATH.")).run(
# We add the host # We add the host
try: try:
self.add_host(hostname, port_spinbutton.get_value_as_int(), username, password) self.add_host(hostname, port_spinbutton.get_value_as_int(),
username, password)
except Exception, e: except Exception, e:
from deluge.ui.gtkui.dialogs import ErrorDialog from deluge.ui.gtkui.dialogs import ErrorDialog
ErrorDialog(_("Error Adding Host"), e).run() ErrorDialog(_("Error Adding Host"), e).run()

View File

@ -192,7 +192,7 @@ class ErrorDialog(BaseDialog):
class AuthenticationDialog(BaseDialog): class AuthenticationDialog(BaseDialog):
""" """
Displays a dialog with an entry field asking for a password. Displays a dialog with entry fields asking for username and password.
When run(), it will return either a gtk.RESPONSE_CANCEL or a When run(), it will return either a gtk.RESPONSE_CANCEL or a
gtk.RESPONSE_OK. gtk.RESPONSE_OK.