Exemple #1
0
    def __init__(self,
                 local_port,
                 remote_port,
                 server,
                 server_ssh_port,
                 ssh_user,
                 public_key_file,
                 private_key_file,
                 error_callback=None,
                 success_callback=None,
                 fallback_server_ssh_port=None,
                 fallback_ssh_server=None,
                 http_forward_address=None,
                 start_callback=None,
                 forward_tunnel=False,
                 session_token=None,
                 keep_alive_interval_seconds=10):
        self.local_port = local_port
        self.remote_port = remote_port
        self.server = server
        self.server_ssh_port = server_ssh_port
        self.ssh_user = ssh_user

        default_public_key_file, default_private_key_file = get_default_key_locations(
        )

        self.public_key_file = public_key_file if public_key_file else default_public_key_file
        self.private_key_file = private_key_file if private_key_file else default_private_key_file
        self.error_callback = error_callback
        self.success_callback = success_callback
        self.fallback_server_ssh_port = fallback_server_ssh_port
        self.fallback_ssh_server = fallback_ssh_server
        self.http_forward_address = http_forward_address
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(IgnoreUnknownHostKeyPolicy())
        self.start_callback = start_callback
        self.forward_tunnel = forward_tunnel
        self.session_token = session_token
        self.keep_alive_interval_seconds = keep_alive_interval_seconds

        self.portForwardingRequestException = None
        self.stopped = False
Exemple #2
0
    def start(self):
        # print('sys.argv: %s' % sys.argv)
        self.init_app(self.args)

        if self.args.daemonize:
            args = self.os_interaction.get_openport_exec()
            args.extend(sys.argv[1:])
            args = self.os_interaction.unset_variable(args, '--daemonize')
            args = self.os_interaction.unset_variable(args, '-d')
            self.os_interaction.spawn_daemon(args)
            logger.info('App started in background.')
            logger.debug(args)
            sys.exit(0)

        key_registration_service.register_key(self.args, self.args.server)

        self.db_handler = dbhandler.DBHandler(self.args.database)
        self.server.db_handler = self.db_handler

        self.config.manager_port = self.args.listener_port
        self.config.openport_address = self.args.server

        if self.args.config_file:
            self.config.config = self.args.config_file

        logger.debug('db location: ' + self.db_handler.db_location)

        if self.args.list:
            self.print_shares()
            sys.exit()

        if self.args.kill:
            self.kill(self.args.kill)
            sys.exit()

        if self.args.kill_all:
            self.kill_all()
            sys.exit()

        if self.args.restart_shares:
            self.restart_sharing()
            logger.debug('exiting')
            sys.exit()

        if self.args.create_migrations:
            from openport.services import migration_service
            migration_service.create_migrations(self.db_handler.db_location)
            sys.exit()

        session = Session()
        session.local_port = int(self.args.local_port)
        session.server_port = self.args.request_port
        session.server_session_token = self.args.request_token
        session.forward_tunnel = self.args.forward_tunnel
        session.active = False  # Will be set active in start_callback.

        if session.forward_tunnel:
            session.server_port = self.args.remote_port
            if self.args.local_port < 0:
                session.local_port = self.os_interaction.get_open_port()

        else:
            db_share = self.db_handler.get_share_by_local_port(
                session.local_port, filter_active=False)
            if db_share:
                logger.debug('previous share found in database')
                if is_running(db_share):
                    logger.info('Port forward already running for port %s' %
                                self.args.local_port)
                    sys.exit(6)

                if db_share.restart_command and not self.args.restart_on_reboot:
                    logger.warn(
                        'Port forward for port %s that would be restarted on reboot will not be restarted anymore.'
                        % self.args.local_port)

                if not session.server_session_token:
                    logger.debug("retrieved db share session token: %s" %
                                 db_share.server_session_token)
                    session.server_session_token = db_share.server_session_token
                    session.server_port = db_share.server_port
            else:
                logger.debug('No db share session could be found.')
            session.http_forward = self.args.http_forward

        if self.args.restart_on_reboot:
            session.restart_command = self.app_service.get_restart_command(
                session,
                database=self.args.database,
                verbose=self.args.verbose,
                server=self.args.server,
            )
            self.app_service.check_username_in_config_file()

        session.ip_link_protection = self.args.ip_link_protection

        session.stop_observers.append(self.stop_callback)
        session.start_observers.append(self.save_share)
        session.error_observers.append(self.error_callback)
        session.success_observers.append(self.success_callback)

        session.app_management_port = self.server.get_port()
        session.start_observers.append(self.server.inform_start)
        session.success_observers.append(self.server.inform_success)
        session.error_observers.append(self.server.inform_failure)
        session.stop_observers.append(self.server.inform_stop)

        ensure_keys_exist(*get_default_key_locations())

        self.session = session

        self.server.run_threaded()
        session.active = True
        self.save_share(session)
        self.openport.start_port_forward(session, server=self.args.server)
Exemple #3
0
    def start_port_forward(self, session, server=DEFAULT_SERVER):

        self.restart_on_failure = True
        self.automatic_restart = False
        self.session = session
        if not session.public_key_file:
            session.public_key_file, session.private_key_file = get_default_key_locations(
            )

        with open(session.public_key_file, 'r') as f:
            public_key = f.readline().strip()

        # This is the main loop. Exit this and the program ends.
        while self.restart_on_failure and not self.stopped:
            try:
                response = openport_api.request_open_port(
                    session.local_port,
                    request_server_port=session.server_port,
                    restart_session_token=session.server_session_token,
                    error_callback=session.notify_error,
                    stop_callback=session.notify_stop,
                    http_forward=session.http_forward,
                    server=server,
                    automatic_restart=self.automatic_restart,
                    public_key=public_key,
                    forward_tunnel=session.forward_tunnel,
                    ip_link_protection=session.ip_link_protection,
                )
                self.last_response = response

                if session.server_port != response.remote_port:
                    self.repeat_message = True

                session.server = response.server
                if not session.forward_tunnel:
                    session.server_port = response.remote_port
                session.pid = os.getpid()
                session.account_id = response.account_id
                session.key_id = response.key_id
                session.server_session_token = response.session_token
                session.http_forward_address = response.http_forward_address
                session.open_port_for_ip_link = response.open_port_for_ip_link

                self.port_forwarding_service = PortForwardingService(
                    session.local_port,
                    session.server_port,
                    session.server,
                    SERVER_SSH_PORT,
                    SERVER_SSH_USER,
                    session.public_key_file,
                    session.private_key_file,
                    success_callback=session.notify_success,
                    error_callback=session.notify_error,
                    fallback_server_ssh_port=response.fallback_ssh_server_port,
                    fallback_ssh_server=response.fallback_ssh_server_ip,
                    http_forward_address=session.http_forward_address,
                    start_callback=self.session_start,
                    forward_tunnel=session.forward_tunnel,
                    session_token=session.server_session_token,
                    keep_alive_interval_seconds=session.
                    keep_alive_interval_seconds,
                )
                self.port_forwarding_service.start()  # hangs
            except openport_api.FatalSessionError as e:
                logger.info('(Re)starting the session was denied: %s' % e)
                break
            except SystemExit as e:
                raise
            except IOError as e:
                if e.errno != errno.EINTR:
                    logger.error(e)
                    sleep(10)
            except TunnelError as e:
                logger.error(e)
                sleep(10)
            except Exception as e:
                # logger.exception(e)
                logger.error('general exception: {}'.format(e))
                sleep(10)
            finally:
                self.automatic_restart = True