def get_share_line(self, share): # "pid: %s - " % share.pid + \ share_line = "localport: %s - " % share.local_port + \ "remote server: %s - " % share.server + \ "remote port: %s - " % share.server_port if share.open_port_for_ip_link: share_line += "open-for-ip-link: %s - " % share.open_port_for_ip_link share_line += "running: %s - " % is_running(share) + \ "restart on reboot: %s" % bool(share.restart_command) if self.config.verbose: share_line += ' - pid: %s' % share.pid + \ ' - id: %s' % share.id + \ ' - token: %s' % share.server_session_token return share_line
def initialize(self): self.server.run_threaded() shares = self.db_handler.get_active_shares() shares.extend(self.db_handler.get_shares_to_restart()) shares = {x.id: x for x in shares}.values() for share in shares: self.add_share(share) if not is_running(share): self.notify_app_down(share) for share in shares: self.server.register_with_app(share)
def restart_sharing(self): shares = self.db_handler.get_shares_to_restart() logger.debug('restarting shares - amount: %s' % len(list(shares))) for share in shares: if not is_running(share): try: logger.info('restarting share: %s' % ' '.join(share.restart_command)) share.restart_command = self.app_service.set_manager_port( share.restart_command) p = self.os_interaction.start_openport_process(share) logger.debug('process started with pid %s' % p.pid) # self.os_interaction.print_output_continuously_threaded(p, 'share port: %s - ' % share.local_port) sleep(1) if p.poll() is not None: all_output = self.os_interaction.get_all_output(p) logger.debug( 'could not start openport process for port %s: StdOut:%s\nStdErr:%s' % (share.local_port, all_output[0], all_output[1])) else: logger.debug('started app with pid %s : %s' % (p.pid, share.restart_command)) sleep(1) except Exception as e: logger.exception(e) else: logger.debug('not starting %s: still running' % share.local_port) if not osinteraction.is_windows() and self.os_interaction.user_is_root( ) and os.path.exists(USER_CONFIG_FILE): with open(USER_CONFIG_FILE, 'r') as f: lines = f.readlines() for line in lines: if not line.strip() or line.strip()[0] == '#': continue username = line.strip().split()[0] if username == 'root': continue openport_command = self.os_interaction.get_openport_exec() command = ['sudo', '-u', username, '-H' ] + openport_command + ['--restart-shares'] logger.debug('restart command: %s' % command) self.os_interaction.spawn_daemon(command)
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)
def remove_killed_share(): while is_running(s): sleep(1) wx.CallAfter(self.remove_share, share) self.db_handler.stop_share(share, restart=False)