Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: remove wild DeepL keys #54

Merged
merged 7 commits into from
Apr 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions src/translation/deepL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { httpsAgent, requestCostTime } from "../axiosConfig";
import { QueryWordInfo } from "../dictionary/youdao/types";
import { getDeepLLangCode } from "../language/languages";
import { AppKeyStore, myDecrypt, myEncrypt } from "../preferences";
import { AppKeyStore, myDecrypt } from "../preferences";
import { DeepLTranslateResult, QueryTypeResult, TranslationType } from "../types";
import { getTypeErrorInfo } from "../utils";

Expand Down Expand Up @@ -45,6 +45,11 @@

const deepLAuthKey = await getDeepLAuthKey();

if (!deepLAuthKey) {
console.log(`---> no deepL key`);
return Promise.reject({ message: "no deepL key" });
tisfeng marked this conversation as resolved.
Show resolved Hide resolved
}

// * deepL api free and deepL pro api use different url host.
const url = deepLAuthKey.endsWith(":fx")
? "https://api-free.deepl.com/v2/translate"
Expand All @@ -57,6 +62,15 @@
};
// console.log(`---> deepL params: ${JSON.stringify(params, null, 4)}`);

if (deepLAuthKey.endsWith(":fx")) {
console.log(`---> deepL api free`);
// checkIfKeyValid
if (!(await checkIfKeyValid(deepLAuthKey))) {
console.log(`---> deepL api free key is invalid`);
return Promise.reject({ message: "deepL api free key is invalid" });
}
}

