Example #1
0
    def test_openport_app__offline(self, mock_post, mock_get):
        set_default_args(self.app, self.test_db)

        self.app.args.local_port = 24
        self.app.args.restart_on_reboot = True
        thr = threading.Thread(target=self.app.start)
        thr.setDaemon(True)
        thr.start()
        sleep(2)
        self.assertTrue(thr.is_alive)

        db_handler = dbhandler.DBHandler(self.test_db, init_db=False)
        self.assertEqual(1, len(db_handler.get_shares_to_restart()))
        self.assertEqual(1, len(db_handler.get_active_shares()))
        self.app.stop()
Example #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)
Example #3
0
def get_nr_of_shares_in_db_file(db_file):
    db_handler = dbhandler.DBHandler(db_file, init_db=False)
    try:
        return len(db_handler.get_active_shares())
    except:
        return 0