fix + update README

This commit is contained in:
root 2025-05-26 08:37:32 +00:00
parent 3f800193e5
commit d6f6b715a6
4 changed files with 11 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.log *.log
*.swp
venv venv

View File

@ -16,3 +16,5 @@ check-type:
check: format lint check-type check: format lint check-type
live:
tail -f /var/log/nginx/access.log | python3 ufwban.py --live

View File

@ -38,6 +38,7 @@ options:
-h, --help show this help message and exit -h, --help show this help message and exit
--dry-run --dry-run
--refresh Drop all the deny ip in the UFW table and return --refresh Drop all the deny ip in the UFW table and return
--to-nginx Generate an Nginx deny configuration
--reload Reload the UFW firewall --reload Reload the UFW firewall
--live Read inputs from stdin --live Read inputs from stdin
``` ```
@ -52,12 +53,15 @@ python ufwban.py --dry-run
# drop all "DENY IN" ufw rules (be careful) # drop all "DENY IN" ufw rules (be careful)
python ufwban.py --refresh python ufwban.py --refresh
# generate an Nginx deny configuration (use --dry-run to see updates before applying)
python ufwban.py --to-nginx
``` ```
* Live mode: * Live mode:
```bash ```bash
# Read and parse Nginx access logs on each new entry and ban ip # Read and parse Nginx access logs on each new entry and ban ip
tail -f /var/log/nginx/access.log | python ufwban.py tail -f /var/log/nginx/access.log | python ufwban.py --live
``` ```
For each modes, a log is available to show which ip has been banned: `ufwban.log`. For each modes, a log is available to show which ip has been banned: `ufwban.log`.

View File

@ -264,6 +264,7 @@ def get_logs_to_deny(logs: list[NginxLog], rules: Rules) -> dict[str, NginxLog]:
def get_nginx_denied_ips() -> list[str]: def get_nginx_denied_ips() -> list[str]:
"""Retrieve existing Nginx denied ips"""
denied_ips = [] denied_ips = []
try: try:
@ -278,7 +279,7 @@ def get_nginx_denied_ips() -> list[str]:
logging.warning(f"not a deny rule: {line} in {NGINX_DENY_CONF}") logging.warning(f"not a deny rule: {line} in {NGINX_DENY_CONF}")
continue continue
denied_ips.append(parts[2][:-1]) denied_ips.append(parts[1].removesuffix("\n")[:-1])
except FileNotFoundError: except FileNotFoundError:
logging.warning(f"{NGINX_DENY_CONF} does not exist") logging.warning(f"{NGINX_DENY_CONF} does not exist")