js代码(打断点扣代码,发现函数d for循环中的h是固定值 = ["320305", "131321201"]),代码如下
function n(t, e) {
for (var n = 0; n < e.length - 2; n += 3) {
var r = e.charAt(n + 2);
r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
r = "+" === e.charAt(n + 1) ? t >>> r : t << r,
t = "+" === e.charAt(n) ? t + r & 4294967295 : t ^ r
}
return t
}
function d(t) {
var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === i) {
var a = t.length;
a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
} else {
for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, u = s.length, l = []; c < u; c++)
"" !== s[c] && l.push.apply(l, function (t) {
if (Array.isArray(t))
return e(t)
}(o = s[c].split("")) || function (t) {
if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
return Array.from(t)
}(o) || function (t, n) {
if (t) {
if ("string" == typeof t)
return e(t, n);
var r = Object.prototype.toString.call(t).slice(8, -1);
return "Object" === r && t.constructor && (r = t.constructor.name),
"Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
}
}(o) || function () {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}()),
c !== u - 1 && l.push(i[c]);
var p = l.length;
p > 30 && (t = l.slice(0, 10).join("") + l.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) + 5).join("") + l.slice(-10).join(""))
}
// for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h = (null !== r ? r : (r = window[d] || "") || "").split("."), f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v++) {
for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h = ["320305", "131321201"], f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v++) {
var _ = t.charCodeAt(v);
_ < 128 ? g[y++] = _ : (_ < 2048 ? g[y++] = _ >> 6 | 192 : (55296 == (64512 & _) && v + 1 < t.length && 56320 == (64512 & t.charCodeAt(v + 1)) ? (_ = 65536 + ((1023 & _) << 10) + (1023 & t.charCodeAt(++v)),
g[y++] = _ >> 18 | 240,
g[y++] = _ >> 12 & 63 | 128) : g[y++] = _ >> 12 | 224,
g[y++] = _ >> 6 & 63 | 128),
g[y++] = 63 & _ | 128)
}
for (var b = f, w = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(97)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(54)), k = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(51)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(98)) + "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(102)), x = 0; x < g.length; x++)
b = n(b += g[x], w);
return b = n(b, k),
(b ^= m) < 0 && (b = 2147483648 + (2147483647 & b)),
"".concat((b %= 1e6).toString(), ".").concat(b ^ f)
}
function get_sign(text) {
return d(text)
}
python代码(需要配置node环境运行上面的js sign.js)
# -*- coding:utf-8 -*-
# @FileName :test.py
# @Time :2024/2/6 19:41
# @Author :有何不可
import time
import json
import re
import execjs
import requests
# 构造js环境进行数据解密
from execjs import _runner_sources
class StructJs:
with open('./sign.js', mode='rt', encoding='utf-8') as f:
js = f.read()
# ctx = execjs.compile(js, cwd=r'D:\yingyong\nodejs\node_modules\npm\node_global\node_modules')
# 创建 JavaScript 运行时 对象
# 参数中的 command 并不重要,传入空字符串即可
local_node_runtime = execjs.ExternalRuntime(
name="Node.js (V8) local",
command='',
encoding='UTF-8',
runner_source=_runner_sources.Node
)
# 这里是重点,需要强制性修改
local_node_runtime._binary_cache = [r'../node/node.exe']
local_node_runtime._available = True
# 将刚创建好的 JavaScript 运行时 注册至 PyExecJS 中
execjs.register('local_node', local_node_runtime)
# 通过 execjs 的 get 方法,即可调用刚才注册到 PyExecJS 的 JavaScript 运行时
ctx = execjs.get('local_node').compile(js, cwd=r'../node/node_modules')
@staticmethod
def get_sign(text):
res = StructJs.ctx.call('get_sign', text)
return res
class Translator:
def __init__(self):
self.session = requests.session()
self.token = self.get_token()
def get_token(self):
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"DNT": "1",
"Pragma": "no-cache",
"Referer": "https://fanyi.baidu.com/",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0",
"sec-ch-ua": "^\\^Not",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "^\\^Windows^^"
}
url = "https://fanyi.baidu.com/"
self.session.headers.update(headers)
self.session.get(url)
response = self.session.get(url)
token = re.search("token: '(.*?)',", response.text).group(1)
return token
def translation(self, text):
url = "https://fanyi.baidu.com/v2transapi"
params = {
"from": "zh",
"to": "en"
}
data = {
"from": "zh",
"to": "en",
"query": text,
"transtype": "realtime",
"simple_means_flag": "3",
"sign": StructJs.get_sign(text),
"token": self.token,
"domain": "common",
"ts": str(time.time() * 1000)
}
self.session.headers.update({
"Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"DNT": "1",
"Origin": "https://fanyi.baidu.com",
"Referer": "https://fanyi.baidu.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"X-Requested-With": "XMLHttpRequest",
"sec-ch-ua": "^\\^Not",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "^\\^Windows^^"
})
response = self.session.post(url, params=params, data=data)
data_dic = response.json()
return data_dic['trans_result']['data'][0]
translator = Translator()
while True:
text = input('请输入:').strip()
if not text:
continue
res = translator.translation(text)
print(res)