mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-01 14:57:14 +08:00
feat: request wrapper
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ const config = require('./config');
|
||||
const Koa = require('koa');
|
||||
const fs = require('fs');
|
||||
const logger = require('./utils/logger');
|
||||
require('./utils/agent-wrapper');
|
||||
require('./utils/request-wrapper');
|
||||
|
||||
const onerror = require('./middleware/onerror');
|
||||
const header = require('./middleware/header');
|
||||
|
||||
@@ -43,7 +43,6 @@ const custom = got.extend({
|
||||
},
|
||||
headers: {
|
||||
'user-agent': config.ua,
|
||||
'x-app': 'RSSHub',
|
||||
},
|
||||
});
|
||||
custom.all = (list) => Promise.all(list);
|
||||
|
||||
@@ -47,7 +47,7 @@ if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.p
|
||||
}
|
||||
}
|
||||
|
||||
const agentWrapper = (url, options) => {
|
||||
const requestWrapper = (url, options) => {
|
||||
if (agent && new RegExp(config.proxy.url_regex).test(url)) {
|
||||
let agentResult;
|
||||
try {
|
||||
@@ -62,6 +62,16 @@ const agentWrapper = (url, options) => {
|
||||
}
|
||||
logger.info(`Proxy for ${url}`);
|
||||
}
|
||||
let hasUA = false;
|
||||
for (const header in options.headers) {
|
||||
if (header.toLowerCase() === 'user-agent') {
|
||||
hasUA = true;
|
||||
}
|
||||
}
|
||||
if (!hasUA) {
|
||||
options.headers['user-agent'] = config.ua;
|
||||
}
|
||||
options.headers['x-app'] = 'RSSHub';
|
||||
};
|
||||
|
||||
const httpWrap = (func) => {
|
||||
@@ -69,9 +79,9 @@ const httpWrap = (func) => {
|
||||
return function(url, request) {
|
||||
if (typeof url === 'object') {
|
||||
const req = url;
|
||||
agentWrapper(req.url || req.href || `${req.protocol}//${req.hostname}${req.path}`, req);
|
||||
requestWrapper(req.url || req.href || `${req.protocol}//${req.hostname}${req.path}`, req);
|
||||
} else {
|
||||
agentWrapper(url, request);
|
||||
requestWrapper(url, request);
|
||||
}
|
||||
return origin.apply(this, arguments);
|
||||
};
|
||||
@@ -7,7 +7,6 @@ const parser = new Parser({
|
||||
},
|
||||
headers: {
|
||||
'User-Agent': config.ua,
|
||||
'X-APP': 'RSSHub',
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user