-
Notifications
You must be signed in to change notification settings - Fork 289
/
api_worker.js
146 lines (119 loc) · 4.41 KB
/
api_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
addEventListener('fetch', event => {
const url = new URL(event.request.url);
//const pathname =url.pathname;
event.respondWith(handleRequest(event.request,url.pathname));
})
// @ts-ignore
const KV = oai_global_variables;
function parseJwt(token) {
const base64Url = token.split('.')[1];// 获取载荷部分
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);// 返回载荷解析后的 JSON 对象
}
async function refreshAT(tochecktoken,an) {
const accessTokenKey = `at_${an}`;
const token = tochecktoken || await KV.get(accessTokenKey) ||'';
if (token && token !== "Bad_RT" && token !== "Old_AT")
{
const payload = parseJwt(token);
const currentTime = Math.floor(Date.now() / 1000);
if (payload.exp > currentTime ){
return token
}
}
const refreshTokenKey = `rt_${an}`;
const url = 'https://token.oaifree.com/api/auth/refresh';
const refreshToken = await KV.get(refreshTokenKey);
if (refreshToken) {
// 发送 POST 请求
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: `refresh_token=${refreshToken}`
});
// 检查响应状态
if (response.ok) {
const data = await response.json();
const newAccessToken = data.access_token;
await KV.put(accessTokenKey, newAccessToken);
return newAccessToken;
} else {
await KV.put(accessTokenKey, "Bad_RT");
return '';
}
}
else {
await KV.put(accessTokenKey, "Old_AT");
return '';
}
}
async function handleRequest(request,pathname) {
// 检查Authorization头是否包含正确的秘钥
const auth = request.headers.get('Authorization');
const adminKeys = await KV.get('Admin');
const adminKeyList = adminKeys.split(',');
if (!auth || !adminKeyList.includes(auth.replace('Bearer ', ''))) {
return new Response('Succeed', { status: 200 });
}
// 从请求中获取用户的数据
const requestData = await request.json();
// 获取 aliveaccount 的值并解析
const aliveAccount = await KV.get('PlusAliveAccounts');
let aliveAccountList = aliveAccount.split(',');
if (aliveAccountList.length > 0) {
// 从 aliveAccountList 中随机选一个
const accountNumber = aliveAccountList[Math.floor(Math.random() * aliveAccountList.length)];
const newaccesstoken = await refreshAT('',accountNumber);
//console.log(`Selected account number: ${accountNumber}, Access token: ${newaccesstoken}`);
// 构建API请求
const apiRequest = new Request(`https://api.oaifree.com${pathname}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${newaccesstoken}`
},
body: JSON.stringify(requestData)
});
// 发送API请求并获取响应
const apiResponse = await fetch(apiRequest);
const responseBody = await apiResponse.text();
// 记录响应状态和响应体
//console.log(`Request: ${JSON.stringify(requestData)}`);
//console.log(`Response status: ${apiResponse.status}, Response body: ${responseBody}`);
if (apiResponse.status === 401) {
// 如果状态码是401,从 aliveAccountList 中删除对应的序号
aliveAccountList = aliveAccountList.filter(account => account !== accountNumber.toString());
await KV.put('PlusAliveAccounts', aliveAccountList.join(','));
// console.log(`Removed account number: ${accountNumber} from aliveAccountList`);
await deletelog('API', accountNumber,'Plus')
}
// 返回API响应给用户
return new Response(responseBody, {
status: apiResponse.status,
headers: apiResponse.headers
});
}
}
async function deletelog(userName, accountNumber,antype) {
const currentTime = new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" });
const logEntry = {
user: userName,
time: currentTime,
accountNumber: accountNumber
};
// Retrieve the existing log array or create a new one if it doesn't exist
// @ts-ignore
const lastDeleteLogs = await KV.get(`${antype}DeleteLogs`);
let logArray = [];
if (lastDeleteLogs) {
logArray = JSON.parse(lastDeleteLogs);
}
logArray.push(logEntry);
// @ts-ignore
await KV.put(`${antype}DeleteLogs`, JSON.stringify(logArray));
}