cluster_manager.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. from core import dns
  9. import urllib.request, json
  10. settings = load_panel_settings()
  11. nodes = settings.get('cluster_nodes', [])
  12. masters = dns.get_zone_masters_list(domain)
  13. if not masters:
  14. master_ip = settings.get('server_public_ip', '').strip()
  15. if master_ip: masters = [master_ip]
  16. for node in nodes:
  17. try:
  18. url = f"{node['url'].rstrip('/')}/api/v1/cluster/zone"
  19. headers = {"X-API-Key": node['api_key'], "Content-Type": "application/json"}
  20. payload = {"domain": domain, "masters": masters}
  21. # BUG FIX: Ασφαλής Opener για να μην κόβεται από Proxies / SSL
  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. def broadcast_adblock_to_cluster(action):
  38. def task():
  39. s = load_panel_settings()
  40. lists = load_adblock_lists()
  41. rules = load_custom_rules()
  42. count = s.get('adblock_domains_count', 0)
  43. update_time = s.get('adblock_last_update', 'Never')
  44. for node in s.get('cluster_nodes', []):
  45. try:
  46. url = f"{node['url'].rstrip('/')}/api/v1/cluster/adblock"
  47. headers = {"X-API-Key": node['api_key'], "Content-Type": "application/json"}
  48. payload = {
  49. "action": action,
  50. "lists": lists,
  51. "rules": rules,
  52. "count": count,
  53. "time": update_time
  54. }
  55. req = urllib.request.Request(url, data=json.dumps(payload).encode(), headers=headers, method='POST')
  56. urllib.request.urlopen(req, timeout=10, context=config.ssl_ctx)
  57. print(f"Cluster AdBlock Sync OK: {node['name']} ({action})")
  58. except Exception as e:
  59. print(f"Cluster AdBlock Sync FAIL: {node['name']} -> {e}")
  60. threading.Thread(target=task, daemon=True).start()