查找一款请求代理工具

2023-01-14 14:56:56 +08:00
 oneisall8955
场景:A 主机访问不了墙外,但是 B 主机可以,A ,B 主机不是同一个内网,A 可通过域名访问 B 的 nginx (b.host.com)

墙外有一个接口,如: https://api.foo.com/bar

A 主机发起请求 POST https://api.foo.com/bar?baz=quz request body={"name":"hello"} 被墙了。
B 主机发起请求是可以的

有没有这样的工具,类似工具: https://ghproxy.com

B 启动服务,A 请求 http://b.host.com/proxy/https://api.foo.com/bar?baz=quz request body={"name":"hello"}

这时候,B 通过 nginx ,获取出 /proxy/ 后面的 url ,再通过代理工具进行转发,响应给 A ?

有大佬知道这样的工具吗?
或者,不用代理工具,通过 B nginx 配置 rewrite 和 pass_proxy 也达到这种目的?

PS:nginx 小白,不太懂配置=_=
821 次点击
所在节点    问与答
5 条回复
totoro625
2023-01-14 15:00:53 +08:00
工具的话试试这个: https://github.com/netptop/siteproxy
一定要是 b.host.com 吗?
oneisall8955
2023-01-14 15:17:32 +08:00
@totoro625 #1 感谢回复,我试试,感觉可以满足
oneisall8955
2023-01-15 17:12:05 +08:00
@oneisall8955 #2

在使用过程中,会发现 siteproxy 会劫持 response body ,进行内容修改,主要是将内容包含链接的全替换成 b 域名前缀。

如 b 的 siteproxy 服务是 https://siteproxy.bhost.com

请求原始 response body 是 {"url":"https://www.baidu.com?q=siteproxy"}

则在 siteproxy 进行替换成 {"url":"https://siteproxy.bhost.com/https/www.baidu.com?q=siteproxy"}

这可能不是 A 服务想要的。

通过修改 siteproxy 源码解决,修改项目的 Proxy.js ,注释掉 handleResponse 方法中的过滤替换代码即可。

如下:

```js
//# for(let key in regReplaceMap) {
//# myRe = new RegExp(key, 'g') // match group
//# body = body.replace(myRe, regReplaceMap[key])
//# }
//# logSave(`##### host:${host}`)
//# if (host) {
//# body = pathReplace({host, httpType, body}) //13ms
//# }
//# logSave(`2`)
//# logSave(`3`)
//# myRe = new RegExp(`/${httpType}/${host}/${httpType}/${host}/`, 'g') // match group
//# body = body.replace(myRe, `/${httpType}/${host}/`)
//#
//# logSave(`4`) //1ms
//# // put siteSpecificReplace at end
//# Object.keys(siteSpecificReplace).forEach( (site) => {
//# if (!req.url) {
//# return
//# }
//# if (req.url.indexOf(site) !== -1 || (req.headers['referer'] && req.headers['referer'].indexOf(site) !== -1)) {
//# keys = Object.keys(siteSpecificReplace[site])
//# keys.forEach( key => {
//# myRe = new RegExp(key, 'g') // match group
//# body = body.replace(myRe, siteSpecificReplace[site][key])
//# })
//# }
//# }) //17ms
//#
//# logSave(`5`)
//# if (gbFlag) {
//# body = iconv.encode(body, 'gbk')
//# }
//# // googlevideo.com manual redirection
//# if (typeof(body) === 'string' && body.startsWith(`${httpprefix}://${serverName}`) && body.indexOf('googlevideo.com') !== -1) {
//# // need to manually redirect it for youtube workaround.
//# console.log(`============== redirect googlevideo.com`)
//# try {
//# res.setHeader('location', body) //0ms
//# } catch(e) {
//# logSave(`error: ${e}`)
//# return
//# }
//# res.statusCode = '302'
//# }
//# logSave(`5 after replacment,displayed string, body.length:${body.length}`)

```
oneisall8955
2023-01-15 17:13:33 +08:00
@oneisall8955 #3 方法名打错,方法名是 handleRespond ,不是 handleResponse
oneisall8955
2023-05-05 10:43:14 +08:00
最近发现,用 https://github.com/hunshcn/gh-proxy 仓库,搭配 cloudflare 的 worker ,自定义域名,修改其中的代码,也可以完成类似的需求:

```js
const customProxyDomainArray=[
// 代理所有的 http 或 https 开头的东西
/^(?:https?:\/\/).*$/i,
]

function checkCustomUrl(u) {
for (let i of customProxyDomainArray ) {
if (u.search(i) === 0) {
return true
}
}
return false
}

async function fetchHandler(e) {
// ... 省略
else if (checkCustomUrl(path)) {
return httpHandler(req, path)
} else {
return fetch(ASSET_URL + path)
}
}
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/908902

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX