-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (183 loc) · 4.46 KB
/
main.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
package main
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/nxadm/tail"
"github.com/urfave/cli/v2"
"io"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"sync"
"syscall"
)
func main() {
app := &cli.App{
Name: "sslkeylogmerge",
Usage: "merge multiple sslkeylogs into one",
Action: mainFunc,
EnableBashCompletion: true,
Suggest: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Usage: "output `file`",
Required: true,
Aliases: []string{"o"},
EnvVars: []string{"SSLKEYLOGFILE"},
TakesFile: true,
},
&cli.StringSliceFlag{
Name: "input",
Usage: "individual input `file`(s)",
Aliases: []string{"i"},
Required: false,
TakesFile: true,
},
&cli.StringSliceFlag{
Name: "watch",
Usage: "watch `directory`(ies)",
Required: false,
Hidden: false,
Aliases: []string{"w"},
TakesFile: true,
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func mainFunc(ctx *cli.Context) error {
// get notified of signals
myCtx, _ := signal.NotifyContext(ctx.Context, syscall.SIGTERM, syscall.SIGINT, syscall.SIGABRT, syscall.SIGKILL)
ctx.Context = myCtx
wg := &sync.WaitGroup{}
// open the output file
outputFilePath := ctx.String("output")
foutFile, err := os.OpenFile(outputFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY|os.O_SYNC, 0600)
if err != nil {
panic(err) // TODO handle this error
}
// make it thread-safe with a lock
fout := &SyncWriter{
mux: sync.Mutex{},
output: foutFile,
}
// make a list of the individual input files
inFiles := ctx.StringSlice("input")
// For each watch directory
/// Set up fsnotify on the directory to watch for new files
//// when a new file is found, start a goroutine to read it
/// List all files in the directory, add them to the list of files
watchDirs := ctx.StringSlice("watch")
if len(watchDirs) > 0 {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err) // todo handle this error
}
for _, d := range watchDirs {
files, _ := os.ReadDir(d)
// assume that any existing files should be included
for _, f := range files {
if !f.IsDir() {
fPath := path.Join(d, f.Name())
inFiles = append(inFiles, fPath)
}
}
go HandleWatcher(ctx, watcher, wg, fout)
// Watch each of the directories
err := watcher.Add(d)
if err != nil {
fmt.Println(err.Error())
}
}
}
/// begin goroutines to read each input file line-by-line
for _, inFilePath := range inFiles {
wg.Add(1)
go ReadFile(ctx, wg, inFilePath, fout)
}
wg.Wait()
// stop all reader goroutines
// write
fout.mux.Lock()
_ = foutFile.Close()
return nil
}
func HandleWatcher(ctx *cli.Context, watcher *fsnotify.Watcher, wg *sync.WaitGroup, output *SyncWriter) {
for {
select {
case event, okay := <-watcher.Events:
{
if okay {
if event.Has(fsnotify.Create) {
// TODO is event.Name an absolute path?
fmt.Printf("New File: %s\n", event.Name)
wg.Add(1)
go ReadFile(ctx, wg, event.Name, output)
}
}
}
case err, okay := <-watcher.Errors:
{
if okay {
fmt.Printf("Watcher Err: %s\n", err.Error())
}
}
case <-ctx.Context.Done():
{
fmt.Println("No longer listening for fsnotify events")
return
}
}
}
}
func ReadFile(ctx *cli.Context, wg *sync.WaitGroup, inFilePath string, output *SyncWriter) {
defer wg.Done()
inFileAbs, _ := filepath.Abs(inFilePath)
outFileAbs, _ := filepath.Abs(ctx.String("output"))
if inFileAbs == outFileAbs {
fmt.Println("Ignoring output file as input in order to avoid infinite loop")
return
}
fin, err := tail.TailFile(inFilePath, tail.Config{Follow: true, ReOpen: true, CompleteLines: true})
if err != nil {
fmt.Printf("err tailing %s: %s\n", inFilePath, err.Error())
return
}
for {
select {
case line := <-fin.Lines:
{
if line == nil {
continue
}
if line.Err != nil {
fmt.Printf("Error: %s\n", err.Error())
}
_, err = output.Write(append([]byte(line.Text), '\n'))
if err != nil {
fmt.Printf("Error writing: %s\n", err.Error())
return
}
}
case <-ctx.Context.Done():
{
fmt.Printf("Stopping Reader for %s\n", inFilePath)
return
}
}
}
}
type SyncWriter struct {
mux sync.Mutex
output io.Writer
}
func (s *SyncWriter) Write(p []byte) (n int, err error) {
s.mux.Lock()
defer s.mux.Unlock()
return s.output.Write(p)
}