def __init__(self, main_instance): AbstractRole.__init__(self, main_instance) self.server = None self.ssl_ctx = None self.processes = {} self.kill_mutex = threading.Lock() self.sessions_repo = SessionsRepository.initialize() self.apps_repo = ApplicationsRepository.initialize()
def setup_apps(reset=False): """ """ from ApplicationsRepository import ApplicationsRepository if reset: ApplicationsRepository.reset() sm_request_manager = SMRequestManager() webapps_dom = sm_request_manager.get_webapps() if webapps_dom == None: return True for webapp_dom in webapps_dom.childNodes: config_nodes = webapp_dom.getElementsByTagName('configuration') if len(config_nodes) < 1: continue config = json.loads(config_nodes[0].firstChild.data) if config.keys() < 1: continue app_id = webapp_dom.getAttribute('id') app_name = config.keys()[0] try: app_id = System.local_encode(app_id) app_name = System.local_encode(app_name) appl = setup_app(config, app_id, app_name, Config.mode) except: Logger.exception( "Setting up an application failed. Correct its configuration.") continue if appl is None: continue ApplicationsRepository.register(appl) Logger.info("Configured %s webapp(s)" % len(ApplicationsRepository.list())) return True
def process(communicator): path = communicator.http.path referer = communicator.http.get_header("Referer") host = communicator.http.get_header("X-Forwarded-Host") or \ communicator.http.get_header("Host") Logger.debug("[WebApps] Client requested " + host + path) for app_def in ApplicationsRepository.list(): if app_def.handles(communicator): app_def.process(communicator) return # check referer if Config.mode == Config.MODE_PATH and referer is not None: url = urlparse.urlparse(referer) for app_def in ApplicationsRepository.list(): if url.path.startswith(app_def.base_path): # redirect new_location = (url.path+'$ROOT$'+path).replace("//", "/") ApplicationsDispatcher.redirect(communicator, new_location) return if path.startswith(ApplicationsDispatcher.DISCONNECT): qs = urlparse.parse_qs(path[len(ApplicationsDispatcher.DISCONNECT):]) Logger.debug("DEBUG: {0}".format(qs)) if qs.get('user') and qs.get('pass') and qs.get('id'): user = qs['user'][0] session = SessionsRepository.find(user, qs['pass'][0]) Logger.debug("DEBUG: user: {0}, session: {1}".format(user,session)) if session is not None: Logger.debug("[WebApps] session {0} switch status to disconnected".format(session.id)) sess_id = session.id session.switch_status(Session.SESSION_STATUS_INACTIVE) SessionsRepository.set(sess_id, session) send_buffer = HTTP_200_status_content.format(qs['id'][0], "disconnected") send_buffer = HTTP_200_status_header.format(len(send_buffer)) + '\r\n\r\n' + send_buffer communicator.send(send_buffer) return else: Logger.warn("[WebApps] no session for user {0}".format(user)) if path.startswith(ApplicationsDispatcher.CONNECT): qs = urlparse.parse_qs(path[len(ApplicationsDispatcher.CONNECT):]) if qs.get('user') and qs.get('pass') and qs.get('id'): user = qs['user'][0] session = SessionsRepository.find(user, qs['pass'][0]) if session is not None: sess_id = session.id if session.status in [Session.SESSION_STATUS_INITED, Session.SESSION_STATUS_INACTIVE]: session.switch_status(Session.SESSION_STATUS_ACTIVE) SessionsRepository.set(sess_id, session) send_buffer = HTTP_200_status_content.format(qs['id'][0], "ready") send_buffer = HTTP_200_status_header.format(len(send_buffer)) + '\r\n\r\n' + send_buffer communicator.send(send_buffer) return else: Logger.warn("[WebApps] can't login to not new session id={0}".format(sess_id)) else: Logger.warn('[WebApps] no session for user {0}'.format(user)) ## if user is redirected from ovd client let check his credentials ## and redirect to domain when app works if path.startswith(ApplicationsDispatcher.OPEN): qs = urlparse.parse_qs(path[len(ApplicationsDispatcher.OPEN):]) if qs.get('user') and qs.get('pass') and qs.get('id'): app_id = qs['id'][0] user = qs['user'][0] session = SessionsRepository.find(user, qs['pass'][0]) if session is not None and app_id: sess_id = session.id if session.status == Session.SESSION_STATUS_ACTIVE: app_name = ApplicationsRepository.get_name_by_id(app_id) if app_name: if app_id in session['published_applications']: session.switch_status(Session.SESSION_STATUS_ACTIVE) SessionsRepository.set(sess_id, session) Logger.info('[WebApps] session id={0} for user {1} activated'.format(sess_id, user)) prot = Config.connection_secure and 'https' or 'http' if Config.mode == Config.MODE_DOMAIN: new_host = '{0}://{1}.{2}{3}'.format(prot, app_name, host, ApplicationsRepository.get_by_id(app_id).start_path) host_wo_port = host.split(':')[0] ApplicationsDispatcher.redirect(communicator, new_host, Config.ulteo_session_cookie, sess_id, "."+host_wo_port, "/") return else: # mode path new_host = '{0}://{1}/webapps/{2}{3}'.format(prot, host, app_name, ApplicationsRepository.get_by_id(app_id).start_path) cookie_path = '/webapps/%s%s'%(app_name, ApplicationsRepository.get_by_id(app_id).start_path) host_wo_port = host.split(':')[0] ApplicationsDispatcher.redirect(communicator, new_host, Config.ulteo_session_cookie, sess_id, host_wo_port, cookie_path) return else: Logger.warn('[WebApps] user {0} is not allowed to open {1}'.format(user, app_id)) else: Logger.warn('[WebApps] no config for app with id {0}'.format(app_id)) else: Logger.warn('[WebApps] can\'t open all when session id={0} is not active'.format(sess_id)) else: Logger.warn('[WebApps] no session for user {0}'.format(user)) raise ApplicationsDispatcher.EDispatchError()
def process(communicator): path = communicator.http.path referer = communicator.http.get_header("Referer") host = communicator.http.get_header("X-Forwarded-Host") or \ communicator.http.get_header("Host") Logger.debug("[WebApps] Client requested " + host + path) for app_def in ApplicationsRepository.list(): if app_def.handles(communicator): app_def.process(communicator) return # check referer if Config.mode == Config.MODE_PATH and referer is not None: url = urlparse.urlparse(referer) for app_def in ApplicationsRepository.list(): if url.path.startswith(app_def.base_path): # redirect new_location = (url.path + '$ROOT$' + path).replace( "//", "/") ApplicationsDispatcher.redirect(communicator, new_location) return if path.startswith(ApplicationsDispatcher.DISCONNECT): qs = urlparse.parse_qs( path[len(ApplicationsDispatcher.DISCONNECT):]) Logger.debug("DEBUG: {0}".format(qs)) if qs.get('user') and qs.get('pass') and qs.get('id'): user = qs['user'][0] session = SessionsRepository.find(user, qs['pass'][0]) Logger.debug("DEBUG: user: {0}, session: {1}".format( user, session)) if session is not None: Logger.debug( "[WebApps] session {0} switch status to disconnected". format(session.id)) sess_id = session.id session.switch_status(Session.SESSION_STATUS_INACTIVE) SessionsRepository.set(sess_id, session) send_buffer = HTTP_200_status_content.format( qs['id'][0], "disconnected") send_buffer = HTTP_200_status_header.format( len(send_buffer)) + '\r\n\r\n' + send_buffer communicator.send(send_buffer) return else: Logger.warn( "[WebApps] no session for user {0}".format(user)) if path.startswith(ApplicationsDispatcher.CONNECT): qs = urlparse.parse_qs(path[len(ApplicationsDispatcher.CONNECT):]) if qs.get('user') and qs.get('pass') and qs.get('id'): user = qs['user'][0] session = SessionsRepository.find(user, qs['pass'][0]) if session is not None: sess_id = session.id if session.status in [ Session.SESSION_STATUS_INITED, Session.SESSION_STATUS_INACTIVE ]: session.switch_status(Session.SESSION_STATUS_ACTIVE) SessionsRepository.set(sess_id, session) send_buffer = HTTP_200_status_content.format( qs['id'][0], "ready") send_buffer = HTTP_200_status_header.format( len(send_buffer)) + '\r\n\r\n' + send_buffer communicator.send(send_buffer) return else: Logger.warn( "[WebApps] can't login to not new session id={0}". format(sess_id)) else: Logger.warn( '[WebApps] no session for user {0}'.format(user)) ## if user is redirected from ovd client let check his credentials ## and redirect to domain when app works if path.startswith(ApplicationsDispatcher.OPEN): qs = urlparse.parse_qs(path[len(ApplicationsDispatcher.OPEN):]) if qs.get('user') and qs.get('pass') and qs.get('id'): app_id = qs['id'][0] user = qs['user'][0] session = SessionsRepository.find(user, qs['pass'][0]) if session is not None and app_id: sess_id = session.id if session.status == Session.SESSION_STATUS_ACTIVE: app_name = ApplicationsRepository.get_name_by_id( app_id) if app_name: if app_id in session['published_applications']: session.switch_status( Session.SESSION_STATUS_ACTIVE) SessionsRepository.set(sess_id, session) Logger.info( '[WebApps] session id={0} for user {1} activated' .format(sess_id, user)) prot = Config.connection_secure and 'https' or 'http' if Config.mode == Config.MODE_DOMAIN: new_host = '{0}://{1}.{2}{3}'.format( prot, app_name, host, ApplicationsRepository.get_by_id( app_id).start_path) host_wo_port = host.split(':')[0] ApplicationsDispatcher.redirect( communicator, new_host, Config.ulteo_session_cookie, sess_id, "." + host_wo_port, "/") return else: # mode path new_host = '{0}://{1}/webapps/{2}{3}'.format( prot, host, app_name, ApplicationsRepository.get_by_id( app_id).start_path) cookie_path = '/webapps/%s%s' % ( app_name, ApplicationsRepository.get_by_id( app_id).start_path) host_wo_port = host.split(':')[0] ApplicationsDispatcher.redirect( communicator, new_host, Config.ulteo_session_cookie, sess_id, host_wo_port, cookie_path) return else: Logger.warn( '[WebApps] user {0} is not allowed to open {1}' .format(user, app_id)) else: Logger.warn( '[WebApps] no config for app with id {0}'. format(app_id)) else: Logger.warn( '[WebApps] can\'t open all when session id={0} is not active' .format(sess_id)) else: Logger.warn( '[WebApps] no session for user {0}'.format(user)) raise ApplicationsDispatcher.EDispatchError()