my struggle

i was trying to implement proxy support in emp3r0r, but found that http2.Transport has no such option. the only option that might work is replacing DialTLS() function with a custom one

i did a lot of research:

the Github issues are still open, i assumed they were not solved yet

thus the dialer hack:

// http2 transport
tr := &http2.Transport{TLSClientConfig: config}

// TODO use a socks5 proxy
// currently it's a hack, as there's no official way of configuring a proxy for HTTP2
if proxyServer != "" {
    dialer, err := proxy.SOCKS5("tcp", proxyServer, nil, proxy.Direct)
    proxyDialer := func(network string, addr string, cfg *tls.Config) (c net.Conn, e error) {
        c, e = dialer.Dial(network, addr) // this is a TCP dialer, thus no TLS, not usable
        return
    }
    if err != nil {
        log.Printf("failed to set proxy: %v", err)
    }
    tr.DialTLS = proxyDialer
}

since proxyDialer is merely a plain TCP dialer, it doesn't automatically give us a TLS session, this approach will never work

i though about implement TLS myself, but it's not elegant at all

finally i paid attention to godoc, and found the ConfigureTransport function

yes this means http2 is just an extension of http, if you want to use things like proxy in transport, you use http(1.1) transport, and ConfigureTransport(t1) to make it http2

oh... who would know?

now looks like those issues on Github have been solved already, its just they dont have any documentation

so im giving one

solution

tr := &http.Transport{TLSClientConfig: config}

// use a socks5 proxy
if proxyServer != "" {
    proxyUrl, err := url.Parse(proxyServer)
    if err != nil {
        log.Printf("Invalid proxy: %v", err)
    }
    tr.Proxy = http.ProxyURL(proxyUrl)
}
err := http2.ConfigureTransport(tr) // upgrade to HTTP2, while keeping http.Transport
if err != nil {
    log.Fatalf("Cannot switch to HTTP2: %v", err)
}

Comments

comments powered by Disqus