-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
203 lines (177 loc) · 5.07 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"encoding/base64"
"errors"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/sirupsen/logrus"
"go.uber.org/automaxprocs/maxprocs"
"github.com/UnAfraid/wg-ui/pkg/api"
"github.com/UnAfraid/wg-ui/pkg/auth"
"github.com/UnAfraid/wg-ui/pkg/config"
"github.com/UnAfraid/wg-ui/pkg/datastore"
"github.com/UnAfraid/wg-ui/pkg/datastore/bbolt"
"github.com/UnAfraid/wg-ui/pkg/dbx"
"github.com/UnAfraid/wg-ui/pkg/manage"
"github.com/UnAfraid/wg-ui/pkg/peer"
"github.com/UnAfraid/wg-ui/pkg/server"
"github.com/UnAfraid/wg-ui/pkg/subscription"
"github.com/UnAfraid/wg-ui/pkg/user"
"github.com/UnAfraid/wg-ui/pkg/wireguard"
"github.com/UnAfraid/wg-ui/pkg/wireguard/backend"
"github.com/UnAfraid/wg-ui/pkg/wireguard/linux"
)
const (
appName = "wg-ui"
)
func main() {
logrus.SetFormatter(&logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339,
})
conf, err := config.Load(appName)
if err != nil {
logrus.
WithError(err).
Fatal("failed to initialize config")
return
}
shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, syscall.SIGTERM, syscall.SIGINT)
if _, err := maxprocs.Set(maxprocs.Logger(logrus.Printf)); err != nil {
logrus.
WithError(err).
Error("failed to set maxprocs")
return
}
debugServer := &http.Server{
Addr: conf.DebugServer.Address(),
}
if conf.DebugServer.Enabled {
go func() {
logrus.WithField("address", conf.DebugServer.Address()).Info("Starting serving debug server")
if err := debugServer.ListenAndServe(); err != nil {
logrus.
WithError(err).
Fatal("Failed to serve debug")
return
}
}()
}
logrus.Info("initializing database..")
db, err := datastore.NewBBoltDB(conf.BoltDB.Path, conf.BoltDB.Timeout)
if err != nil {
logrus.
WithError(err).
Fatal("failed initialize datastore")
return
}
jwtSecretBytes, err := base64.StdEncoding.DecodeString(conf.JwtSecret)
if err != nil {
logrus.
WithError(err).
Fatal("failed to base64 decode jwt secret")
return
}
transactionScoper := dbx.NewBBoltTransactionScoper(db)
subscriptionImpl := subscription.NewInMemorySubscription()
serverRepository := bbolt.NewServerRepository(db)
serverService := server.NewService(serverRepository, transactionScoper, subscriptionImpl)
peerRepository := bbolt.NewPeerRepository(db)
peerService := peer.NewService(peerRepository, transactionScoper, serverService, subscriptionImpl)
userRepository := bbolt.NewUserRepository(db)
userService, err := user.NewService(userRepository, transactionScoper, subscriptionImpl, conf.Initial.Email, conf.Initial.Password)
if err != nil {
logrus.
WithError(err).
Fatal("failed to initialize user service")
return
}
var wireguardBackend backend.Backend
switch strings.ToLower(conf.Backend) {
case "linux":
wireguardBackend, err = linux.NewLinuxBackend()
if err != nil {
logrus.
WithError(err).
Fatal("failed to initialize linux backend for wireguard")
return
}
default:
logrus.
WithError(err).
Fatal("unsupported wireguard backend")
return
}
wireguardService := wireguard.NewService(wireguardBackend)
defer func() {
if err := wireguardService.Close(context.Background()); err != nil {
logrus.
WithError(err).
Error("failed to close wireguard service")
}
}()
authService := auth.NewService(jwt.SigningMethodHS256, jwtSecretBytes, jwtSecretBytes, conf.JwtDuration)
manageService := manage.NewService(
transactionScoper,
userService,
serverService,
peerService,
wireguardService,
conf.AutomaticStatsUpdateInterval,
conf.AutomaticStatsUpdateOnlyWithSubscribers,
)
defer manageService.Close()
router := api.NewRouter(
conf,
authService,
userService,
serverService,
peerService,
manageService,
)
httpServer := http.Server{
Addr: conf.HttpServer.Address(),
Handler: router,
}
go func() {
logrus.WithField("address", conf.HttpServer.Address()).Info("Starting serving http server")
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.
WithError(err).
Fatal("failed to listen and serve http server")
}
}()
<-shutdownChan
logrus.Info("Shutting down")
logrus.Info("Shutting down http server")
httpServerShutdownTimeoutCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := httpServer.Shutdown(httpServerShutdownTimeoutCtx); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.
WithError(err).
Fatal("failed to shutdown http server")
return
}
if conf.DebugServer.Enabled {
logrus.Info("Shutting down debug http server")
debugHttpServerShutdownTimeoutCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := debugServer.Shutdown(debugHttpServerShutdownTimeoutCtx); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.
WithError(err).
Fatal("failed to shutdown debug server")
return
}
}
}