direct.py 3.2 KB

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