-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathmain.go
More file actions
235 lines (212 loc) · 6.85 KB
/
main.go
File metadata and controls
235 lines (212 loc) · 6.85 KB
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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2022 wind
// SPDX-FileContributor: wind (573966@qq.com)
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
rv8 "github.com/redis/go-redis/v9"
"github.com/wind-c/comqtt/v2/cluster/log"
"github.com/wind-c/comqtt/v2/config"
"github.com/wind-c/comqtt/v2/mqtt"
"github.com/wind-c/comqtt/v2/mqtt/hooks/auth"
"github.com/wind-c/comqtt/v2/mqtt/hooks/storage/badger"
"github.com/wind-c/comqtt/v2/mqtt/hooks/storage/bolt"
"github.com/wind-c/comqtt/v2/mqtt/hooks/storage/redis"
"github.com/wind-c/comqtt/v2/mqtt/listeners"
"github.com/wind-c/comqtt/v2/mqtt/rest"
"github.com/wind-c/comqtt/v2/plugin"
hauth "github.com/wind-c/comqtt/v2/plugin/auth/http"
mauth "github.com/wind-c/comqtt/v2/plugin/auth/mysql"
pauth "github.com/wind-c/comqtt/v2/plugin/auth/postgresql"
rauth "github.com/wind-c/comqtt/v2/plugin/auth/redis"
cokafka "github.com/wind-c/comqtt/v2/plugin/bridge/kafka"
"go.etcd.io/bbolt"
)
func pprof() {
go func() {
log.Info("listen pprof", "error", http.ListenAndServe(":6060", nil))
}()
}
func main() {
sigCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
err := realMain(sigCtx)
onError(err, "")
}
func realMain(ctx context.Context) error {
var err error
var confFile string
cfg := config.New()
flag.StringVar(&confFile, "conf", "", "read the program parameters from the config file")
flag.UintVar(&cfg.StorageWay, "storage-way", 1, "storage way optional items:0 memory, 1 bolt, 2 badger, 3 redis")
flag.UintVar(
&cfg.Auth.Way,
"auth-way",
0,
"authentication way optional items:0 anonymous, 1 username and password, 2 clientid",
)
flag.UintVar(
&cfg.Auth.Datasource,
"auth-ds",
0,
"authentication datasource optional items:0 free, 1 redis, 2 mysql, 3 postgresql, 4 http",
)
flag.StringVar(&cfg.Auth.ConfPath, "auth-path", "", "config file path should correspond to the auth-datasource")
flag.StringVar(&cfg.Mqtt.TCP, "tcp", ":1883", "network address for Mqtt TCP listener")
flag.StringVar(&cfg.Mqtt.WS, "ws", ":1882", "network address for Mqtt Websocket listener")
flag.StringVar(&cfg.Mqtt.QUIC, "quic", ":2000", "network address for Mqtt Websocket listener")
flag.StringVar(&cfg.Mqtt.HTTP, "http", ":8080", "network address for web info dashboard listener")
flag.BoolVar(&cfg.Log.Enable, "log-enable", true, "log enabled or not")
flag.StringVar(&cfg.Log.Filename, "log-file", "./logs/comqtt.log", "log filename")
// parse arguments
flag.Parse()
// load config file
if len(confFile) > 0 {
if cfg, err = config.Load(confFile); err != nil {
onError(err, "")
}
}
// enable pprof
if cfg.PprofEnable {
pprof()
}
// init log
log.Init(&cfg.Log)
if cfg.Log.Enable && cfg.Log.Output == log.OutputFile {
fmt.Println("log output to the files, please check")
}
// create server instance and init hooks
cfg.Mqtt.Options.Logger = log.Default()
server := mqtt.New(&cfg.Mqtt.Options)
log.Info("comqtt server initializing...")
initStorage(server, cfg)
initAuth(server, cfg)
initBridge(server, cfg)
// gen tls config
var listenerConfig *listeners.Config
var listenerQuicConfig *listeners.Config
if tlsConfig, err := config.GenTlsConfig(cfg); err != nil {
onError(err, "")
} else {
if tlsConfig != nil {
listenerConfig = &listeners.Config{TLSConfig: tlsConfig, ZeroRTT: cfg.Mqtt.Tls.ZeroRTT}
listenerQuicConfig = listenerConfig
}
}
// add tcp listener
tcp := listeners.NewTCP("tcp", cfg.Mqtt.TCP, listenerConfig)
onError(server.AddListener(tcp), "add tcp listener")
if listenerConfig == nil {
// quic requires min tls1.3
// ALPN requires
if tlsConfig, err := config.GenerateSelfSignedCert(); err != nil {
onError(err, "")
} else {
listenerQuicConfig = &listeners.Config{TLSConfig: tlsConfig, ZeroRTT: cfg.Mqtt.Tls.ZeroRTT}
}
}
// add quic listener
quic := listeners.NewQUIC("quic", cfg.Mqtt.QUIC, listenerQuicConfig)
onError(server.AddListener(quic), "add quic listener")
// add websocket listener
ws := listeners.NewWebsocket("ws", cfg.Mqtt.WS, listenerConfig)
onError(server.AddListener(ws), "add websocket listener")
// add http listener
http := listeners.NewHTTP("stats", cfg.Mqtt.HTTP, nil, rest.New(server).GenHandlers())
onError(server.AddListener(http), "add http listener")
errCh := make(chan error, 1)
// start server
go func() {
err := server.Serve()
if err != nil {
errCh <- err
}
}()
// log.Info("comqtt server started")
select {
case err := <-errCh:
onError(err, "server error")
case <-ctx.Done():
log.Warn("caught signal, stopping...")
}
server.Close()
log.Info("main.go finished")
return nil
}
func initAuth(server *mqtt.Server, conf *config.Config) {
logMsg := "init auth"
if conf.Auth.Way == config.AuthModeAnonymous {
server.AddHook(new(auth.AllowHook), nil)
} else if conf.Auth.Way == config.AuthModeUsername || conf.Auth.Way == config.AuthModeClientid {
switch conf.Auth.Datasource {
case config.AuthDSRedis:
opts := rauth.Options{}
onError(plugin.LoadYaml(conf.Auth.ConfPath, &opts), logMsg)
onError(server.AddHook(new(rauth.Auth), &opts), logMsg)
case config.AuthDSMysql:
opts := mauth.Options{}
onError(plugin.LoadYaml(conf.Auth.ConfPath, &opts), logMsg)
onError(server.AddHook(new(mauth.Auth), &opts), logMsg)
case config.AuthDSPostgresql:
opts := pauth.Options{}
onError(plugin.LoadYaml(conf.Auth.ConfPath, &opts), logMsg)
onError(server.AddHook(new(pauth.Auth), &opts), logMsg)
case config.AuthDSHttp:
opts := hauth.Options{}
onError(plugin.LoadYaml(conf.Auth.ConfPath, &opts), logMsg)
onError(server.AddHook(new(hauth.Auth), &opts), logMsg)
}
} else {
onError(config.ErrAuthWay, logMsg)
}
}
func initStorage(server *mqtt.Server, conf *config.Config) {
logMsg := "init storage"
switch conf.StorageWay {
case config.StorageWayBolt:
onError(server.AddHook(new(bolt.Hook), &bolt.Options{
Path: conf.StoragePath,
Options: &bbolt.Options{
Timeout: 500 * time.Millisecond,
},
}), logMsg)
case config.StorageWayBadger:
onError(server.AddHook(new(badger.Hook), &badger.Options{
Path: conf.StoragePath,
}), logMsg)
case config.StorageWayRedis:
onError(server.AddHook(new(redis.Hook), &redis.Options{
HPrefix: conf.Redis.HPrefix,
Options: &rv8.Options{
Addr: conf.Redis.Options.Addr,
DB: conf.Redis.Options.DB,
Password: conf.Redis.Options.Password,
},
}), logMsg)
}
}
func initBridge(server *mqtt.Server, conf *config.Config) {
logMsg := "init bridge"
switch conf.BridgeWay {
case config.BridgeWayNone:
return
case config.BridgeWayKafka:
opts := cokafka.Options{}
onError(plugin.LoadYaml(conf.BridgePath, &opts), logMsg)
onError(server.AddHook(new(cokafka.Bridge), &opts), logMsg)
}
}
// onError handle errors and simplify code
func onError(err error, msg string) {
if err != nil {
log.Error(msg, "error", err)
os.Exit(1)
}
}