Created
May 15, 2024 20:33
-
-
Save bernardoVale/e07f64ae807e2635a7925af942943178 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" | |
"flag" | |
"fmt" | |
"io" | |
"net" | |
"net/http" | |
"time" | |
) | |
var ( | |
address string | |
) | |
func main() { | |
flag.StringVar(&address, "address", "208.76.14.119:0", "address") | |
flag.Parse() | |
addr, err := net.ResolveTCPAddr("tcp", address) | |
if err != nil { | |
panic(err) | |
} | |
dialer := &net.Dialer{LocalAddr: addr} | |
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) { | |
conn, err := dialer.Dial(network, addr) | |
return conn, err | |
} | |
client := &http.Client{ | |
Timeout: time.Second * 5, | |
Transport: &http.Transport{ | |
DialContext: dialContext, | |
}, | |
} | |
resp, err := client.Get("https://checkip.amazonaws.com") | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("status: %s\n", resp.Status) | |
body, err := io.ReadAll(resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
_ = resp.Body.Close() | |
fmt.Printf(string(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment