Esempio n. 1
0
def user_devices(request, username):
    user = get_object_or_404(User, username=username)
    if not user == request.user and not request.user.is_staff:
        return HttpResponseRedirect(reverse('member_profile', kwargs={'username': request.user.username}))

    error = None
    if request.method == 'POST':
        device_id = request.POST.get('device_id')
        device = UserDevice.objects.get(id=device_id)

        action = request.POST.get('action')
        if action == "Register":
            device.user = user

        device_name = request.POST.get('device_name')
        device_name = device_name.strip()[:32]
        device.device_name = device_name
        device.save()
        return HttpResponseRedirect(reverse('member_profile', kwargs={'username': request.user.username}))

    devices = user.userdevice_set.all()
    ip = network.get_addr(request)
    this_device = arp.device_by_ip(ip)

    context = {'user': user, 'devices': devices, 'this_device': this_device,
        'ip': ip, 'error': error, 'settings': settings}
    return render(request, 'members/profile_devices.html', context)
Esempio n. 2
0
def keymaster(request):
    try:
        ip = network.get_addr(request)
        keymaster = Keymaster.objects.by_ip(ip)
        if not keymaster:
            raise Exception("No Keymaster for incoming IP (%s)" % ip)
        logger.debug("Incoming connection from: %s" % ip)

        # Decrypt the incoming message
        connection = keymaster.get_encrypted_connection()
        incoming_message = connection.receive_message(request)
        logger.debug("Incoming Message: '%s' " % incoming_message)

        # Process the incoming message
        if incoming_message == Messages.TEST_QUESTION:
            outgoing_message = Messages.TEST_RESPONSE
        elif incoming_message == Messages.GET_TIME:
            return JsonResponse({'text_message':time.strftime("%c")})
        elif incoming_message == Messages.PULL_CONFIGURATION:
            outgoing_message = keymaster.pull_config()
        elif incoming_message == Messages.CHECK_IN:
            outgoing_message = keymaster.check_door_codes()
        elif incoming_message == Messages.PULL_DOOR_CODES:
            outgoing_message = keymaster.pull_door_codes()
        elif incoming_message == Messages.PUSH_EVENT_LOGS:
            incoming_data = connection.data
            #logger.debug("Incoming Data: '%s' " % incoming_data)
            outgoing_message = keymaster.process_event_logs(incoming_data)
        elif incoming_message == Messages.MARK_SUCCESS:
            keymaster.mark_success()
            outgoing_message = Messages.SUCCESS_RESPONSE
        elif incoming_message == Messages.MARK_SYNC:
            keymaster.mark_sync()
            outgoing_message = Messages.SUCCESS_RESPONSE
        elif incoming_message == Messages.LOG_MESSAGE:
            incoming_data = connection.data
            message = incoming_data['log_text']
            keymaster.log_message(message)
            outgoing_message = Messages.SUCCESS_RESPONSE
        else:
            raise Exception("Invalid Message")
        logger.debug("Outgoing Message: '%s' " % outgoing_message)

        # Encrypt our response
        encrypted_response = connection.encrypt_message(outgoing_message)
    except Exception as e:
        traceback.print_exc()
        logger.error(e)
        return JsonResponse({'error': str(e)})

    return JsonResponse({'message':encrypted_response.decode("utf-8")})