return new Promise((resolve, reject) => {
axios
.post(url, querystring.stringify(params), { httpsAgent })
Expand Down Expand Up @@ -89,13 +103,13 @@
// https://www.deepl.com/zh/docs-api/api-access/error-handling/
if (errorCode === 456) {
errorInfo.message = "Quota exceeded"; // Quota exceeded. The character limit has been reached.
if (wildEncryptedDeepLKeys.length) {
getAndStoreDeepLKey(wildEncryptedDeepLKeys).then(() => {
requestDeepLTranslate(queryWordInfo)
.then((result) => resolve(result))
.catch((err) => reject(err));
});
}
// if (wildEncryptedDeepLKeys.length) {
// getAndStoreDeepLKey(wildEncryptedDeepLKeys).then(() => {
// requestDeepLTranslate(queryWordInfo)
// .then((result) => resolve(result))
// .catch((err) => reject(err));
// });
// }
return;
}

Expand All @@ -112,20 +126,21 @@
/**
* This deepl key is from Github, we do not guarantee that it will work all the time.
* https://github.com/search?p=1&q=deepl+key+%3Afx&type=Code
* wildEncryptedDeepLKeys is no longer provided.
*/
const wildEncryptedDeepLKeys = [
"U2FsdGVkX19Mt7cnRCJQINAzLGqqZAhqPcbxeKrBUV62/Dd0u1Qa0QxY8ljYWjmCAz8NwG+uOmD8Ez0HijCJnw==",
"U2FsdGVkX1+7yAdmxTGWdRJ6oeDcZ+1YzndxtkdpuOk6jWBjNezThjj8NgT+flfxOPccJXXlIilvRssFzPnagg==",
"U2FsdGVkX1+NJ1HnZbrmW0KMdbTTHPAE2LmATthkMS2EFt1lJ0W74GBi+rlwJeBKZrn6R9ne4fdI7WV0vpCcrQ==",
"U2FsdGVkX1856l+ibQOyYvNwU53suxx6UHzBT1xBuIzhh5JvHmD/qG5gImiDpJbw62LWQxBXNn7kRvM+O2jRYg==",
"U2FsdGVkX190UMu/gorJ/qgwhayFJilCPE5kSfOutkELsUnylfAZEtJGVPin3njGRwC2odphwTigbCzEcJ4kAw==",
"U2FsdGVkX1+iLWPtCcBXjS4TLLbBql8KOU4wvfcGhm/nAcYIu2BIaco8iORmW9CCKEKSLkUd3aSCaSDPgnuClA==",
"U2FsdGVkX1/seyI1CRkqz8+73B33fCplJrqDNkiXC83XBr3Jc8Rz14Bhx6ldVbpkcy8sk18/CQyCAWbgiJPEjQ==",
"U2FsdGVkX1/vD+AUbRlTFmGMqQGsbzjngY2NUwiLgYrMRA9KD0sTI7Xq8DJz3aMpB8PAuZZMcMFmqjedu5yobw==",
"U2FsdGVkX1+1Iexu0P8IEaxZchH/LYi9BCAQNbt8d0ImP0/NyTc+W3JhlBtTcB31SfstKOURNQQW6Ol3ZCxfew==",
"U2FsdGVkX1+yGtuvj9lX2qJZXMiaAs1KMB3jwK0SPnVGfATSyXC8LGBnVTyX6TNyLvSvnINJQp1dLZzDb85bLQ==",
"U2FsdGVkX1/61u2OfkPsFuw54CA3I1imQ5FwUymFsvkyaOXQkMm+sr+NGGlfLvHNcp6SvQgmrQuof8F/pRY51w==",
];
// const wildEncryptedDeepLKeys = [
tisfeng marked this conversation as resolved.
Show resolved Hide resolved
// "U2FsdGVkX19Mt7cnRCJQINAzLGqqZAhqPcbxeKrBUV62/Dd0u1Qa0QxY8ljYWjmCAz8NwG+uOmD8Ez0HijCJnw==",
// "U2FsdGVkX1+7yAdmxTGWdRJ6oeDcZ+1YzndxtkdpuOk6jWBjNezThjj8NgT+flfxOPccJXXlIilvRssFzPnagg==",
// "U2FsdGVkX1+NJ1HnZbrmW0KMdbTTHPAE2LmATthkMS2EFt1lJ0W74GBi+rlwJeBKZrn6R9ne4fdI7WV0vpCcrQ==",
// "U2FsdGVkX1856l+ibQOyYvNwU53suxx6UHzBT1xBuIzhh5JvHmD/qG5gImiDpJbw62LWQxBXNn7kRvM+O2jRYg==",
// "U2FsdGVkX190UMu/gorJ/qgwhayFJilCPE5kSfOutkELsUnylfAZEtJGVPin3njGRwC2odphwTigbCzEcJ4kAw==",
// "U2FsdGVkX1+iLWPtCcBXjS4TLLbBql8KOU4wvfcGhm/nAcYIu2BIaco8iORmW9CCKEKSLkUd3aSCaSDPgnuClA==",
// "U2FsdGVkX1/seyI1CRkqz8+73B33fCplJrqDNkiXC83XBr3Jc8Rz14Bhx6ldVbpkcy8sk18/CQyCAWbgiJPEjQ==",
// "U2FsdGVkX1/vD+AUbRlTFmGMqQGsbzjngY2NUwiLgYrMRA9KD0sTI7Xq8DJz3aMpB8PAuZZMcMFmqjedu5yobw==",
// "U2FsdGVkX1+1Iexu0P8IEaxZchH/LYi9BCAQNbt8d0ImP0/NyTc+W3JhlBtTcB31SfstKOURNQQW6Ol3ZCxfew==",
// "U2FsdGVkX1+yGtuvj9lX2qJZXMiaAs1KMB3jwK0SPnVGfATSyXC8LGBnVTyX6TNyLvSvnINJQp1dLZzDb85bLQ==",
// "U2FsdGVkX1/61u2OfkPsFuw54CA3I1imQ5FwUymFsvkyaOXQkMm+sr+NGGlfLvHNcp6SvQgmrQuof8F/pRY51w==",
// ];

/**
* Get a deepL key.
Expand Down Expand Up @@ -169,7 +184,7 @@
* https://www.deepl.com/zh/docs-api/other-functions/monitoring-usage/
*/
function checkIfKeyValid(key: string): Promise<boolean> {
console.log(`test a deepL key: ${key}`);

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
const url = "https://api-free.deepl.com/v2/usage";
const params = {
auth_key: key,
Expand All @@ -182,26 +197,28 @@
const usage = res.data as DeepLUsage;
console.log(`---> deepL usage: ${JSON.stringify(usage)}`);
if (usage.character_count < usage.character_limit) {
console.log(`---> valid key: ${key}`);

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
resolve(true);
} else {
console.log(`---> execeded quota: ${key}`);

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
This logs sensitive data returned by
an access to deepLAuthKey
as clear text.
resolve(false);
}
})
.catch((err) => {
console.error(`---> isValidKey deepL error: ${err}`);

// if error, remove key from wildEncryptedDeepLKeys
const encryptedKey = myEncrypt(key);
wildEncryptedDeepLKeys.splice(wildEncryptedDeepLKeys.indexOf(encryptedKey), 1);
console.log(`---> remove error key: ${key}`);
// nolonger provide wildEncryptedDeepLKeys
// // if error, remove key from wildEncryptedDeepLKeys
// const encryptedKey = myEncrypt(key);
// wildEncryptedDeepLKeys.splice(wildEncryptedDeepLKeys.indexOf(encryptedKey), 1);
// console.log(`---> remove error key: ${key}`);

resolve(false);
});
});
}

// followings are used for wildEncryptedDeepLKeys
/**
* Get a deepL key and store it. Do not check if key is valid.
*/
Expand Down
Loading