Esempio n. 1
0
def logout(request):
    """
    Log users out (destroy all sessions) and re-direct them to the main page.
    """
    # Save profile and user object
    user = request.user
    profile = request.user.userprofile
    # Create guacamole object attached to request.user.username and with current guacamole password
    g = GuacamoleAuth(request)
    # Do a guacamole logout
    gcookie = g.logout()
    # We can then remove the cached configuration
    g.del_auth()
    # Get the response object
    response = logout_then_login(request)
    # Remove the guacamole cookie from response object
    response.delete_cookie(**gcookie['cookie'])
    # Setup i18n settings of the logged in user into session of an anonymous user
    profile.activate_locale(request)
    # Get auth logger and log the logout :)
    auth_logger.info('User %s successfully logged out from %s (%s)',
                     user, get_client_ip(request), request.META.get('HTTP_USER_AGENT', ''))

    # Bye bye
    return response
Esempio n. 2
0
def vnc(request, hostname):
    """
    Guacamole remote console view.
    """
    # get VM object or raise 404
    vm = get_vm(request, hostname, sr=('dc', 'owner', 'node'))

    # basic view data
    context = {'vm': vm, 'gc': {}}

    # First check if VNC is available for this vm and vm is running
    if not (vm.status in (vm.RUNNING, vm.STOPPING) and vm.node
            and vm.node.address and vm.vnc_port
            and vm.node.status in Node.STATUS_OPERATIONAL
            and vm.vnc_port == vm.json_active.get('vnc_port', None)):
        # VNC settings not found or VM is not running
        return render(request, 'gui/vm/vnc.html', context)

    # Test if VNC port is open (e.g. when the VM was just started, but the vnc is not yet initialized)
    if not GuacamoleAuth.test_vnc(vm):
        # VNC port is not open yet, but maybe it will be in few seconds (see the JS in vnc.html)
        context['gc']['ready'] = False
        return render(request, 'gui/vm/vnc.html', context)

    # set Guacamole tunnel setting
    context['gc'] = {
        'ready': True,
        'tunnel': settings.GUACAMOLE_HTU,
        'wss': settings.GUACAMOLE_WSS,
        'vnc_zoom':
        bool(request.GET.get('zoom', settings.GUACAMOLE_DEFAULT_ZOOM)),
    }

    # return vnc window without login (we already have a JSESSIONID cookie)
    if request.GET.get('nologin', False):
        context['gc']['token'] = request.session.get(settings.GUACAMOLE_TOKEN,
                                                     '')
        return render(request, 'gui/vm/vnc.html', context)

    # Create guacamole object attached to request.user.username and VM
    # A password for the user will be generated automatically
    g = GuacamoleAuth(request, vm)
    # Create a VM usermap for this request.user and VM and set guacamole configuration into cache
    g.set_auth()
    # Perform a login to guacamole, which will give you a JSESSIONID cookie.
    gcookie = g.login()
    context['gc']['token'] = gcookie.get('token', '')
    # Get a response object
    response = render(request, 'gui/vm/vnc.html', context)
    # Set the guacamole cookie into the response object
    if 'cookie' in gcookie:
        response.set_cookie(**gcookie['cookie'])
    # Delete usermap (it is cached in guacamole)
    g.del_auth()

    # Give it to the user
    return response