V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
oneisall8955
V2EX  ›  问与答

查找一款请求代理工具

  •  
  •   oneisall8955 · 2023-01-14 14:56:56 +08:00 · 816 次点击
    这是一个创建于 484 天前的主题,其中的信息可能已经有所发展或是发生改变。
    场景: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 小白,不太懂配置=_=
    第 1 条附言  ·  2023-01-14 19:07:40 +08:00
    https://github.com/netptop/siteproxy
    该项目可以,项目的 cf 的 workers 方式不支持 post 请求,但是用 vpc 方式配合域名才行
    5 条回复    2023-05-05 10:43:14 +08:00
    totoro625
        1
    totoro625  
       2023-01-14 15:00:53 +08:00
    工具的话试试这个: https://github.com/netptop/siteproxy
    一定要是 b.host.com 吗?
    oneisall8955
        2
    oneisall8955  
    OP
       2023-01-14 15:17:32 +08:00
    @totoro625 #1 感谢回复,我试试,感觉可以满足
    oneisall8955
        3
    oneisall8955  
    OP
       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
        4
    oneisall8955  
    OP
       2023-01-15 17:13:33 +08:00
    @oneisall8955 #3 方法名打错,方法名是 handleRespond ,不是 handleResponse
    oneisall8955
        5
    oneisall8955  
    OP
       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)
    }
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1874 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:30 · PVG 00:30 · LAX 09:30 · JFK 12:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.