This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
157 lines (128 loc) · 4.12 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
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"crypto/tls"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jmoiron/sqlx"
"github.com/p-l/fringe/client"
"github.com/p-l/fringe/internal/httpd"
"github.com/p-l/fringe/internal/radiusd"
"github.com/p-l/fringe/internal/repos"
"github.com/p-l/fringe/internal/system"
"github.com/spf13/viper"
"golang.org/x/crypto/acme/autocert"
"layeh.com/radius"
"modernc.org/ql"
)
const terminationWait = time.Second * 5
func openDB(databaseFile string) *sqlx.DB {
// Initialize Database connexion
ql.RegisterDriver()
db, err := sqlx.Open("ql", databaseFile)
if err != nil {
log.Panicf("could not connect to database: %v", err)
}
return db
}
func openUserRepo(connexion *sqlx.DB) *repos.UserRepository {
userRepo, err := repos.NewUserRepository(connexion)
if err != nil {
log.Panicf("could not initate user repository: %v", err)
}
return userRepo
}
func newWebServers(config system.Config, userRepo *repos.UserRepository, jwtSecret string) (*http.Server, *http.Server) {
clientAssets := client.Files()
// HTTPS
httpsSrv := httpd.NewHTTPServer(
config,
userRepo,
clientAssets,
jwtSecret)
// TLS Cert Manager
var certManager *autocert.Manager
var tlsConfig *tls.Config
if config.Web.UseLetsEncrypt {
certManager = &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(config.Web.Domain),
Cache: autocert.DirCache("certs"),
}
tlsConfig = &tls.Config{
GetCertificate: certManager.GetCertificate,
MinVersion: tls.VersionTLS12,
}
} else {
tlsConfig = system.TLSConfigWithSelfSignedCert(system.AllLocalIPAddresses())
}
// Add the TLS configuration to the https server
httpsSrv.TLSConfig = tlsConfig
// HTTP to HTTPS Redirection and autocert server
redirectSrv := httpd.NewRedirectServer(
config.Services.HTTPBindAddress,
config.Web.Domain,
certManager)
return httpsSrv, redirectSrv
}
func main() {
// Load and validate configuration
viperConf := viper.New()
viperConf.SetConfigName("config") // name of config file (without extension)
viperConf.SetConfigType("toml") // REQUIRED if the config file does not have the extension in the name
viperConf.AddConfigPath("/etc/fringe/") // path to look for the config file in
viperConf.AddConfigPath(".") // optionally look for config in the working directory
config := system.LoadConfig(viperConf)
// Get the Secrets
secrets := system.LoadSecretsFromFile(config.Storage.SecretsFile)
// Get User Repository
db := openDB(config.Storage.UserDatabaseFile)
userRepo := openUserRepo(db)
// Servers
radiusSrv := radiusd.NewRadiusServer(userRepo, secrets.Radius, config.Services.RadiusBindAddress)
httpsSrv, redirectSrv := newWebServers(config, userRepo, secrets.JWT)
// Start Radius
go func() {
if err := radiusSrv.ListenAndServe(); err != nil {
log.Panicf("radius server died with error: %v", err)
}
}()
// Start HTTPS
go func() {
if err := httpsSrv.ListenAndServeTLS("", ""); err != nil {
log.Panicf("web server (https) died with error: %v", err)
}
}()
// Start HTTP
go func() {
if err := redirectSrv.ListenAndServe(); err != nil {
log.Panicf("web redirect server (http) died with error: %v", err)
}
}()
waitOn(httpsSrv, redirectSrv, radiusSrv, db)
}
func waitOn(httpSrv *http.Server, redirectSrv *http.Server, radiusSrv *radius.PacketServer, connexion *sqlx.DB) {
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT or SIGTERM
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), terminationWait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
_ = httpSrv.Shutdown(ctx)
_ = radiusSrv.Shutdown(ctx)
_ = redirectSrv.Shutdown(ctx)
_ = connexion.Close()
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Println("shutting down")
os.Exit(0) //nolint:gocritic
}