Last active
February 27, 2025 09:47
-
-
Save poolski/fc7bf93c0e269e7c99e3b5141cc36f60 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"context" | |
"crypto/tls" | |
"fmt" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
"time" | |
"github.com/geckoboard/goutils/net/safedialer" | |
) | |
const ( | |
dialTimeout = 10 * time.Second | |
) | |
func main() { | |
url := os.Args[1] | |
client := newHTTPClient() | |
resp, err := client.Get(url) | |
if err != nil { | |
log.Fatalf("Request failed: %v", err) | |
} | |
defer resp.Body.Close() | |
fmt.Printf("Response status: %s\n", resp.Status) | |
} | |
func newHTTPClient() *http.Client { | |
dialer := safedialer.New("8.8.8.8:53", dialTimeout) | |
// Wrap the dialer to log DNS errors | |
wrappedDialer := func(ctx context.Context, network, addr string) (net.Conn, error) { | |
conn, err := dialer(ctx, network, addr) | |
if err != nil { | |
log.Printf("Dial error: %v (network: %s, addr: %s)", err, network, addr) | |
} | |
return conn, err | |
} | |
transport := &http.Transport{ | |
DialContext: wrappedDialer, | |
TLSClientConfig: &tls.Config{ | |
InsecureSkipVerify: true, | |
}, | |
DisableKeepAlives: true, | |
} | |
return &http.Client{ | |
Transport: transport, | |
Timeout: 15 * time.Second, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment