Skip to content

Instantly share code, notes, and snippets.

@richzw
Created July 12, 2022 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richzw/c2beeaaae5c6e14faca9de4c321b9a50 to your computer and use it in GitHub Desktop.
Save richzw/c2beeaaae5c6e14faca9de4c321b9a50 to your computer and use it in GitHub Desktop.
func main() {
addr := flag.String("addr", ":4000", "HTTPS network address")
certFile := flag.String("certfile", "cert.pem", "certificate PEM file")
keyFile := flag.String("keyfile", "key.pem", "key PEM file")
flag.Parse()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
})
w, err := os.OpenFile("/keypath/https-key.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
fmt.Printf("failed to open file err %+v", err)
return
}
cs := make([]uint16, len(cipherSuites))
copy(cs, cipherSuites)
var tlsCfg tls.Config
tlsCfg.Certificates = make([]tls.Certificate, 1)
tlsCfg.Certificates[0], err = tls.LoadX509KeyPair(*certFile, *keyFile)
tlsCfg.NextProtos = []string{"h2"}
tlsCfg.ClientAuth = tls.RequestClientCert
tlsCfg.SessionTicketsDisabled = true
tlsCfg.InsecureSkipVerify = true
tlsCfg.KeyLogWriter = w
tlsCfg.MinVersion = tls.VersionTLS12
tlsCfg.CipherSuites = cs
tlsCfg.PreferServerCipherSuites = true
srv := &http.Server{
Addr: *addr,
Handler: mux,
TLSConfig: &tlsCfg,
}
log.Printf("Starting server on %s", *addr)
err = srv.ListenAndServeTLS("", "")
log.Fatal(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment