forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
197 lines (163 loc) · 5.34 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
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
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"io/ioutil"
"path"
"sync"
"github.com/Velocidex/yaml/v2"
config "www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
crypto_client "www.velocidex.com/golang/velociraptor/crypto/client"
crypto_utils "www.velocidex.com/golang/velociraptor/crypto/utils"
"www.velocidex.com/golang/velociraptor/executor"
"www.velocidex.com/golang/velociraptor/http_comms"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/server"
"www.velocidex.com/golang/velociraptor/utils"
)
var (
pool_client_command = app.Command(
"pool_client", "Run a pool client for load testing.")
pool_client_number = pool_client_command.Flag(
"number", "Total number of clients to run.").Int()
pool_client_writeback_dir = pool_client_command.Flag(
"writeback_dir", "The directory to store all writebacks.").Default(".").
ExistingDir()
pool_client_concurrency = pool_client_command.Flag(
"concurrency", "How many real queries to run.").Default("10").Int()
)
type counter struct {
i int
mu sync.Mutex
}
func (self *counter) Inc() {
self.mu.Lock()
defer self.mu.Unlock()
self.i++
if self.i%100 == 0 {
fmt.Printf("Starting %v clients\n", self.i)
}
}
func doPoolClient() error {
number_of_clients := *pool_client_number
if number_of_clients <= 0 {
number_of_clients = 2
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client_config, err := makeDefaultConfigLoader().
WithRequiredClient().
WithVerbose(*verbose_flag).
LoadAndValidate()
if err != nil {
return fmt.Errorf("Unable to load config file: %w", err)
}
sm, err := startEssentialServices(client_config)
if err != nil {
return fmt.Errorf("Starting services: %w", err)
}
defer sm.Close()
server.IncreaseLimits(client_config)
// Make a copy of all the configs for each client.
configs := make([]*config_proto.Config, 0, number_of_clients)
serialized, _ := json.Marshal(client_config)
for i := 0; i < number_of_clients; i++ {
client_config := &config_proto.Config{}
err := json.Unmarshal(serialized, &client_config)
if err != nil {
return fmt.Errorf("Copying configs: %w", err)
}
configs = append(configs, client_config)
}
c := counter{}
for i := 0; i < number_of_clients; i++ {
go func(i int) error {
client_config := configs[i]
filename := fmt.Sprintf("pool_client.yaml.%d", i)
client_config.Client.WritebackLinux = path.Join(
*pool_client_writeback_dir, filename)
client_config.Client.WritebackWindows = client_config.Client.WritebackLinux
if client_config.Client.LocalBuffer != nil {
client_config.Client.LocalBuffer.DiskSize = 0
}
client_config.Client.Concurrency = uint64(*pool_client_concurrency)
existing_writeback := &config_proto.Writeback{}
writeback, err := config.WritebackLocation(client_config)
if err != nil {
return fmt.Errorf("Unable to load writeback file: %w", err)
}
data, err := ioutil.ReadFile(writeback)
// Failing to read the file is not an error - the file may not
// exist yet.
if err == nil || len(data) == 0 {
err = yaml.Unmarshal(data, existing_writeback)
if err != nil {
fmt.Printf("Unable to load config file %v: %v", filename, err)
existing_writeback = &config_proto.Writeback{}
}
}
// Merge the writeback with the config.
client_config.Writeback = existing_writeback
// Force new events to be read from the server
client_config.Writeback.EventQueries = nil
// Make sure the config is ok.
err = crypto_utils.VerifyConfig(client_config)
if err != nil {
return fmt.Errorf("Invalid config: %w", err)
}
manager, err := crypto_client.NewClientCryptoManager(
client_config, []byte(client_config.Writeback.PrivateKey))
if err != nil {
return fmt.Errorf("Unable to parse config file: %w", err)
}
exe, err := executor.NewPoolClientExecutor(ctx, client_config, i)
if err != nil {
return fmt.Errorf("Can not create executor: %w", err)
}
comm, err := http_comms.NewHTTPCommunicator(
client_config,
manager,
exe,
client_config.Client.ServerUrls,
nil,
utils.RealClock{},
)
if err != nil {
return fmt.Errorf("Can not create HTTPCommunicator: %w", err)
}
c.Inc()
// Run the client in the background.
comm.Run(ctx)
return nil
}(i)
}
// Block forever.
<-ctx.Done()
return nil
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case pool_client_command.FullCommand():
FatalIfError(pool_client_command, doPoolClient)
default:
return false
}
return true
})
}