
    ɧ-i                     t   d Z ddlmZmZ ddlmZ ddlZddlmZm	Z	 ddl
Z
ddlmZ ddlZddlmZ ej                            d e ee          j        j                             ddlmZ  ed	           Z e
j                    Zej        Zej        Zd
 Zddedededeee	f         fdZ ddedefdZ!dS )z
Rate limiting middleware
Simple IP-based rate limiting (4 requests per IP like DeepSite)
Uses in-memory storage for rate limiting (no database required)
    )RequestHTTPExceptionwrapsN)DictAny)defaultdict)Path)settingsc                      dddS )Nr   )countwindow_start r       F/var/www/html/web-builder-api.evdpl.com/src/middleware/rate_limiter.py<lambda>r      s    Aq*I*I r   c                  &   t          j                     } t          5  g }t                                          D ]3\  }}t          dz  }| |d         z
  |k    r|                    |           4|D ]
}t          |= 	 ddd           dS # 1 swxY w Y   dS )z;Clean up expired rate limit entries to prevent memory leaks   r   N)time_storage_lock_rate_limit_storageitemsDEFAULT_WINDOW_SECONDSappend)current_timeexpired_ipsipdatattls        r   _cleanup_expired_entriesr       s    9;;L	 
( 
(+1133 	' 	'HB )1,Cd>22S88""2&&& 	( 	(B#B''	(
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
( 
(s   ABB
B
     
ip_addressmax_requestswindow_secondsreturnc                    t          j                     }||z
  }t          5  t          t                    dk    rt	                       t          |          }|d         |k     r#d|d<   ||d<   dd|dz
  ||z   dcddd           S |d         |k    r"d|d         d	|d         |z   dcddd           S |dxx         dz  cc<   d|d         ||d         z
  |d         |z   dcddd           S # 1 swxY w Y   dS )
a:  
    Check if IP address is within rate limit using in-memory storage.
    
    Args:
        ip_address: Client IP address
        max_requests: Maximum requests allowed (default: 4)
        window_seconds: Time window in seconds (default: 3600 = 1 hour)
    
    Returns:
        Dict with rate limit status
    d   r      r   T)allowedr   	remaining
reset_timeNFr   )r   r   lenr   r    )r#   r$   r%   r   r   r   s         r   _check_rate_limit_in_memoryr.   ,   s    9;;L.0L	 " ""##c))$&&&":. ,..DM#/D )A-*^;	 " " " " " " " "& G},,$!']!""&~"6"G	 )" " " " " " " "8 W"#!']!-W!="&~"6"G	 ;" " " " " " " " " " " " " " " " " "s   AC,!C,1.C,,C03C0requestsperc                 :      t            t           fd}|S )z9
    Rate limiting decorator using in-memory storage
    Nc                 B     t                      fd            }|S )Nc                    K   d }| D ]}t          |t                    r|} n|s3|                                D ]\  }}t          |t                    r|} n|s | i | d {V S |j        j        }t          |
	          }|d         sBt          dd
 d	 dt          |d         t          j                    z
             d           | i | d {V S )	Nr*   i  zRate limit exceeded. Maximum z requests per z seconds. Try again in r,   z	 seconds.)status_codedetail)	
isinstancer   r   clienthostr.   r   intr   )argskwargsrequestargkeyvalue	client_iprate_limit_resultfuncr0   r/   s           r   wrapperz.rate_limit.<locals>.decorator.<locals>.wrapperj   s      G   c7++ !GE
  "(,,..  JC!%11 "'  3!T426222222222  +I !<IxQT U U$Y/ # # m8  m  mSV  m  mor  tE  FR  tS  VZ  V_  Va  Va  ta  pb  pb  m  m  m    t.v.........r   r   )rB   rC   r0   r/   s   ` r   	decoratorzrate_limit.<locals>.decoratori   s?    	t"	/ "	/ "	/ "	/ "	/ "	/ 
"	/H r   )DEFAULT_MAX_REQUESTSr   )r/   r0   rD   s   `` r   
rate_limitrF   _   sB    
 '
{$& & & & & &N r   )r!   r"   )NN)"__doc__fastapir   r   	functoolsr   r   typingr   r   	threadingcollectionsr	   syspathlibr
   pathinsertstr__file__parentcore.configr   r   Lockr   max_requests_per_iprE   max_requests_windowr   r    r9   r.   rF   r   r   r   <module>rX      s    + * * * * * * *                    # # # # # # 



       33ttH~~,344 5 5 5             "k"I"IJJ 	    3 !5 ( ( (1 1C 1s 1X[ 1gkloqtltgu 1 1 1 1f1 1 1# 1 1 1 1 1 1r   