-
Notifications
You must be signed in to change notification settings - Fork 8
/
converters.go
363 lines (322 loc) · 10.7 KB
/
converters.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package converters
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"sort"
"strings"
"sync"
"github.com/spq/pkappa2/internal/index"
)
const (
MAX_PROCESS_COUNT = 8
)
type (
Converter struct {
executablePath string
name string
// Keep track of when a process was claimed by a stream.
// If the epoch changed since the process was claimed, the process is no longer valid.
reset_epoch int
// Used by Reset to stop new processes from starting while resetting is in process.
rwmutex sync.RWMutex
// Synchronizes access to the `started_processes` and `available_processes` members
mutex sync.Mutex
// Used to signal waiting Data() calls that a process is available.
// reserveProcess() waits on this channel when all processes are in use.
signal chan struct{}
// All processes started for this converter.
started_processes map[*Process]struct{}
// Processes that are currently idle.
available_processes []*Process
// Processes that died unexpectedly.
failed_processes []*Process
}
ProcessStats struct {
Running bool
ExitCode int
Pid int
Errors int
}
ProcessStderr struct {
Pid int
Stderr []string
}
// JSON Protocol
converterStreamMetadata struct {
StreamID uint64
ClientHost string
ClientPort uint16
ServerHost string
ServerPort uint16
Protocol string
}
converterStreamChunk struct {
Direction string
Content string
}
)
var (
directionsToString = map[index.Direction]string{
index.DirectionClientToServer: "client-to-server",
index.DirectionServerToClient: "server-to-client",
}
directionsToInt = map[string]index.Direction{
"client-to-server": index.DirectionClientToServer,
"server-to-client": index.DirectionServerToClient,
}
)
func New(converterName, executablePath string) *Converter {
converter := Converter{
executablePath: executablePath,
name: converterName,
signal: make(chan struct{}),
started_processes: make(map[*Process]struct{}),
}
return &converter
}
func (converter *Converter) Name() string {
return converter.name
}
func (converter *Converter) ProcessStats() []ProcessStats {
converter.mutex.Lock()
defer converter.mutex.Unlock()
output := []ProcessStats{}
for process := range converter.started_processes {
output = append(output, ProcessStats{
Running: true,
ExitCode: process.ExitCode(),
Pid: process.Pid(),
Errors: len(process.Stderr()),
})
}
// Keep stderr and exitcode of processes that have exited.
for _, process := range converter.failed_processes {
output = append(output, ProcessStats{
Running: false,
ExitCode: process.ExitCode(),
Pid: process.Pid(),
Errors: len(process.Stderr()),
})
}
sort.Slice(output, func(i, j int) bool {
return output[i].Pid < output[j].Pid
})
return output
}
func (converter *Converter) Stderr(pid int) *ProcessStderr {
converter.mutex.Lock()
defer converter.mutex.Unlock()
for process := range converter.started_processes {
if process.Pid() != pid {
continue
}
return &ProcessStderr{
Stderr: process.Stderr(),
Pid: process.Pid(),
}
}
for _, process := range converter.failed_processes {
if process.Pid() != pid {
continue
}
return &ProcessStderr{
Stderr: process.Stderr(),
Pid: process.Pid(),
}
}
return nil
}
func (converter *Converter) MaxProcessCount() int {
return MAX_PROCESS_COUNT
}
// Stop the converter process.
func (converter *Converter) Reset() {
converter.rwmutex.Lock()
defer converter.rwmutex.Unlock()
// Signal in-use processes to stop after they finish their current job.
converter.reset_epoch++
// Kill all currently idle processes.
for _, process := range converter.available_processes {
close(process.input)
delete(converter.started_processes, process)
// Tell any waiting Data call to start a new process.
select {
case converter.signal <- struct{}{}:
default:
}
}
converter.failed_processes = nil
converter.available_processes = nil
}
func (converter *Converter) reserveProcess() (*Process, int) {
// See if we want to stop the process and we're in a Reset call. Reset would grab a write lock.
converter.rwmutex.RLock()
defer converter.rwmutex.RUnlock()
converter.mutex.Lock()
defer converter.mutex.Unlock()
// TODO: If Reset is called before Data is called, the process will start again, which we might not want.
for {
if len(converter.available_processes) > 0 {
process := converter.available_processes[len(converter.available_processes)-1]
converter.available_processes = converter.available_processes[:len(converter.available_processes)-1]
return process, converter.reset_epoch
}
if len(converter.started_processes) < MAX_PROCESS_COUNT {
process := NewProcess(converter.name, converter.executablePath)
converter.started_processes[process] = struct{}{}
return process, converter.reset_epoch
}
// Wait for signal from process that it's done.
converter.mutex.Unlock()
converter.rwmutex.RUnlock()
<-converter.signal
converter.rwmutex.RLock()
converter.mutex.Lock()
}
}
func (converter *Converter) releaseProcess(process *Process, reset_epoch int) bool {
converter.rwmutex.RLock()
defer converter.rwmutex.RUnlock()
converter.mutex.Lock()
defer converter.mutex.Unlock()
// Signal that a process is available.
select {
case converter.signal <- struct{}{}:
default:
}
if reset_epoch != converter.reset_epoch {
// The converter was reset while this process was running.
close(process.input)
// Drain the output until the process exits.
for range process.output {
}
delete(converter.started_processes, process)
if process.ExitCode() != 0 || len(process.Stderr()) > 0 {
converter.failed_processes = append(converter.failed_processes, process)
}
return false
}
converter.available_processes = append(converter.available_processes, process)
return true
}
func (converter *Converter) Data(stream *index.Stream, moreDetails bool) (data []index.Data, clientBytes, serverBytes uint64, err error) {
// TODO: Start a timeout here, so that we don't wait forever for the converter to respond
// Grab stream data before getting any locks, since this can take a while.
packets, err := stream.Data()
if err != nil {
return nil, 0, 0, fmt.Errorf("converter (%s): Failed to get packets: %w", converter.name, err)
}
metadata := converterStreamMetadata{
StreamID: stream.ID(),
ClientHost: stream.ClientHostIP(),
ClientPort: stream.ClientPort,
ServerHost: stream.ServerHostIP(),
ServerPort: stream.ServerPort,
Protocol: stream.Protocol(),
}
metadataEncoded, err := json.Marshal(metadata)
if err != nil {
return nil, 0, 0, fmt.Errorf("converter (%s): Failed to encode metadata: %w", converter.name, err)
}
process, reset_epoch := converter.reserveProcess()
log.Printf("Converter (%s): Running for stream %d", converter.name, stream.ID())
// Initiate converter protocol
process.input <- append(metadataEncoded, '\n')
readOutputLine := func(line []byte) error {
var convertedPacket converterStreamChunk
if err := json.Unmarshal(line, &convertedPacket); err != nil {
converter.releaseProcess(process, -1)
if moreDetails {
return fmt.Errorf("converter (%s): Failed to read converted packet: %w. Line:\n%s", converter.name, err, line)
}
return fmt.Errorf("converter (%s): Failed to read converted packet: %w", converter.name, err)
}
decodedData, err := base64.StdEncoding.DecodeString(convertedPacket.Content)
if err != nil {
converter.releaseProcess(process, -1)
if moreDetails {
return fmt.Errorf("converter (%s): Failed to decode converted packet data: %w. Line:\n%s", converter.name, err, line)
}
return fmt.Errorf("converter (%s): Failed to decode converted packet data: %w", converter.name, err)
}
direction, ok := directionsToInt[convertedPacket.Direction]
if !ok {
converter.releaseProcess(process, -1)
return fmt.Errorf("converter (%s): Invalid direction: %q", converter.name, convertedPacket.Direction)
}
// Merge with previous packet if both are in the same direction.
if len(data) > 0 && data[len(data)-1].Direction == direction {
data[len(data)-1].Content = append(data[len(data)-1].Content, decodedData...)
} else {
data = append(data, index.Data{Content: decodedData, Direction: direction})
}
if direction == index.DirectionClientToServer {
clientBytes += uint64(len(decodedData))
} else {
serverBytes += uint64(len(decodedData))
}
return nil
}
for _, packet := range packets {
// See if there's any output available already.
select {
case line := <-process.output:
// The protocol requires that the list of packets is terminated with an empty line.
// So if we get an empty line before the end of the list, the converter process
// exited unexpectedly or didn't follow the protocol.
if len(line) == 0 {
converter.releaseProcess(process, -1)
return nil, 0, 0, fmt.Errorf("converter (%s): Converter process exited unexpectedly. Received empty line before sending all packets", converter.name)
}
if err := readOutputLine(line); err != nil {
return nil, 0, 0, err
}
default:
}
jsonPacket := converterStreamChunk{
Direction: directionsToString[packet.Direction],
Content: base64.StdEncoding.EncodeToString(packet.Content),
}
// FIXME: Should we notify the converter about this somehow?
jsonPacketEncoded, err := json.Marshal(jsonPacket)
if err != nil {
converter.releaseProcess(process, -1)
return nil, 0, 0, fmt.Errorf("converter (%s): Failed to encode packet: %w", converter.name, err)
}
process.input <- append(jsonPacketEncoded, '\n')
}
process.input <- []byte("\n")
for line := range process.output {
if len(line) == 0 {
break
}
if err := readOutputLine(line); err != nil {
return nil, 0, 0, err
}
}
var convertedMetadata converterStreamMetadata
line, ok := <-process.output
if !ok {
converter.releaseProcess(process, -1)
if moreDetails {
stderr := process.Stderr()
if len(stderr) > 0 {
return nil, 0, 0, fmt.Errorf("converter (%s): Converter process exited unexpectedly (exitcode %d). Stderr:\n%s", converter.name, process.ExitCode(), strings.Join(stderr[:], "\n"))
}
}
return nil, 0, 0, fmt.Errorf("converter (%s): Converter process exited unexpectedly (exitcode %d)", converter.name, process.ExitCode())
}
if err := json.Unmarshal(line, &convertedMetadata); err != nil {
converter.releaseProcess(process, -1)
if moreDetails {
return nil, 0, 0, fmt.Errorf("converter (%s): Failed to read converted metadata: %w. Line:\n%s", converter.name, err, line)
}
return nil, 0, 0, fmt.Errorf("converter (%s): Failed to read converted metadata: %w", converter.name, err)
}
if !converter.releaseProcess(process, reset_epoch) {
return nil, 0, 0, fmt.Errorf("converter (%s): Converter was reset while running", converter.name)
}
return
}