import re
import json

SENSITIVE_KEYS = {'password', 'token', 'secret', 'authorization', 'cookie', 'sessionid'}

def mask_sensitive_data(data):
    """
    Recursively traverse dictionary and mask sensitive keys.
    """
    if isinstance(data, dict):
        new_data = {}
        for k, v in data.items():
            if isinstance(k, str) and any(s in k.lower() for s in SENSITIVE_KEYS):
                new_data[k] = '******'
            else:
                new_data[k] = mask_sensitive_data(v)
        return new_data
    elif isinstance(data, list):
        return [mask_sensitive_data(item) for item in data]
    else:
        return data

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip
