Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

direct.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. from twisted.conch import error
  4. from twisted.conch.ssh import transport
  5. from twisted.internet import defer, protocol, reactor
  6. class SSHClientFactory(protocol.ClientFactory):
  7. def __init__(self, d, options, verifyHostKey, userAuthObject):
  8. self.d = d
  9. self.options = options
  10. self.verifyHostKey = verifyHostKey
  11. self.userAuthObject = userAuthObject
  12. def clientConnectionLost(self, connector, reason):
  13. if self.options["reconnect"]:
  14. connector.connect()
  15. def clientConnectionFailed(self, connector, reason):
  16. if self.d is None:
  17. return
  18. d, self.d = self.d, None
  19. d.errback(reason)
  20. def buildProtocol(self, addr):
  21. trans = SSHClientTransport(self)
  22. if self.options["ciphers"]:
  23. trans.supportedCiphers = self.options["ciphers"]
  24. if self.options["macs"]:
  25. trans.supportedMACs = self.options["macs"]
  26. if self.options["compress"]:
  27. trans.supportedCompressions[0:1] = ["zlib"]
  28. if self.options["host-key-algorithms"]:
  29. trans.supportedPublicKeys = self.options["host-key-algorithms"]
  30. return trans
  31. class SSHClientTransport(transport.SSHClientTransport):
  32. def __init__(self, factory):
  33. self.factory = factory
  34. self.unixServer = None
  35. def connectionLost(self, reason):
  36. if self.unixServer:
  37. d = self.unixServer.stopListening()
  38. self.unixServer = None
  39. else:
  40. d = defer.succeed(None)
  41. d.addCallback(
  42. lambda x: transport.SSHClientTransport.connectionLost(self, reason)
  43. )
  44. def receiveError(self, code, desc):
  45. if self.factory.d is None:
  46. return
  47. d, self.factory.d = self.factory.d, None
  48. d.errback(error.ConchError(desc, code))
  49. def sendDisconnect(self, code, reason):
  50. if self.factory.d is None:
  51. return
  52. d, self.factory.d = self.factory.d, None
  53. transport.SSHClientTransport.sendDisconnect(self, code, reason)
  54. d.errback(error.ConchError(reason, code))
  55. def receiveDebug(self, alwaysDisplay, message, lang):
  56. self._log.debug(
  57. "Received Debug Message: {message}",
  58. message=message,
  59. alwaysDisplay=alwaysDisplay,
  60. lang=lang,
  61. )
  62. if alwaysDisplay: # XXX what should happen here?
  63. print(message)
  64. def verifyHostKey(self, pubKey, fingerprint):
  65. return self.factory.verifyHostKey(
  66. self, self.transport.getPeer().host, pubKey, fingerprint
  67. )
  68. def setService(self, service):
  69. self._log.info("setting client server to {service}", service=service)
  70. transport.SSHClientTransport.setService(self, service)
  71. if service.name != "ssh-userauth" and self.factory.d is not None:
  72. d, self.factory.d = self.factory.d, None
  73. d.callback(None)
  74. def connectionSecure(self):
  75. self.requestService(self.factory.userAuthObject)
  76. def connect(host, port, options, verifyHostKey, userAuthObject):
  77. d = defer.Deferred()
  78. factory = SSHClientFactory(d, options, verifyHostKey, userAuthObject)
  79. reactor.connectTCP(host, port, factory)
  80. return d