-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmain.go
59 lines (48 loc) · 1.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
)
var domain = flag.String("domain", "localhost", "Provide the domain name for the server")
var mtls = flag.Bool("mtls", false, "Enable Mutual authentication")
func main() {
flag.Parse()
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "text/plain")
fmt.Fprint(rw, "Hello World")
})
server := &http.Server{Addr: ":8443"}
if *mtls {
server = createServerWithMTLS()
}
// Start the server loading the certificate and key
err := server.ListenAndServeTLS("3_application/certs/"+*domain+".cert.pem", "3_application/private/"+*domain+".key.pem")
if err != nil {
log.Fatal("Unable to start server", err)
}
}
func createServerWithMTLS() *http.Server {
// Add the cert chain as the intermediate signs both the servers and the clients certificates
clientCACert, err := ioutil.ReadFile("2_intermediate/certs/ca-chain.cert.pem")
if err != nil {
log.Fatal(err)
}
clientCertPool := x509.NewCertPool()
clientCertPool.AppendCertsFromPEM(clientCACert)
tlsConfig := &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: clientCertPool,
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
}
tlsConfig.BuildNameToCertificate()
return &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
}
}