Spaces:
Paused
Paused
Upload 17 files
Browse files- app/sub_manager.py +104 -5
app/sub_manager.py
CHANGED
|
@@ -303,11 +303,110 @@ secret: ""
|
|
| 303 |
config_content = "secret: ''\n" + config_content
|
| 304 |
has_patch = True
|
| 305 |
|
| 306 |
-
#
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
# 如果我们修改了配置,保存回文件
|
| 313 |
if has_patch:
|
|
|
|
| 303 |
config_content = "secret: ''\n" + config_content
|
| 304 |
has_patch = True
|
| 305 |
|
| 306 |
+
# 尝试解析YAML并修复代理组引用问题
|
| 307 |
+
try:
|
| 308 |
+
import yaml
|
| 309 |
+
|
| 310 |
+
# 解析配置
|
| 311 |
+
config_yaml = yaml.safe_load(config_content)
|
| 312 |
+
|
| 313 |
+
# 检查是否有代理和代理组
|
| 314 |
+
if config_yaml and isinstance(config_yaml, dict):
|
| 315 |
+
if "proxies" in config_yaml and "proxy-groups" in config_yaml:
|
| 316 |
+
# 获取所有代理节点名称
|
| 317 |
+
proxy_names = set()
|
| 318 |
+
for proxy in config_yaml.get("proxies", []):
|
| 319 |
+
if isinstance(proxy, dict) and "name" in proxy:
|
| 320 |
+
proxy_names.add(proxy["name"])
|
| 321 |
+
|
| 322 |
+
# 添加内置代理
|
| 323 |
+
proxy_names.add("DIRECT")
|
| 324 |
+
proxy_names.add("REJECT")
|
| 325 |
+
|
| 326 |
+
# 修复代理组引用
|
| 327 |
+
groups_fixed = False
|
| 328 |
+
for group in config_yaml.get("proxy-groups", []):
|
| 329 |
+
if isinstance(group, dict) and "proxies" in group:
|
| 330 |
+
# 删除组中引用的不存在的代理
|
| 331 |
+
valid_proxies = []
|
| 332 |
+
for proxy in group["proxies"]:
|
| 333 |
+
if proxy in proxy_names or proxy == group.get("name", ""):
|
| 334 |
+
valid_proxies.append(proxy)
|
| 335 |
+
else:
|
| 336 |
+
logger.warning(f"删除代理组 {group.get('name', '未知')} 中的无效引用: {proxy}")
|
| 337 |
+
groups_fixed = True
|
| 338 |
+
|
| 339 |
+
# 确保代理组至少有一个有效代理
|
| 340 |
+
if not valid_proxies:
|
| 341 |
+
valid_proxies.append("DIRECT")
|
| 342 |
+
logger.warning(f"为空代理组 {group.get('name', '未知')} 添加默认代理: DIRECT")
|
| 343 |
+
groups_fixed = True
|
| 344 |
+
|
| 345 |
+
group["proxies"] = valid_proxies
|
| 346 |
+
|
| 347 |
+
# 检查是否有GLOBAL组,如果没有,添加一个
|
| 348 |
+
has_global = False
|
| 349 |
+
for group in config_yaml.get("proxy-groups", []):
|
| 350 |
+
if isinstance(group, dict) and group.get("name") == "GLOBAL":
|
| 351 |
+
has_global = True
|
| 352 |
+
break
|
| 353 |
+
|
| 354 |
+
if not has_global:
|
| 355 |
+
# 添加GLOBAL策略组
|
| 356 |
+
logger.info("添加GLOBAL策略组")
|
| 357 |
+
if "proxy-groups" not in config_yaml:
|
| 358 |
+
config_yaml["proxy-groups"] = []
|
| 359 |
+
|
| 360 |
+
# 确定要放入GLOBAL组的代理
|
| 361 |
+
global_proxies = ["DIRECT"]
|
| 362 |
+
# 添加所有代理节点到GLOBAL组
|
| 363 |
+
for proxy in config_yaml.get("proxies", []):
|
| 364 |
+
if isinstance(proxy, dict) and "name" in proxy:
|
| 365 |
+
proxy_name = proxy["name"]
|
| 366 |
+
if proxy_name != "DIRECT" and proxy_name != "REJECT":
|
| 367 |
+
global_proxies.append(proxy_name)
|
| 368 |
+
|
| 369 |
+
config_yaml["proxy-groups"].insert(0, {
|
| 370 |
+
"name": "GLOBAL",
|
| 371 |
+
"type": "select",
|
| 372 |
+
"proxies": global_proxies
|
| 373 |
+
})
|
| 374 |
+
groups_fixed = True
|
| 375 |
+
has_patch = True
|
| 376 |
+
|
| 377 |
+
# 如果修复了代理组,重新生成配置
|
| 378 |
+
if groups_fixed:
|
| 379 |
+
config_content = yaml.dump(config_yaml, sort_keys=False, allow_unicode=True)
|
| 380 |
+
has_patch = True
|
| 381 |
+
else:
|
| 382 |
+
# 配置缺少代理或代理组,添加基本结构
|
| 383 |
+
if "proxies" not in config_yaml:
|
| 384 |
+
config_yaml["proxies"] = [{"name": "DIRECT", "type": "direct"}]
|
| 385 |
+
has_patch = True
|
| 386 |
+
|
| 387 |
+
if "proxy-groups" not in config_yaml:
|
| 388 |
+
config_yaml["proxy-groups"] = [{
|
| 389 |
+
"name": "GLOBAL",
|
| 390 |
+
"type": "select",
|
| 391 |
+
"proxies": ["DIRECT"]
|
| 392 |
+
}]
|
| 393 |
+
has_patch = True
|
| 394 |
+
|
| 395 |
+
# 更新配置内容
|
| 396 |
+
config_content = yaml.dump(config_yaml, sort_keys=False, allow_unicode=True)
|
| 397 |
+
except Exception as yaml_err:
|
| 398 |
+
logger.error(f"解析YAML时出错: {str(yaml_err)}")
|
| 399 |
+
# 无法解析YAML,添加基本的GLOBAL策略组
|
| 400 |
+
if "GLOBAL" not in config_content:
|
| 401 |
+
config_content += """
|
| 402 |
+
proxy-groups:
|
| 403 |
+
- name: GLOBAL
|
| 404 |
+
type: select
|
| 405 |
+
proxies:
|
| 406 |
+
- DIRECT
|
| 407 |
+
"""
|
| 408 |
+
has_patch = True
|
| 409 |
+
logger.info("添加了基本的GLOBAL策略组")
|
| 410 |
|
| 411 |
# 如果我们修改了配置,保存回文件
|
| 412 |
if has_patch:
|