service.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. The parent class for all the SSH services. Currently implemented services
  5. are ssh-userauth and ssh-connection.
  6. Maintainer: Paul Swartz
  7. """
  8. from typing import Dict
  9. from twisted.logger import Logger
  10. class SSHService:
  11. # this is the ssh name for the service:
  12. name: bytes = None # type:ignore[assignment]
  13. protocolMessages: Dict[int, str] = {} # map #'s -> protocol names
  14. transport = None # gets set later
  15. _log = Logger()
  16. def serviceStarted(self):
  17. """
  18. called when the service is active on the transport.
  19. """
  20. def serviceStopped(self):
  21. """
  22. called when the service is stopped, either by the connection ending
  23. or by another service being started
  24. """
  25. def logPrefix(self):
  26. return "SSHService {!r} on {}".format(
  27. self.name, self.transport.transport.logPrefix()
  28. )
  29. def packetReceived(self, messageNum, packet):
  30. """
  31. called when we receive a packet on the transport
  32. """
  33. # print self.protocolMessages
  34. if messageNum in self.protocolMessages:
  35. messageType = self.protocolMessages[messageNum]
  36. f = getattr(self, "ssh_%s" % messageType[4:], None)
  37. if f is not None:
  38. return f(packet)
  39. self._log.info(
  40. "couldn't handle {messageNum} {packet!r}",
  41. messageNum=messageNum,
  42. packet=packet,
  43. )
  44. self.transport.sendUnimplemented()