o
    h•jh  ã                   @   s8   d dl Z d dlmZ e  d¡ZdededB fdd„ZdS )é    N)ÚRequestzapp.utils.ip_extractorÚrequestÚreturnc                 C   s¬   | j  dd¡}| j  d¡}| j  d¡}| j  d¡}| jr | jjnd}d}|r1| d¡d  ¡ p0d}|s7|r7|}td	|dd
… › d|› d|› d|› d|› d|› d�dd� |S )a<  
    Extracts client IP address from HTTP request headers or direct connection host.
    Checks X-Forwarded-For header first (first entry if comma-separated list),
    falling back to direct client.host IP.
    
    Note: X-Forwarded-For should be trusted only when running behind a trusted proxy/load balancer.
    z
user-agentÚUnknownzx-forwarded-forzcf-connecting-ipz	x-real-ipNú,r   z[IP_DEBUG] UA='é<   z' | X-Forwarded-For='z' | CF-Connecting-IP='z' | X-Real-IP='z' | Client.Host='z' | Resolved_IP='ú'T)Úflush)ÚheadersÚgetÚclientÚhostÚsplitÚstripÚprint)r   Ú
user_agentÚforwarded_forÚcf_connecting_ipÚ	x_real_ipÚclient_hostÚ	client_ip© r   ú7/web/html/dev/chatbot/backend/app/utils/ip_extractor.pyÚextract_client_ip   s2   ÿþýüûù
r   )ÚloggingÚfastapir   Ú	getLoggerÚloggerÚstrr   r   r   r   r   Ú<module>   s    
