Created
August 5, 2025 01:14
-
-
Save dimaqq/03a8f060718688658772f435d1337e1f 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" | |
| "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) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above is courtesy of chat gpt.