-
Notifications
You must be signed in to change notification settings - Fork 217
/
main.go
117 lines (107 loc) · 2.71 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
package main
import (
"context"
"log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"github.com/DrmagicE/gmqtt"
"github.com/DrmagicE/gmqtt/pkg/packets"
)
var validUserMu sync.Mutex
var validUser = map[string]string{
"root": "rootpwd",
"qos0": "0pwd",
"qos1": "1pwd",
"publishonly": "ppwd",
"subscribeonly": "spwd",
}
func validateUser(username string, password string) bool {
validUserMu.Lock()
defer validUserMu.Unlock()
if pwd, ok := validUser[username]; ok {
if pwd == password {
return true
}
}
return false
}
func main() {
ln, err := net.Listen("tcp", ":1883")
if err != nil {
log.Fatalln(err.Error())
return
}
//authentication
onConnect := func(ctx context.Context, client gmqtt.Client) (code uint8) {
username := client.OptionsReader().Username()
password := client.OptionsReader().Password()
if validateUser(username, password) {
return packets.CodeAccepted
}
return packets.CodeBadUsernameorPsw
}
// acl
onSubscribe := func(ctx context.Context, client gmqtt.Client, topic packets.Topic) (qos uint8) {
if client.OptionsReader().Username() == "root" {
return topic.Qos
}
if client.OptionsReader().Username() == "qos0" {
if topic.Qos <= packets.QOS_0 {
return topic.Qos
}
return packets.QOS_0
}
if client.OptionsReader().Username() == "qos1" {
if topic.Qos <= packets.QOS_1 {
return topic.Qos
}
return packets.QOS_1
}
if client.OptionsReader().Username() == "publishonly" {
return packets.SUBSCRIBE_FAILURE
}
return topic.Qos
}
onMsgArrived := func(ctx context.Context, client gmqtt.Client, msg packets.Message) (valid bool) {
if client.OptionsReader().Username() == "subscribeonly" {
client.Close()
return false
}
//Only qos1 & qos0 are acceptable(will be delivered)
if msg.Qos() == packets.QOS_2 {
return false
}
return true
}
onClose := func(ctx context.Context, client gmqtt.Client, err error) {
log.Println("client id:"+client.OptionsReader().ClientID()+"is closed with error:", err)
}
onStop := func(ctx context.Context) {
log.Println("stop")
}
onDeliver := func(ctx context.Context, client gmqtt.Client, msg packets.Message) {
log.Printf("delivering message %s to client %s", msg.Payload(), client.OptionsReader().ClientID())
}
hooks := gmqtt.Hooks{
OnConnect: onConnect,
OnSubscribe: onSubscribe,
OnMsgArrived: onMsgArrived,
OnClose: onClose,
OnStop: onStop,
OnDeliver: onDeliver,
}
s := gmqtt.NewServer(
gmqtt.WithTCPListener(ln),
gmqtt.WithHook(hooks),
)
log.Println("started...")
s.Run()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
<-signalCh
s.Stop(context.Background())
log.Println("stopped")
}