-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
287 lines (278 loc) · 8.35 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
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
//go:generate goversioninfo -icon=ico/icon.ico -manifest=main.exe.manifest -arm=true
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"github.com/cloudfoundry/jibber_jabber"
mqtt "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
)
var (
language string
timeFormat string = "2006-01-02 15:04:05"
logFile string
userFile string
logData string
logStatus string
logFileE bool = false
logDataE bool = false
logStatusE bool = false
fileTimestamp bool = false
logFileF *os.File
logDataF *os.File
logStatusF *os.File
monochrome bool = false
isWindows bool = runtime.GOOS == "windows"
onlyIdE bool = false
onlyTopicE bool = false
onlyPayloadE bool = false
onlyIdS []string = []string{}
onlyTopicS []string = []string{}
onlyPayloadS []string = []string{}
)
func main() {
go http.ListenAndServe(":9999", nil)
var (
version string = "1.5.5"
versionView bool = false
listen string
onlyID string
onlyTopic string
onlyPayload string
certCA string
certCert string
certKey string
certPassword string
useTLS bool = false
certClientAuth int = 0
)
// 初始化启动参数
flag.BoolVar(&versionView, "v", false, "Print version info")
flag.StringVar(&language, "l", "auto", "Language ( en | chs )")
flag.StringVar(&listen, "p", "0.0.0.0:1883", "Define listening on IP:Port (default: 0.0.0.0:1883 )")
flag.StringVar(&onlyID, "c", "", "Only allow these client IDs (comma separated)")
flag.StringVar(&onlyTopic, "t", "", "Only allow these topics (comma separated)")
flag.StringVar(&onlyPayload, "w", "", "Only allow these words in message content (comma separated)")
flag.StringVar(&logData, "m", "", "Log message to csv file")
flag.StringVar(&logStatus, "s", "", "Log state changes to a csv file")
flag.StringVar(&logFile, "o", "", "Save log to txt/log file")
flag.BoolVar(&fileTimestamp, "ts", false, "Use timestamps in logged files")
flag.BoolVar(&monochrome, "n", false, "Use a monochrome color scheme (When an abnormal character appears in Windows cmd.exe)")
flag.StringVar(&userFile, "u", "", "Users and permissions file (.json, visit README.md) path")
flag.StringVar(&certCA, "ca", "", "CA certificate file path")
flag.StringVar(&certCert, "ce", "", "Server certificate file path")
flag.StringVar(&certKey, "ck", "", "Server key file path")
flag.StringVar(&certPassword, "cp", "", "Server key file password")
flag.IntVar(&certClientAuth, "cv", -1, "Policy the server will follow for TLS Client Authentication")
flag.Parse()
if language == "auto" {
language = "en"
syslang, _ := jibber_jabber.DetectIETF()
if len(syslang) > 0 {
if strings.Contains(syslang, "zh") {
language = "chs"
}
}
}
logPrint("I", lang("TITLE")+" v"+version+" for "+runtime.GOOS+" (KagurazakaYashi@Tongdy, 2024)")
logPrint("I", lang("HELP")+" https://github.com/tongdysoft/mqtt-test-server")
// 初始化设置
if versionView {
return
}
if len(onlyID) > 0 {
onlyIdS = strings.Split(onlyID, ",")
logPrint("C", fmt.Sprintf("%s%s: %s", lang("ONLY"), lang("CLIENT"), onlyIdS))
onlyIdE = true
}
if len(onlyTopic) > 0 {
onlyTopicS = strings.Split(onlyTopic, ",")
logPrint("C", fmt.Sprintf("%s%s: %s", lang("ONLY"), lang("TOPIC"), onlyTopicS))
onlyTopicE = true
}
if len(onlyPayload) > 0 {
onlyPayloadS = strings.Split(onlyPayload, ",")
logPrint("C", fmt.Sprintf("%s%s: %s", lang("ONLY"), lang("WORD"), onlyPayloadS))
onlyPayloadE = true
}
logInit(listen, lang("TITLE")+" v"+version)
// 监听结束信号
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
done <- true
close(sigs)
}()
// 加载 SSL 证书
var clientAuth tls.ClientAuthType = clientAuthDefault(certClientAuth, certCA, certCert)
tlsConfig := &tls.Config{ClientAuth: clientAuth}
certPool := x509.NewCertPool()
if len(certCA) > 0 {
contentC, err := os.ReadFile(certCA)
if err != nil {
logPrint("X", fmt.Sprintf("%s%s: %s: %s)", lang("CACERT"), lang("ERROR"), certCA, err.Error()))
return
}
var isok bool = certPool.AppendCertsFromPEM(contentC)
if !isok {
logPrint("X", fmt.Sprintf("%s%s: %s (%d)", lang("CACERT"), lang("ERROR"), certCA, len(contentC)))
return
}
tlsConfig = &tls.Config{
ClientCAs: certPool,
ClientAuth: clientAuth,
}
useTLS = true
logPrint("C", fmt.Sprintf("%s: %s (%d)", lang("CACERT"), certCA, len(contentC)))
}
if len(certCert) > 0 && len(certKey) == 0 {
logPrint("X", fmt.Sprintf("%s%s: %s", lang("SERVERKEY"), lang("ERROR"), lang("NOTEMPTY")))
return
}
if len(certCert) == 0 && len(certKey) > 0 {
logPrint("X", fmt.Sprintf("%s%s: %s", lang("SERVERCERT"), lang("ERROR"), lang("NOTEMPTY")))
return
}
if len(certCert) > 0 && len(certKey) > 0 {
contentC, err := os.ReadFile(certCert)
if err != nil {
logPrint("X", fmt.Sprintf("%s%s: %s: %s", lang("SERVERCERT"), lang("ERROR"), certCert, err.Error()))
return
}
logPrint("C", fmt.Sprintf("%s: %s (%d)", lang("SERVERCERT"), certCert, len(contentC)))
contentK, err := os.ReadFile(certKey)
if err != nil {
logPrint("X", fmt.Sprintf("%s%s: %s: %s", lang("SERVERKEY"), lang("ERROR"), certKey, err.Error()))
return
}
logPrint("C", fmt.Sprintf("%s: %s (%d)", lang("SERVERKEY"), certKey, len(contentK)))
var cert tls.Certificate = LoadCert(contentC, contentK, certPassword)
if cert.Certificate == nil {
return
}
if len(certCA) > 0 {
tlsConfig = &tls.Config{
ClientCAs: certPool,
Certificates: []tls.Certificate{cert},
}
} else {
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
}
if len(certPassword) > 0 {
logPrint("C", fmt.Sprintf("%s: (%d)", lang("SERVERKEYPWD"), len(certPassword)))
}
// 初始化 MQTT 服务器
logPrint("I", lang("BOOTING"), listen)
var server *mqtt.Server = mqtt.New(nil)
level := new(slog.LevelVar)
server.Log = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
}))
level.Set(slog.LevelError)
var userDataDefault string = `
auth:
- allow: true
acl:
- filters:
'#': 3
`
var err error = nil
var userData []byte = []byte(userDataDefault)
if len(userFile) > 0 {
userData, err = os.ReadFile(userFile)
if err != nil {
log.Fatal(err)
}
}
err = server.AddHook(new(auth.Hook), &auth.Options{
Data: userData, // 从字节数组(文件二进制)读取 yaml 或 json
})
if err != nil {
log.Fatal(err)
}
var tcp *listeners.TCP
if useTLS {
tcp = listeners.NewTCP(listeners.Config{
Address: listen,
TLSConfig: tlsConfig,
})
} else {
tcp = listeners.NewTCP(listeners.Config{
Address: listen,
})
}
err = server.AddListener(tcp)
if err != nil {
log.Fatal(err)
}
// err = server.AddListener(tcp, &conf)
// if err != nil {
// log.Fatal(err)
// }
err = server.AddHook(new(MQTTHook), &MQTTHookOptions{
Server: server,
})
if err != nil {
log.Fatal(err)
}
// 启动 MQTT 服务器
go func() {
err := server.Serve()
if err != nil {
logPrint("X", lang("SERVERFAIL"), listen)
log.Fatal(err)
}
}()
// server.Events.OnProcessMessage = func(cl events.Client, pk events.Packet) (pkx events.Packet, err error) {
// return pkx, err
// }
// 设备连接出错
// server.Events.OnError = func(cl events.Client, err error) {
// logPrint("D", fmt.Sprintf("%v", err), strCL(cl))
// }
// 有设备连接到服务器
// server.Events.OnConnect = func(cl events.Client, pk events.Packet) {}
// 设备断开连接
// server.Events.OnDisconnect = func(cl events.Client, err error) {}
// 收到订阅请求
// server.Events.OnSubscribe = func(filter string, cl events.Client, qos byte) {}
// 收到取消订阅请求
// server.Events.OnUnsubscribe = func(filter string, cl events.Client) {}
// 收到消息
// server.Events.OnMessage = func(cl events.Client, pk events.Packet) (pkx events.Packet, err error) {}
// 启动完毕
logPrint("I", lang("BOOTOK"), listen)
// 处理结束信号
<-done
close(done)
logPrint("X", lang("NEEDSTOP"), listen)
server.Close()
if logFileE {
logFileF.Close()
}
if logDataE {
logDataF.Close()
}
if logStatusE {
logStatusF.Close()
}
logPrint("I", lang("END"))
os.Exit(0)
}