Skip to content

Commit

Permalink
Update _worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jyx04 authored Jun 11, 2024
1 parent 1331929 commit 72928bc
Showing 1 changed file with 64 additions and 10 deletions.
74 changes: 64 additions & 10 deletions _worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ addEventListener('fetch', event => {


//通用函数


function parseJwt(token) {
const base64Url = token.split('.')[1];// 获取载荷部分
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
Expand All @@ -16,6 +18,49 @@ addEventListener('fetch', event => {
return JSON.parse(jsonPayload);// 返回载荷解析后的 JSON 对象
}

//刷新AT
async function refreshAT(tochecktoken,an) {
// 检查 token 是否存在,如果不存在或为空字符串,直接返回 true
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 '';
}
}


// 使用哈希函数加密username
function generatePassword(token) {
Expand Down Expand Up @@ -760,24 +805,33 @@ async function exportToken(tokenType, accountType) {
const accountTypeKey = `${accountType}AliveAccounts`;

// 获取对应类型的账户列表
let aliveAccount = await KV.get(accountTypeKey) || '';
let aliveAccount = await KV.get(accountTypeKey);
if (!aliveAccount) {
return new Response('No accounts found', { status: 404 });
}

let accountNumbers = aliveAccount.split(',');

// 获取所有账户号码对应的令牌
let tokens = [];
for (let accountNumber of accountNumbers) {
let tokenKey = `${tokenType}_${accountNumber}`;
let token = await KV.get(tokenKey);
if (token) {
tokens.push(token);
}

// 分批次处理账户,假设每批次处理 10 个账户
const batchSize = 10;
for (let i = 0; i < accountNumbers.length; i += batchSize) {
const batch = accountNumbers.slice(i, i + batchSize);

// 使用 Promise.all 并行处理
const batchTokens = await Promise.all(batch.map(async (accountNumber) => {
if (tokenType == 'at') {
return await refreshAT('', accountNumber);
} else {
let rtKey = `${tokenType}_${accountNumber}`;
return await KV.get(rtKey);
}
}));

tokens.push(...batchTokens);
}

// 创建txt文件
// 创建 txt 文件
let fileContent = tokens.join('\n');
let fileName = `${tokenType}.txt`;

Expand Down

0 comments on commit 72928bc

Please sign in to comment.