增加伪装header和补充readme

This commit is contained in:
Vinlic 2024-03-08 22:04:04 +08:00
parent 9a50c30ef7
commit c1c5ebea4b
2 changed files with 68 additions and 5 deletions

47
README.md Normal file
View File

@ -0,0 +1,47 @@
# KIMI Free 服务
## 安装
请先安装好Node.js环境并且配置好环境变量确认node命令可用。
安装依赖
```shell
npm i
```
安装PM2进行进程守护
```shell
npm i -g pm2
```
编译构建看到dist目录就是构建完成
```shell
npm run build
```
启动服务
```shell
pm2 start dist/index.js --name "kimi-free-api"
```
查看服务实时日志
```shell
pm2 logs kimi-free-api
```
重启服务
```shell
pm2 reload kimi-free-api
```
停止服务
```shell
pm2 stop kimi-free-api
```

View File

@ -9,12 +9,20 @@ import logger from '@/lib/logger.ts';
import util from '@/lib/util.ts'; import util from '@/lib/util.ts';
const ACCESS_TOKEN_EXPIRES = 300; const ACCESS_TOKEN_EXPIRES = 300;
const FAKE_HEADERS = {
'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
};
const accessTokenMap = new Map(); const accessTokenMap = new Map();
async function requestToken(refreshToken: string) { async function requestToken(refreshToken: string) {
const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', { const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', {
headers: { headers: {
Authorization: `Bearer ${refreshToken}` Authorization: `Bearer ${refreshToken}`,
Referer: 'https://kimi.moonshot.cn',
...FAKE_HEADERS
}, },
validateStatus: () => true validateStatus: () => true
}); });
@ -47,7 +55,9 @@ async function createConversation(name: string, refreshToken: string) {
is_example: false is_example: false
}, { }, {
headers: { headers: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`,
Referer: 'https://kimi.moonshot.cn',
...FAKE_HEADERS
}, },
validateStatus: () => true validateStatus: () => true
}); });
@ -61,7 +71,9 @@ async function removeConversation(convId: string, refreshToken: string) {
const token = await acquireToken(refreshToken); const token = await acquireToken(refreshToken);
const result = await axios.delete(`https://kimi.moonshot.cn/api/chat/${convId}`, { const result = await axios.delete(`https://kimi.moonshot.cn/api/chat/${convId}`, {
headers: { headers: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
...FAKE_HEADERS
}, },
validateStatus: () => true validateStatus: () => true
}); });
@ -77,7 +89,9 @@ async function createCompletion(messages: any[], refreshToken: string, useSearch
use_search: useSearch use_search: useSearch
}, { }, {
headers: { headers: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
...FAKE_HEADERS
}, },
validateStatus: () => true, validateStatus: () => true,
responseType: 'stream' responseType: 'stream'
@ -97,7 +111,9 @@ async function createCompletionStream(messages: any[], refreshToken: string, use
use_search: useSearch use_search: useSearch
}, { }, {
headers: { headers: {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`,
Referer: `https://kimi.moonshot.cn/chat/${convId}`,
...FAKE_HEADERS
}, },
validateStatus: () => true, validateStatus: () => true,
responseType: 'stream' responseType: 'stream'