From cd6b5e0f0b87b46e4199c23d455e832712d08e63 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sun, 23 Jul 2023 21:28:00 +0100 Subject: [PATCH] feat: bypass reverse proxy for requests with cookies --- docs/en/install/README.md | 6 ++++++ docs/install/README.md | 6 ++++++ lib/utils/request-wrapper.js | 6 ++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/en/install/README.md b/docs/en/install/README.md index dcdead4b8..53e95dcd3 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -565,6 +565,12 @@ resolved by the SOCKS server, recommanded, prevents DNS poisoning or DNS leak), ### Reverse proxy +::: warning + +This proxy method cannot proxy requests that contain cookies. + +::: + `REVERSE_PROXY_URL`: Reverse proxy URL, RSSHub will use this URL as a prefix to initiate requests, for example `https://proxy.example.com?target=`, requests to `https://google.com` will be automatically converted to `https://proxy.example.com?target=https%3A%2F%2Fgoogle.com` You can use Cloudflare Workers to build a simple reverse proxy, for example: diff --git a/docs/install/README.md b/docs/install/README.md index 99ff95a8f..818abce2c 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -585,6 +585,12 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式 ### 反向代理 +::: warning 注意 + +这种代理方式无法代理包含 cookie 的请求。 + +::: + `REVERSE_PROXY_URL`: 反向代理地址,RSSHub 将会使用该地址作为前缀来发起请求,例如 `https://proxy.example.com/?target=`,对 `https://google.com` 发起的请求将被自动转换为 `https://proxy.example.com/?target=https%3A%2F%2Fgoogle.com` 你可以使用 Cloudflare Workers 来搭建一个简易的反向代理,例如: diff --git a/lib/utils/request-wrapper.js b/lib/utils/request-wrapper.js index 58a54b50e..46c35f8bb 100644 --- a/lib/utils/request-wrapper.js +++ b/lib/utils/request-wrapper.js @@ -40,8 +40,10 @@ if (agent) { }; } else if (config.reverseProxyUrl) { proxyWrapper = (url, options) => { - if (!((options.url || url) + '').startsWith(config.reverseProxyUrl)) { - options.url = new URL(`${config.reverseProxyUrl}${encodeURIComponent(options.url || url)}`); + const urlIn = options.url.toString() || url; + const proxyRegex = new RegExp(proxyObj.url_regex); + if (proxyRegex.test(urlIn) && !urlIn.startsWith(config.reverseProxyUrl) && !options.cookieJar) { + options.url = new URL(`${config.reverseProxyUrl}${encodeURIComponent(urlIn)}`); return true; } return false;