Compare commits
2 Commits
master
...
35fe9c8c25
Author | SHA1 | Date | |
---|---|---|---|
35fe9c8c25 | |||
30bac8f70a |
@@ -6,7 +6,6 @@ from requests.auth import HTTPBasicAuth
|
|||||||
import subprocess
|
import subprocess
|
||||||
from glob import glob
|
from glob import glob
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import re # Dodane dla walidacji regexów
|
|
||||||
|
|
||||||
REQUIRED_SERVER_KEYS = {"url", "username", "password"}
|
REQUIRED_SERVER_KEYS = {"url", "username", "password"}
|
||||||
REQUIRED_CLIENT_KEYS = {"ip", "services", "schedule"}
|
REQUIRED_CLIENT_KEYS = {"ip", "services", "schedule"}
|
||||||
@@ -70,32 +69,6 @@ def validate_config(config):
|
|||||||
if section.startswith("client:") and not REQUIRED_CLIENT_KEYS.issubset(keys):
|
if section.startswith("client:") and not REQUIRED_CLIENT_KEYS.issubset(keys):
|
||||||
raise ValueError(f"Błędna konfiguracja w {section}: wymagane {REQUIRED_CLIENT_KEYS}")
|
raise ValueError(f"Błędna konfiguracja w {section}: wymagane {REQUIRED_CLIENT_KEYS}")
|
||||||
|
|
||||||
def is_valid_regex(pattern):
|
|
||||||
"""Walidacja regexów (opcjonalne)"""
|
|
||||||
try:
|
|
||||||
re.compile(pattern)
|
|
||||||
return True
|
|
||||||
except re.error:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def expand_aliases(config):
|
|
||||||
aliases = {}
|
|
||||||
for section in config.sections():
|
|
||||||
if section.startswith("alias:"):
|
|
||||||
alias_name = section.split(":")[1]
|
|
||||||
domains = []
|
|
||||||
for d in config.get(section, "domains").split(","):
|
|
||||||
d = d.strip()
|
|
||||||
if d:
|
|
||||||
# Opcjonalna walidacja regexów (tylko dla /.../)
|
|
||||||
if d.startswith("/") and d.endswith("/"):
|
|
||||||
if not is_valid_regex(d[1:-1]):
|
|
||||||
print(f"⚠️ Nieprawidłowy regex w aliasie {alias_name}: {d}")
|
|
||||||
continue
|
|
||||||
domains.append(d)
|
|
||||||
aliases[alias_name] = domains
|
|
||||||
return aliases
|
|
||||||
|
|
||||||
def load_configs(files):
|
def load_configs(files):
|
||||||
merged = configparser.ConfigParser()
|
merged = configparser.ConfigParser()
|
||||||
for file in files:
|
for file in files:
|
||||||
@@ -116,7 +89,6 @@ def test_server_connection(url, auth):
|
|||||||
|
|
||||||
def main(config_paths, silent=False):
|
def main(config_paths, silent=False):
|
||||||
config = load_configs(config_paths)
|
config = load_configs(config_paths)
|
||||||
aliases = expand_aliases(config)
|
|
||||||
|
|
||||||
servers = {
|
servers = {
|
||||||
section: {
|
section: {
|
||||||
@@ -127,22 +99,14 @@ def main(config_paths, silent=False):
|
|||||||
for section in config.sections() if section.startswith("server")
|
for section in config.sections() if section.startswith("server")
|
||||||
}
|
}
|
||||||
|
|
||||||
clients = {}
|
clients = {
|
||||||
for section in config.sections():
|
section: {
|
||||||
if section.startswith("client:"):
|
|
||||||
raw_services = [s.strip() for s in config.get(section, "services").split(",")]
|
|
||||||
expanded_services = []
|
|
||||||
for service in raw_services:
|
|
||||||
if service in aliases:
|
|
||||||
expanded_services.extend(aliases[service])
|
|
||||||
else:
|
|
||||||
expanded_services.append(service)
|
|
||||||
|
|
||||||
clients[section] = {
|
|
||||||
"ip": config.get(section, "ip"),
|
"ip": config.get(section, "ip"),
|
||||||
"services": expanded_services,
|
"services": [s.strip() for s in config.get(section, "services").split(",")],
|
||||||
"schedule": config.get(section, "schedule"),
|
"schedule": config.get(section, "schedule"),
|
||||||
}
|
}
|
||||||
|
for section in config.sections() if section.startswith("client:")
|
||||||
|
}
|
||||||
|
|
||||||
for srv_name, srv in servers.items():
|
for srv_name, srv in servers.items():
|
||||||
if not silent:
|
if not silent:
|
||||||
@@ -159,11 +123,7 @@ def main(config_paths, silent=False):
|
|||||||
for cdata in clients.values():
|
for cdata in clients.values():
|
||||||
if should_block(cdata["schedule"]):
|
if should_block(cdata["schedule"]):
|
||||||
for service in cdata["services"]:
|
for service in cdata["services"]:
|
||||||
# Zachowaj oryginalną składnię (regex, wyjątki, itp.)
|
rule = f"||{service}^$client={cdata['ip']}"
|
||||||
if service.startswith(("||", "@@", "/", "!", "#", "127.0.0.1")):
|
|
||||||
rule = f"{service}$client={cdata['ip']}"
|
|
||||||
else:
|
|
||||||
rule = f"||{service}^$client={cdata['ip']}" # Domyślna reguła
|
|
||||||
rules_set.add(rule)
|
rules_set.add(rule)
|
||||||
if not silent:
|
if not silent:
|
||||||
show_block_details(cdata["ip"], service, cdata["schedule"])
|
show_block_details(cdata["ip"], service, cdata["schedule"])
|
||||||
|
@@ -3,27 +3,11 @@ url = http://192.168.1.1:3000
|
|||||||
username = admin
|
username = admin
|
||||||
password = mypass
|
password = mypass
|
||||||
|
|
||||||
[server:office-dns1]
|
[server:office]
|
||||||
url = http://10.0.0.1:3000
|
url = http://10.0.0.1:3000
|
||||||
username = admin
|
username = admin
|
||||||
password = secret123
|
password = secret123
|
||||||
|
|
||||||
[server:office-dns2]
|
|
||||||
url = http://10.0.0.1:3000
|
|
||||||
username = admin
|
|
||||||
password = secret123
|
|
||||||
|
|
||||||
[alias:tiktok]
|
|
||||||
domains = tiktok.com, www.tiktok.com, m.tiktok.com, vm.tiktok.com, vt.tiktok.com,
|
|
||||||
api.tiktok.com, log.tiktok.com, tiktokcdn.com, tiktokv.com,
|
|
||||||
musical.ly, bytedance.com
|
|
||||||
|
|
||||||
[alias:social]
|
|
||||||
domains = /(facebook|twitter|instagram)\.com/,@@||linkedin.com^ # Regex + wyjątek
|
|
||||||
|
|
||||||
[alias:custom]
|
|
||||||
domains = 127.0.0.1 malware.com, ! Komentarz, /^ads?\./
|
|
||||||
|
|
||||||
[client:tv]
|
[client:tv]
|
||||||
ip = 192.168.1.101
|
ip = 192.168.1.101
|
||||||
services = youtube.com, netflix.com
|
services = youtube.com, netflix.com
|
||||||
@@ -41,7 +25,7 @@ schedule = custom:8-20
|
|||||||
|
|
||||||
[client:guest]
|
[client:guest]
|
||||||
ip = 192.168.1.200
|
ip = 192.168.1.200
|
||||||
services = tiktok # alias
|
services = tiktok.com
|
||||||
schedule = custom:0-0 ; zero godzin = nie blokuj
|
schedule = custom:0-0 ; zero godzin = nie blokuj
|
||||||
|
|
||||||
[client:office-laptop]
|
[client:office-laptop]
|
||||||
|
Reference in New Issue
Block a user