file_manager.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os
  2. import json
  3. import datetime
  4. import secrets
  5. from werkzeug.security import generate_password_hash
  6. import config
  7. def read_file(path):
  8. try:
  9. if os.path.exists(path):
  10. with open(path, "r", errors='ignore') as f: return f.read()
  11. except: pass
  12. return ""
  13. def write_file(path, content):
  14. with open(path, "w") as f: f.write(content)
  15. def load_panel_settings():
  16. default = {
  17. "templates": {}, "zone_owners": {}, "stats_retention": 30,
  18. "query_log_enabled": "no", "allow_transfer": ["none"],
  19. "also_notify": [], "allow_query": ["any"],
  20. "api_key": secrets.token_hex(32),
  21. "server_public_ip": "",
  22. "cluster_nodes": [],
  23. "master_node": {},
  24. "adblock_last_update": "Never",
  25. "adblock_domains_count": 0
  26. }
  27. if os.path.exists(config.PANEL_SETTINGS):
  28. try:
  29. with open(config.PANEL_SETTINGS, "r") as f:
  30. data = json.load(f)
  31. for k, v in default.items():
  32. if k not in data: data[k] = v
  33. # Έλεγχος αν υπάρχει api_key (backward compatibility)
  34. if "api_key" not in json.loads(read_file(config.PANEL_SETTINGS) or "{}"):
  35. write_file(config.PANEL_SETTINGS, json.dumps(data, indent=4))
  36. return data
  37. except: pass
  38. write_file(config.PANEL_SETTINGS, json.dumps(default, indent=4))
  39. return default
  40. def save_panel_settings(data):
  41. write_file(config.PANEL_SETTINGS, json.dumps(data, indent=4))
  42. def load_adblock_lists():
  43. if os.path.exists(config.ADBLOCK_LISTS_FILE):
  44. try:
  45. with open(config.ADBLOCK_LISTS_FILE, "r") as f:
  46. data = json.load(f)
  47. existing_ids = [l["id"] for l in data]
  48. for d_list in config.DEFAULT_ADBLOCK_LISTS:
  49. if d_list["id"] not in existing_ids:
  50. data.append(d_list)
  51. return data
  52. except: pass
  53. write_file(config.ADBLOCK_LISTS_FILE, json.dumps(config.DEFAULT_ADBLOCK_LISTS, indent=4))
  54. return config.DEFAULT_ADBLOCK_LISTS
  55. def save_adblock_lists(data):
  56. write_file(config.ADBLOCK_LISTS_FILE, json.dumps(data, indent=4))
  57. def load_custom_rules():
  58. if os.path.exists(config.ADBLOCK_CUSTOM_RULES_FILE):
  59. try:
  60. with open(config.ADBLOCK_CUSTOM_RULES_FILE, "r") as f: return json.load(f)
  61. except: pass
  62. return {"allowed": [], "deny": []}
  63. def save_custom_rules(data):
  64. write_file(config.ADBLOCK_CUSTOM_RULES_FILE, json.dumps(data, indent=4))
  65. def load_users():
  66. default_admin = {"admin": {"password": generate_password_hash("admin123"), "role": "admin", "assigned_templates": [], "view_mode": "cards"}}
  67. if not os.path.exists(config.USERS_FILE):
  68. save_users(default_admin)
  69. return default_admin
  70. try:
  71. with open(config.USERS_FILE, "r") as f:
  72. users = json.load(f)
  73. if not users: return default_admin
  74. for u in users:
  75. if "view_mode" not in users[u]: users[u]["view_mode"] = "cards"
  76. if "assigned_templates" not in users[u]: users[u]["assigned_templates"] = []
  77. return users
  78. except: return default_admin
  79. def save_users(data):
  80. write_file(config.USERS_FILE, json.dumps(data, indent=4))
  81. def log_audit(user, domain, action):
  82. try:
  83. with open(config.AUDIT_FILE, "r") as f: log = json.load(f)
  84. except: log = []
  85. log.insert(0, {"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "user": user, "domain": domain, "action": action})
  86. # Κρατάμε μόνο τις τελευταίες 500 εγγραφές για να μην φουσκώσει το αρχείο
  87. with open(config.AUDIT_FILE, "w") as f: json.dump(log[:500], f)
  88. def get_audit_log(domain):
  89. try:
  90. with open(config.AUDIT_FILE, "r") as f: log = json.load(f)
  91. return [l for l in log if l['domain'] == domain]
  92. except: return []