Skip to content

Instantly share code, notes, and snippets.

@mattfoster
Created February 16, 2017 08:44
Show Gist options
  • Save mattfoster/04ffe9366a0dc333e20d1c1a1d11c8d4 to your computer and use it in GitHub Desktop.
Save mattfoster/04ffe9366a0dc333e20d1c1a1d11c8d4 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"net/http/httptrace"
)
// transport is an http.RoundTripper that keeps track of the in-flight
// request and implements hooks to report HTTP tracing events.
type transport struct {
current *http.Request
}
// RoundTrip wraps http.DefaultTransport.RoundTrip to keep track
// of the current request.
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
t.current = req
return http.DefaultTransport.RoundTrip(req)
}
// GotConn prints whether the connection has been used previously
// for the current request.
func (t *transport) GotConn(info httptrace.GotConnInfo) {
fmt.Printf("Connection reused for %v? %v\n", t.current.URL, info.Reused)
c := info.Conn
if cc, ok := c.(*tls.Conn); ok {
log.Printf("%+v", cc)
}
}
func main() {
t := &transport{}
req, _ := http.NewRequest("GET", "https://google.com", nil)
trace := &httptrace.ClientTrace{
GotConn: t.GotConn,
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
client := &http.Client{Transport: t}
if _, err := client.Do(req); err != nil {
log.Fatal(err)
}
}
@mattfoster
Copy link
Author

And output:

$ ./google
Connection reused for https://google.com? false
2017/02/16 08:44:18 &{conn:0xc4200ec000 isClient:true handshakeMutex:{state:0 sema:0} handshakeCond:<nil> handshakeErr:<nil> vers:771 haveVers:true config:0xc4200f0000 handshakeComplete:true handshakes:1 didResume:false cipherSuite:49195 ocspResponse:[] scts:[[0 238 75 189 183 117 206 96 186 225 66 105 31 171 225 158 102 163 15 126 95 176 114 216 131 0 196 123 137 122 168 253 203 0 0 1 89 250 102 103 241 0 0 4 3 0 72 48 70 2 33 0 196 253 244 171 197 229 177 202 44 65 150 158 98 187 121 100 160 88 128 165 242 26 126 160 68 59 28 66 71 161 77 174 2 33 0 204 182 164 211 135 111 77 84 88 230 222 112 36 114 202 190 80 196 151 15 127 209 121 171 119 181 122 229 134 124 204 139] [0 172 59 154 237 127 169 103 71 87 21 158 109 125 87 86 114 249 217 129 0 148 30 155 222 255 236 161 49 59 117 120 45 0 0 1 89 250 102 102 49 0 0 4 1 1 0 28 97 144 115 66 236 103 132 31 147 151 239 163 202 90 61 148 27 151 243 83 69 210 75 211 131 5 205 17 249 175 62 179 192 162 122 213 170 102 72 210 135 173 107 130 119 23 121 149 219 60 50 54 46 132 192 129 114 202 243 97 74 254 36 77 235 93 224 36 205 31 120 197 88 33 167 142 206 174 98 125 108 4 239 204 186 144 73 132 203 214 61 217 178 204 187 162 1 33 95 150 148 211 87 115 70 151 166 191 208 193 143 172 174 105 47 77 118 27 4 67 80 186 5 106 97 250 83 2 177 126 9 126 204 155 197 14 7 37 82 151 36 193 126 208 136 135 163 248 179 89 2 46 174 201 41 114 254 121 61 124 191 116 54 74 171 96 221 112 194 53 41 44 252 209 81 26 57 87 85 98 8 126 143 147 203 182 181 56 39 0 192 69 210 104 121 71 78 185 162 198 105 112 168 225 243 67 169 4 224 58 77 163 241 58 222 232 25 129 136 20 46 114 244 108 59 184 176 75 61 151 144 113 89 183 221 236 144 235 18 66 224 64 132 211 220 140 51 147 90 187 99 10 245 189 240]] peerCertificates:[0xc420126000 0xc420126480 0xc420126900] verifiedChains:[[0xc420126000 0xc420126480 0xc42025d680]] serverName: secureRenegotiation:true clientFinishedIsFirst:true clientFinished:[119 37 46 41 52 151 83 211 222 154 199 187] serverFinished:[62 249 254 216 222 1 160 168 219 116 194 149] clientProtocol:h2 clientProtocolFallback:false in:{Mutex:{state:1 sema:0} err:<nil> version:771 cipher:0xc42038c540 mac:<nil> seq:[0 0 0 0 0 0 0 3] bfree:0xc4200ea0f0 additionalData:[0 0 0 0 0 0 0 2 23 3 3 0 13] nextCipher:<nil> nextMac:<nil> inDigestBuf:[] outDigestBuf:[]} out:{Mutex:{state:0 sema:0} err:<nil> version:771 cipher:0xc42038c480 mac:<nil> seq:[0 0 0 0 0 0 0 3] bfree:0xc4200ea090 additionalData:[0 0 0 0 0 0 0 2 23 3 3 0 9] nextCipher:<nil> nextMac:<nil> inDigestBuf:[] outDigestBuf:[]} rawInput:0xc4200ea0c0 input:<nil> hand:{buf:[20 0 0 12 62 249 254 216 222 1 160 168 219 116 194 149] off:16 runeBytes:[0 0 0 0] bootstrap:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] lastRead:2} buffering:false sendBuf:[] bytesSent:430 packetsSent:2 activeCall:0 tmp:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}
Connection reused for https://www.google.co.uk/?gfe_rd=cr&ei=YmalWP-gMaGN8QfD4p6wDg? false
2017/02/16 08:44:18 &{conn:0xc4200ec020 isClient:true handshakeMutex:{state:0 sema:0} handshakeCond:<nil> handshakeErr:<nil> vers:771 haveVers:true config:0xc42031c2c0 handshakeComplete:true handshakes:1 didResume:false cipherSuite:49199 ocspResponse:[] scts:[[0 164 185 9 144 180 24 88 20 135 187 19 162 204 103 112 10 60 53 152 4 249 27 223 184 227 119 205 14 200 13 220 16 0 0 1 89 250 18 177 125 0 0 4 3 0 71 48 69 2 32 113 233 248 3 183 201 225 92 29 114 62 205 96 33 65 171 201 232 59 86 252 226 135 149 51 48 67 0 195 212 201 242 2 33 0 213 121 19 220 84 7 31 117 170 240 119 1 93 240 55 176 208 190 1 26 83 237 156 90 17 77 145 216 19 248 48 122] [0 172 59 154 237 127 169 103 71 87 21 158 109 125 87 86 114 249 217 129 0 148 30 155 222 255 236 161 49 59 117 120 45 0 0 1 89 250 18 177 177 0 0 4 1 1 0 43 113 195 106 76 229 164 140 175 245 144 52 106 204 146 230 244 136 77 180 4 1 93 33 90 12 123 105 42 95 141 73 129 138 16 5 242 33 62 86 129 57 141 128 90 135 55 154 57 135 174 33 248 162 96 72 217 236 170 173 4 29 109 255 193 52 56 61 17 23 197 116 32 119 75 133 106 134 232 67 88 195 116 214 105 203 139 48 203 45 82 186 129 14 102 83 24 58 162 202 27 182 173 122 16 73 98 38 163 37 224 238 31 67 241 57 3 179 245 228 105 121 217 185 33 120 165 181 47 223 241 177 165 57 186 158 196 244 248 199 166 249 211 60 54 144 31 180 128 76 102 21 87 133 208 166 236 239 96 81 236 116 58 121 246 175 255 97 109 203 82 49 42 118 2 140 250 228 150 160 111 55 179 49 158 117 173 193 25 222 214 91 137 79 195 249 104 155 129 254 110 81 194 247 98 221 65 35 253 230 23 152 214 85 53 220 64 208 204 169 157 151 111 40 57 100 130 109 126 130 45 231 85 37 34 14 105 34 46 125 211 38 177 134 194 123 251 124 16 66 12 174 253 59 85 187]] peerCertificates:[0xc42038a900 0xc420375b00 0xc420360000] verifiedChains:[[0xc42038a900 0xc420375b00 0xc42025d680]] serverName: secureRenegotiation:true clientFinishedIsFirst:true clientFinished:[76 90 19 90 123 93 215 171 251 10 73 47] serverFinished:[3 37 94 170 252 226 252 30 116 55 200 110] clientProtocol:h2 clientProtocolFallback:false in:{Mutex:{state:1 sema:0} err:<nil> version:771 cipher:0xc42038d900 mac:<nil> seq:[0 0 0 0 0 0 0 3] bfree:0xc4203b2b40 additionalData:[0 0 0 0 0 0 0 2 23 3 3 0 13] nextCipher:<nil> nextMac:<nil> inDigestBuf:[] outDigestBuf:[]} out:{Mutex:{state:0 sema:0} err:<nil> version:771 cipher:0xc42038d8c0 mac:<nil> seq:[0 0 0 0 0 0 0 3] bfree:0xc4203b2ae0 additionalData:[0 0 0 0 0 0 0 2 23 3 3 0 9] nextCipher:<nil> nextMac:<nil> inDigestBuf:[] outDigestBuf:[]} rawInput:0xc4203b2b70 input:<nil> hand:{buf:[20 0 0 12 3 37 94 170 252 226 252 30 116 55 200 110] off:16 runeBytes:[0 0 0 0] bootstrap:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] lastRead:2} buffering:false sendBuf:[] bytesSent:436 packetsSent:2 activeCall:0 tmp:[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}

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