Esempio n. 3
0
def keymaster(request):
    try:
        ip = network.get_addr(request)
        keymaster = Keymaster.objects.by_ip(ip)
        if not keymaster:
            raise Exception("No Keymaster for incoming IP (%s)" % ip)
        logger.debug("Incoming connection from: %s" % ip)

        # Decrypt the incoming message
        connection = keymaster.get_encrypted_connection()
        incoming_message = connection.receive_message(request)
        logger.debug("Incoming Message: '%s' " % incoming_message)

        # Process the incoming message
        if incoming_message == Messages.TEST_QUESTION:
            outgoing_message = Messages.TEST_RESPONSE
        elif incoming_message == Messages.GET_TIME:
            return JsonResponse({'text_message': time.strftime("%c")})
        elif incoming_message == Messages.PULL_CONFIGURATION:
            outgoing_message = keymaster.pull_config()
        elif incoming_message == Messages.CHECK_IN:
            outgoing_message = keymaster.check_door_codes()
        elif incoming_message == Messages.PULL_DOOR_CODES:
            outgoing_message = keymaster.pull_door_codes()
        elif incoming_message == Messages.PUSH_EVENT_LOGS:
            incoming_data = connection.data
            #logger.debug("Incoming Data: '%s' " % incoming_data)
            outgoing_message = keymaster.process_event_logs(incoming_data)
        elif incoming_message == Messages.MARK_SUCCESS:
            keymaster.mark_success()
            outgoing_message = Messages.SUCCESS_RESPONSE
        elif incoming_message == Messages.MARK_SYNC:
            keymaster.mark_sync()
            outgoing_message = Messages.SUCCESS_RESPONSE
        elif incoming_message == Messages.LOG_MESSAGE:
            incoming_data = connection.data
            message = incoming_data['log_text']
            keymaster.log_message(message)
            outgoing_message = Messages.SUCCESS_RESPONSE
        else:
            raise Exception("Invalid Message")
        logger.debug("Outgoing Message: '%s' " % outgoing_message)

        # Encrypt our response
        encrypted_response = connection.encrypt_message(outgoing_message)
    except Exception as e:
        logger.error(e)
        return JsonResponse({'error': str(e)})

    return JsonResponse({'message': encrypted_response})
Esempio n. 4
0
def user_devices(request, username):
    user = get_object_or_404(User, username=username)
    if not user == request.user and not request.user.is_staff:
        return HttpResponseRedirect(
            reverse('member:profile:view',
                    kwargs={'username': request.user.username}))

    error = None
    if request.method == 'POST':
        device_id = request.POST.get('device_id')
        device = UserDevice.objects.get(id=device_id)

        action = request.POST.get('action')
        if action == "Register":
            device.user = user

        device_name = request.POST.get('device_name')
        device_name = device_name.strip()[:32]
        device.device_name = device_name
        device.save()
        return HttpResponseRedirect(
            reverse('member:profile:view',
                    kwargs={'username': request.user.username}))

    devices = user.userdevice_set.all()
    ip = network.get_addr(request)
    this_device = arp.device_by_ip(ip)

    context = {
        'user': user,
        'devices': devices,
        'this_device': this_device,
        'ip': ip,
        'error': error,
        'settings': settings
    }
    return render(request, 'members/profile/profile_devices.html', context)
Esempio n. 5
0
def view_ip(request):
    ip = network.get_addr(request)
    return render(request, 'staff/view_ip.html', {'ip': ip})
Esempio n. 6
0
def index(request):
    ip = network.get_addr(request)
    context = {'settings': settings, 'ip': ip, 'request': request}
    return render(request, 'staff/settings/index.html', context)
Esempio n. 7
0
def index(request):
    ip = network.get_addr(request)
    context = {'settings': settings, 'ip': ip, 'request': request}
    return render(request, 'staff/settings/index.html', context)
Esempio n. 8
0
def view_ip(request):
    ip = network.get_addr(request)
    return render(request, 'staff/view_ip.html', {'ip': ip})
Esempio n. 9
0
def register_user_ip(sender, user, request, **kwargs):
    logtime = localtime(now())
    ip = network.get_addr(request)
    if ip:
        ip_log = UserRemoteAddr.objects.create(logintime=logtime, user=user, ip_address=ip)
    logger.info("register_user_ip: Address for %s = %s @ %s" % (user, ip, logtime))
Esempio n. 10
0
def register_user_ip(sender, user, request, **kwargs):
    logtime = localtime(now())
    ip = network.get_addr(request)
    if ip:
        ip_log = UserRemoteAddr.objects.create(logintime=logtime, user=user, ip_address=ip)
    logger.info("register_user_ip: Address for %s = %s @ %s" % (user, ip, logtime))