mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	decorators: Extract rate_limit_request_by_ip function.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							119f1da04a
						
					
				
				
					commit
					4418aefde4
				
			@@ -862,6 +862,15 @@ def rate_limit_ip(request: HttpRequest, ip_addr: str, domain: str) -> None:
 | 
				
			|||||||
    RateLimitedIPAddr(ip_addr, domain=domain).rate_limit_request(request)
 | 
					    RateLimitedIPAddr(ip_addr, domain=domain).rate_limit_request(request)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def rate_limit_request_by_ip(request: HttpRequest, domain: str) -> None:
 | 
				
			||||||
 | 
					    # REMOTE_ADDR is set by SetRemoteAddrFromRealIpHeader in conjunction
 | 
				
			||||||
 | 
					    # with the nginx configuration to guarantee this to be *the* correct
 | 
				
			||||||
 | 
					    # IP address to use - without worrying we'll grab the IP of a proxy.
 | 
				
			||||||
 | 
					    ip_addr = request.META["REMOTE_ADDR"]
 | 
				
			||||||
 | 
					    assert ip_addr
 | 
				
			||||||
 | 
					    rate_limit_ip(request, ip_addr, domain=domain)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def rate_limit_remote_server(
 | 
					def rate_limit_remote_server(
 | 
				
			||||||
    request: HttpRequest, remote_server: "RemoteZulipServer", domain: str
 | 
					    request: HttpRequest, remote_server: "RemoteZulipServer", domain: str
 | 
				
			||||||
) -> None:
 | 
					) -> None:
 | 
				
			||||||
@@ -893,12 +902,7 @@ def rate_limit() -> Callable[[ViewFuncT], ViewFuncT]:
 | 
				
			|||||||
            user = request.user
 | 
					            user = request.user
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if isinstance(user, AnonymousUser):
 | 
					            if isinstance(user, AnonymousUser):
 | 
				
			||||||
                # REMOTE_ADDR is set by SetRemoteAddrFromRealIpHeader in conjunction
 | 
					                rate_limit_request_by_ip(request, domain="api_by_ip")
 | 
				
			||||||
                # with the nginx configuration to guarantee this to be *the* correct
 | 
					 | 
				
			||||||
                # IP address to use - without worrying we'll grab the IP of a proxy.
 | 
					 | 
				
			||||||
                ip_addr = request.META["REMOTE_ADDR"]
 | 
					 | 
				
			||||||
                assert ip_addr
 | 
					 | 
				
			||||||
                rate_limit_ip(request, ip_addr, domain="api_by_ip")
 | 
					 | 
				
			||||||
                return func(request, *args, **kwargs)
 | 
					                return func(request, *args, **kwargs)
 | 
				
			||||||
            elif settings.ZILENCER_ENABLED and isinstance(user, RemoteZulipServer):
 | 
					            elif settings.ZILENCER_ENABLED and isinstance(user, RemoteZulipServer):
 | 
				
			||||||
                rate_limit_remote_server(request, user, domain="api_by_remote_server")
 | 
					                rate_limit_remote_server(request, user, domain="api_by_remote_server")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user