feat: support reverse proxy

This commit is contained in:
DIYgod
2023-07-23 20:28:38 +01:00
parent 7856617044
commit 3a5d293b7f
6 changed files with 111 additions and 12 deletions
+30 -1
View File
@@ -542,7 +542,7 @@ RSSHub supports two caching methods: memory and redis
Partial routes have a strict anti-crawler policy, and can be configured to use proxy.
Proxy can be configured via either **Proxy URI** or **Proxy options**. When both are configured, RSSHub will use the configuration in **Proxy URI**.
Proxy can be configured through **Proxy URI**, **Proxy options**, or **Reverse proxy**.
#### Proxy URI
@@ -563,6 +563,35 @@ resolved by the SOCKS server, recommanded, prevents DNS poisoning or DNS leak),
> - `http://user:pass@127.0.0.1:8080`
> - `https://127.0.0.1:8443`
### Reverse proxy
`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:
```js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
let target = url.searchParams.get('target')
if (!target) {
return new Response('Hello, this is Cloudflare Proxy Service. To proxy your requests, please use the "target" URL parameter.')
} else {
target = decodeURIComponent(target)
const newRequest = new Request(target, {
headers: request.headers,
method: request.method,
body: request.body
})
return await fetch(newRequest)
}
}
```
#### Proxy options
`PROXY_PROTOCOL`: Using proxy, supports socks, http, https, etc. See [socks-proxy-agent](https://www.npmjs.com/package/socks-proxy-agent) NPM package page and [source](https://github.com/TooTallNate/proxy-agents/blob/63adbcefdb4783cc67c0eb90200886b4064e8639/packages/socks-proxy-agent/src/index.ts#L81) for what these protocols mean. See also [cURL OOTW: SOCKS5](https://daniel.haxx.se/blog/2020/05/26/curl-ootw-socks5/) for reference.
+30 -1
View File
@@ -551,7 +551,7 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
部分路由反爬严格,可以配置使用代理抓取。
可通过**代理 URI**或**代理选项**两种方式来配置代理,当两种配置方式同时被设置时,RSSHub 将会使用**代理 URI**中的配置。
可通过**代理 URI**或**代理选项**或**反向代理**三种方式来配置代理。
#### 代理 URI
@@ -583,6 +583,35 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
`PROXY_URL_REGEX`: 启用代理的 URL 正则表达式,默认全部开启 `.*`
### 反向代理
`REVERSE_PROXY_URL`: 反向代理地址,RSSHub 将会使用该地址作为前缀来发起请求,例如 `https://proxy.example.com/?target=`,对 `https://google.com` 发起的请求将被自动转换为 `https://proxy.example.com/?target=https%3A%2F%2Fgoogle.com`
你可以使用 Cloudflare Workers 来搭建一个简易的反向代理,例如:
```js
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
let target = url.searchParams.get('target')
if (!target) {
return new Response('Hello, this is Cloudflare Proxy Service. To proxy your requests, please use the "target" URL parameter.')
} else {
target = decodeURIComponent(target)
const newRequest = new Request(target, {
headers: request.headers,
method: request.method,
body: request.body
})
return await fetch(newRequest)
}
}
```
### 用户认证
`protected_route.js` 内的路由将启用 HTTP Basic Authentication 认证