-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.go
181 lines (149 loc) · 4.4 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"encoding/json"
http "github.com/Noooste/fhttp"
"github.com/Noooste/simplecert"
"github.com/Noooste/tlsconfig"
"log"
"os"
"regexp"
"time"
)
var numberReg = regexp.MustCompile(`/(\d+)$`)
type Config struct {
Domain string `json:"domain"`
}
func main() {
//load config.json
configFile, err := os.Open("config.json")
if err != nil {
log.Fatal(err)
}
defer configFile.Close()
//decode config.json
var config Config
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
//handle
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Location", "/get")
res.WriteHeader(302)
})
http.HandleFunc("/get", showTLS)
http.HandleFunc("/redirect/", redirectNTimes)
http.HandleFunc("/delay/", delayResponse)
http.HandleFunc("/cookie", getCookie)
if config.Domain != "localhost" {
startServerWithDomain(config.Domain)
} else {
startServer()
}
}
func serve(ctx context.Context, srv *http.Server) {
// lets go
go func() {
if err := srv.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %+s\n", err)
}
}()
log.Printf("server started")
<-ctx.Done()
log.Printf("server stopped")
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
err := srv.Shutdown(ctxShutDown)
if err == http.ErrServerClosed {
log.Printf("server exited properly")
} else if err != nil {
log.Printf("server encountered an error on exit: %+s\n", err)
}
}
func startServerWithDomain(domain string) {
go (&http.Server{
Addr: ":80",
Handler: nil,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}).ListenAndServe()
var domains = []string{
domain,
}
var (
// the structure that handles reloading the certificate
certReloader *simplecert.CertReloader
err error
numRenews int
ctx, cancel = context.WithCancel(context.Background())
// init strict tlsConfig (this will enforce the use of modern TLS configurations)
// you could use a less strict configuration if you have a customer facing web application that has visitors with old browsers
tlsConf = tlsconfig.NewServerTLSConfig(tlsconfig.TLSModeServerStrict)
)
// a simple constructor for a http.Server with our Handler
makeServer := func() *http.Server {
return &http.Server{
Addr: ":443",
Handler: nil,
TLSConfig: tlsConf,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
}
// init server
srv := makeServer()
// init simplecert configuration
cfg := simplecert.Default
// configure
cfg.Domains = domains
cfg.CacheDir = "letsencrypt"
cfg.SSLEmail = "no-email-please@sap.alice-and-bob.xyz"
// this function will be called just before certificate renewal starts and is used to gracefully stop the service
// (we need to temporarily free port 443 in order to complete the TLS challenge)
cfg.WillRenewCertificate = func() {
// stop server
cancel()
}
// this function will be called after the certificate has been renewed, and is used to restart your service.
cfg.DidRenewCertificate = func() {
numRenews++
// restart server: both context and server instance need to be recreated!
ctx, cancel = context.WithCancel(context.Background())
srv = makeServer()
// force reload the updated cert from disk
certReloader.ReloadNow()
// here we go again
go serve(ctx, srv)
}
// init simplecert configuration
// this will block initially until the certificate has been obtained for the first time.
// on subsequent runs, simplecert will load the certificate from the cache directory on disk.
certReloader, err = simplecert.Init(cfg, func() {
os.Exit(0)
})
if err != nil {
log.Fatal("simplecert init failed: ", err)
}
// enable hot reload
tlsConf.GetCertificate = certReloader.GetCertificateFunc()
log.Println("server started on port " + srv.Addr)
serve(ctx, srv)
<-make(chan bool)
}
func startServer() {
serv := &http.Server{
Addr: ":443",
Handler: nil,
TLSConfig: tlsconfig.NewServerTLSConfig(tlsconfig.TLSModeServerStrict),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
go func() {
if err := serv.ListenAndServeTLS("security/fullchain.pem", "security/privkey.pem"); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %+s\n", err)
}
}()
log.Printf("server started on port 443")
<-make(chan bool)
}