forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
96 lines (78 loc) · 2.27 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
package responder
import (
"fmt"
"sync"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/logging"
)
// 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 (
GlobalPoolEventResponder = NewPoolEventResponder()
)
type PoolEventResponder struct {
mu sync.Mutex
client_responders map[int]chan *crypto_proto.VeloMessage
}
func NewPoolEventResponder() *PoolEventResponder {
return &PoolEventResponder{
client_responders: make(map[int]chan *crypto_proto.VeloMessage),
}
}
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) NewResponder(
config_obj *config_proto.Config,
req *crypto_proto.VeloMessage) *Responder {
// The PoolEventResponder input
in := make(chan *crypto_proto.VeloMessage)
// Prepare a new responder that will feed us.
result := &Responder{
request: req,
output: in,
logger: logging.GetLogger(config_obj, &logging.ClientComponent),
}
go func() {
for {
message, ok := <-in
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 {
// Try to push the message if possible.
case c <- message:
default:
}
}
}
}()
return result
}