forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
110 lines (87 loc) · 2.44 KB
/
pool.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
package responder
import (
"context"
"fmt"
"sync"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/json"
)
// The pool event responder is a singleton which distributes any
// responses to all pool clients. It is used in order to initialize
// the pool client event table:
// 1. There is a singleton actions.EventTable object running a single
// set of queries.
//
// 2. The global EventTable uses the global responder to forward event
// result set.
//
// 3. The global responder multiplexes the same result set to all pool
// clients.
// Therefore each event query result set will be duplicated to every
// pool client immediately.
var (
mu sync.Mutex
GlobalPoolEventResponder *PoolEventResponder
)
type PoolEventResponder struct {
mu sync.Mutex
ctx context.Context
// Event table will push messages to this channel and we will
// distribute them to all the other clients.
EventTableInput chan *crypto_proto.VeloMessage
client_responders map[int]chan *crypto_proto.VeloMessage
}
func GetPoolEventResponder(ctx context.Context) *PoolEventResponder {
mu.Lock()
defer mu.Unlock()
if GlobalPoolEventResponder != nil {
return GlobalPoolEventResponder
}
result := &PoolEventResponder{
ctx: ctx,
EventTableInput: make(chan *crypto_proto.VeloMessage),
client_responders: make(map[int]chan *crypto_proto.VeloMessage),
}
result.Start()
GlobalPoolEventResponder = result
return result
}
func (self *PoolEventResponder) RegisterPoolClientResponder(
id int, outbound chan *crypto_proto.VeloMessage) {
self.mu.Lock()
defer self.mu.Unlock()
self.client_responders[id] = outbound
}
// Gets a new responder which is feeding the GlobalPoolEventResponder
func (self *PoolEventResponder) Start() {
go func() {
for {
select {
case <-self.ctx.Done():
return
case message, ok := <-self.EventTableInput:
if !ok {
return
}
children := make([]chan *crypto_proto.VeloMessage, 0,
len(self.client_responders))
self.mu.Lock()
for _, c := range self.client_responders {
children = append(children, c)
}
self.mu.Unlock()
fmt.Printf("Pushing message to %v listeners\n", len(children))
json.Debug(message)
for _, c := range children {
select {
case <-self.ctx.Done():
return
// Try to push the message if possible.
case c <- message:
default:
}
}
}
}
}()
}