diff --git a/webssh/handler.py b/webssh/handler.py index 43db8bd..5aaeef8 100644 --- a/webssh/handler.py +++ b/webssh/handler.py @@ -380,7 +380,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): def get_source_address(self): value = self.get_value('source_address') if not is_valid_ip_address(value): - raise InvalidValueError('Invalid ip address: {}'.format(value)) + raise InvalidValueError('Invalid source ip address: {}'.format(value)) return value def lookup_hostname(self, hostname, port): @@ -401,7 +401,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): privatekey, filename = self.get_privatekey() passphrase = self.get_argument('passphrase', u'') totp = self.get_argument('totp', u'') - source_address = self.get_argument('source_address', u'') + source_address = self.get_source_address() if isinstance(self.policy, paramiko.RejectPolicy): self.lookup_hostname(hostname, port) @@ -411,8 +411,19 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): else: pkey = None + if source_address: + logging.info("Binding socket for source ip {}".format(source_address)) + sock = socket.socket() + sock.settimeout(options.timeout) # Set a timeout on blocking socket operations + try: + sock.bind((source_address, 0)) + except OSError: + raise InvalidValueError('Unable to bind source address {} socket'.format(source_address)) + else: + sock = None + self.ssh_client.totp = totp - args = (hostname, port, username, password, pkey, source_address) + args = (hostname, port, username, password, pkey, sock) logging.debug(args) return args @@ -458,17 +469,16 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): dst_addr = args[:2] logging.info('Connecting to {}:{}'.format(*dst_addr)) - # WIP ZM - sock = socket.socket() - sock.settimeout(5) # otherwise it will hang if host is unreachable - sock.bind((args[5], 0)) - print(args) - sock.connect((args[0], args[1])) - - + sock = args[5] + if sock: + logging.info('Connecting source address socket') + try: + sock.connect(dst_addr) + except socket.error: + raise ValueError('Unable to connect source address socket to {}:{}'.format(*dst_addr)) try: - ssh.connect(*args, timeout=options.timeout, sock=sock) + ssh.connect(*args, timeout=options.timeout) except socket.error: raise ValueError('Unable to connect to {}:{}'.format(*dst_addr)) except paramiko.BadAuthenticationType: diff --git a/webssh/templates/index.html b/webssh/templates/index.html index 3268c38..4b27e32 100644 --- a/webssh/templates/index.html +++ b/webssh/templates/index.html @@ -77,6 +77,8 @@