cluster_manager.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import json
  2. import threading
  3. import urllib.request
  4. import config
  5. from core.file_manager import load_panel_settings, load_adblock_lists, load_custom_rules
  6. def broadcast_to_cluster(action, domain):
  7. def task():
  8. # FIX: Το σωστό όνομα του αρχείου είναι dns_manager!
  9. from core import dns_manager as dns
  10. import urllib.request, json
  11. settings = load_panel_settings()
  12. nodes = settings.get('cluster_nodes', [])
  13. masters = dns.get_zone_masters_list(domain)
  14. if not masters:
  15. master_ip = settings.get('server_public_ip', '').strip()
  16. if master_ip: masters = [master_ip]
  17. for node in nodes:
  18. try:
  19. url = f"{node['url'].rstrip('/')}/api/v1/cluster/zone"
  20. headers = {"X-API-Key": node['api_key'], "Content-Type": "application/json"}
  21. payload = {"domain": domain, "masters": masters}
  22. proxy_handler = urllib.request.ProxyHandler({})
  23. https_handler = urllib.request.HTTPSHandler(context=config.ssl_ctx)
  24. opener = urllib.request.build_opener(proxy_handler, https_handler)
  25. if action == 'add_slave':
  26. req = urllib.request.Request(url, data=json.dumps(payload).encode('utf-8'), headers=headers, method='POST')
  27. elif action == 'delete_zone':
  28. req = urllib.request.Request(f"{url}/{domain}", headers=headers, method='DELETE')
  29. opener.open(req, timeout=10)
  30. print(f"Cluster Sync OK: {node['name']} ({action} {domain})")
  31. except Exception as e:
  32. print(f"Cluster Sync FAIL: {node['name']} -> {e}")
  33. import threading
  34. threading.Thread(target=task, daemon=True).start()
  35. import threading
  36. threading.Thread(target=task, daemon=True).start()
  37. import threading
  38. threading.Thread(target=task, daemon=True).start()
  39. def broadcast_adblock_to_cluster(action):
  40. def task():
  41. s = load_panel_settings()
  42. lists = load_adblock_lists()
  43. rules = load_custom_rules()
  44. count = s.get('adblock_domains_count', 0)
  45. update_time = s.get('adblock_last_update', 'Never')
  46. for node in s.get('cluster_nodes', []):
  47. try:
  48. url = f"{node['url'].rstrip('/')}/api/v1/cluster/adblock"
  49. headers = {"X-API-Key": node['api_key'], "Content-Type": "application/json"}
  50. payload = {
  51. "action": action,
  52. "lists": lists,
  53. "rules": rules,
  54. "count": count,
  55. "time": update_time
  56. }
  57. req = urllib.request.Request(url, data=json.dumps(payload).encode(), headers=headers, method='POST')
  58. urllib.request.urlopen(req, timeout=10, context=config.ssl_ctx)
  59. print(f"Cluster AdBlock Sync OK: {node['name']} ({action})")
  60. except Exception as e:
  61. print(f"Cluster AdBlock Sync FAIL: {node['name']} -> {e}")
  62. threading.Thread(target=task, daemon=True).start()