Skip to content

Commit 6b4d35f

Browse files
committed
Feature:Add experimental features
1 parent 60cd9b5 commit 6b4d35f

File tree

2 files changed

+554
-0
lines changed

2 files changed

+554
-0
lines changed

api/alive.js

+321
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
const corsHeaders = {
2+
'Access-Control-Allow-Origin': '*',
3+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
4+
'Access-Control-Allow-Headers': 'Content-Type',
5+
};
6+
7+
export default async function (req, res) {
8+
console.log('Received request:', req.method, req.url);
9+
10+
if (req.method === 'OPTIONS') {
11+
res.writeHead(204, corsHeaders);
12+
res.end();
13+
return;
14+
}
15+
16+
if (req.method === 'POST') {
17+
try {
18+
let body = '';
19+
for await (const chunk of req) {
20+
body += chunk;
21+
}
22+
console.log('Request body:', body);
23+
24+
const content = JSON.parse(body);
25+
console.log('Parsed request content:', content);
26+
const type = content.type;
27+
28+
if (type === 'refreshTokens') {
29+
const responseBody = await handleRefreshTokens(content.tokens);
30+
res.writeHead(200, {
31+
'Content-Type': 'application/json',
32+
...corsHeaders,
33+
});
34+
res.end(JSON.stringify(responseBody));
35+
} else if (type === 'sessionKeys') {
36+
const responseBody = await handleSessionKeys(content);
37+
res.writeHead(200, {
38+
'Content-Type': 'application/json',
39+
...corsHeaders,
40+
});
41+
res.end(JSON.stringify(responseBody));
42+
} else if (type === 'geminiAPI') {
43+
const responseBody = await handleTestAPIs(content);
44+
res.writeHead(200, {
45+
'Content-Type': 'application/json',
46+
...corsHeaders,
47+
});
48+
res.end(JSON.stringify(responseBody));
49+
} else {
50+
res.writeHead(400, {
51+
'Content-Type': 'application/json',
52+
...corsHeaders,
53+
});
54+
res.end(JSON.stringify({ error: 'Invalid request type' }));
55+
return;
56+
}
57+
} catch (error) {
58+
console.error('Error processing request:', error);
59+
res.writeHead(500, {
60+
'Content-Type': 'application/json',
61+
...corsHeaders,
62+
});
63+
res.end(JSON.stringify({ error: error.message }));
64+
}
65+
} else {
66+
res.writeHead(404, { 'Content-Type': 'text/plain', ...corsHeaders });
67+
res.end('Not Found');
68+
}
69+
}
70+
71+
async function handleRefreshTokens(refreshTokens) {
72+
try {
73+
const results = await Promise.all(refreshTokens.map(checkTokenValidity));
74+
return results;
75+
} catch (error) {
76+
throw new Error('Error processing refresh tokens: ' + error.message);
77+
}
78+
}
79+
80+
function checkTokenValidity(refreshToken) {
81+
return fetch('https://token.oaifree.com/api/auth/refresh', {
82+
method: 'POST',
83+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
84+
body: 'refresh_token=' + encodeURIComponent(refreshToken),
85+
})
86+
.then(function (response) {
87+
if (response.ok) {
88+
return response.json().then(function (data) {
89+
return {
90+
refreshToken: refreshToken,
91+
accessToken: data.access_token,
92+
valid: true,
93+
};
94+
});
95+
}
96+
return { refreshToken: refreshToken, accessToken: null, valid: false };
97+
})
98+
.catch(function () {
99+
return { refreshToken: refreshToken, accessToken: null, valid: false };
100+
});
101+
}
102+
103+
async function handleSessionKeys(content) {
104+
const sessionKeys = content.tokens;
105+
const maxAttempts = content.maxAttempts;
106+
const requestsPerSecond = content.requestsPerSecond;
107+
const delayBetweenRequests = 1000 / requestsPerSecond;
108+
109+
function delay(ms) {
110+
return new Promise(function (resolve) {
111+
setTimeout(resolve, ms);
112+
});
113+
}
114+
115+
async function checkSessionKey(sessionKey) {
116+
let attempts = 0;
117+
let successCount = 0;
118+
119+
async function attemptCheck() {
120+
attempts++;
121+
try {
122+
const response = await fetch(
123+
'https://api.claude.ai/api/organizations',
124+
{
125+
headers: {
126+
accept: 'application/json',
127+
cookie: 'sessionKey=' + sessionKey,
128+
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64)',
129+
},
130+
}
131+
);
132+
133+
if (!response.ok) {
134+
throw new Error('HTTP error! status: ' + response.status);
135+
}
136+
const responseText = await response.text();
137+
138+
if (
139+
responseText.toLowerCase().includes('unauthorized') ||
140+
responseText.trim() === ''
141+
) {
142+
throw new Error('Invalid response');
143+
}
144+
const objects = JSON.parse(responseText);
145+
successCount++;
146+
const name = objects[0].name || 'Unknown';
147+
const capabilities = objects[0].capabilities
148+
? objects[0].capabilities.join(';')
149+
: '';
150+
return {
151+
sessionKey: sessionKey,
152+
name: name,
153+
capabilities: capabilities,
154+
available: true,
155+
attempts: attempts,
156+
successRate: successCount / attempts,
157+
};
158+
} catch (error) {
159+
if (attempts < maxAttempts) {
160+
await delay(delayBetweenRequests);
161+
return attemptCheck();
162+
}
163+
return {
164+
sessionKey: sessionKey,
165+
name: 'Invalid',
166+
capabilities: '',
167+
available: false,
168+
attempts: attempts,
169+
successRate: successCount / attempts,
170+
};
171+
}
172+
}
173+
174+
return attemptCheck();
175+
}
176+
177+
try {
178+
const results = await Promise.all(sessionKeys.map(checkSessionKey));
179+
return results;
180+
} catch (error) {
181+
throw new Error('Error processing session keys: ' + error.message);
182+
}
183+
}
184+
185+
async function handleTestAPIs(content) {
186+
const apiKeys = content.tokens;
187+
const model = content.model;
188+
const rateLimit = content.rateLimit;
189+
const prompt = content.prompt;
190+
const user = content.user;
191+
192+
if (!apiKeys || !Array.isArray(apiKeys) || apiKeys.length === 0) {
193+
throw new Error('Invalid or empty API keys');
194+
}
195+
196+
try {
197+
const results = await batchTestAPI(apiKeys, model, rateLimit, prompt, user);
198+
const validKeys = results
199+
.filter(function (r) {
200+
return r.valid;
201+
})
202+
.map(function (r) {
203+
return r.key;
204+
});
205+
const invalidKeys = results
206+
.filter(function (r) {
207+
return !r.valid;
208+
})
209+
.map(function (r) {
210+
return r.key;
211+
});
212+
const errors = results
213+
.filter(function (r) {
214+
return r.error;
215+
})
216+
.map(function (r) {
217+
return { key: r.key, error: r.error };
218+
});
219+
const validResults = results.filter(function (r) {
220+
return r.valid && r.data;
221+
});
222+
223+
return {
224+
valid: validKeys.length,
225+
invalid: invalidKeys.length,
226+
invalidKeys: invalidKeys,
227+
errors: errors,
228+
validResults: validResults,
229+
};
230+
} catch (error) {
231+
throw new Error('Error testing APIs: ' + error.message);
232+
}
233+
}
234+
235+
function batchTestAPI(apiKeys, model, rateLimit, prompt, user) {
236+
const results = [];
237+
const delayBetweenRequests = 1000 / rateLimit;
238+
239+
function delay(ms) {
240+
return new Promise(function (resolve) {
241+
setTimeout(resolve, ms);
242+
});
243+
}
244+
245+
function testNextKey(index) {
246+
if (index >= apiKeys.length) {
247+
return Promise.resolve(results);
248+
}
249+
250+
return testAPI(apiKeys[index], model, prompt, user)
251+
.then(function (result) {
252+
results.push(result);
253+
})
254+
.catch(function (error) {
255+
results.push({
256+
key: apiKeys[index],
257+
valid: false,
258+
error: error.message,
259+
});
260+
})
261+
.then(function () {
262+
return delay(delayBetweenRequests);
263+
})
264+
.then(function () {
265+
return testNextKey(index + 1);
266+
});
267+
}
268+
269+
return testNextKey(0);
270+
}
271+
272+
function testAPI(apiKey, model, prompt, user) {
273+
const url =
274+
'https://generativelanguage.googleapis.com/v1beta/models/' +
275+
model +
276+
':generateContent?key=' +
277+
apiKey;
278+
return fetch(url, {
279+
method: 'POST',
280+
headers: {
281+
'Content-Type': 'application/json',
282+
},
283+
body: JSON.stringify({
284+
contents: [
285+
{
286+
parts: [{ text: prompt }, { text: user }],
287+
},
288+
],
289+
safetySettings: [
290+
{
291+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
292+
threshold: 'BLOCK_NONE',
293+
},
294+
{ category: 'HARM_CATEGORY_HATE_SPEECH', threshold: 'BLOCK_NONE' },
295+
{ category: 'HARM_CATEGORY_HARASSMENT', threshold: 'BLOCK_NONE' },
296+
{
297+
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
298+
threshold: 'BLOCK_NONE',
299+
},
300+
],
301+
}),
302+
})
303+
.then(function (response) {
304+
if (!response.ok) {
305+
return response.text().then(function (errorText) {
306+
throw new Error(
307+
'API request failed: ' +
308+
response.status +
309+
' ' +
310+
response.statusText +
311+
' - ' +
312+
errorText
313+
);
314+
});
315+
}
316+
return response.json();
317+
})
318+
.then(function (data) {
319+
return { key: apiKey, valid: true, data: data };
320+
});
321+
}

0 commit comments

Comments
 (0)