/
report.go
138 lines (123 loc) · 3.81 KB
/
report.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
// Copyright 2015-2023 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain
// rights in this software.
package main
import (
"bytes"
"fmt"
"text/tabwriter"
"time"
log "github.com/sandia-minimega/minimega/v2/pkg/minilog"
)
var (
dnsReportChan chan uint64
ftpReportChan chan uint64
ftpTLSReportChan chan uint64
httpReportChan chan uint64
httpTLSReportChan chan uint64
sshReportChan chan uint64
smtpReportChan chan uint64
ircReportChan chan uint64
dnsReportHits uint64
ftpReportHits uint64
ftpTLSReportHits uint64
httpReportHits uint64
httpTLSReportHits uint64
sshReportBytes uint64
smtpReportMail uint64
ircReportHits uint64
)
func init() {
dnsReportChan = make(chan uint64, 1024)
ftpReportChan = make(chan uint64, 1024)
ftpTLSReportChan = make(chan uint64, 1024)
httpReportChan = make(chan uint64, 1024)
httpTLSReportChan = make(chan uint64, 1024)
sshReportChan = make(chan uint64, 1024)
smtpReportChan = make(chan uint64, 1024)
ircReportChan = make(chan uint64, 1024)
go func() {
for {
select {
case <-dnsReportChan:
dnsReportHits++
case <-ftpReportChan:
ftpReportHits++
case <-ftpTLSReportChan:
ftpTLSReportHits++
case <-httpReportChan:
httpReportHits++
case <-httpTLSReportChan:
httpTLSReportHits++
case i := <-sshReportChan:
sshReportBytes += i
case <-smtpReportChan:
smtpReportMail++
case <-ircReportChan:
ircReportHits++
}
}
}()
}
func report(reportWait time.Duration) {
lastTime := time.Now()
lastDnsReportHits := dnsReportHits
lastftpReportHits := ftpReportHits
lastftpTLSReportHits := ftpTLSReportHits
lasthttpReportHits := httpReportHits
lasthttpTLSReportHits := httpTLSReportHits
lastsshReportBytes := sshReportBytes
lastsmtpReportMail := smtpReportMail
lastircReportHits := ircReportHits
for {
time.Sleep(reportWait)
elapsedTime := time.Since(lastTime)
lastTime = time.Now()
edns := dnsReportHits - lastDnsReportHits
eftp := ftpReportHits - lastftpReportHits
eftptls := ftpTLSReportHits - lastftpTLSReportHits
ehttp := httpReportHits - lasthttpReportHits
etls := httpTLSReportHits - lasthttpTLSReportHits
essh := sshReportBytes - lastsshReportBytes
esmtp := smtpReportMail - lastsmtpReportMail
eirc := ircReportHits - lastircReportHits
lastDnsReportHits = dnsReportHits
lastftpReportHits = ftpReportHits
lastftpTLSReportHits = ftpTLSReportHits
lasthttpReportHits = httpReportHits
lasthttpTLSReportHits = httpTLSReportHits
lastsshReportBytes = sshReportBytes
lastsmtpReportMail = smtpReportMail
lastircReportHits = ircReportHits
log.Debugln("total elapsed time: ", elapsedTime)
buf := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buf, 0, 8, 0, '\t', 0)
if *f_dns {
fmt.Fprintf(w, "dns\t%v\t%.01f hits/min\n", dnsReportHits, float64(edns)/elapsedTime.Minutes())
}
if *f_ftp {
fmt.Fprintf(w, "ftp\t%v\t%.01f hits/min\n", ftpReportHits, float64(eftp)/elapsedTime.Minutes())
}
if *f_ftps {
fmt.Fprintf(w, "ftps\t%v\t%.01f hits/min\n", ftpTLSReportHits, float64(eftptls)/elapsedTime.Minutes())
}
if *f_http {
fmt.Fprintf(w, "http\t%v\t%.01f hits/min\n", httpReportHits, float64(ehttp)/elapsedTime.Minutes())
}
if *f_https {
fmt.Fprintf(w, "https\t%v\t%.01f hits/min\n", httpTLSReportHits, float64(etls)/elapsedTime.Minutes())
}
if *f_ssh {
fmt.Fprintf(w, "ssh\t%v\t%.01f bytes/min\n", sshReportBytes, float64(essh)/elapsedTime.Minutes())
}
if *f_smtp {
fmt.Fprintf(w, "smtp\t%v\t%.01f mails/min\n", smtpReportMail, float64(esmtp)/elapsedTime.Minutes())
}
if *f_irc {
fmt.Fprintf(w, "irc\t%v\t%.01f hits/min\n", ircReportHits, float64(eirc)/elapsedTime.Minutes())
}
w.Flush()
fmt.Println(buf.String())
}
}