-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (124 loc) · 3.36 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"github.com/spf13/viper"
)
const CONFIG_VAR_NAME = "CONFIG_PATH"
const DEFAULT_CONFIG_PATH = "config/dev.json"
func main() {
loadConfig()
log.Println("Started")
go startHttpServer()
go startTlsServer()
quitChannel := make(chan os.Signal)
signal.Notify(quitChannel, os.Interrupt, os.Kill)
// Wait for OS interrupt signal
<-quitChannel
log.Println("Stopped")
}
func loadConfig() {
configPath := os.Getenv(CONFIG_VAR_NAME)
if len(configPath) == 0 {
configPath = DEFAULT_CONFIG_PATH
}
viper.SetConfigFile(configPath)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Failed to read configuration: %v", err)
}
}
func startHttpServer() {
redirectAddress := viper.GetString("server.external_address")
redir := http.NewServeMux()
redir.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("https://%s", redirectAddress)
http.Redirect(w, r, url, http.StatusMovedPermanently)
})
server := http.Server{
Addr: fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.http_port")),
Handler: redir,
}
err := server.ListenAndServe()
if err != nil {
log.Fatalf("Failed to start redirect server: %v", err)
}
}
func startTlsServer() {
mux := http.NewServeMux()
mux.HandleFunc("/", rootHandler)
mux.HandleFunc("/json", jsonHandler)
server := http.Server{
Addr: fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.https_port")),
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: tls.RequestClientCert,
},
Handler: mux,
}
err := server.ListenAndServeTLS(
viper.GetString("server.cert"),
viper.GetString("server.key"))
if err != nil {
log.Fatalf("Failed to start TLS server: %v", err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
if len(r.TLS.PeerCertificates) == 0 {
w.WriteHeader(http.StatusPreconditionRequired)
content, err := ioutil.ReadFile("content/nocert.html")
if err != nil {
log.Fatalf("Failed to load content: %v", err)
}
w.Write([]byte(content))
return
}
c := FromX509Certificate(r.TLS.PeerCertificates[0])
t, err := template.ParseFiles("content/success.tpl.html")
if err != nil {
log.Fatalf("Failed to load template: %v", err)
}
p := Page{
Title: "Mutual TLS Echo",
Cert: c,
JsonEndpoint: fmt.Sprintf("https://%s/json", viper.GetString("server.external_address")),
}
err = t.Execute(w, p)
if err != nil {
log.Fatalf("Failed to render template: %v", err)
}
}
func jsonHandler(w http.ResponseWriter, r *http.Request) {
setCommonHeaders(w)
w.Header().Set("content-type", "application/json")
if len(r.TLS.PeerCertificates) == 0 {
w.WriteHeader(http.StatusPreconditionRequired)
content := `{
"error": "Client certificate not provided.",
"help": "https://github.com/abliqo/mtls-echo-doc"
}`
w.Write([]byte(content))
return
}
c := FromX509Certificate(r.TLS.PeerCertificates[0])
jsonBytes, err := json.Marshal(c)
if err != nil {
log.Fatalf("Failed to convert certificate to json: %v", err)
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(jsonBytes)
if err != nil {
log.Printf("Failed to write response: %v", err)
}
}
func setCommonHeaders(w http.ResponseWriter) {
w.Header().Set("cache-control", "no-cache")
w.Header().Set("server", "mtls-echo")
}