proxy.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (C) 2012 Yipit, Inc <coders@yipit.com>
  2. #
  3. # This file is part of paramiko.
  4. #
  5. # Paramiko is free software; you can redistribute it and/or modify it under the
  6. # terms of the GNU Lesser General Public License as published by the Free
  7. # Software Foundation; either version 2.1 of the License, or (at your option)
  8. # any later version.
  9. #
  10. # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
  11. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. import os
  19. import shlex
  20. import signal
  21. from select import select
  22. import socket
  23. import time
  24. # Try-and-ignore import so platforms w/o subprocess (eg Google App Engine) can
  25. # still import paramiko.
  26. subprocess, subprocess_import_error = None, None
  27. try:
  28. import subprocess
  29. except ImportError as e:
  30. subprocess_import_error = e
  31. from paramiko.ssh_exception import ProxyCommandFailure
  32. from paramiko.util import ClosingContextManager
  33. class ProxyCommand(ClosingContextManager):
  34. """
  35. Wraps a subprocess running ProxyCommand-driven programs.
  36. This class implements a the socket-like interface needed by the
  37. `.Transport` and `.Packetizer` classes. Using this class instead of a
  38. regular socket makes it possible to talk with a Popen'd command that will
  39. proxy traffic between the client and a server hosted in another machine.
  40. Instances of this class may be used as context managers.
  41. """
  42. def __init__(self, command_line):
  43. """
  44. Create a new CommandProxy instance. The instance created by this
  45. class can be passed as an argument to the `.Transport` class.
  46. :param str command_line:
  47. the command that should be executed and used as the proxy.
  48. """
  49. if subprocess is None:
  50. raise subprocess_import_error
  51. self.cmd = shlex.split(command_line)
  52. self.process = subprocess.Popen(
  53. self.cmd,
  54. stdin=subprocess.PIPE,
  55. stdout=subprocess.PIPE,
  56. stderr=subprocess.PIPE,
  57. bufsize=0,
  58. )
  59. self.timeout = None
  60. def send(self, content):
  61. """
  62. Write the content received from the SSH client to the standard
  63. input of the forked command.
  64. :param str content: string to be sent to the forked command
  65. """
  66. try:
  67. self.process.stdin.write(content)
  68. except IOError as e:
  69. # There was a problem with the child process. It probably
  70. # died and we can't proceed. The best option here is to
  71. # raise an exception informing the user that the informed
  72. # ProxyCommand is not working.
  73. raise ProxyCommandFailure(" ".join(self.cmd), e.strerror)
  74. return len(content)
  75. def recv(self, size):
  76. """
  77. Read from the standard output of the forked program.
  78. :param int size: how many chars should be read
  79. :return: the string of bytes read, which may be shorter than requested
  80. """
  81. try:
  82. buffer = b""
  83. start = time.time()
  84. while len(buffer) < size:
  85. select_timeout = None
  86. if self.timeout is not None:
  87. elapsed = time.time() - start
  88. if elapsed >= self.timeout:
  89. raise socket.timeout()
  90. select_timeout = self.timeout - elapsed
  91. r, w, x = select([self.process.stdout], [], [], select_timeout)
  92. if r and r[0] == self.process.stdout:
  93. buffer += os.read(
  94. self.process.stdout.fileno(), size - len(buffer)
  95. )
  96. return buffer
  97. except socket.timeout:
  98. if buffer:
  99. # Don't raise socket.timeout, return partial result instead
  100. return buffer
  101. raise # socket.timeout is a subclass of IOError
  102. except IOError as e:
  103. raise ProxyCommandFailure(" ".join(self.cmd), e.strerror)
  104. def close(self):
  105. os.kill(self.process.pid, signal.SIGTERM)
  106. @property
  107. def closed(self):
  108. return self.process.returncode is not None
  109. @property
  110. def _closed(self):
  111. # Concession to Python 3 socket-like API
  112. return self.closed
  113. def settimeout(self, timeout):
  114. self.timeout = timeout