grpc-go 如何为每个连接单独设置代理

grpc-go 写的真的垃圾, 找了大半天都没有找到怎么为每个连接单独设置代理的示例

共 1 个回复


shynome

找到了

package main

import (
    "fmt"
    "net"
    "net/url"
    "time"

    "golang.org/x/net/proxy"

    "google.golang.org/grpc"
)

func main(){
  proxyOption = grpc.WithDialer(func(addr string, timeout time.Duration) (conn net.Conn, err error) {
    var dialer = &net.Dialer{Timeout: timeout}
    proxyURLstr := fmt.Sprintf("socks5://%v:%v", "127.0.0.1", "1080")
    proxyURL, err := url.Parse(proxyURLstr)
    if err != nil { return }
    connProxy, err := proxy.FromURL(proxyURL, dialer)
    if err != nil { return }
    return connProxy.Dial("tcp", addr)
  })
  cc, err := grpc.Dial(addr, proxyOption, grpc.WithInsecure())
}
# 0