| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import os
- import json
- import datetime
- import secrets
- from werkzeug.security import generate_password_hash
- import config
- def read_file(path):
- try:
- if os.path.exists(path):
- with open(path, "r", errors='ignore') as f: return f.read()
- except: pass
- return ""
- def write_file(path, content):
- with open(path, "w") as f: f.write(content)
- def load_panel_settings():
- default = {
- "templates": {}, "zone_owners": {}, "stats_retention": 30,
- "query_log_enabled": "no", "allow_transfer": ["none"],
- "also_notify": [], "allow_query": ["any"],
- "api_key": secrets.token_hex(32),
- "server_public_ip": "",
- "cluster_nodes": [],
- "master_node": {},
- "adblock_last_update": "Never",
- "adblock_domains_count": 0
- }
- if os.path.exists(config.PANEL_SETTINGS):
- try:
- with open(config.PANEL_SETTINGS, "r") as f:
- data = json.load(f)
- for k, v in default.items():
- if k not in data: data[k] = v
- # Έλεγχος αν υπάρχει api_key (backward compatibility)
- if "api_key" not in json.loads(read_file(config.PANEL_SETTINGS) or "{}"):
- write_file(config.PANEL_SETTINGS, json.dumps(data, indent=4))
- return data
- except: pass
- write_file(config.PANEL_SETTINGS, json.dumps(default, indent=4))
- return default
- def save_panel_settings(data):
- write_file(config.PANEL_SETTINGS, json.dumps(data, indent=4))
- def load_adblock_lists():
- if os.path.exists(config.ADBLOCK_LISTS_FILE):
- try:
- with open(config.ADBLOCK_LISTS_FILE, "r") as f:
- data = json.load(f)
- existing_ids = [l["id"] for l in data]
- for d_list in config.DEFAULT_ADBLOCK_LISTS:
- if d_list["id"] not in existing_ids:
- data.append(d_list)
- return data
- except: pass
- write_file(config.ADBLOCK_LISTS_FILE, json.dumps(config.DEFAULT_ADBLOCK_LISTS, indent=4))
- return config.DEFAULT_ADBLOCK_LISTS
- def save_adblock_lists(data):
- write_file(config.ADBLOCK_LISTS_FILE, json.dumps(data, indent=4))
- def load_custom_rules():
- if os.path.exists(config.ADBLOCK_CUSTOM_RULES_FILE):
- try:
- with open(config.ADBLOCK_CUSTOM_RULES_FILE, "r") as f: return json.load(f)
- except: pass
- return {"allowed": [], "deny": []}
- def save_custom_rules(data):
- write_file(config.ADBLOCK_CUSTOM_RULES_FILE, json.dumps(data, indent=4))
- def load_users():
- default_admin = {"admin": {"password": generate_password_hash("admin123"), "role": "admin", "assigned_templates": [], "view_mode": "cards"}}
- if not os.path.exists(config.USERS_FILE):
- save_users(default_admin)
- return default_admin
- try:
- with open(config.USERS_FILE, "r") as f:
- users = json.load(f)
- if not users: return default_admin
- for u in users:
- if "view_mode" not in users[u]: users[u]["view_mode"] = "cards"
- if "assigned_templates" not in users[u]: users[u]["assigned_templates"] = []
- return users
- except: return default_admin
- def save_users(data):
- write_file(config.USERS_FILE, json.dumps(data, indent=4))
- def log_audit(user, domain, action):
- try:
- with open(config.AUDIT_FILE, "r") as f: log = json.load(f)
- except: log = []
- log.insert(0, {"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "user": user, "domain": domain, "action": action})
- # Κρατάμε μόνο τις τελευταίες 500 εγγραφές για να μην φουσκώσει το αρχείο
- with open(config.AUDIT_FILE, "w") as f: json.dump(log[:500], f)
- def get_audit_log(domain):
- try:
- with open(config.AUDIT_FILE, "r") as f: log = json.load(f)
- return [l for l in log if l['domain'] == domain]
- except: return []
|