-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (114 loc) · 3.44 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
package main
import (
"fmt"
"github.com/mochi-co/mqtt/v2"
"github.com/mochi-co/mqtt/v2/hooks/storage/badger"
"mqtt-server/config"
"mqtt-server/hooks"
"os"
"os/signal"
"syscall"
"github.com/mochi-co/mqtt/v2/hooks/auth"
"github.com/mochi-co/mqtt/v2/listeners"
"github.com/rs/zerolog/log"
)
func bool2byte(b bool) byte {
if b {
return 1
}
return 0
}
func main() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
if err := config.Init(); err != nil {
log.Panic().Err(err).Msg("failed to parse init")
}
cfg := config.GetConfig()
if cfg.TCPEnabled == false && cfg.WSEnabled == false {
log.Panic().Msg("at least one protocol must be enabled")
}
options := &mqtt.Options{
Capabilities: mqtt.DefaultServerCapabilities,
}
if cfg.MQTTMaximumQos != -1 {
options.Capabilities.MaximumQos = byte(cfg.MQTTMaximumQos)
}
if cfg.MQTTMinimumProtocolVersion != -1 {
options.Capabilities.MinimumProtocolVersion = byte(cfg.MQTTMinimumProtocolVersion)
}
if cfg.MQTTMaximumMessageExpiryInterval != -1 {
options.Capabilities.MaximumMessageExpiryInterval = cfg.MQTTMaximumMessageExpiryInterval
}
if cfg.MQTTReceiveMaximum != -1 {
options.Capabilities.ReceiveMaximum = uint16(cfg.MQTTReceiveMaximum)
}
if cfg.MQTTServerKeepAlive != -1 {
options.Capabilities.ServerKeepAlive = uint16(cfg.MQTTServerKeepAlive)
}
options.Capabilities.MaximumPacketSize = cfg.MQTTMaximumPacketSize
options.Capabilities.RetainAvailable = bool2byte(cfg.MQTTRetainAvailable)
options.Capabilities.WildcardSubAvailable = bool2byte(cfg.MQTTWildcardSubAvailable)
options.Capabilities.SharedSubAvailable = bool2byte(cfg.MQTTSharedSubAvailable)
server := mqtt.New(options)
if cfg.HTTPAuthURL != "" || cfg.HTTPAclURL != "" {
httpHook := new(hooks.HTTPAuthHook)
httpHook.Configure()
err := server.AddHook(httpHook, nil)
if err != nil {
server.Log.Panic().Err(err).Msg("failed to add http hook")
}
} else {
if cfg.PathAuth == "" {
err := server.AddHook(new(auth.AllowHook), nil)
if err != nil {
server.Log.Panic().Err(err).Msg("failed to add allow all hook")
}
} else {
// Get ledger from yaml file
data, err := os.ReadFile(cfg.PathAuth)
if err != nil {
server.Log.Panic().Err(err).Msg("failed to read auth file")
}
err = server.AddHook(new(auth.Hook), &auth.Options{
Data: data, // build ledger from byte slice, yaml or json
})
if err != nil {
server.Log.Panic().Err(err).Msg("failed to parse auth file")
}
}
}
if cfg.PathPersistence != "" {
err := server.AddHook(new(badger.Hook), &badger.Options{
Path: cfg.PathPersistence,
})
if err != nil {
server.Log.Panic().Err(err).Msg("failed to add persistence storage")
}
}
if cfg.TCPEnabled {
tcp := listeners.NewTCP("t1", fmt.Sprintf(":%d", cfg.TCPPort), nil)
err := server.AddListener(tcp)
if err != nil {
server.Log.Panic().Err(err).Msg("failed to initialize tcp")
}
}
if cfg.WSEnabled {
ws := listeners.NewWebsocket("ws1", fmt.Sprintf(":%d", cfg.WSPort), nil)
err := server.AddListener(ws)
if err != nil {
server.Log.Panic().Err(err).Msg("failed to initialize websocket")
}
}
go func() {
err := server.Serve()
if err != nil {
server.Log.Panic().Err(err).Msg("failed to serve server")
}
}()
server.Log.Info().Int("pid", os.Getpid()).Msg("server started")
<-sigint
server.Log.Warn().Msg("caught signal, stopping...")
server.Close()
server.Log.Info().Msg("main.go finished")
}