forked from safing/portmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
92 lines (74 loc) Β· 1.75 KB
/
core.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
package core
import (
"flag"
"fmt"
"time"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/modules/subsystems"
_ "github.com/safing/portmaster/netenv"
_ "github.com/safing/portmaster/status"
_ "github.com/safing/portmaster/ui"
"github.com/safing/portmaster/updates"
)
const (
eventShutdown = "shutdown"
eventRestart = "restart"
)
var (
module *modules.Module
disableShutdownEvent bool
)
func init() {
module = modules.Register("core", prep, start, nil, "base", "subsystems", "status", "updates", "api", "notifications", "ui", "netenv", "network", "interception", "compat")
subsystems.Register(
"core",
"Core",
"Base Structure and System Integration",
module,
"config:core/",
nil,
)
flag.BoolVar(
&disableShutdownEvent,
"disable-shutdown-event",
false,
"disable shutdown event to keep app and notifier open when core shuts down",
)
modules.SetGlobalShutdownFn(shutdownHook)
}
func prep() error {
registerEvents()
// init config
err := registerConfig()
if err != nil {
return err
}
if err := registerAPIEndpoints(); err != nil {
return err
}
return nil
}
func start() error {
if err := startPlatformSpecific(); err != nil {
return fmt.Errorf("failed to start plattform-specific components: %w", err)
}
registerLogCleaner()
return nil
}
func registerEvents() {
module.RegisterEvent(eventShutdown, true)
module.RegisterEvent(eventRestart, true)
}
func shutdownHook() {
// Notify everyone of the restart/shutdown.
if !updates.IsRestarting() {
// Only trigger shutdown event if not disabled.
if !disableShutdownEvent {
module.TriggerEvent(eventShutdown, nil)
}
} else {
module.TriggerEvent(eventRestart, nil)
}
// Wait a bit for the event to propagate.
time.Sleep(100 * time.Millisecond)
}