def req_session_destroy(self, session_id): session = SessionsRepository.get(session_id, check_active=False) if session is not None: session.status = Session.SESSION_STATUS_DESTROYED SessionsRepository.set(session_id, session) Logger.info("[WebApps] session id={0} destroyed".format(session_id)) else: Logger.info("[WebApps] session id={0} not found".format(session_id)) session = Session(session_id, {}) session.status = Session.SESSION_STATUS_UNKNOWN return self.req_answer(self.session2xmlstatus(session))
def req_session_destroy(self, session_id): session = SessionsRepository.get(session_id, check_active=False) if session is not None: session.status = Session.SESSION_STATUS_DESTROYED SessionsRepository.set(session_id, session) Logger.info( "[WebApps] session id={0} destroyed".format(session_id)) else: Logger.info( "[WebApps] session id={0} not found".format(session_id)) session = Session(session_id, {}) session.status = Session.SESSION_STATUS_UNKNOWN return self.req_answer(self.session2xmlstatus(session))
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 process(self, communicator): # Create Context sess_id = SessionsRepository.get_session_id(communicator) session = SessionsRepository.get(sess_id) if sess_id else None if session is None: communicator.send(HTTP_403) return if self.id not in session['published_applications']: communicator.send(HTTP_403) return path = communicator.http.path[len(self.base_path):] index = path.find("$ROOT$") if index != -1: index += len("$ROOT$") path = path[index:] context = Context(communicator, session, path) self.request_processor.process(context) SessionsRepository.set(sess_id, session)
def process(self, communicator): # Create Context sess_id = SessionsRepository.get_session_id(communicator) session = SessionsRepository.get(sess_id) if sess_id else None if session is None: communicator.send(HTTP_403) return if self.id not in session['published_applications']: communicator.send(HTTP_403) return path = communicator.http.path[len(self.base_path):] index = path.find("$ROOT$"); if index != -1: index += len("$ROOT$") path = path[index:] context = Context(communicator, session, path) self.request_processor.process(context) SessionsRepository.set(sess_id, session)
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()
class Dialog(AbstractDialog): def __init__(self, role_instance): self.role_instance = role_instance @staticmethod def getName(): return "webapps" def process(self, request): path = request["path"] if request["method"] == "GET": Logger.debug("[WebApps] do_GET " + path) if path == "/sync": Logger.info("[WebApps] Starting config synchronization") setup_apps(reset=True) return self.req_answerText('OK') elif path.startswith("/session/status/"): buf = path[len("/session/status/"):] return self.req_session_status(buf) elif path.startswith("/session/destroy/"): buf = path[len("/session/destroy/"):] return self.req_session_destroy(buf) elif request["method"] == "POST": Logger.debug("[WebApps] do_POST " + path) if path == "/session/create": return self.req_session_create(request) Logger.info("WebApps role Dialog::process(%s)" % (str(request))) return None def req_session_create(self, request): try: document = minidom.parseString(request["data"]) session_node = document.documentElement if session_node.nodeName != "session": raise Exception("invalid root node") if not session_node.hasAttribute("id"): raise Exception("invalid root node") if not session_node.hasAttribute("mode"): raise Exception("invalid root node") session = {} session["id"] = session_node.getAttribute("id") session["mode"] = session_node.getAttribute("mode") if len(session["id"]) == 0: raise Exception("Missing attribute id") if session["mode"] == "desktop": session["mode"] = Session.MODE_DESKTOP elif session["mode"] == "applications": session["mode"] = Session.MODE_APPLICATIONS else: raise Exception("Missing attribute id") user_node = session_node.getElementsByTagName("user")[0] for attr in [ "login", "password", "displayName", "USER_LOGIN", "USER_PASSWD" ]: if not user_node.hasAttribute(attr): raise Exception("invalid child node: missing attribute " + attr) session[attr] = user_node.getAttribute(attr) published_apps = [] apps_node = session_node.getElementsByTagName("applications")[0] for app_node in apps_node.getElementsByTagName("application"): if not app_node.hasAttribute('id'): raise Exception("invalid child node: missing attribute id") published_apps.append(app_node.getAttribute('id')) session['published_applications'] = published_apps except Exception, err: Logger.warn("Invalid xml input: " + str(err)) doc = Document() root_node = doc.createElement('error') root_node.setAttribute("id", "usage") doc.appendChild(root_node) return self.req_answer(doc) session = SessionsRepository.create(session) return self.req_answer(self.session2xmlstatus(session))
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()