def remove_session( agent_id=None, user=Default.User, customer=Default.Customer ): pid = ra.db.get_rd_pid(agent_id=agent_id) pid_msg = '' if pid: if stop_novnc(agent_id, pid): logger.info('Stopped NoVNC process for %s.' % agent_id) else: pid_msg = 'Unable to stop NoVNC for agent %s. ' % agent_id logger.error(pid_msg) session_closed, session_msg = ra.db.remove_connection( agent_id=agent_id ) if session_msg: logger.info(session_msg) operation = RaOperation( ra.RaValue.StopRemoteDesktop, agent_id, username=user, customer=customer ) operation_id = save_operation(operation) _id_msg = '' if operation_id: operation.operation_id = operation_id store_in_agent_queue(operation) logger.info('Added operation to queue for %s.' % agent_id) else: _id_msg = ( 'Unable to send stop operation to agent %s.' ' Agent side will most likely stay open... ' % agent_id ) logger.error(_id_msg) ra.add_feedback(agent_id, ra.Status.Closing) result = (pid and operation_id and session_closed) message = pid_msg + _id_msg + session_msg return { 'pass': result, 'message': message }
def _ra_stop_magic(json_operation): operation_id = json_operation.get('operation_id') agent_id = json_operation.get('agent_id') data = json_operation.get('data') error = json_operation.get('error') if agent_id: ra.add_feedback(agent_id, ra.Status.Closed, message='') save_result(agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.StopRemoteDesktop)
def remove_session(agent_id=None, user=Default.User, customer=Default.Customer): pid = ra.db.get_rd_pid(agent_id=agent_id) pid_msg = '' if pid: if stop_novnc(agent_id, pid): logger.info('Stopped NoVNC process for %s.' % agent_id) else: pid_msg = 'Unable to stop NoVNC for agent %s. ' % agent_id logger.error(pid_msg) session_closed, session_msg = ra.db.remove_connection(agent_id=agent_id) if session_msg: logger.info(session_msg) operation = RaOperation(ra.RaValue.StopRemoteDesktop, agent_id, username=user, customer=customer) operation_id = save_operation(operation) _id_msg = '' if operation_id: operation.operation_id = operation_id store_in_agent_queue(operation) logger.info('Added operation to queue for %s.' % agent_id) else: _id_msg = ('Unable to send stop operation to agent %s.' ' Agent side will most likely stay open... ' % agent_id) logger.error(_id_msg) ra.add_feedback(agent_id, ra.Status.Closing) result = (pid and operation_id and session_closed) message = pid_msg + _id_msg + session_msg return {'pass': result, 'message': message}
def _ra_stop_magic(json_operation): operation_id = json_operation.get('operation_id') agent_id = json_operation.get('agent_id') data = json_operation.get('data') error = json_operation.get('error') if agent_id: ra.add_feedback( agent_id, ra.Status.Closed, message='' ) save_result( agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.StopRemoteDesktop )
def _ra_magic(json_operation): error = '' operation_id = json_operation.get('operation_id') agent_id = json_operation.get('agent_id') data = json_operation.get('data') if data: success = data.get('success') error = data.get('error') host_port = data.get('host_port') else: success = False error += 'Agent did not send valid data.' host_port = None if ( not agent_id and not operation_id and not host_port ): ra.add_feedback( agent_id, ra.Status.Error, message='Agent did not send valid data.' ) logger.error( '%s - Unable to create remote desktop for agent: %s. ' 'Agent sent invalid data.' % ('system_user', agent_id) ) return if not ra.db.connection_exist(agent_id=agent_id): logger.error( 'Unknown agent (ID# %s) asking for remote desktop connection.' 'How did this happen?!' % agent_id ) return if success: try: web_port = None offset = 0 while True: # Get a port available system level. web_port = get_available_port(ra.PortRange, offset) # Checks if a port is reserved for a connection. if ra.db.port_available(port=web_port): break offset += 1 if offset == 5000: web_port = None break time.sleep(0.3) if web_port is None: raise Exception("No web port available.") except Exception as e: msg = 'No ports in range available?! How??' error += msg logger.error(str(e)) logger.error( '%s - Unable to create remote desktop for agent: %s. ' 'Error: %s' % ('system_user', agent_id, msg) ) ra.add_feedback( agent_id, ra.Status.Error, 'No ports available...?' ) save_result( agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop ) ra.db.remove_connection(agent_id=agent_id) return res, pid = novnc.launch_novnc(agent_id, web_port, host_port) if res: host = ra.get_hostname() uri = ra.create_vnc_uri(host, web_port) ra.add_feedback( agent_id, ra.Status.Ready, web_port=web_port, uri=uri, hostname=host ) ra.db.edit_connection( agent_id=agent_id, web_port=web_port, status=ra.Status.Ready, process_id=pid ) save_result( agent_id, operation_id, None, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop ) else: msg = 'Unable to start noVNC. Agent already in use?' error += msg ra.add_feedback( agent_id, ra.Status.Error, msg ) ra.db.remove_connection(agent_id=agent_id) save_result( agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop ) else: ra.add_feedback( agent_id, ra.Status.Error, message='Agent side error: %s' % error ) ra.db.remove_connection(agent_id=agent_id) logger.error( '%s - Unable to create remote desktop for agent: %s. ' 'Error: %s' % ('system_user', agent_id, error) ) save_result( agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop )
def new_rd_session(agent_id=None, user=Default.User, customer=Default.Customer): if not agent_id: return {'pass': False, 'message': 'No agent id was provided.'} res, session = ra.session_exist(agent_id) if res: status, web_port = session if status == ra.Status.Waiting: return { 'pass': False, 'message': ('Session exist for this agent. Waiting for response.'), ra.RaKey.Status: status } host = ra.get_hostname() uri = ra.create_vnc_uri(host, web_port) if status == ra.Status.Ready: return { 'pass': False, 'message': ('Session exist for this agent. Using session.'), ra.RaKey.Status: status, ra.RaKey.WebPort: web_port, ra.RaKey.Hostname: host, ra.RaKey.Uri: uri } if status == ra.Status.Timeout: ra.db.edit_connection(agent_id=agent_id, status=ra.Status.Ready) return { 'pass': False, 'message': ('Session exist for this agent. Reusing.'), ra.RaKey.Status: status, ra.RaKey.WebPort: web_port, ra.RaKey.Hostname: host, ra.RaKey.Uri: uri } if status == ra.Status.Closing: return { 'pass': False, 'message': ('Session exist but is being closed.' 'Please try again in a few seconds.'), ra.RaKey.Status: status, } operation = RaOperation(ra.RaValue.RemoteDesktop, agent_id, username=user, customer=customer, uri=ra.RaUri.StartRemoteDesktop % agent_id, method='POST') port = None tunnel_needed = True if tunnel_needed: params = None port_range = list(ra.PortRange) for p in ra.PortRange: params = reverse_tunnel_params(port_range) if ra.db.port_available(port=params[TunnelKey.HostPort]): break port_range.remove(p) params = None if params: port = params[TunnelKey.HostPort] ssh_port = params[TunnelKey.SSHPort] operation.set_tunnel(host_port=port, ssh_port=ssh_port) else: return { 'pass': False, 'message': "Could not resolve host port for tunnel." } operation_id = save_operation(operation) if operation_id: result, msg = ra.db.save_connection(agent_id=agent_id, host_port=port, status=ra.Status.Waiting) if result: operation.operation_id = operation_id ra.add_feedback(agent_id, ra.Status.Waiting) store_in_agent_queue(operation) return { 'pass': True, 'message': 'Remote desktop created. Waiting...' } else: return {'pass': False, 'message': msg} else: return { 'pass': False, 'message': "Unable to save operation. Invalid operation ID." }
def new_rd_session( agent_id=None, user=Default.User, customer=Default.Customer ): if not agent_id: return { 'pass': False, 'message': 'No agent id was provided.' } res, session = ra.session_exist(agent_id) if res: status, web_port = session if status == ra.Status.Waiting: return { 'pass': False, 'message': ( 'Session exist for this agent. Waiting for response.' ), ra.RaKey.Status: status } host = ra.get_hostname() uri = ra.create_vnc_uri(host, web_port) if status == ra.Status.Ready: return { 'pass': False, 'message': ( 'Session exist for this agent. Using session.' ), ra.RaKey.Status: status, ra.RaKey.WebPort: web_port, ra.RaKey.Hostname: host, ra.RaKey.Uri: uri } if status == ra.Status.Timeout: ra.db.edit_connection( agent_id=agent_id, status=ra.Status.Ready ) return { 'pass': False, 'message': ( 'Session exist for this agent. Reusing.' ), ra.RaKey.Status: status, ra.RaKey.WebPort: web_port, ra.RaKey.Hostname: host, ra.RaKey.Uri: uri } if status == ra.Status.Closing: return { 'pass': False, 'message': ( 'Session exist but is being closed.' 'Please try again in a few seconds.' ), ra.RaKey.Status: status, } operation = RaOperation( ra.RaValue.RemoteDesktop, agent_id, username=user, customer=customer, uri=ra.RaUri.StartRemoteDesktop % agent_id, method='POST' ) port = None tunnel_needed = True if tunnel_needed: params = None port_range = list(ra.PortRange) for p in ra.PortRange: params = reverse_tunnel_params(port_range) if ra.db.port_available( port=params[TunnelKey.HostPort] ): break port_range.remove(p) params = None if params: port = params[TunnelKey.HostPort] ssh_port = params[TunnelKey.SSHPort] operation.set_tunnel(host_port=port, ssh_port=ssh_port) else: return { 'pass': False, 'message': "Could not resolve host port for tunnel." } operation_id = save_operation(operation) if operation_id: result, msg = ra.db.save_connection( agent_id=agent_id, host_port=port, status=ra.Status.Waiting ) if result: operation.operation_id = operation_id ra.add_feedback(agent_id, ra.Status.Waiting) store_in_agent_queue(operation) return { 'pass': True, 'message': 'Remote desktop created. Waiting...' } else: return { 'pass': False, 'message': msg } else: return { 'pass': False, 'message': "Unable to save operation. Invalid operation ID." }
def _ra_magic(json_operation): error = '' operation_id = json_operation.get('operation_id') agent_id = json_operation.get('agent_id') data = json_operation.get('data') if data: success = data.get('success') error = data.get('error') host_port = data.get('host_port') else: success = False error += 'Agent did not send valid data.' host_port = None if (not agent_id and not operation_id and not host_port): ra.add_feedback(agent_id, ra.Status.Error, message='Agent did not send valid data.') logger.error('%s - Unable to create remote desktop for agent: %s. ' 'Agent sent invalid data.' % ('system_user', agent_id)) return if not ra.db.connection_exist(agent_id=agent_id): logger.error( 'Unknown agent (ID# %s) asking for remote desktop connection.' 'How did this happen?!' % agent_id) return if success: try: web_port = None offset = 0 while True: # Get a port available system level. web_port = get_available_port(ra.PortRange, offset) # Checks if a port is reserved for a connection. if ra.db.port_available(port=web_port): break offset += 1 if offset == 5000: web_port = None break time.sleep(0.3) if web_port is None: raise Exception("No web port available.") except Exception as e: msg = 'No ports in range available?! How??' error += msg logger.error(str(e)) logger.error('%s - Unable to create remote desktop for agent: %s. ' 'Error: %s' % ('system_user', agent_id, msg)) ra.add_feedback(agent_id, ra.Status.Error, 'No ports available...?') save_result(agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop) ra.db.remove_connection(agent_id=agent_id) return res, pid = novnc.launch_novnc(agent_id, web_port, host_port) if res: host = ra.get_hostname() uri = ra.create_vnc_uri(host, web_port) ra.add_feedback(agent_id, ra.Status.Ready, web_port=web_port, uri=uri, hostname=host) ra.db.edit_connection(agent_id=agent_id, web_port=web_port, status=ra.Status.Ready, process_id=pid) save_result(agent_id, operation_id, None, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop) else: msg = 'Unable to start noVNC. Agent already in use?' error += msg ra.add_feedback(agent_id, ra.Status.Error, msg) ra.db.remove_connection(agent_id=agent_id) save_result(agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop) else: ra.add_feedback(agent_id, ra.Status.Error, message='Agent side error: %s' % error) ra.db.remove_connection(agent_id=agent_id) logger.error('%s - Unable to create remote desktop for agent: %s. ' 'Error: %s' % ('system_user', agent_id, error)) save_result(agent_id, operation_id, error, data, ra.RaUri.RdResults, "POST", RaValue.RemoteDesktop)