Spaces:
Running
Running
添加了远程 Mixin 的功能
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import os
|
2 |
import re
|
|
|
3 |
import yaml
|
4 |
import httpx
|
5 |
import base64
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
from fastapi import FastAPI, Response, HTTPException, Request
|
8 |
|
@@ -23,8 +25,63 @@ app = FastAPI()
|
|
23 |
|
24 |
# 从环境变量中获取密钥
|
25 |
load_dotenv()
|
26 |
-
API_KEY
|
27 |
-
SUBSCRIBE_URLS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def subscribe_mixin(content: str) -> str:
|
30 |
"""输入 YAML 字符串,输出转换后的 YAML 字符串
|
@@ -76,6 +133,13 @@ def subscribe_mixin(content: str) -> str:
|
|
76 |
d["proxy-groups"] = [new_proxy_group]
|
77 |
|
78 |
d.pop('socks-port', None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
modified_yaml = yaml.dump(d, allow_unicode=True, indent=2)
|
80 |
|
81 |
return modified_yaml
|
|
|
1 |
import os
|
2 |
import re
|
3 |
+
import ast
|
4 |
import yaml
|
5 |
import httpx
|
6 |
import base64
|
7 |
+
import inspect
|
8 |
from dotenv import load_dotenv
|
9 |
from fastapi import FastAPI, Response, HTTPException, Request
|
10 |
|
|
|
25 |
|
26 |
# 从环境变量中获取密钥
|
27 |
load_dotenv()
|
28 |
+
API_KEY = os.environ.get('API_KEY')
|
29 |
+
SUBSCRIBE_URLS = os.environ.get('SUBSCRIBE_URLS') # 存储真实URL的变量
|
30 |
+
INJECT_SCRIPT = os.environ.get('INJECT_SCRIPT')
|
31 |
+
|
32 |
+
def validate_function_signature(code_string):
|
33 |
+
""" 使用 ast 验证函数签名是否符合要求。
|
34 |
+
"""
|
35 |
+
try:
|
36 |
+
module = ast.parse(code_string)
|
37 |
+
if not module.body:
|
38 |
+
print("错误:代码为空。")
|
39 |
+
return False
|
40 |
+
if len(module.body) != 1 or not isinstance(module.body[0], ast.FunctionDef):
|
41 |
+
print("错误:代码必须包含单个函数定义。")
|
42 |
+
return False
|
43 |
+
function_def = module.body[0]
|
44 |
+
# Step 01. 检查函数名
|
45 |
+
if function_def.name != "handle_mixin":
|
46 |
+
print(f"错误:函数名必须为 'handle_mixin',而不是 '{function_def.name}'。")
|
47 |
+
return False
|
48 |
+
# Step 02. 检查参数个数
|
49 |
+
args = function_def.args
|
50 |
+
if args.defaults or args.kw_defaults or args.vararg or args.kwarg or args.posonlyargs:
|
51 |
+
print("错误:函数不能有默认参数、可变位置参数、可变关键字参数、或者仅位置参数")
|
52 |
+
return False
|
53 |
+
if len(args.args) != 1:
|
54 |
+
print(f"错误:函数必须恰好接受一个参数,而不是 {len(args.args)} 个。")
|
55 |
+
return False
|
56 |
+
# Step 03. 检查参数名称
|
57 |
+
if args.args[0].arg != "content":
|
58 |
+
print(f"错误:参数名称必须为 'content',而不是 '{args.args[0].arg}'。")
|
59 |
+
return False
|
60 |
+
return True
|
61 |
+
except SyntaxError as e:
|
62 |
+
print(f"语法错误:{e}")
|
63 |
+
return False
|
64 |
+
except Exception as e:
|
65 |
+
print(f"发生错误:{e}")
|
66 |
+
return False
|
67 |
+
|
68 |
+
def load_mixin_function(code_string) -> callable:
|
69 |
+
if not validate_function_signature(code_string):
|
70 |
+
return
|
71 |
+
try:
|
72 |
+
code_obj = compile(code_string, '<string>', 'exec')
|
73 |
+
local_namespace = {}
|
74 |
+
exec(code_obj, local_namespace)
|
75 |
+
handle_mixin = local_namespace['handle_mixin']
|
76 |
+
return handle_mixin
|
77 |
+
except Exception as e:
|
78 |
+
print(f"解析函数时出错:{e}")
|
79 |
+
|
80 |
+
try:
|
81 |
+
handle_mixin = load_mixin_function(INJECT_SCRIPT)
|
82 |
+
except Exception as e:
|
83 |
+
handle_mixin = lambda x:x
|
84 |
+
print(f"无法加载 MINIX 函数, 错误信息: {e}")
|
85 |
|
86 |
def subscribe_mixin(content: str) -> str:
|
87 |
"""输入 YAML 字符串,输出转换后的 YAML 字符串
|
|
|
133 |
d["proxy-groups"] = [new_proxy_group]
|
134 |
|
135 |
d.pop('socks-port', None)
|
136 |
+
|
137 |
+
# 在此处尝试调用 mixin 函数
|
138 |
+
try:
|
139 |
+
d = handle_mixin(d)
|
140 |
+
except Exception as e:
|
141 |
+
print(f"执行 Minix 函数时出错!")
|
142 |
+
|
143 |
modified_yaml = yaml.dump(d, allow_unicode=True, indent=2)
|
144 |
|
145 |
return modified_yaml
|