-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
209 lines (190 loc) · 5.25 KB
/
state.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
package main
import (
"log"
"sync"
"time"
"github.com/blasrodri/frown/lsof"
"github.com/blasrodri/frown/stats"
"github.com/blasrodri/frown/ui"
)
type connectionsState struct {
connDeets map[int]map[lsof.SocketID]*lsof.ConnectionDetails
processes map[int]*lsof.Process
listOpenSockets map[int]map[lsof.SocketID]bool
SocketIDToPid map[lsof.SocketID]int
mux sync.Mutex
}
func newConnectionState() *connectionsState {
return &connectionsState{
connDeets: make(map[int]map[lsof.SocketID]*lsof.ConnectionDetails),
processes: make(map[int]*lsof.Process),
listOpenSockets: make(map[int]map[lsof.SocketID]bool),
SocketIDToPid: make(map[lsof.SocketID]int),
}
}
func (c *connectionsState) getConnDetails(pid int) map[string]*lsof.ConnectionDetails {
result, ok := c.connDeets[pid]
if !ok {
return nil
}
return result
}
func (c *connectionsState) setConnDetails(deets []*lsof.ConnectionDetails) {
for _, connDeet := range deets {
// Check that the socket id has been mapped to a pid
// and then add the connection details
pid, ok := c.SocketIDToPid[connDeet.SocketID]
if ok {
_, ok := c.connDeets[pid]
if ok {
c.connDeets[pid][connDeet.SocketID] = connDeet
c.listOpenSockets[pid][connDeet.SocketID] = true
}
}
}
// TODO: Calculate hash
}
func (c *connectionsState) setProcesses(processes []*lsof.Process) {
for _, process := range processes {
if c.processes == nil {
c.processes = make(map[int]*lsof.Process)
}
c.processes[process.Pid] = process
// If we have not seen this pid before, then create the map
// to store its open sockets
if c.listOpenSockets[process.Pid] == nil {
c.listOpenSockets[process.Pid] = make(map[lsof.SocketID]bool)
}
if c.connDeets[process.Pid] == nil {
c.connDeets[process.Pid] = make(map[lsof.SocketID]*lsof.ConnectionDetails)
}
}
// TODO: Calculate hash
}
func (c *connectionsState) getAllPIDs() []int {
result := make([]int, len(c.processes))
idx := 0
for k := range c.processes {
result[idx] = k
}
return result
}
func (c *connectionsState) setOpenSockets(pid int, listOpenSockets []lsof.SocketID) {
mOpSock := make(map[lsof.SocketID]bool, len(c.listOpenSockets))
for _, v := range listOpenSockets {
mOpSock[v] = true
}
for k := range c.listOpenSockets[pid] {
_, ok := mOpSock[k]
if !ok {
delete(c.connDeets[pid], k)
}
}
}
func manageState(config *ui.UIConfig, uiFunc func(*ui.UIConfig, <-chan *stats.Report, chan<- bool)) {
state := newConnectionState()
processesChan := make(chan []*lsof.Process)
connectionsChan := make(chan []*lsof.ConnectionDetails)
reportChan := make(chan *stats.Report)
closeChan := make(chan bool)
go manageProcceses(processesChan)
go manageConnections(state, connectionsChan)
go reportSats(state, reportChan)
go uiFunc(config, reportChan, closeChan)
var shouldStop = false
go func() {
shouldStopTemp := <-closeChan
state.mux.Lock()
shouldStop = shouldStopTemp
state.mux.Unlock()
}()
var keepRunning = true
for keepRunning {
time.Sleep(100 * time.Duration(time.Millisecond))
select {
case listProcesses := <-processesChan:
state.mux.Lock()
state.setProcesses(listProcesses)
state.mux.Unlock()
// remove state associated to dead processes
go func() {
state.mux.Lock()
for pid := range state.processes {
p := &lsof.Process{
Pid: pid,
}
openSocketsPid, err := lsof.ListOpenSockets(p)
if err != nil {
// Assume that the pid is dead. Remove it from the state
delete(state.connDeets, pid)
delete(state.processes, pid)
openSocketsForPid, ok := state.listOpenSockets[pid]
if ok {
for openSock := range openSocketsForPid {
delete(state.SocketIDToPid, openSock)
}
}
delete(state.listOpenSockets, pid)
}
for _, SocketID := range openSocketsPid {
state.SocketIDToPid[SocketID] = pid
}
}
state.mux.Unlock()
}()
case connDeets := <-connectionsChan:
state.mux.Lock()
state.setConnDetails(connDeets)
state.mux.Unlock()
default:
// Not much to do
}
state.mux.Lock()
keepRunning = !shouldStop
state.mux.Unlock()
}
}
func manageProcceses(processChan chan<- []*lsof.Process) {
for {
time.Sleep(200 * time.Duration(time.Millisecond))
userPids, err := lsof.GetUserProcessList()
if err != nil {
log.Fatal(err)
}
processChan <- userPids
}
}
func manageConnections(c *connectionsState, connectionsChan chan<- []*lsof.ConnectionDetails) {
for {
time.Sleep(200 * time.Duration(time.Millisecond))
connDeets, err := lsof.MonitorUserConnections()
if err != nil {
log.Fatal(err)
}
connectionsChan <- connDeets
}
}
func reportSats(c *connectionsState, reportChan chan<- *stats.Report) {
for {
report := stats.NewReport()
time.Sleep(200 * time.Duration(time.Millisecond))
c.mux.Lock()
for pid, sockIDToConnDeets := range c.connDeets {
if c.processes[pid] == nil {
continue
}
processName := c.processes[pid].Name
for SocketID, connDeets := range sockIDToConnDeets {
connectionReport, err := stats.AnalyzeSecurity(connDeets)
if err != nil {
log.Fatal(err)
}
report.AddConnectionReport(processName, pid, SocketID, connectionReport)
}
}
c.mux.Unlock()
if len(report.ProcessInfo) > 0 {
reportChan <- report
}
}
}