/
events.go
97 lines (84 loc) · 2.21 KB
/
events.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
package core
/*
Sliver Implant Framework
Copyright (C) 2019 Bishop Fox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Event - Sliver connect/disconnect
type Event struct {
Sliver *Sliver
Job *Job
Client *Client
EventType string
Data []byte
Err error
}
type eventBroker struct {
stop chan struct{}
publish chan Event
subscribe chan chan Event
unsubscribe chan chan Event
send chan Event
}
func (b *eventBroker) Start() {
subscribers := map[chan Event]struct{}{}
for {
select {
case <-b.stop:
for sub := range subscribers {
close(sub)
}
return
case sub := <-b.subscribe:
subscribers[sub] = struct{}{}
case sub := <-b.unsubscribe:
delete(subscribers, sub)
case event := <-b.publish:
for sub := range subscribers {
sub <- event
}
}
}
}
func (b *eventBroker) Stop() {
close(b.stop)
}
// Subscribe - Generate a new subscription channel
func (b *eventBroker) Subscribe() chan Event {
events := make(chan Event, 5)
b.subscribe <- events
return events
}
// Unsubscribe - Remove a subscription channel
func (b *eventBroker) Unsubscribe(events chan Event) {
b.unsubscribe <- events
close(events)
}
// Publish - Push a message to all subscribers
func (b *eventBroker) Publish(event Event) {
b.publish <- event
}
func newBroker() *eventBroker {
broker := &eventBroker{
stop: make(chan struct{}),
publish: make(chan Event, 1),
subscribe: make(chan chan Event, 1),
unsubscribe: make(chan chan Event, 1),
send: make(chan Event, 1),
}
go broker.Start()
return broker
}
var (
// EventBroker - Distributes event messages
EventBroker = newBroker()
)