Go语言 强制代理服务器将域解析为自定义IP

92dk7w1h  于 2023-06-19  发布在  Go
关注(0)|答案(1)|浏览(136)

我有一个goproxy(github.com/elazarl/goproxy)服务器在本地运行

proxy := goproxy.NewProxyHttpServer()
http.ListenAndServe(":4242", proxy)

我通过它代理我的请求,对于一些域,我想解决他们的自定义IP为http和https请求
我尽力了

proxy.OnRequest().HandleConnectFunc(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
    // Allow the connection to proceed for specific hosts
    if host == "mydomain.com" {
        host = "myip:443"
        ctx.Req.URL.Host = host
        ctx.Req.Host = host
    }
    return goproxy.MitmConnect, host
})

这将给予我这个错误与curl(没有--insecure标志)

curl failed to verify the legitimacy of the server and therefore could not 
establish a secure connection to it. To learn more about this situation and 
how to fix it, please visit the web page mentioned above.

而不是浏览器。
还有其他的方法吗
编辑1.尝试了一个服务器,其证书将被我的证书存储信任,在浏览器x1c 0d1x中得到了这个
尝试修改net.DefaultResolver并将dns解析程序更改为自定义服务器

net.DefaultResolver = &net.Resolver{
    PreferGo: true,
    Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
        // Use the default address resolution for other hosts
        fmt.Println("domaIN", network, address)
        return (&net.Dialer{}).DialContext(ctx, network, "127.0.0.1:53")
    },
}

但这也只适用于http请求我可以改变代理传输的DialContext,但这也只适用于http请求& DialTLSContext不适用于代理请求。
我想知道我是否可以让这个工作的代理https请求太多,或者是一个改变/etc/hosts是我唯一的选择。

4nkexdtk

4nkexdtk1#

你没有标题中提到的问题,即。设置其他目标IP地址。相反,您遇到的问题是,该主机随后返回的证书无法被客户端验证-可能是因为它不是由客户端信任的CA颁发的。
这是一个需要在每个客户端(信任发布CA的CA)或服务器(使用由客户端信任的CA发布的证书)上解决的问题,但不能在代理上解决。否则中间人的攻击会很容易。

相关问题