feat(core): auto referer

This commit is contained in:
DIYgod 2019-10-22 17:25:32 +08:00
parent 3735788552
commit 7a2dc47bf7
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
2 changed files with 31 additions and 2 deletions

View File

@ -48,6 +48,7 @@ if (config.proxy && config.proxy.protocol && config.proxy.host && config.proxy.p
}
const requestWrapper = (url, options) => {
// agent
if (agent && new RegExp(config.proxy.url_regex).test(url)) {
let agentResult;
try {
@ -65,6 +66,8 @@ const requestWrapper = (url, options) => {
}
logger.info(`Proxy for ${url}`);
}
// ua
let hasUA = false;
for (const header in options.headers) {
if (header.toLowerCase() === 'user-agent') {
@ -77,7 +80,21 @@ const requestWrapper = (url, options) => {
}
options.headers['user-agent'] = config.ua;
}
// server
options.headers.server = 'RSSHub';
// referer
if (!options.headers.referer && !options.headers.Referer) {
const protocol = url.match('https?://');
url = url.replace(/(https?:)?\/\//, '');
const domain = url
.split('.')
.slice(-2)
.join('.')
.split('/')[0];
options.headers.referer = (protocol ? protocol[0] : 'https://') + 'www.' + domain;
}
};
const httpWrap = (func) => {

View File

@ -35,14 +35,26 @@ afterEach(() => {
describe('got', () => {
it('headers', async () => {
nock('http://rsshub.test')
.get('/test')
.times(2)
.get(/.*/)
.times(3)
.reply(function() {
expect(this.req.headers.server).toBe('RSSHub');
expect(this.req.headers.referer).toBe('http://www.rsshub.test');
return [200, simpleResponse];
});
nock(/rsshub\.test/)
.get(/.*/)
.times(2)
.reply(function() {
expect(this.req.headers.referer).toBe('https://www.rsshub.test');
return [200, simpleResponse];
});
await got.get('http://rsshub.test/test');
await got.get('http://rsshub.test');
await got.get('rsshub.test/test');
await got.get('api.rsshub.test/test');
await parser.parseURL('http://rsshub.test/test');
});