Hi Ryan,
You can get it via httptrace (https://blog.golang.org/http-tracing)
Example:
req, _ := http.NewRequest("GET", "http://example.com", nil)
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf("Got Conn: %s\n",
connInfo.Conn.LocalAddr().String()) <------------------------- This has the
local outgoing port
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
if _, err := http.DefaultTransport.RoundTrip(req); err != nil {
log.Fatal(err)
}
On Wednesday, October 7, 2020 at 9:09:54 AM UTC-7 [email protected] wrote:
> Is it possible to capture the outgoing port for a given HTTP request?
>
> I'm using a knockoff of ab that I wrote in go to send repeated requests to
> a given web service. Sometimes we get an error and I want to look at a
> packet trace of it. The problem is it's really hard to find one failed
> request in 1,000 in a tcp dump. If I can see the source port, that would
> help me narrow it down.
>
> The code I'm doing is effectively this (forgive any typos, this is a quick
> & dirty recopy, not a cut & paste):
>
> tlsConfig := &tls.Config{
> InsecureSkipVerify: true,
> }
>
> transport := &http.Transport{
> DisableKeepAlives: true,
> TLSClientCOnfig: tlsCOnfig,
> ResponseHeaderTimeout: time.Duration(headerTimeout) * time.Second,
> }
>
> client := &http.Client{
> Timeout: time.Duration(timeOut) * time.second,
> Transport: transport,
> }
>
> response, err :=client.Get(*targetURL) // How can I capture the
> outgoing port from this?
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/ae1b90e8-8562-47d1-92c8-801551f7a257n%40googlegroups.com.