Skip to content

Instantly share code, notes, and snippets.

@dlsniper
Created March 29, 2015 11:19
Show Gist options
  • Save dlsniper/9792c842255b46dcece2 to your computer and use it in GitHub Desktop.
Save dlsniper/9792c842255b46dcece2 to your computer and use it in GitHub Desktop.
HTTPS server in go using COMODO Wildcard SSL certificate (exported as nginx bundle)
package main
import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
mr "math/rand"
"net/http"
_ "net/http/pprof"
"runtime"
"time"
"github.com/gorilla/mux"
)
var startTime time.Time
func init() {
startTime = time.Now()
runtime.GOMAXPROCS(runtime.NumCPU())
mr.Seed(time.Now().UTC().UnixNano())
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
w.Write([]byte(`Hello world!`))
})
server := &http.Server{
Addr: conf.ListenHostPort,
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
server.TLSConfig = configTLS()
log.Printf("Starting SSL server at \"%s\" in %s", conf.ListenHostPort, time.Now().Sub(startTime))
log.Fatal(server.ListenAndServeTLS("./cert/path/here.pem", "./cert/path/here.key"))
}
func configTLS() *tls.Config {
TLSConfig := &tls.Config{}
TLSConfig.CipherSuites = []uint16{
tls.TLS_FALLBACK_SCSV,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
}
TLSConfig.Rand = rand.Reader
TLSConfig.MinVersion = tls.VersionTLS10
TLSConfig.SessionTicketsDisabled = false
TLSConfig.InsecureSkipVerify = false
TLSConfig.ClientAuth = tls.VerifyClientCertIfGiven
TLSConfig.PreferServerCipherSuites = true
TLSConfig.ClientSessionCache = tls.NewLRUClientSessionCache(1000)
TLSConfig.RootCAs = loadCertificates()
return TLSConfig
}
func loadCertificates() *x509.CertPool {
pem, err := ioutil.ReadFile("./cert/path/here.ca-bundle")
if err != nil {
panic(err)
}
rootCertPool := x509.NewCertPool()
if !rootCertPool.AppendCertsFromPEM(pem) {
panic("Failed appending certs")
}
return rootCertPool
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment