V10.426 deduplicate 111 proxy reject logs
This commit is contained in:
@@ -16,6 +16,7 @@ import logging
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
LISTEN_HOST = os.getenv("OLLAMA111_PROXY_LISTEN_HOST", "192.168.0.111")
|
||||
@@ -37,6 +38,8 @@ def _allowed_networks() -> tuple[ipaddress._BaseNetwork, ...]:
|
||||
|
||||
|
||||
ALLOWED_NETWORKS = _allowed_networks()
|
||||
REJECT_LOG_DEDUP_SEC = float(os.getenv("OLLAMA111_PROXY_REJECT_LOG_DEDUP_SEC", "60"))
|
||||
_LAST_REJECT_LOG: dict[str, float] = {}
|
||||
|
||||
|
||||
def _is_allowed(peer_ip: str) -> bool:
|
||||
@@ -69,7 +72,11 @@ async def _handle_client(reader: asyncio.StreamReader, writer: asyncio.StreamWri
|
||||
peer = writer.get_extra_info("peername")
|
||||
peer_ip = peer[0] if peer else "unknown"
|
||||
if not _is_allowed(peer_ip):
|
||||
logging.warning("reject peer=%s allowed=%s", peer_ip, ",".join(ALLOWED_CIDRS))
|
||||
now = time.monotonic()
|
||||
last_log = _LAST_REJECT_LOG.get(peer_ip, 0.0)
|
||||
if now - last_log >= REJECT_LOG_DEDUP_SEC:
|
||||
logging.warning("reject peer=%s allowed=%s", peer_ip, ",".join(ALLOWED_CIDRS))
|
||||
_LAST_REJECT_LOG[peer_ip] = now
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user