def test_rogue_ssh_session__correct__old_version(self): port = self.osinteraction.get_open_port() response = request_open_port(port, server=self.test_server, client_version='0.9.3') command = [ '/usr/bin/ssh', 'open@%s' % self.test_server.split('//')[1], '-R', '%s:localhost:%s' % (response.remote_port, port) ] # No response.session_token! print(command) p = subprocess.Popen(command, bufsize=2048, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) run_method_with_timeout(lambda: wait_for_response( lambda: p.poll() is not None, timeout=10, throw=False), 10, raise_exception=False) if p.returncode is not None: print(p.communicate()) self.assertEqual(p.returncode, None)
def wait_for_response(function, args=[], kwargs={}, timeout=30, throw=True, max_method_run_time=None): if max_method_run_time is None: max_method_run_time = timeout start_time = datetime.datetime.now() while start_time + datetime.timedelta( seconds=timeout) > datetime.datetime.now(): try: output = run_method_with_timeout(function, max_method_run_time, args=args, kwargs=kwargs, raise_exception=True) if output: return output except TimeoutException: logger.debug('method timeout') pass logger.debug('no response, try again') sleep(1) if throw: raise Exception('function did not response in time') return False
def click_open_for_ip_link(link): if link: link = link.replace('https', 'http') logger.info('clicking link %s' % link) # ctx = ssl.create_default_context() # ctx.check_hostname = False # ctx.verify_mode = ssl.CERT_NONE req = urllib2.Request(link) response = run_method_with_timeout( lambda: urllib2.urlopen(req, timeout=10).read(), 10) assert response is not None assert 'is now open' in response
def send_ping(share, print_error=True): port = share.app_management_port logger.debug('Sending ping to %s.' % port) url = 'http://127.0.0.1:%s/ping' % (port,) logger.debug('sending get request ' + url) try: r = run_method_with_timeout(requests.get, 5, args=[url]) if r.text.strip() != 'pong': logger.error(response) return False return True except Exception as detail: log_function = logger.error if print_error else logger.debug log_function("An error has occurred while pinging the app: %s" % detail) return False
def keep_alive(self): while not self.stopped: time.sleep(self.keep_alive_interval_seconds) if self.stopped: return if self.portForwardingRequestException is not None: if self.error_callback: self.error_callback(self.portForwardingRequestException) logger.exception(self.portForwardingRequestException) logger.debug('sending keep_alive') try: stdin, stdout, stderr = run_method_with_timeout( lambda: self.client.exec_command(self.session_token), timeout_s=10) except (TimeoutException, paramiko.SSHException): raise TunnelError('Connection to the server seems to be lost.') # logger.debug('keep_alive sent: stdout %s' % stdout.read()) # logger.debug('keep_alive sent: stderr %s' % sterr.read()) if self.success_callback: self.success_callback()
def start(self): """This will connect to the server and start port forwarding to the given port of the localhost""" self.client.load_system_host_keys() logger.debug('Connecting to ssh host %s:%d ...' % (self.server, self.server_ssh_port)) pk = paramiko.RSAKey(filename=self.private_key_file) try: self.client.connect(self.server, self.server_ssh_port, username=self.ssh_user, pkey=pk, look_for_keys=False) self.stopped = False except Exception as e: logger.error('*** Failed to connect to %s:%d: %r' % (self.server, self.server_ssh_port, e)) if self.fallback_server_ssh_port is not None: try: logger.debug('Connecting to fallback ssh host %s:%d ...' % (self.fallback_ssh_server, self.fallback_server_ssh_port)) self.client.connect(self.fallback_ssh_server, self.fallback_server_ssh_port, username=self.ssh_user, pkey=pk, look_for_keys=False) self.stopped = False except Exception as e: logger.error( '*** Failed to fallback connect to %s:%d: %r' % (self.fallback_ssh_server, self.fallback_server_ssh_port, e)) if self.error_callback: self.error_callback(e) return else: if self.error_callback: self.error_callback(e) return try: stdin, stdout, stderr = run_method_with_timeout( lambda: self.client.exec_command(self.session_token), timeout_s=10) except (TimeoutException, paramiko.SSHException): raise TunnelError('Connection to the server seems to be lost.') try: self.portForwardingRequestException = None if self.forward_tunnel: thr = threading.Thread(target=self._forward_remote_port) else: thr = threading.Thread(target=self._forward_local_port) thr.setDaemon(True) thr.start() if self.start_callback: start_callback_thread = threading.Thread( target=self.start_callback) start_callback_thread.setDaemon(True) start_callback_thread.start() self.keep_alive() except KeyboardInterrupt as e: self.stop() logger.info('Ctrl-c: Port forwarding stopped.') # sys.exit(0) except EOFError as e: # Tunnel is stopped. self.stop() logger.debug(e) except: self.stop() raise