3 Commits 155bf00d04 ... e951a22ec7

Author SHA1 Message Date
  Michael Buesch e951a22ec7 sshtunnel: Add support for cancelling handshaking. 4 years ago
  Michael Buesch de7928b034 gui: Fix sleep helper function, if user event processing is requested 4 years ago
  Michael Buesch 949f33b623 sshtunnel: Update for new openssh version 4 years ago
2 changed files with 21 additions and 6 deletions
  1. 9 3
      awlsim/coreclient/sshtunnel.py
  2. 12 3
      awlsim/gui/awlsimclient.py

+ 9 - 3
awlsim/coreclient/sshtunnel.py

@@ -38,6 +38,7 @@ import select
 import signal
 import time
 import getpass
+import re
 
 
 class SSHTunnel(object):
@@ -205,14 +206,15 @@ class SSHTunnel(object):
 
 	PROMPT_PW	= "'s Password:"
 	PROMPT_AUTH	= "The authenticity of host "
-	PROMPT_YESNO	= " (yes/no)?"
+	PROMPT_YESNO	= re.compile(r".*\s\(\[?yes\]?/\[?no\]?(/[\[\]\w\s\d_\-]+)?\)\s*\?\s*")
 	AUTH_FINISH	= "Authenticated to "
 
 	def __handshake(self, ptyMasterFd, timeout):
 		timeoutEnd = monotonic_time() + (timeout or 0)
 		sentPw, authReq, finished = False, [], False
 		while not finished:
-			self.sleep(0.1)
+			if not self.sleep(0.1):
+				raise AwlSimError("Establishing SSH tunnel cancelled.")
 			if timeout and monotonic_time() >= timeoutEnd:
 				raise AwlSimError("Timeout establishing SSH tunnel.")
 			fromSsh = self.__read(ptyMasterFd)
@@ -249,7 +251,8 @@ class SSHTunnel(object):
 				if self.PROMPT_AUTH.lower() in lineLow:
 					authReq.append(line)
 					continue
-				if self.PROMPT_YESNO.lower() in lineLow and authReq:
+				if (self.PROMPT_YESNO.match(lineLow, re.IGNORECASE) and
+				    authReq):
 					ok = self.hostAuth("\n".join(authReq))
 					if not ok:
 						raise AwlSimError("SSH tunnel host "
@@ -265,8 +268,11 @@ class SSHTunnel(object):
 
 	def sleep(self, seconds):
 		"""Sleep for a number of seconds.
+		Returns True, if everything is Ok.
+		Returns False, if the connection operation shall be cancelled.
 		"""
 		time.sleep(seconds)
+		return True
 
 	def sshMessage(self, message, isDebug):
 		"""Print a SSH log message.

+ 12 - 3
awlsim/gui/awlsimclient.py

@@ -28,8 +28,9 @@ from awlsim.coreclient.sshtunnel import *
 
 def sleepWithEventLoop(seconds, excludeInput=True):
 	end = monotonic_time() + seconds
-	eventFlags = QEventLoop.AllEvents |\
-		(QEventLoop.ExcludeUserInputEvents if excludeInput else 0)
+	eventFlags = QEventLoop.AllEvents
+	if excludeInput:
+		eventFlags |= QEventLoop.ExcludeUserInputEvents
 	while monotonic_time() < end:
 		QApplication.processEvents(eventFlags, 10)
 		QThread.msleep(10)
@@ -39,6 +40,8 @@ class GuiSSHTunnel(SSHTunnel, QDialog):
 	"""
 
 	def __init__(self, parent, *args, **kwargs):
+		self.__cancelRequest = False
+
 		QDialog.__init__(self, parent)
 		SSHTunnel.__init__(self, *args, **kwargs)
 
@@ -52,10 +55,16 @@ class GuiSSHTunnel(SSHTunnel, QDialog):
 
 		self.resize(750, 180)
 
+	def closeEvent(self, ev):
+		self.__cancelRequest = True
+		QDialog.closeEvent(self, ev)
+
 	def sleep(self, seconds):
-		sleepWithEventLoop(seconds, excludeInput=True)
+		sleepWithEventLoop(seconds, excludeInput=False)
+		return not self.__cancelRequest
 
 	def connect(self):
+		self.__cancelRequest = False
 		self.hide()
 		self.setWindowModality(Qt.ApplicationModal)
 		self.show()