英文:
Go: Send websocket requests to a proxy port
问题
我有一个配置为代理HTTP和WebSocket请求的Envoy代理实例。请注意,这不是一个CONNECT代理。我希望我的WebSocket客户端创建一个WebSocket请求,就像它将其发送到原始目标一样,然后将有效负载传递给代理的监听器。
连接到本地代理的推荐方法是什么?我相信这取决于具体使用的Go WebSocket包。我可以看到一些包允许覆盖使用的http.Client,但目标地址是使用指定的WebSocket URL确定的。我唯一的替代方法是直接将请求发送到ws://proxy_ip:proxy_port/path
,并使用代理配置为路由的某个自定义HTTP头指定目标。我对这种方法不太喜欢。
英文:
I have an Envoy proxy instance configured to proxy http and websocket requests. Note that this is not a CONNECT proxy. I want my websocket client to create a websocket request as if it was sending it to the original destination and then deliver the payload to the proxy's listener instead.
What's the recommended way to connect to the local proxy? I believe this is dependent on the specific Go websocket package being used. I can see packages that allow overriding the http.Client used, but the destination address is determined using the websocket URL specified. The only alternative I have is to send the request to ws://proxy_ip:proxy_port/path
directly, and specify the destination using some custom HTTP header that the proxy is configured to use for routing. I am not a big fan of this approach.
答案1
得分: 1
使用Gorilla的Dialer.NetDialContext来拨号到与请求不同的地址:
d := websocket.Dialer{
NetDialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.DialContext(ctx, network, "proxy_ip:proxy_port")
},
}
c, r, err := d.Dial("ws://example.com/path", nil)
if err != nil {
// 处理错误
}
英文:
> I want to dial an address different from that in the request
Use Gorilla's Dialer.NetDialContext to dial an address different from the request:
d := websocket.Dialer{
NetDialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.DialContext(ctx, network, "proxy_ip:proxy_port")
},
}
c, r, err := d.Dial("ws://example.com/path", nil)
if err != nil {
// handle error
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论