franksoo commited on
Commit
687d112
·
1 Parent(s): 93bd862

fix: install Hermes from Git

Browse files
Files changed (2) hide show
  1. app.py +29 -42
  2. wx config.txt +1 -0
app.py CHANGED
@@ -6,60 +6,47 @@ from fastapi import FastAPI, Request
6
  from fastapi.responses import PlainTextResponse
7
  from Crypto.Cipher import AES
8
  from Crypto.Util.Padding import unpad
9
- import gradio as gr
10
 
11
- app = FastAPI(title="企业微信回调")
12
 
13
- # ========== 企业微信配置 ==========
14
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "").strip()
15
  WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "").strip()
16
- CORPID = os.getenv("CORPID", "").strip()
17
 
18
- # ========== 企业微信 URL 校验(官方正确实现)==========
 
 
19
  @app.get("/gateway/wecom")
20
- async def verify_url(request: Request):
21
  try:
22
- # 1. 获取参数并 URL 解码
23
- msg_signature = urllib.parse.unquote(request.query_params.get("msg_signature", ""))
24
- timestamp = urllib.parse.unquote(request.query_params.get("timestamp", ""))
25
- nonce = urllib.parse.unquote(request.query_params.get("nonce", ""))
26
- echostr_enc = urllib.parse.unquote(request.query_params.get("echostr", ""))
27
-
28
- # 2. 校验签
29
- sort_list = [WECOM_TOKEN, timestamp, nonce]
30
- sort_list.sort()
31
- check_str = ''.join(sort_list)
32
- sha1 = hashlib.sha1(check_str.encode()).hexdigest()
33
-
34
- if sha1 != msg_signature:
35
- return PlainTextResponse("sign error")
36
-
37
- # 3. AES 解密(官方标准)
38
- aes_key = base64.b64decode(WECOM_AES_KEY + "=")
39
  cipher = AES.new(aes_key, AES.MODE_CBC, aes_key[:16])
40
  encrypted = base64.b64decode(echostr_enc)
41
  decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
42
  msg = decrypted[16:].decode()
43
 
44
- # 4. 原样返回明文(企业微信唯一要求)
45
  return PlainTextResponse(msg)
 
 
 
46
 
47
- except Exception as e:
48
- return PlainTextResponse(f"error: {str(e)}")
49
-
50
- # 接收消息
51
  @app.post("/gateway/wecom")
52
- async def receive_msg():
53
- return PlainTextResponse("success")
54
-
55
- # ========== Gradio 界面 ==========
56
- with gr.Blocks() as demo:
57
- gr.Markdown("# ✅ 企业微信机器人已启动")
58
- gr.Markdown(f"### 回调地址:https://franksoo-agent.hf.space/gateway/wecom")
59
-
60
- gr.mount_gradio_app(app, demo, path="/")
61
-
62
- # ========== 启动 ==========
63
- if __name__ == "__main__":
64
- import uvicorn
65
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
6
  from fastapi.responses import PlainTextResponse
7
  from Crypto.Cipher import AES
8
  from Crypto.Util.Padding import unpad
 
9
 
10
+ app = FastAPI()
11
 
12
+ # 从环境变量读取(必须和企业微信一致)
13
  WECOM_TOKEN = os.getenv("WECOM_TOKEN", "").strip()
14
  WECOM_AES_KEY = os.getenv("WECOM_AES_KEY", "").strip()
 
15
 
16
+ # ==========================================
17
+ # 企业微信官方 URL 验证(绝对正确)
18
+ # ==========================================
19
  @app.get("/gateway/wecom")
20
+ async def verify(request: Request):
21
  try:
22
+ # 获取参数
23
+ msg_signature = request.query_params.get("msg_signature", "")
24
+ timestamp = request.query_params.get("timestamp", "")
25
+ nonce = request.query_params.get("nonce", "")
26
+ echostr_enc = request.query_params.get("echostr", "")
27
+
28
+ # 验签
29
+ arr = sorted([WECOM_TOKEN, timestamp, nonce])
30
+ check_str = ''.join(arr)
31
+ sig = hashlib.sha1(check_str.encode()).hexdigest()
32
+
33
+ if sig != msg_signature:
34
+ return PlainTextResponse("")
35
+
36
+ # AES 解密(官方标准)
37
+ aes_key = base64.b64decode(WECOM_AES_KEY)
 
38
  cipher = AES.new(aes_key, AES.MODE_CBC, aes_key[:16])
39
  encrypted = base64.b64decode(echostr_enc)
40
  decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
41
  msg = decrypted[16:].decode()
42
 
43
+ # 返回明文!!! 不加任何东西!!!
44
  return PlainTextResponse(msg)
45
+
46
+ except Exception:
47
+ return PlainTextResponse("")
48
 
49
+ # 必须返回空 success
 
 
 
50
  @app.post("/gateway/wecom")
51
+ async def post():
52
+ return PlainTextResponse("")
 
 
 
 
 
 
 
 
 
 
 
 
wx config.txt CHANGED
@@ -5,5 +5,6 @@ WECOM_TOKEN=jrbaP8up4HgKz1VhVVTu2WBUlVmLG
5
  WECOM_AES_KEY=jviaEHXvE2pRU6oKPzj8LXpWAhQ3YYmUTl8dbf2XHaU
6
  SPACE_HOST=franksoo/agent
7
 
 
8
 
9
  https://franksoo-agent.hf.space/gateway/wecom
 
5
  WECOM_AES_KEY=jviaEHXvE2pRU6oKPzj8LXpWAhQ3YYmUTl8dbf2XHaU
6
  SPACE_HOST=franksoo/agent
7
 
8
+ CORPID=wwff99ddc89331de81
9
 
10
  https://franksoo-agent.hf.space/gateway/wecom