Created
May 19, 2023 21:46
-
-
Save imduffy15/f0ac332bca1413d0dc7bc4a0e61e9794 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"crypto/tls" | |
"log" | |
"net" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
) | |
func main() { | |
proxy := httputil.NewSingleHostReverseProxy(&url.URL{ | |
Scheme: "https", | |
Host: "sha256.badssl.com", | |
}) | |
// Set a custom DialTLS to access the TLS connection state | |
proxy.Transport = &http.Transport{DialTLS: dialTLS} | |
// Change req.Host so badssl.com host check is passed | |
director := proxy.Director | |
proxy.Director = func(req *http.Request) { | |
director(req) | |
req.Host = req.URL.Host | |
} | |
log.Fatal(http.ListenAndServe("127.0.0.1:3000", proxy)) | |
} | |
func dialTLS(network, addr string) (net.Conn, error) { | |
conn, err := net.Dial(network, addr) | |
if err != nil { | |
return nil, err | |
} | |
host, _, err := net.SplitHostPort(addr) | |
if err != nil { | |
return nil, err | |
} | |
cfg := &tls.Config{ServerName: host} | |
tlsConn := tls.Client(conn, cfg) | |
if err := tlsConn.Handshake(); err != nil { | |
conn.Close() | |
return nil, err | |
} | |
cs := tlsConn.ConnectionState() | |
cert := cs.PeerCertificates[0] | |
// Verify here | |
cert.VerifyHostname(host) | |
log.Println(cert.Subject) | |
return tlsConn, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment