-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (60 loc) · 1.78 KB
/
main.go
File metadata and controls
69 lines (60 loc) · 1.78 KB
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
package main
import (
"errors"
"net/http"
"os"
"time"
)
func main() {
cfg, err := loadConfig()
if err != nil {
logf("%v", err)
os.Exit(1)
}
tlsConfig, err := LoadTLSCredentials(cfg.SSLCert, cfg.SSLKey)
if err != nil {
logf("failed to load TLS credentials: %v", err)
os.Exit(1)
}
info, err := checkCertificate(cfg.SSLCert)
if err != nil {
logf("Certificate validation failed for %s: %v", cfg.SSLCert, err)
} else {
switch {
case info.SelfSigned:
logf("Certificate is self-signed: %s", cfg.SSLCert)
default:
logf("Certificate appears to be CA-signed: %s", cfg.SSLCert)
}
if !info.NotAfter.IsZero() {
now := time.Now()
if now.After(info.NotAfter) {
logf("Warning: Certificate has expired: %s", cfg.SSLCert)
logf("Expiration: %s", info.NotAfter.Format(time.RFC3339))
} else {
days := int(info.NotAfter.Sub(now).Hours() / 24)
logf("Certificate valid for %d more days (expires: %s)", days, info.NotAfter.Format(time.RFC3339))
}
}
}
logf("TLS enabled on port %s with cert=%s key=%s", cfg.APIPort, cfg.SSLCert, cfg.SSLKey)
logf("API key protection enabled")
s := &server{cfg: cfg}
mux := http.NewServeMux()
mux.HandleFunc("GET /", s.handleRoot)
mux.HandleFunc("POST /node/update", s.handleUpdate)
mux.HandleFunc("POST /node/core_update", s.handleCoreUpdate)
mux.HandleFunc("POST /node/geofiles", s.handleGeofiles)
httpServer := &http.Server{
Addr: ":" + cfg.APIPort,
Handler: mux,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
TLSConfig: tlsConfig,
}
logf("node-serviced listening on https://localhost:%s", cfg.APIPort)
if err := httpServer.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
logf("server error: %v", err)
os.Exit(1)
}
}