Spaces:
Paused
Paused
NtGdi commited on
Commit ·
c9a9c69
1
Parent(s): 71280f6
feat: add anonymous token support
Browse files- README.md +14 -0
- internal/anonymous.go +31 -0
- internal/chat.go +11 -0
README.md
CHANGED
|
@@ -10,6 +10,7 @@ zai-proxy 是一个基于 Go 语言的代理服务,将 z.ai 网页聊天转换
|
|
| 10 |
- 支持思考模式 (thinking)
|
| 11 |
- 支持联网搜索模式 (search)
|
| 12 |
- 支持多模态图片输入
|
|
|
|
| 13 |
- **自动生成签名**
|
| 14 |
- **自动更新签名版本号**
|
| 15 |
|
|
@@ -50,6 +51,19 @@ docker run -d -p 8080:8000 -e LOG_LEVEL=debug ghcr.io/kao0312/zai-proxy:latest
|
|
| 50 |
|
| 51 |
## 获取 z.ai Token
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
1. 登录 https://chat.z.ai
|
| 54 |
2. 打开浏览器开发者工具 (F12)
|
| 55 |
3. 切换到 Application/Storage 标签
|
|
|
|
| 10 |
- 支持思考模式 (thinking)
|
| 11 |
- 支持联网搜索模式 (search)
|
| 12 |
- 支持多模态图片输入
|
| 13 |
+
- 支持匿名 Token(免登录)
|
| 14 |
- **自动生成签名**
|
| 15 |
- **自动更新签名版本号**
|
| 16 |
|
|
|
|
| 51 |
|
| 52 |
## 获取 z.ai Token
|
| 53 |
|
| 54 |
+
### 方式一:使用匿名 Token(免登录)
|
| 55 |
+
|
| 56 |
+
直接使用 `free` 作为 API key,服务会自动获取一个匿名 token:
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
curl http://localhost:8000/v1/chat/completions \
|
| 60 |
+
-H "Authorization: Bearer free" \
|
| 61 |
+
-H "Content-Type: application/json" \
|
| 62 |
+
-d '{"model": "GLM-4.6", "messages": [{"role": "user", "content": "hello"}]}'
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### 方式二:使用个人 Token
|
| 66 |
+
|
| 67 |
1. 登录 https://chat.z.ai
|
| 68 |
2. 打开浏览器开发者工具 (F12)
|
| 69 |
3. 切换到 Application/Storage 标签
|
internal/anonymous.go
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package internal
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"encoding/json"
|
| 5 |
+
"fmt"
|
| 6 |
+
"net/http"
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
type AnonymousAuthResponse struct {
|
| 10 |
+
Token string `json:"token"`
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
// GetAnonymousToken 从 z.ai 获取匿名 token
|
| 14 |
+
func GetAnonymousToken() (string, error) {
|
| 15 |
+
resp, err := http.Get("https://chat.z.ai/api/v1/auths/")
|
| 16 |
+
if err != nil {
|
| 17 |
+
return "", err
|
| 18 |
+
}
|
| 19 |
+
defer resp.Body.Close()
|
| 20 |
+
|
| 21 |
+
if resp.StatusCode != http.StatusOK {
|
| 22 |
+
return "", fmt.Errorf("status %d", resp.StatusCode)
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
var authResp AnonymousAuthResponse
|
| 26 |
+
if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
|
| 27 |
+
return "", err
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
return authResp.Token, nil
|
| 31 |
+
}
|
internal/chat.go
CHANGED
|
@@ -233,6 +233,17 @@ func HandleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
|
| 233 |
return
|
| 234 |
}
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
var req ChatRequest
|
| 237 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 238 |
http.Error(w, "Invalid request", http.StatusBadRequest)
|
|
|
|
| 233 |
return
|
| 234 |
}
|
| 235 |
|
| 236 |
+
// 如果 token 是 "free",获取匿名 token
|
| 237 |
+
if token == "free" {
|
| 238 |
+
anonymousToken, err := GetAnonymousToken()
|
| 239 |
+
if err != nil {
|
| 240 |
+
LogError("Failed to get anonymous token: %v", err)
|
| 241 |
+
http.Error(w, "Failed to get anonymous token", http.StatusInternalServerError)
|
| 242 |
+
return
|
| 243 |
+
}
|
| 244 |
+
token = anonymousToken
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
var req ChatRequest
|
| 248 |
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
| 249 |
http.Error(w, "Invalid request", http.StatusBadRequest)
|