/
processes.go
99 lines (80 loc) · 1.84 KB
/
processes.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
package ports
import (
"fmt"
"log"
"strings"
"github.com/shirou/gopsutil/v3/process"
)
const (
vegaProcessName = "vega"
faucetProcessName = "faucet"
dataNodeProcessName = "data-node"
walletProcessName = "vegawallet"
dataNodeCmdName = "datanode"
walletCmdName = "wallet"
portStatusListen = "LISTEN"
nomadProcessName = "nomad"
visorProcessName = "visor"
)
var networkProcessesNames = map[string]struct{}{
vegaProcessName: {},
dataNodeProcessName: {},
walletProcessName: {},
}
// ScanNetworkProcessesPorts returns map of ports to process name for Capsule own processes.
func ScanNetworkProcessesPorts() (map[int64]string, error) {
ps, err := process.Processes()
if err != nil {
return nil, fmt.Errorf("failed to get processes: %w", err)
}
out := map[int64]string{}
for _, p := range ps {
parent, err := p.Parent()
if err != nil {
continue
}
parentName, err := parent.Name()
if err != nil {
continue
}
if !(strings.Contains(parentName, nomadProcessName) || strings.Contains(parentName, visorProcessName)) {
continue
}
currentName, err := p.Name()
if err != nil {
continue
}
if _, ok := networkProcessesNames[currentName]; !ok {
continue
}
cs, err := p.Connections()
if err != nil {
log.Printf("failed to get listen connections for process %s: %s", currentName, err)
continue
}
outName := currentName
if outName == vegaProcessName {
cmds, err := p.CmdlineSlice()
if err != nil {
continue
}
if len(cmds) == 0 {
continue
}
switch cmds[1] {
case faucetProcessName:
outName = faucetProcessName
case dataNodeCmdName:
outName = dataNodeProcessName
case walletCmdName:
outName = walletCmdName
}
}
for _, c := range cs {
if c.Status == portStatusListen {
out[int64(c.Laddr.Port)] = outName
}
}
}
return out, nil
}