Fix: Send requests using Node.js's native HTTPS, without relying on curl, and with cross-platform compatibility.
This commit is contained in:
1 parent
e6823c8312
commit
b71e1271c3
2 files changed
+30
-15
No files matched your search
+29
-14
@@ -1,4 +1,4 @@
|
||||
import { execSync, spawnSync } from 'child_process';
|
||||
import { execSync } from 'child_process';
|
||||
import * as cheerio from 'cheerio';
|
||||
import { getCache, setCache, getConfig } from './cache.js';
|
||||
import chalk from 'chalk';
|
||||
@@ -157,21 +157,36 @@ async function searchJavdb(id) {
|
||||
const session = decodeURIComponent(config.session);
|
||||
const cookie = `locale=zh; over18=1; _jdb_session=${session}`;
|
||||
|
||||
// 用 spawnSync 传数组参数,完全不经过 shell
|
||||
// 特殊字符 +/= 直接传给 curl,不会被 shell 破坏
|
||||
function fetchJavdb(url) {
|
||||
const result = spawnSync('curl', [
|
||||
'-sL', url,
|
||||
'-H', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
|
||||
'-H', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'-H', 'Accept-Language: zh-CN,zh;q=0.9',
|
||||
'-H', `Cookie: ${cookie}`,
|
||||
], { timeout: 15000, maxBuffer: 1024 * 1024 * 10 });
|
||||
return result.stdout.toString();
|
||||
// 用 Node.js 原生 https 发请求,不依赖 curl,跨平台兼容
|
||||
async function fetchJavdbHtml(url) {
|
||||
const { default: https } = await import('https');
|
||||
const { default: http } = await import('http');
|
||||
return new Promise((resolve, reject) => {
|
||||
const client = url.startsWith('https') ? https : http;
|
||||
const req = client.get(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||
'Cookie': cookie,
|
||||
},
|
||||
}, (res) => {
|
||||
// 处理重定向
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
resolve(fetchJavdbHtml(res.headers.location));
|
||||
return;
|
||||
}
|
||||
const chunks = [];
|
||||
res.on('data', chunk => chunks.push(chunk));
|
||||
res.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
|
||||
});
|
||||
req.on('error', reject);
|
||||
req.setTimeout(15000, () => { req.destroy(); reject(new Error('timeout')); });
|
||||
});
|
||||
}
|
||||
|
||||
const searchUrl = `https://javdb.com/search?q=${encodeURIComponent(id)}&f=all`;
|
||||
const searchHtml = fetchJavdb(searchUrl);
|
||||
const searchHtml = await fetchJavdbHtml(searchUrl);
|
||||
|
||||
const $ = cheerio.load(searchHtml);
|
||||
let detailPath = null;
|
||||
@@ -192,7 +207,7 @@ async function searchJavdb(id) {
|
||||
let detailHtml = '';
|
||||
for (let i = 0; i < 3; i++) {
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
detailHtml = fetchJavdb(`https://javdb.com${detailPath}`);
|
||||
detailHtml = await fetchJavdbHtml(`https://javdb.com${detailPath}`);
|
||||
if (detailHtml.length > 1000) break;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "javinfo",
|
||||
"version": "1.1.9",
|
||||
"version": "1.1.10",
|
||||
"description": "AV番号命令行查询工具",
|
||||
"main": "./index.js",
|
||||
"type": "module",
|
||||
|
||||
Reference in new issue
Block a user