-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (48 loc) · 1.21 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"
"flag"
"log"
"net/http"
)
func main() {
confFile := flag.String("c", "config.json", "Config")
flag.Parse()
conf, err := NewConfig(*confFile)
if err != nil {
log.Fatalln(err)
}
tlsConfig := tls.Config{}
// Load certs
for host, route := range conf.Routes {
log.Printf("Route %s -> %s\n", host, route.Host)
var (
cert tls.Certificate
err error
)
if route.CertPEM != "" && route.KeyPEM != "" {
cert, err = tls.X509KeyPair([]byte(route.CertPEM), []byte(route.KeyPEM))
} else {
cert, err = tls.LoadX509KeyPair(route.CertFile, route.KeyFile)
}
if err != nil {
log.Fatalln(err)
}
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
}
tlsConfig.BuildNameToCertificate()
server := http.Server{
Addr: conf.Listen,
Handler: NewProxy(conf.Routes, conf.DefaultHost),
TLSConfig: &tlsConfig,
}
// Redirect to https
go http.ListenAndServe(conf.HttpsRedirectorListen, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
url := *req.URL
url.Host = req.Host
url.Scheme = "https"
http.Redirect(w, req, url.String(), http.StatusMovedPermanently)
}))
// Main server
log.Fatalln(server.ListenAndServeTLS("", ""))
}