avatar.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- test-case-name: twisted.conch.test.test_conch -*-
  2. from zope.interface import implementer
  3. from twisted.conch.error import ConchError
  4. from twisted.conch.interfaces import IConchUser
  5. from twisted.conch.ssh.connection import OPEN_UNKNOWN_CHANNEL_TYPE
  6. from twisted.logger import Logger
  7. from twisted.python.compat import nativeString
  8. @implementer(IConchUser)
  9. class ConchUser:
  10. _log = Logger()
  11. def __init__(self):
  12. self.channelLookup = {}
  13. self.subsystemLookup = {}
  14. @property
  15. def conn(self):
  16. return self._conn
  17. @conn.setter
  18. def conn(self, value):
  19. self._conn = value
  20. def lookupChannel(self, channelType, windowSize, maxPacket, data):
  21. klass = self.channelLookup.get(channelType, None)
  22. if not klass:
  23. raise ConchError(OPEN_UNKNOWN_CHANNEL_TYPE, "unknown channel")
  24. else:
  25. return klass(
  26. remoteWindow=windowSize,
  27. remoteMaxPacket=maxPacket,
  28. data=data,
  29. avatar=self,
  30. )
  31. def lookupSubsystem(self, subsystem, data):
  32. self._log.debug(
  33. "Subsystem lookup: {subsystem!r}", subsystem=self.subsystemLookup
  34. )
  35. klass = self.subsystemLookup.get(subsystem, None)
  36. if not klass:
  37. return False
  38. return klass(data, avatar=self)
  39. def gotGlobalRequest(self, requestType, data):
  40. # XXX should this use method dispatch?
  41. requestType = nativeString(requestType.replace(b"-", b"_"))
  42. f = getattr(self, "global_%s" % requestType, None)
  43. if not f:
  44. return 0
  45. return f(data)