Created
July 21, 2020 12:59
-
-
Save piksel/e3a8b8cffdac0d7513c722c6e29fdafd to your computer and use it in GitHub Desktop.
Go net/http default client proxy override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"log" | |
"net" | |
"net/http" | |
"net/url" | |
"time" | |
) | |
func main() { | |
const URL = "https://ip.p1k.se" | |
proxyurl, err := url.Parse("socks5://localhost:1337") | |
if err != nil { | |
log.Fatalf("Error parsing proxy URL: %q", err) | |
} | |
http.DefaultClient.Transport = &http.Transport{ | |
Proxy: http.ProxyURL(proxyurl), | |
DialContext: (&net.Dialer{ | |
Timeout: 30 * time.Second, | |
KeepAlive: 30 * time.Second, | |
}).DialContext, | |
ForceAttemptHTTP2: true, | |
MaxIdleConns: 100, | |
IdleConnTimeout: 90 * time.Second, | |
TLSHandshakeTimeout: 10 * time.Second, | |
ExpectContinueTimeout: 1 * time.Second, | |
} | |
log.Printf("Fetching response from %q", URL) | |
resp, err := http.Get(URL) | |
if err != nil { | |
log.Fatalf("Error fetching from remote: %v", err) | |
} | |
log.Printf("Got response status %s", resp.Status) | |
defer resp.Body.Close() | |
buffer, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatalf("Error reading response body: %v", err) | |
} | |
body := string(buffer) | |
log.Printf("Body: %q", body) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment