singbox_each_node.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import json
  4. import shutil
  5. import yaml # 保留导入 yaml 模块
  6. # 新文件夹名称
  7. new_folder_name = "config"
  8. downloads_path = os.path.join(os.path.expanduser('~'), 'Downloads')
  9. # 新文件夹的完整路径
  10. new_folder_path = os.path.join(downloads_path, new_folder_name)
  11. # 如果文件夹存在,删除整个文件夹
  12. if os.path.exists(new_folder_path):
  13. shutil.rmtree(new_folder_path)
  14. # 创建新的文件夹
  15. os.makedirs(new_folder_path)
  16. config_path = os.path.join(os.getcwd(), "merge.yaml")
  17. with open(config_path, 'r', encoding='utf-8') as file:
  18. data = yaml.safe_load(file) # 使用 yaml 模块加载 YAML 文件
  19. if not data:
  20. print(f"Error reading {config_path}")
  21. exit(1)
  22. config_name_serial_start = 1
  23. port_start = 56000
  24. for proxy in data['proxies']:
  25. if '剩余流量' in proxy['name']:
  26. continue
  27. if '套餐到期' in proxy['name']:
  28. continue
  29. password = 'password'
  30. if proxy['type'] == 'vmess':
  31. password = 'uuid'
  32. # 构建 SingBox 配置模板
  33. config_template = {
  34. "log": {
  35. "level": "info"
  36. },
  37. "inbounds": [
  38. {
  39. "type": "mixed",
  40. "listen": "::",
  41. "listen_port": port_start
  42. }
  43. ],
  44. "outbounds": [
  45. {
  46. "type": proxy['type'],
  47. "tag": f'proxy_{config_name_serial_start}',
  48. "server": proxy['server'],
  49. "server_port": proxy['port'],
  50. f'{password}': proxy.setdefault('uuid', proxy.setdefault('password', '')),
  51. "tls": {
  52. "enabled": False
  53. }
  54. }
  55. ],
  56. "route": {
  57. "rules": [
  58. {
  59. "type": "default",
  60. "outbound": f'proxy_{config_name_serial_start}'
  61. }
  62. ]
  63. }
  64. }
  65. config_name = f"config{str(config_name_serial_start)}.json"
  66. save_path = os.path.join(new_folder_path, config_name)
  67. with open(save_path, 'w', encoding='utf-8') as file:
  68. json.dump(config_template, file, ensure_ascii=False, indent=4)
  69. config_name_serial_start += 1
  70. port_start += 1
  71. print("OK")