예제 #1
0
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."
        }
예제 #2
0
파일: creator.py 프로젝트: j-lowry/vFense
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."}
예제 #3
0
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
        )
예제 #4
0
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)