remote_access.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """
  2. * Implementation of remote access
  3. Use localhost.run ssh remote port forwarding service by running a ssh subprocess in PyWebIO application.
  4. The stdout of ssh process is the connection info.
  5. * Strategy
  6. Wait at most one minute to get stdout, if it gets a normal out, the connection is successfully established.
  7. Otherwise report error.
  8. * One Issue
  9. When the PyWebIO application process exits, the ssh process becomes an orphan process and does not exit.
  10. * Solution.
  11. Use a child process to create the ssh process, the child process monitors the PyWebIO application process
  12. to see if it alive, and when the PyWebIO application exit, the child process kills the ssh process and exit.
  13. """
  14. import json
  15. import logging
  16. import os
  17. import re
  18. import shlex
  19. import threading
  20. import time
  21. from subprocess import Popen, PIPE
  22. logger = logging.getLogger(__name__)
  23. success_msg = """
  24. ================================================================================
  25. PyWebIO Application Remote Access
  26. Remote access address: https://{address}
  27. The remote access service is provided by localhost.run (https://localhost.run/).
  28. The remote access address will be reset in every 6 hours and only one
  29. application can enable remote access at the same time, if you use the free tier.
  30. To set up and manage custom domains go to https://admin.localhost.run/
  31. ================================================================================
  32. """
  33. ssh_key_gen_msg = """
  34. ===============================================================================
  35. PyWebIO Application Remote Access Error
  36. You need an SSH key to access the remote access service.
  37. Please follow Gitlab's most excellent howto to generate an SSH key pair:
  38. https://docs.gitlab.com/ee/ssh/
  39. Note that only rsa and ed25519 keys are supported.
  40. ===============================================================================
  41. """
  42. _ssh_process = None # type: Popen
  43. def remote_access_process(local_port=8080, setup_timeout=60, key_path=None, custom_domain=None):
  44. global _ssh_process
  45. ppid = os.getppid()
  46. assert ppid != 1
  47. domain_part = '%s:' % custom_domain if custom_domain is not None else ''
  48. key_path_arg = '-i %s' % key_path if key_path is not None else ''
  49. cmd = "ssh %s -oStrictHostKeyChecking=no -R %s80:localhost:%s localhost.run -- --output json" % (
  50. key_path_arg, domain_part, local_port)
  51. args = shlex.split(cmd)
  52. logger.debug('remote access service command: %s', cmd)
  53. _ssh_process = proc = Popen(args, stdout=PIPE, stderr=PIPE)
  54. logger.debug('remote access process pid: %s', proc.pid)
  55. success = False
  56. def timeout_killer(wait_sec):
  57. time.sleep(wait_sec)
  58. if not success and proc.poll() is None:
  59. proc.kill()
  60. threading.Thread(target=timeout_killer, kwargs=dict(wait_sec=setup_timeout), daemon=True).start()
  61. stdout = proc.stdout.readline().decode('utf8')
  62. connection_info = {}
  63. try:
  64. connection_info = json.loads(stdout)
  65. success = True
  66. except json.decoder.JSONDecodeError:
  67. if not success and proc.poll() is None:
  68. proc.kill()
  69. if success:
  70. if connection_info.get('status', 'fail') != 'success':
  71. print("Failed to establish remote access, this is the error message from service provider:",
  72. connection_info.get('message', ''))
  73. else:
  74. print(success_msg.format(address=connection_info['address']))
  75. # wait ssh or parent process exit
  76. while os.getppid() == ppid and proc.poll() is None:
  77. time.sleep(1)
  78. if proc.poll() is None: # parent process exit, kill ssh process
  79. logger.debug('App process exit, killing ssh process')
  80. proc.kill()
  81. else: # ssh process exit by itself or by timeout killer
  82. stderr = proc.stderr.read().decode('utf8')
  83. conn_id = re.search(r'connection id is (.*?),', stderr)
  84. logger.debug('Remote access connection id: %s', conn_id.group(1) if conn_id else '')
  85. ssh_error_msg = stderr.rsplit('**', 1)[-1].rsplit('===', 1)[-1].lower().strip()
  86. if 'permission denied' in ssh_error_msg:
  87. print(ssh_key_gen_msg)
  88. elif ssh_error_msg:
  89. print(ssh_error_msg)
  90. else:
  91. print('PyWebIO application remote access service exit.')
  92. def start_remote_access_service(local_port=8080, setup_timeout=60, ssh_key_path=None, custom_domain=None):
  93. pid = os.fork()
  94. if pid == 0: # in child process
  95. try:
  96. remote_access_process(local_port=local_port, setup_timeout=setup_timeout,
  97. key_path=ssh_key_path, custom_domain=custom_domain)
  98. except KeyboardInterrupt: # ignore KeyboardInterrupt
  99. pass
  100. finally:
  101. if _ssh_process:
  102. logger.debug('Exception occurred, killing ssh process')
  103. _ssh_process.kill()
  104. raise SystemExit
  105. else:
  106. return pid
  107. if __name__ == '__main__':
  108. import argparse
  109. logging.basicConfig(level=logging.DEBUG)
  110. parser = argparse.ArgumentParser(description="localhost.run Remote Access service")
  111. parser.add_argument("--local-port", help="the local port to connect the tunnel to", type=int, default=8080)
  112. parser.add_argument("--custom-domain", help="optionally connect a tunnel to a custom domain", default=None)
  113. parser.add_argument("--key-path", help="custom SSH key path", default=None)
  114. args = parser.parse_args()
  115. start_remote_access_service(local_port=args.local_port, ssh_key_path=args.key_path, custom_domain=args.custom_domain)
  116. os.wait() # Wait for completion of a child process