-
Notifications
You must be signed in to change notification settings - Fork 5
/
receiver.go
126 lines (108 loc) · 2.56 KB
/
receiver.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
package client
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/erasche/gologme/loggers"
gologme "github.com/erasche/gologme/types"
)
type lgr struct {
KeyLogger loggers.LogGenerator
WindowLogger loggers.LogGenerator
Receiver *Receiver
winLogs []*gologme.WindowLogs
keyLogs []*gologme.KeyLogs
}
func (l *lgr) setupLoggers() {
keyLogger, err := loggers.CreateLogGenerator(map[string]string{
"LOGGER": "keys",
"X11_DEVICE_ID": "11",
})
// Must have a key Logger available. Maybe no panic later
if err != nil {
panic(err)
}
windowLogger, err := loggers.CreateLogGenerator(map[string]string{
"LOGGER": "windows",
})
// Must have a window Logger available. Maybe no panic later
if err != nil {
panic(err)
}
l.KeyLogger = keyLogger
l.WindowLogger = windowLogger
l.winLogs = make([]*gologme.WindowLogs, 0)
l.keyLogs = make([]*gologme.KeyLogs, 0)
}
func (l *lgr) Updater(windowLogGranularity int, keyLogGranularity int) {
go func() {
// Fetch freshest logs
c := time.Tick(
time.Millisecond * time.Duration(windowLogGranularity),
)
for _ = range c {
newWLogs := l.WindowLogger.GetFreshestTxtLogs()
if newWLogs != nil {
l.winLogs = append(
l.winLogs,
newWLogs,
)
}
}
}()
go func() {
// Fetch freshest logs
c := time.Tick(
time.Millisecond * time.Duration(keyLogGranularity),
)
for _ = range c {
newKLogs := l.KeyLogger.GetFreshestNumLogs()
if newKLogs != nil {
l.keyLogs = append(
l.keyLogs,
newKLogs,
)
}
}
}()
}
func (l *lgr) SendLogs() {
widx := len(l.winLogs)
kidx := len(l.keyLogs)
l.Receiver.Send(l.winLogs[:widx], l.keyLogs[:kidx])
// This seems like we need a sync on it, but I'm not smart enough for that
// stuff just yet.
l.winLogs = l.winLogs[widx:]
l.keyLogs = l.keyLogs[kidx:]
}
// Golog function which starts local loggers
func Golog(windowLogGranularity int, keyLogGranularity int, standalone bool, serverAddr string) {
// Setup our receiver
receiver := &Receiver{
ServerAddress: serverAddr,
}
// Setup our loggers
l := &lgr{
Receiver: receiver,
}
l.setupLoggers()
// Trigger our updater in the background
l.Updater(windowLogGranularity, keyLogGranularity)
// Trap the exit signal
exitChan := make(chan os.Signal, 1)
signal.Notify(exitChan, os.Interrupt)
signal.Notify(exitChan, syscall.SIGTERM)
// And push remaining entries to the server
go func() {
// In cleanup, we need to
<-exitChan
l.SendLogs()
os.Exit(0)
}()
// Every 10 seconds, send logs
c := time.Tick(10 * time.Second)
for _ = range c {
l.SendLogs()
}
}