Edit checker.ts
This commit is contained in:
@@ -39,42 +39,59 @@ export class XaiKeyChecker {
|
||||
}, CHECK_TIMEOUT);
|
||||
|
||||
try {
|
||||
const response = await fetch("https://api.x.ai/v1/chat/completions", {
|
||||
// First check API key endpoint to verify key validity
|
||||
const apiResponse = await fetch("https://api.x.ai/v1/api-key", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${key.key}`,
|
||||
},
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
if (apiResponse.status !== 200) {
|
||||
// Key is invalid or has some other issue
|
||||
return "invalid";
|
||||
}
|
||||
|
||||
const apiData = await apiResponse.json();
|
||||
const isBlocked = apiData.team_blocked || apiData.api_key_blocked || apiData.api_key_disabled;
|
||||
|
||||
if (isBlocked) {
|
||||
return "invalid";
|
||||
}
|
||||
|
||||
// If the key passed the first check, test a minimal API call to verify quota
|
||||
const testResponse = await fetch("https://api.x.ai/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${key.key}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "grok-3",
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
max_tokens: 0,
|
||||
messages: [],
|
||||
model: "grok-3-mini-latest",
|
||||
frequency_penalty: -3.0,
|
||||
}),
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
switch (response.status) {
|
||||
case 200:
|
||||
case 400: {
|
||||
// Check the response body for "Incorrect%20API"
|
||||
const responseText = await response.text();
|
||||
if (responseText.includes("Incorrect%20API")) {
|
||||
return "invalid";
|
||||
}
|
||||
return "valid";
|
||||
}
|
||||
case 401:
|
||||
return "invalid";
|
||||
case 403:
|
||||
return "invalid";
|
||||
case 429:
|
||||
return "quota";
|
||||
default:
|
||||
this.log.warn(
|
||||
{ status: response.status, hash: key.hash },
|
||||
"Unexpected status code while checking key"
|
||||
);
|
||||
return "invalid";
|
||||
// If we get 400 or 200, the key is valid (400 might be parameter error but key is valid)
|
||||
if (testResponse.status === 400 || testResponse.status === 200) {
|
||||
return "valid";
|
||||
} else if (testResponse.status === 429) {
|
||||
return "quota";
|
||||
} else if (testResponse.status === 403) {
|
||||
this.log.warn(
|
||||
{ status: testResponse.status, hash: key.hash },
|
||||
"Forbidden (403) response, key is invalid"
|
||||
);
|
||||
return "invalid";
|
||||
} else {
|
||||
this.log.warn(
|
||||
{ status: testResponse.status, hash: key.hash },
|
||||
"Unexpected status code while testing key usage"
|
||||
);
|
||||
return "invalid";
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
|
||||
Reference in New Issue
Block a user