Skip to content

Instantly share code, notes, and snippets.

@dimaqq
Created August 5, 2025 01:14
Show Gist options
  • Select an option

  • Save dimaqq/03a8f060718688658772f435d1337e1f to your computer and use it in GitHub Desktop.

Select an option

Save dimaqq/03a8f060718688658772f435d1337e1f to your computer and use it in GitHub Desktop.
package main
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
)
func main() {
client := &http.Client{
Transport: &http.Transport{
// 1. Base dialer
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
// addr is "hostname:port"
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
// 2. If this is our FQDN, swap in the custom IP
if host == "api.example.com" {
host = "203.0.113.42" // your override IP
}
d := &net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}
return d.DialContext(ctx, network, net.JoinHostPort(host, port))
},
// 3. Make sure TLS uses the real hostname for SNI & cert checks
TLSClientConfig: &tls.Config{
ServerName: "api.example.com",
},
},
}
req, _ := http.NewRequest("GET", "https://api.example.com/hello", nil)
// 4. (Optional) ensure the HTTP Host header is correct
req.Host = "api.example.com"
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Status:", resp.Status)
}
@dimaqq
Copy link
Author

dimaqq commented Aug 5, 2025

The code above is courtesy of chat gpt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment