-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugins.go
280 lines (242 loc) · 7.85 KB
/
plugins.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
package gott
import (
"gott/utils"
"log"
"net"
"os"
"path"
"plugin"
"go.uber.org/zap"
)
type gottPlugin struct {
name string
plug *plugin.Plugin
onSocketOpen func(conn net.Conn) bool
onBeforeConnect func(clientID, username, password string) bool
onConnect func(clientID, username, password string) bool
onMessage func(clientID, username string, topic, payload []byte, dup, qos byte, retain bool)
onBeforePublish func(clientID, username string, topic, payload []byte, dup, qos byte, retain bool) bool
onPublish func(clientID, username string, topic, payload []byte, dup, qos byte, retain bool)
onBeforeSubscribe func(clientID, username string, topic []byte, qos byte) bool
onSubscribe func(clientID, username string, topic []byte, qos byte)
onBeforeUnsubscribe func(clientID, username string, topic []byte) bool
onUnsubscribe func(clientID, username string, topic []byte)
onDisconnect func(clientID, username string, graceful bool)
cleanup func()
}
func (b *Broker) bootstrapPlugins() {
if !utils.PathExists(pluginDir) {
log.Println("Plugins directory does not exist. Creating a new one.")
if err := os.Mkdir(pluginDir, 0775); err != nil {
log.Fatalln("Failed to create the plugins directory:", err)
}
}
for _, pstring := range b.config.pluginNames {
p, err := plugin.Open(path.Join(pluginDir, pstring))
if err != nil {
log.Printf("Skipping loading plugin %s: %v", pstring, err)
continue
}
pluginObj := gottPlugin{
name: pstring,
plug: p,
}
if bootstrap, err := p.Lookup("Bootstrap"); err == nil {
if bootFunc, ok := bootstrap.(func()); ok {
bootFunc()
} else if bootFunc, ok := bootstrap.(func(map[interface{}]interface{})); ok {
bootFunc(b.config.pluginConfig[pstring])
}
}
h, err := p.Lookup("OnSocketOpen")
if err == nil {
f, ok := h.(func(conn net.Conn) bool)
b.logger.Debug("plugin loader OnSocketOpen", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onSocketOpen = f
}
}
h, err = p.Lookup("OnBeforeConnect")
if err == nil {
f, ok := h.(func(clientID, username, password string) bool)
b.logger.Debug("plugin loader OnBeforeConnect", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onBeforeConnect = f
}
}
if h, err = p.Lookup("OnConnect"); err == nil {
f, ok := h.(func(clientID, username, password string) bool)
b.logger.Debug("plugin loader OnConnect", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onConnect = f
}
}
if h, err = p.Lookup("OnMessage"); err == nil {
f, ok := h.(func(clientID, username string, topic, payload []byte, dup, qos byte, retain bool))
b.logger.Debug("plugin loader OnMessage", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onMessage = f
}
}
if h, err = p.Lookup("OnBeforePublish"); err == nil {
f, ok := h.(func(clientID, username string, topic, payload []byte, dup, qos byte, retain bool) bool)
b.logger.Debug("plugin loader OnBeforePublish", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onBeforePublish = f
}
}
if h, err = p.Lookup("OnPublish"); err == nil {
f, ok := h.(func(clientID, username string, topic, payload []byte, dup, qos byte, retain bool))
b.logger.Debug("plugin loader OnPublish", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onPublish = f
}
}
if h, err = p.Lookup("OnBeforeSubscribe"); err == nil {
f, ok := h.(func(clientID, username string, topic []byte, qos byte) bool)
b.logger.Debug("plugin loader OnBeforeSubscribe", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onBeforeSubscribe = f
}
}
if h, err = p.Lookup("OnSubscribe"); err == nil {
f, ok := h.(func(clientID, username string, topic []byte, qos byte))
b.logger.Debug("plugin loader OnSubscribe", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onSubscribe = f
}
}
if h, err = p.Lookup("OnBeforeUnsubscribe"); err == nil {
f, ok := h.(func(clientID, username string, topic []byte) bool)
b.logger.Debug("plugin loader OnBeforeUnsubscribe", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onBeforeUnsubscribe = f
}
}
if h, err = p.Lookup("OnUnsubscribe"); err == nil {
f, ok := h.(func(clientID, username string, topic []byte))
b.logger.Debug("plugin loader OnUnsubscribe", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onUnsubscribe = f
}
}
if h, err = p.Lookup("OnDisconnect"); err == nil {
f, ok := h.(func(clientID, username string, graceful bool))
b.logger.Debug("plugin loader OnDisconnect", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.onDisconnect = f
}
}
if h, err = p.Lookup("Cleanup"); err == nil {
f, ok := h.(func())
b.logger.Debug("plugin loader Cleanup", zap.String("name", pstring), zap.Bool("loaded", ok))
if ok {
pluginObj.cleanup = f
}
}
b.plugins = append(b.plugins, pluginObj)
b.logger.Debug("plugin loaded", zap.String("name", pstring))
}
}
func (b *Broker) cleanupPlugins() {
for _, p := range b.plugins {
if p.cleanup != nil {
p.cleanup()
}
}
}
func (b *Broker) invokeOnSocketOpen(conn net.Conn) bool {
for _, p := range b.plugins {
if p.onSocketOpen != nil {
if !p.onSocketOpen(conn) {
return false
}
}
}
return true
}
func (b *Broker) invokeOnBeforeConnect(clientID, username, password string) bool {
for _, p := range b.plugins {
if p.onBeforeConnect != nil {
if !p.onBeforeConnect(clientID, username, password) {
return false
}
}
}
return true
}
func (b *Broker) invokeOnConnect(clientID, username, password string) bool {
for _, p := range b.plugins {
if p.onConnect != nil {
if !p.onConnect(clientID, username, password) {
return false
}
}
}
return true
}
func (b *Broker) invokeOnMessage(clientID, username string, topic, payload []byte, dup, qos byte, retain bool) {
for _, p := range b.plugins {
if p.onMessage != nil {
p.onMessage(clientID, username, topic, payload, dup, qos, retain)
}
}
}
func (b *Broker) invokeOnBeforePublish(clientID, username string, topic, payload []byte, dup, qos byte, retain bool) bool {
for _, p := range b.plugins {
if p.onBeforePublish != nil {
if !p.onBeforePublish(clientID, username, topic, payload, dup, qos, retain) {
return false
}
}
}
return true
}
func (b *Broker) invokeOnPublish(clientID, username string, topic, payload []byte, dup, qos byte, retain bool) {
for _, p := range b.plugins {
if p.onPublish != nil {
p.onPublish(clientID, username, topic, payload, dup, qos, retain)
}
}
}
func (b *Broker) invokeOnBeforeSubscribe(clientID, username string, topic []byte, qos byte) bool {
for _, p := range b.plugins {
if p.onBeforeSubscribe != nil {
if !p.onBeforeSubscribe(clientID, username, topic, qos) {
return false
}
}
}
return true
}
func (b *Broker) invokeOnSubscribe(clientID, username string, topic []byte, qos byte) {
for _, p := range b.plugins {
if p.onSubscribe != nil {
p.onSubscribe(clientID, username, topic, qos)
}
}
}
func (b *Broker) invokeOnBeforeUnsubscribe(clientID, username string, topic []byte) bool {
for _, p := range b.plugins {
if p.onBeforeUnsubscribe != nil {
if !p.onBeforeUnsubscribe(clientID, username, topic) {
return false
}
}
}
return true
}
func (b *Broker) invokeOnUnsubscribe(clientID, username string, topic []byte) {
for _, p := range b.plugins {
if p.onUnsubscribe != nil {
p.onUnsubscribe(clientID, username, topic)
}
}
}
func (b *Broker) invokeOnDisconnect(clientID, username string, graceful bool) {
for _, p := range b.plugins {
if p.onDisconnect != nil {
p.onDisconnect(clientID, username, graceful)
}
}
}