config_split.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # -*- coding: utf-8 -*-
  2. import yaml
  3. import os
  4. def xfltd(serial):
  5. # 获取当前路径
  6. current_path = os.getcwd()
  7. yaml_path = os.path.join(current_path, 'XFLTD.yaml')
  8. if not os.path.exists(f'{yaml_path}'):
  9. return
  10. with open(f'{yaml_path}', 'r', encoding='utf-8') as file:
  11. config = yaml.safe_load(file)
  12. split_config_path = os.path.join(current_path, 'split_config')
  13. if not os.path.exists(split_config_path):
  14. os.makedirs(split_config_path)
  15. # 为每个代理创建一个新的配置文件
  16. for proxy in config['proxies']:
  17. if '剩余流量' in proxy['name'] or '套餐到期' in proxy['name']:
  18. continue
  19. new_config = {
  20. "mixed-port": config["mixed-port"],
  21. "allow-lan": config["allow-lan"],
  22. "bind-address": config["bind-address"],
  23. "mode": config["mode"],
  24. "log-level": config["log-level"],
  25. "external-controller": config["external-controller"],
  26. "dns": config["dns"],
  27. "proxies": [proxy], # 只包含当前代理
  28. "proxy-groups": [{'name': 'XFLTD', 'type': 'select', 'proxies': [proxy['name']]}],
  29. "rules": config["rules"]
  30. }
  31. config_name = 'config_' + str(serial).zfill(4) + '.yaml'
  32. save_file_path = os.path.join(split_config_path, config_name)
  33. # 将新的配置写入文件
  34. with open(save_file_path, 'w', encoding='utf-8') as outfile:
  35. yaml.dump(new_config, outfile, allow_unicode=True, indent=4)
  36. print(f'Generated {config_name}')
  37. serial += 1
  38. return serial
  39. def bitz(serial):
  40. # 获取当前路径
  41. current_path = os.getcwd()
  42. yaml_path = os.path.join(current_path, 'Bitz Net.yaml')
  43. if not os.path.exists(f'{yaml_path}'):
  44. return
  45. with open(f'{yaml_path}', 'r', encoding='utf-8') as file:
  46. config = yaml.safe_load(file)
  47. # 获取当前路径
  48. current_path = os.getcwd()
  49. split_config_path = os.path.join(current_path, 'split_config')
  50. if not os.path.exists(split_config_path):
  51. os.makedirs(split_config_path)
  52. # 为每个代理创建一个新的配置文件
  53. for proxy in config['proxies']:
  54. new_config = {
  55. "allow-lan": config["allow-lan"],
  56. "bind-address": config["bind-address"],
  57. "dns": config["dns"],
  58. "external-controller": config["external-controller"],
  59. "log-level": config["log-level"],
  60. "mixed-port": config["mixed-port"],
  61. "mode": config["mode"],
  62. "proxies": [proxy], # 只包含当前代理
  63. "proxy-groups": [
  64. {
  65. "name": "Bitz Net",
  66. "proxies": [proxy["name"]],
  67. "type": "select"
  68. },
  69. {
  70. "interval": 86400,
  71. "name": "自动选择",
  72. "proxies": [proxy["name"]],
  73. "type": "url-test",
  74. "url": "http://www.gstatic.com/generate_204"
  75. },
  76. {
  77. "interval": 7200,
  78. "name": "故障转移",
  79. "proxies": [proxy["name"]],
  80. "type": "fallback",
  81. "url": "http://www.gstatic.com/generate_204"
  82. }
  83. ],
  84. "rules": config["rules"]
  85. }
  86. config_name = 'config_' + str(serial).zfill(4) + '.yaml'
  87. save_file_path = os.path.join(split_config_path, config_name)
  88. # 将新的配置写入文件
  89. with open(save_file_path, 'w', encoding='utf-8') as outfile:
  90. yaml.dump(new_config, outfile, allow_unicode=True, indent=4)
  91. print(f'Generated {config_name}')
  92. serial += 1
  93. return serial
  94. def patriot(serial):
  95. current_path = os.getcwd()
  96. yaml_path = os.path.join(current_path, 'index.php.yaml')
  97. if not os.path.exists(f'{yaml_path}'):
  98. return
  99. # 读取原始的 YAML 配置文件
  100. with open(f'{yaml_path}', 'r', encoding='utf-8') as file:
  101. config = yaml.safe_load(file)
  102. # 获取当前路径
  103. current_path = os.getcwd()
  104. split_config_path = os.path.join(current_path, 'split_config')
  105. if not os.path.exists(split_config_path):
  106. os.makedirs(split_config_path)
  107. # 为每个代理创建一个新的配置文件
  108. for proxy in config['proxies']:
  109. new_config = {
  110. "port": config["port"],
  111. "socks-port": config["socks-port"],
  112. "allow-lan": config["allow-lan"],
  113. "mode": config["mode"],
  114. "log-level": config["log-level"],
  115. "external-controller": config["external-controller"],
  116. "secret": config["secret"],
  117. "proxies": [proxy],
  118. "proxy-groups": [
  119. {"name": "auto", "type": "url-test", "proxies": [proxy["name"]], "url": "https://www.bing.com",
  120. "interval": 300},
  121. {"name": "fallback-auto", "type": "fallback", "proxies": [proxy["name"]], "url": "https://www.bing.com",
  122. "interval": 300},
  123. {"name": "select", "type": "select", "proxies": [proxy["name"]]}
  124. ],
  125. "rules": config["rules"]
  126. }
  127. config_name = 'config_' + str(serial).zfill(4) + '.yaml'
  128. save_file_path = os.path.join(split_config_path, config_name)
  129. # 将新的配置写入文件
  130. with open(save_file_path, 'w', encoding='utf-8') as outfile:
  131. yaml.dump(new_config, outfile, allow_unicode=True, indent=4)
  132. print(f'Generated {config_name}')
  133. serial += 1
  134. return serial
  135. if __name__ == '__main__':
  136. serial = 1
  137. serial = xfltd(serial)
  138. serial = patriot(serial)
  139. serial = bitz(serial)
  140. print(f'线路总数: {serial - 1}')