/
configure.go
479 lines (426 loc) · 12.7 KB
/
configure.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package cmd
import (
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/imperviousai/imp-daemon/auth"
"github.com/imperviousai/imp-daemon/comm"
"github.com/imperviousai/imp-daemon/config"
"github.com/imperviousai/imp-daemon/contacts"
"github.com/imperviousai/imp-daemon/core"
"github.com/imperviousai/imp-daemon/id"
"github.com/imperviousai/imp-daemon/key"
"github.com/imperviousai/imp-daemon/kv"
"github.com/imperviousai/imp-daemon/lightning"
"github.com/imperviousai/imp-daemon/lightning/node"
"github.com/imperviousai/imp-daemon/messages"
"github.com/imperviousai/imp-daemon/service"
"github.com/imperviousai/imp-daemon/service/relay"
"github.com/imperviousai/imp-daemon/state"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type ConfigureContext struct {
Core core.Core
Websocket service.Websocket
HttpDIDComm *http.Server
Auth auth.AuthService
// Global things that don't need to be hot reloaded typically
KeyManager key.KeyManager
DB state.DBManager
KV kv.KvManager
}
func ConfigureCore(globalCfg config.GlobalConfig, prevContext *ConfigureContext) *ConfigureContext {
cfg := globalCfg.GetConfig()
var err error
// Setup state
// If being reloaded, transfer previous global contexts
var keyManager key.KeyManager
var db state.DBManager
if prevContext != nil {
if prevContext.DB != nil {
db = prevContext.DB
}
if prevContext.KeyManager != nil {
keyManager = prevContext.KeyManager
}
}
// setup didcomm http server
httpDIDCommServer, httpDIDCommMux := ConfigureDIDCommHttpServer(cfg)
// Setup key management
// if DB is already loaded, skip unlocking
// IsReady should be true for non-encrypted types
if db == nil || !db.IsReady() {
zap.L().Debug("[Cfg] Configuring database")
db, err = ConfigureDb(cfg)
if err != nil {
zap.L().Panic(err.Error())
}
// if passphrase is passed in the config, then auto unlock
if !db.IsReady() && cfg.Key.Passphrase != "" {
// Double check if db file even exiss
// Do not let users unknowing set passphrase
// on very first load automatically
isInitialized := db.IsInitialized()
if isInitialized {
err = db.Unlock(cfg.Key.Passphrase)
if err != nil {
zap.L().Panic(err.Error())
}
zap.L().Debug("[Cfg] Auto unlocked db")
} else {
panic("Auto init passphrase while db file does not exist; remove cfg.key.passphrase")
}
}
}
// if key manager is not already loaded, load now
if keyManager == nil || keyManager.IsLocked() {
zap.L().Debug("[Cfg] Configuring key manager")
keyManager, err = key.New(key.Config{Db: db})
if err != nil {
zap.L().Panic(err.Error())
}
// If passphrase is passed in the config then auto unlock
if cfg.Key.Passphrase != "" {
err = keyManager.UnlockSeed(cfg.Key.Passphrase)
if err != nil {
zap.L().Panic(err.Error())
}
zap.L().Debug("[Cfg] Auto unlocked key manager")
}
}
// Setup auth
authService, err := ConfigureAuth(cfg, db)
if err != nil {
zap.L().Panic(err.Error())
}
// channel for shared lightning message listening
msgChan := make(chan comm.IncomingDIDMessage)
// Setup lightning manager & nodes
zap.L().Debug("[Cfg] Configuring lightning manager")
lightningManager, err := ConfigureLightningManager(cfg, msgChan)
if err != nil {
zap.L().Panic(err.Error())
}
// Setup http didcomm
zap.L().Debug("[Cfg] Configuring HTTP DIDComm")
httpDIDComm, err := ConfigureHTTPDIDComm(cfg, msgChan)
if err != nil {
zap.L().Panic(err.Error())
}
if httpDIDComm != nil {
httpDIDCommMux.Handle("/didcomm/http", httpDIDComm)
}
// Setup websocket didcomm
websocketDIDComm, err := ConfigureWebsocketDIDComm(cfg, msgChan)
if err != nil {
zap.L().Panic(err.Error())
}
if websocketDIDComm != nil {
httpDIDCommMux.Handle("/didcomm/websocket", websocketDIDComm)
}
zap.L().Debug("[Cfg] Configuring lightning nodes")
if err := AddLightningNodes(cfg, lightningManager); err != nil {
zap.L().Panic(err.Error())
}
// Create the websockets to be listened to
zap.L().Debug("[Cfg] Setting up websocket service")
ws := service.NewWebsocket()
// Setup ION
var ionSvc id.Ion
if cfg.ION.Active {
zap.L().Debug("[Cfg] Setting up ION")
ionSvc, err = ConfigureIon(cfg, db, keyManager)
if err != nil {
zap.L().Panic(err.Error())
}
}
// Setup identity functionality
identity, err := id.New(id.Config{
Db: db,
Ion: ionSvc,
KeyManager: keyManager,
UniversalResolverUrls: cfg.DID.UniversalResolverUrls,
})
if err != nil {
zap.L().Panic(err.Error())
}
// Setup Messages manager
zap.L().Debug("[Cfg] Setting up Messages")
messagesManager, err := messages.New(&messages.Config{
Db: db,
})
if err != nil {
zap.L().Panic(err.Error())
}
// Setup DIDComm functionalilty
didComm, err := comm.NewDIDComm(&comm.DIDCommConfig{
IncomingDataChan: msgChan,
LightningManager: lightningManager,
HttpComm: httpDIDComm,
WebsocketDIDComm: websocketDIDComm,
Identity: identity,
MessagesManager: messagesManager,
})
if err != nil {
zap.L().Panic(err.Error())
}
// Setup Contacts manager
zap.L().Debug("[Cfg] Setting up Contacts")
contactsManager, err := contacts.New(&contacts.Config{
Db: db,
IdManager: identity,
})
if err != nil {
zap.L().Panic(err.Error())
}
// Setup Kv manager
zap.L().Debug("[Cfg] Setting up Key Value Store")
kvManager, err := kv.New(&kv.Config{
Kvdb: db,
})
if err != nil {
zap.L().Panic(err.Error())
}
// Setup services
zap.L().Debug("[Cfg] Registering services")
serviceHandler, err := ConfigureServiceHandler(cfg, msgChan, keyManager, identity, messagesManager, didComm, db, ws)
if err != nil {
zap.L().Panic(err.Error())
}
// Add configurations to Core
zap.L().Debug("[Cfg] Configuring imp core")
core, err := core.NewImpCore(&core.Config{
Auth: authService,
ServiceHandler: serviceHandler,
LightningManager: lightningManager,
Id: identity,
KeyManager: keyManager,
ContactsManager: contactsManager,
MessageManager: messagesManager,
DIDComm: didComm,
DBManager: db,
GlobalConfig: globalCfg,
KvManager: kvManager,
})
if err != nil {
zap.L().Panic(err.Error())
}
return &ConfigureContext{
Core: core,
KeyManager: keyManager,
Auth: authService,
DB: db,
Websocket: ws,
HttpDIDComm: httpDIDCommServer,
KV: kvManager,
}
}
func ConfigureLogger(globalCfg config.GlobalConfig) (*zap.Logger, error) {
outputPaths := []string{"stdout"}
errorOutputPaths := []string{"stderr"}
// First make sure file exists
if globalCfg != nil && !globalCfg.GetConfig().Log.IgnoreFileWrite {
path, err := os.UserHomeDir()
if err != nil {
zap.L().Panic(err.Error())
}
if runtime.GOOS == "windows" { // https://github.com/uber-go/zap/issues/621
path = filepath.Join("log.txt")
} else {
path = filepath.Join(path, ".imp", "log.txt")
}
if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil {
zap.L().Panic(err.Error())
}
if _, err := os.Stat(path); os.IsNotExist(err) {
f, err := os.Create(filepath.Clean(path))
if err != nil {
zap.L().Panic(err.Error())
}
defer func() {
if err := f.Close(); err != nil {
zap.L().Error(err.Error())
}
}()
}
outputPaths = append(outputPaths, path)
errorOutputPaths = append(errorOutputPaths, path)
}
return zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: outputPaths,
ErrorOutputPaths: errorOutputPaths,
}.Build()
}
func ConfigureServiceHandler(cfg config.Config,
incomingDataChan chan comm.IncomingDIDMessage,
keyManager key.KeyManager,
identity id.Identity,
messageManager messages.MessageManager,
didComm comm.DIDComm,
db state.DBManager,
ws service.Websocket) (service.Handler, error) {
serviceHandler := service.NewHandler(incomingDataChan, ws, keyManager, identity, messageManager)
for _, s := range cfg.ServiceList {
if !s.Active {
continue
}
zap.L().Debug("[Cfg] Registering service", zap.String("service", s.ServiceType))
// Register service from config
switch s.ServiceType {
case "relay":
r, err := relay.NewRelayService(relay.Config{
DidComm: didComm,
Db: db,
})
if err != nil {
return nil, fmt.Errorf("could not create service: %s\n", err.Error())
}
err = serviceHandler.RegisterService(s.CustomMessageType, r)
if err != nil {
return nil, fmt.Errorf("could not register service: %s\n", err.Error())
}
case "relay-registration":
r, err := relay.NewRelayRegistrationService(relay.RegistrationConfig{
DidComm: didComm,
Db: db,
})
if err != nil {
return nil, fmt.Errorf("could not create service: %s\n", err.Error())
}
err = serviceHandler.RegisterService(s.CustomMessageType, r)
if err != nil {
return nil, fmt.Errorf("could not register service: %s\n", err.Error())
}
// Do nothing if not recognizing a service type.
default:
zap.L().Warn("[Cfg] Unfamiliar with service type", zap.String("service", s.ServiceType))
}
}
return serviceHandler, nil
}
func ConfigureAuth(cfg config.Config, db state.DBManager) (auth.AuthService, error) {
return auth.NewAuthService(auth.AuthConfig{
Db: db,
})
}
func ConfigureHTTPDIDComm(cfg config.Config, channel chan comm.IncomingDIDMessage) (comm.HttpComm, error) {
if cfg.Server.HttpDIDAddr != "" {
zap.L().Debug("[Cfg] Configuring Http DIDComm")
return comm.NewHttpComm(&comm.HttpCommConfig{
IncomingDataChan: channel,
})
}
return nil, nil
}
func ConfigureLightningManager(cfg config.Config, channel chan comm.IncomingDIDMessage) (lightning.LightningManager, error) {
lnm := lightning.NewLightningManager(&lightning.LightningManagerConfig{
IncomingDataChan: channel,
})
return lnm, nil
}
func ConfigureWebsocketDIDComm(cfg config.Config, channel chan comm.IncomingDIDMessage) (comm.WebsocketComm, error) {
if cfg.Server.HttpDIDAddr != "" {
zap.L().Debug("[Cfg] Configuring Websocket DIDComm")
return comm.NewWebsocketComm(&comm.WebsocketCommConfig{
IncomingDataChan: channel,
})
}
return nil, nil
}
func AddLightningNodes(cfg config.Config, lnm lightning.LightningManager) error {
lndNode, err := ConfigureLightningNodes(cfg)
if err != nil {
zap.L().Error("Could not connect to lightning node, skipping lightning setup...",
zap.Error(err),
)
}
if lndNode == nil {
return nil
}
zap.L().Debug("[Cfg] Adding newly created lightning node")
if err := lnm.AddNode(lndNode); err != nil {
return err
}
return nil
}
func ConfigureLightningNodes(cfg config.Config) (node.Node, error) {
if cfg.Lightning.LndNode.Ip == "" {
// Allow running without lightning node
return nil, nil
}
zap.L().Debug("[Cfg] Connecting to new lightning node")
lndNode, err := node.NewLndNode(&node.LndConfig{
IpAddress: cfg.Lightning.LndNode.Ip,
Port: cfg.Lightning.LndNode.Port,
MacaroonPath: cfg.Lightning.LndNode.AdminMacaroon,
MacaroonHex: cfg.Lightning.LndNode.AdminMacaroonHex,
TlsCertPath: cfg.Lightning.LndNode.TlsCert,
TlsCertHex: cfg.Lightning.LndNode.TlsCertHex,
Pubkey: cfg.Lightning.LndNode.PubKey,
ShouldListen: cfg.Lightning.LndNode.Listening,
})
if err != nil {
return nil, err
}
return lndNode, nil
}
func ConfigureDb(cfg config.Config) (state.DBManager, error) {
return state.NewDB(cfg.Sql.Type, cfg.Sql.ConnectionString, cfg.Kv.Db)
}
func ConfigureIon(cfg config.Config, db state.DBManager, keymanager key.KeyManager) (id.Ion, error) {
return id.NewIon(&id.IonConfig{
IonUrl: cfg.ION.Url,
Db: db,
KeyManager: keymanager,
})
}
func ConfigureDIDCommHttpServer(cfg config.Config) (*http.Server, *http.ServeMux) {
if cfg.Server.Enabled && cfg.Server.HttpDIDAddr != "" {
h := http.NewServeMux()
return &http.Server{
Addr: cfg.Server.HttpDIDAddr,
Handler: h,
ReadHeaderTimeout: 10 * time.Second,
}, h
}
return nil, nil
}
func GetStringInBetweenTwoString(str string, startS string, endS string) (result string) {
s := strings.Index(str, startS)
if s == -1 {
return str
}
newS := str[s+len(startS):]
e := strings.Index(newS, endS)
if e == -1 {
return str
}
result = newS[:e]
return result
}