Skip to content

Instantly share code, notes, and snippets.

@satrobit
Created August 2, 2019 16:31
Show Gist options
  • Save satrobit/0a43ad3c61132d05dc1660d1a7c757de to your computer and use it in GitHub Desktop.
Save satrobit/0a43ad3c61132d05dc1660d1a7c757de to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
)
func main() {
log.SetFlags(log.Lshortfile)
config := &tls.Config{
GetCertificate: returnCert,
}
ln, err := tls.Listen("tcp", ":443", config)
if err != nil {
log.Println(err)
return
}
defer ln.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello TLS!")
})
http.Serve(ln, nil)
}
func returnCert(helloInfo *tls.ClientHelloInfo) (*tls.Certificate, error) {
// helloInfo.ServerName ( This contains our Server Name )
cer, err := tls.LoadX509KeyPair("server.crt", "server.key")
if err != nil {
log.Println(err)
return nil, nil
}
return &cer, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment