GenerateClashConfigSingleNode.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import yaml
  4. import shutil
  5. # 新文件夹名称
  6. new_folder_name = "config"
  7. downloads_path = os.path.join(os.path.expanduser('~'), 'Downloads')
  8. # 新文件夹的完整路径
  9. new_folder_path = os.path.join(downloads_path, new_folder_name)
  10. # 如果文件夹存在,删除整个文件夹
  11. if os.path.exists(new_folder_path):
  12. shutil.rmtree(new_folder_path)
  13. # 创建新的文件夹
  14. os.makedirs(new_folder_path)
  15. config_path = os.path.join(os.getcwd(), "merge.yaml")
  16. with open(config_path, 'r', encoding='utf-8') as file:
  17. data = yaml.safe_load(file)
  18. if not data:
  19. print(f"Error reading {config_path}")
  20. exit(1)
  21. config_name_serial_start = 1
  22. port_start = 7890
  23. for proxy in data['proxies']:
  24. if '剩余流量' in proxy['name']:
  25. continue
  26. if '套餐到期' in proxy['name']:
  27. continue
  28. config_template = {
  29. "mixed-port": port_start,
  30. "allow-lan": True,
  31. "bind-address": "*",
  32. "mode": "rule",
  33. "log-level": "info",
  34. "external-controller": "0.0.0.0:9090",
  35. "secret": "",
  36. "dns": {
  37. "enable": False,
  38. "ipv6": False,
  39. "nameserver": [],
  40. "fallback": []
  41. },
  42. "proxies": [proxy],
  43. "proxy-groups": [{'name': 'single node', 'type': 'select', 'proxies': [proxy['name']]}],
  44. }
  45. config_name = f"config{str(config_name_serial_start)}.yaml"
  46. save_path = os.path.join(new_folder_path, config_name)
  47. with open(save_path, 'w', encoding='utf-8') as file:
  48. yaml.dump(config_template, file, allow_unicode=True, default_flow_style=False)
  49. config_name_serial_start += 1
  50. # port_start += 1
  51. print(config_name_serial_start-1)
  52. print('OK')