From 160fb4e5781b6dfd233a5dff5148840b3e6c2f87 Mon Sep 17 00:00:00 2001 From: Sergey Pinus <57861007+bropines@users.noreply.github.com> Date: Sat, 30 Nov 2024 07:11:51 +0500 Subject: [PATCH 1/4] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1d9ac11..bcfc22e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# temporary not working + DeepLX Vercel ============= From dd868118a7dfce68b6de8cad83c86efd60267c09 Mon Sep 17 00:00:00 2001 From: Sergey Pinus Date: Sat, 30 Nov 2024 19:03:56 +0500 Subject: [PATCH 2/4] Fix attempt 1 --- api/index.js | 2 +- api/translate.js | 6 ++--- translate.js | 67 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/api/index.js b/api/index.js index d5264d2..4117f2f 100644 --- a/api/index.js +++ b/api/index.js @@ -1,6 +1,6 @@ module.exports = (req, res) => { res.json({ code: 200, - message: "Welcome to the DeepL Free API. Please POST to /api/translate. Visit http://github.com/OwO-Network/DeepLX for more information." + message: "Welcome to the DeepL Free API. Please POST to /api/translate. Visit http://github.com/OwO-Network/DeepLX for more information. \n If ip not working, write issue on github" }); }; diff --git a/api/translate.js b/api/translate.js index 0997968..f0ae0ee 100644 --- a/api/translate.js +++ b/api/translate.js @@ -7,7 +7,7 @@ module.exports = async (req, res) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); - res.setHeader('Access-Control-Max-Age', '86400'); // Кэширование preflight-запросов на 1 день + res.setHeader('Access-Control-Max-Age', '86400'); return res.status(204).end(); } @@ -20,10 +20,10 @@ module.exports = async (req, res) => { }); } - const { text, source_lang = 'auto', target_lang = 'RU', tag_handling = '' } = req.body; + const { text, source_lang = 'auto', target_lang = 'RU', tag_handling = '', dl_session = '', proxy = '' } = req.body; try { - const result = await translate(text, source_lang, target_lang, tag_handling); + const result = await translate(text, source_lang, target_lang, tag_handling, dl_session, proxy); const duration = Date.now() - startTime; console.log(`[LOG] ${new Date().toISOString()} | 200 | ${duration}ms | POST "/translate"`); diff --git a/translate.js b/translate.js index 0704b2b..bdcbfd2 100644 --- a/translate.js +++ b/translate.js @@ -9,7 +9,7 @@ function getICount(translateText) { } function getRandomNumber() { - return random(8300000, 8399998) * 1000; + return Math.floor(Math.random() * (8399998 - 8300000 + 1)) + 8300000; } function getTimestamp(iCount) { @@ -39,32 +39,55 @@ function formatPostString(postData) { } async function makeRequest(postData, method, dlSession = '', proxy = '') { - const url = `${DEEPL_BASE_URL}?client=chrome-extension,1.6.0&method=${method}`; + const url = `${DEEPL_BASE_URL}?client=chrome-extension%2C1.28.0&method=${method}`; const postDataStr = formatPostString(postData); const headers = { 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.9', + 'Authorization': 'None', + 'Cache-Control': 'no-cache', 'Content-Type': 'application/json', - 'Origin': 'chrome-extension://bppidhpdkcbahckohjehbehjmcnhpkck', + 'DNT': '1', + 'Origin': 'chrome-extension://cofdbpoegempjloogbagkncekinflcnj', + 'Pragma': 'no-cache', + 'Priority': 'u=1, i', 'Referer': 'https://www.deepl.com/', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', + 'Sec-Fetch-Dest': 'empty', + 'Sec-Fetch-Mode': 'cors', + 'Sec-Fetch-Site': 'none', + 'Sec-GPC': '1', + 'User-Agent': 'DeepLBrowserExtension/1.28.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64)', }; if (dlSession) { headers['Cookie'] = `dl_session=${dlSession}`; } + const axiosOptions = { + method: 'POST', + url: url, + headers: headers, + data: postDataStr, + responseType: 'arraybuffer', + decompress: false, + validateStatus: function (status) { + return status >= 200 && status < 500; + }, + }; + + if (proxy) { + const [host, port] = proxy.split(':'); + axiosOptions.proxy = { + host: host, + port: parseInt(port, 10), + }; + } else { + axiosOptions.proxy = false; + } + try { - const response = await axios.post(url, postDataStr, { - headers: headers, - responseType: 'arraybuffer', - decompress: false, - ...(proxy && { proxy: proxy }), - validateStatus: function (status) { - return status >= 200 && status < 500; - }, - }); + const response = await axios(axiosOptions); let data; const encoding = response.headers['content-encoding']; @@ -96,7 +119,7 @@ async function splitText(text, tagHandling) { lang_user_selected: 'auto', }, splitting: 'newlines', - text_type: (tagHandling === 'html' || tagHandling === 'xml' || isRichText(text)) ? 'richtext' : 'plaintext', + text_type: tagHandling || isRichText(text) ? 'richtext' : 'plaintext', }, }; @@ -116,7 +139,7 @@ async function translate( throw new Error('Нет текста для перевода.'); } - const splitResult = await splitText(text, tagHandling); + const splitResult = await splitText(text, tagHandling === 'html' || tagHandling === 'xml'); if (!splitResult || !splitResult.result) { throw new Error('Не удалось разделить текст.'); } @@ -134,9 +157,9 @@ async function translate( jobs.push({ kind: 'default', + preferred_num_beams: 4, raw_en_context_before: contextBefore, raw_en_context_after: contextAfter, - preferred_num_beams: 1, sentences: [ { id: idx + 1, @@ -158,6 +181,11 @@ async function translate( const iCount = getICount(text); const id = getRandomNumber(); + const commonJobParams = { + mode: 'translate', + ...(hasRegionalVariant && { regionalVariant: targetLang }), + }; + const postData = { jsonrpc: '2.0', method: 'LMT_handle_jobs', @@ -165,14 +193,11 @@ async function translate( params: { jobs: jobs, lang: { - source_lang_user_selected: detectedSourceLang.toUpperCase(), + source_lang_computed: detectedSourceLang.toUpperCase(), target_lang: targetLangCode.toUpperCase(), }, priority: 1, - commonJobParams: { - mode: 'translate', - ...(hasRegionalVariant && { regionalVariant: targetLang }), - }, + commonJobParams: commonJobParams, timestamp: getTimestamp(iCount), }, }; From 311164e7585ae401898982f12fbebbd4e283c4cf Mon Sep 17 00:00:00 2001 From: Sergey Pinus Date: Sat, 30 Nov 2024 19:09:10 +0500 Subject: [PATCH 3/4] Try 2 --- translate.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/translate.js b/translate.js index bdcbfd2..101f799 100644 --- a/translate.js +++ b/translate.js @@ -1,5 +1,4 @@ const axios = require('axios').default; -const { random } = require('lodash'); const zlib = require('zlib'); const DEEPL_BASE_URL = 'https://www2.deepl.com/jsonrpc'; @@ -45,6 +44,7 @@ async function makeRequest(postData, method, dlSession = '', proxy = '') { const headers = { 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.9', + 'Accept-Encoding': 'gzip, deflate, br', // Добавлено 'Authorization': 'None', 'Cache-Control': 'no-cache', 'Content-Type': 'application/json', @@ -93,6 +93,8 @@ async function makeRequest(postData, method, dlSession = '', proxy = '') { const encoding = response.headers['content-encoding']; if (encoding === 'br') { data = zlib.brotliDecompressSync(response.data).toString(); + } else if (encoding === 'gzip') { + data = zlib.gunzipSync(response.data).toString(); // Обработка Gzip } else { data = response.data.toString(); } From 05e47de4a3cf2dd0570ed4609bf77026708a0c86 Mon Sep 17 00:00:00 2001 From: Sergey Pinus <57861007+bropines@users.noreply.github.com> Date: Sat, 30 Nov 2024 19:41:25 +0500 Subject: [PATCH 4/4] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index bcfc22e..1d9ac11 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# temporary not working - DeepLX Vercel =============