Skip to content

Commit

Permalink
Merge pull request #245 from hjdhnx/main
Browse files Browse the repository at this point in the history
修复嗅探过程中莫名其妙报错的问题
  • Loading branch information
Hiram-Wong authored Jun 4, 2024
2 parents 3d7c893 + 813c88c commit e184c94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"electron-updater": "^6.1.8",
"fs-extra": "^11.2.0",
"ip": "^2.0.1",
"puppeteer-core": "~20.0.0",
"puppeteer-core": "~21.3.8",
"puppeteer-in-electron": "^3.0.5",
"v8-compile-cache": "^2.4.0"
},
Expand Down Expand Up @@ -79,7 +79,7 @@
"dashjs": "^4.7.4",
"dayjs": "^1.11.11",
"dplayer": "^1.27.1",
"electron": "^22.0.0",
"electron": "~22.3.27",
"electron-builder": "^24.13.3",
"electron-vite": "^2.2.0",
"eslint": "^9.4.0",
Expand Down
24 changes: 15 additions & 9 deletions src/main/utils/pie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ const puppeteerInElectron = async (
};

page.on('request', async (req) => {
if (req.isInterceptResolutionHandled()) return; // 已处理过的请求不再处理

const reqUrl = req.url(); // 请求url
// logger.info(`[reqUrl]:${reqUrl}`);
if (req.isInterceptResolutionHandled()) return req.abort().catch((err) => logger.error(err)); // 已处理过的请求不再处理

const reqHeaders = req.headers(); // 请求头
const { referer, 'user-agent': userAgent } = reqHeaders;
const headers = {};
Expand All @@ -99,33 +100,38 @@ const puppeteerInElectron = async (

if (customRegex && reqUrl.match(new RegExp(customRegex, 'gi'))) {
logger.info(`[pie]正则匹配:${reqUrl}`);
page.removeAllListeners("request");
await cleanup(pageId);
req.abort().catch((e) => logger.error(e));
resolve(handleResponse(200, 'success', { url: reqUrl, header: headers }));
return resolve(handleResponse(200, 'success', { url: reqUrl, header: headers }));
}

if (isVideoUrl(reqUrl)) {
logger.info(`[pie]后缀名匹配:${reqUrl}`);
page.removeAllListeners("request");
await cleanup(pageId);
req.abort().catch((e) => logger.error(e));
resolve(handleResponse(200, 'success', { url: reqUrl, header: headers }));
return resolve(handleResponse(200, 'success', { url: reqUrl, header: headers }));
}

if (req.method().toLowerCase() === 'head') {
req.abort().catch((err) => logger.error(err));
// logger.info(`[pie][head]:${reqUrl}`);
return req.abort().catch((err) => logger.error(err));
}

if (['font'].includes(req.resourceType())) {
req.abort().catch((err) => logger.error(err));
//
if (/\.(png|jpg|jpeg|ttf)$/.test(reqUrl) && ["stylesheet", "image", "font"].includes(req.resourceType())) {
// logger.info(`[pie][font]:${reqUrl}`);
return req.abort().catch((err) => logger.error(err));
}

req.continue().catch((err) => logger.error(err));
return req.continue().catch((err) => logger.error(err));
});

// 设置超时
if (!pageStore[pageId].timerId) {
logger.info('--------!timerId---------');
pageStore[pageId].timerId = setTimeout(async () => {
page.removeAllListeners("request");
await cleanup(pageId);
logger.info(`[pie]id: ${pageId} sniffer timeout`);
reject(handleResponse(500, 'fail', new Error('sniffer timeout')));
Expand Down

0 comments on commit e184c94

Please sign in to comment.