-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
260 lines (215 loc) · 6.09 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"os"
"regexp"
"strconv"
"time"
)
type NAT struct {
Proto NATProto
InsideGlobal net.IP
InsideGlobalPort int
InsideLocal net.IP
InsideLocalPort int
OutsideLocal net.IP
OutsideLocalPort int
OutsideGlobal net.IP
OutsideGlobalPort int
Created time.Time
Used time.Time
Timeout time.Duration
}
type NATS []*NAT
func (nats NATS) Where(fn func(*NAT) bool) NATS {
filtered := make(NATS, 0)
for _, nat := range nats {
if fn(nat) {
filtered = append(filtered, nat)
}
}
return filtered
}
type NATProto uint8
func (nat NATProto) MarshalJSON() ([]byte, error) {
return json.Marshal(NAT_NAMES[nat])
}
func (nat *NATProto) UnmarshalJSON(data []byte) error {
var dest string
err := json.Unmarshal(data, &dest)
*nat = NAT_REVERSE_NAMES[dest]
return err
}
const (
UDP_NAT NATProto = iota
TCP_NAT
STATIC_NAT
ICMP_NAT
DATE_FORMAT = "01/02/06 15:04:05"
)
var (
ROUTE_SEP = []byte("\n\n")
ROUTE_HEADER = []byte("Pro")
ROUTE_REGEXP = regexp.MustCompile(`^(-{3}|tcp|udp|icmp)\s+([\-\:0-9\.]+)\s+([\-\:0-9\.]+)\s+([\-\:0-9\.]+)\s+([\-\:0-9\.]+)$`)
TIME_REGEXP = regexp.MustCompile(`^\s+create:\s+([^,]+),\s+use:\s+([^,]+),\s+timeout:\s+([^,]+)$`)
DURATION_REGEXP = regexp.MustCompile(`^(\d\d):(\d\d):(\d\d)$`)
DURATION_REPLACE = []byte(`${1}h${2}m${3}s`)
NAT_TRANSLATION = map[byte]NATProto{
byte('u'): UDP_NAT,
byte('t'): TCP_NAT,
byte('-'): STATIC_NAT,
byte('i'): ICMP_NAT,
}
NAT_NAMES = map[NATProto]string{
UDP_NAT: "udp",
TCP_NAT: "tcp",
STATIC_NAT: "static",
ICMP_NAT: "icmp",
}
NAT_REVERSE_NAMES = reverseNATNames(NAT_NAMES)
)
func reverseNATNames(names map[NATProto]string) map[string]NATProto {
reversed := make(map[string]NATProto, len(names))
for key, value := range names {
reversed[value] = key
}
return reversed
}
func routeSplitFunc(data []byte, atEOF bool) (int, []byte, error) {
if len(data) == 0 && atEOF {
return 0, nil, nil
}
from, to, advance := 0, 0, 0
i := bytes.Index(data, ROUTE_SEP)
if i == -1 {
if !atEOF {
// We don't have a whole route, request more data
return 0, nil, nil
} else {
if bytes.HasSuffix(data, []byte("\n")) {
// Ends in a newline at EOF, we're done
return len(data), data[:len(data)-1], nil
} else {
return 0, nil, errors.New("Improperly formatted file: it must end with an empty line")
}
}
} else {
to = i
// We want to omit the seperator
advance = i + len(ROUTE_SEP)
}
if bytes.HasPrefix(data, ROUTE_HEADER) {
// This includes the header, we need to omit it
// Find the end of the header line, and omit the newline character
from = bytes.Index(data, []byte("\n")) + 1
}
return advance, data[from:to], nil
}
func (nat *NAT) Parse(data []byte) error {
var err error
lines := bytes.SplitN(data, []byte("\n"), 3)
ips := ROUTE_REGEXP.FindSubmatch(lines[0])
nat.Proto = NAT_TRANSLATION[ips[1][0]]
if nat.Proto == STATIC_NAT {
nat.InsideGlobal = net.ParseIP(string(ips[2]))
nat.InsideLocal = net.ParseIP(string(ips[3]))
nat.OutsideLocal = net.ParseIP(string(ips[4]))
nat.OutsideGlobal = net.ParseIP(string(ips[5]))
} else {
nat.InsideGlobal, nat.InsideGlobalPort, err = parseIpPort(ips[2], "Inside Global")
if err != nil {
return err
}
nat.InsideLocal, nat.InsideLocalPort, err = parseIpPort(ips[3], "Inside Local")
if err != nil {
return err
}
nat.OutsideLocal, nat.OutsideLocalPort, err = parseIpPort(ips[4], "Outside Local")
if err != nil {
return err
}
nat.OutsideGlobal, nat.OutsideGlobalPort, err = parseIpPort(ips[5], "Outside Global")
if err != nil {
return err
}
}
times := TIME_REGEXP.FindSubmatch(lines[1])
nat.Created, err = time.Parse(DATE_FORMAT, string(times[1]))
if err != nil {
return err
}
nat.Used, err = time.Parse(DATE_FORMAT, string(times[2]))
if err != nil {
return err
}
timeout := DURATION_REGEXP.ReplaceAll(times[3], DURATION_REPLACE)
nat.Timeout, err = time.ParseDuration(string(timeout))
return err
}
func parseIpPort(data []byte, name string) (net.IP, int, error) {
host, port, err := net.SplitHostPort(string(data))
if err != nil {
errStr := fmt.Sprintf("Could not parse %s address: %s", name, err)
return nil, 0, errors.New(errStr)
}
hostIp := net.ParseIP(host)
hostPort, err := strconv.Atoi(port)
if err != nil {
errStr := fmt.Sprintf("Could not parse %s port: %s", name, err)
return hostIp, 0, errors.New(errStr)
}
return hostIp, hostPort, nil
}
func main() {
routeScanner := bufio.NewScanner(os.Stdin)
routeScanner.Split(routeSplitFunc)
fmt.Println("Getting data")
var nats NATS
for routeScanner.Scan() {
nat := new(NAT)
err := nat.Parse(routeScanner.Bytes())
if err != nil {
log.Fatalln(err)
}
nats = append(nats, nat)
}
fmt.Println("Data parsed, getting counts")
long_time := 1 * time.Hour
long_time_left := func(nat *NAT) bool { return nat.Timeout > long_time }
nat_type := func(t NATProto) func(nat *NAT) bool {
return func(nat *NAT) bool {
return nat.Proto == t
}
}
udp_nats := nats.Where(nat_type(UDP_NAT))
tcp_nats := nats.Where(nat_type(TCP_NAT))
icmp_nats := nats.Where(nat_type(ICMP_NAT))
long_udp_nats := udp_nats.Where(long_time_left)
long_tcp_nats := tcp_nats.Where(long_time_left)
long_icmp_nats := icmp_nats.Where(long_time_left)
var sum_udp_timeout time.Duration
for _, nat := range udp_nats {
sum_udp_timeout += nat.Timeout
}
var sum_tcp_timeout time.Duration
for _, nat := range tcp_nats {
sum_tcp_timeout += nat.Timeout
}
fmt.Printf("average udp timeout: %v\n", sum_udp_timeout/time.Duration(len(udp_nats)))
fmt.Printf("average tcp timeout: %v\n", sum_tcp_timeout/time.Duration(len(tcp_nats)))
fmt.Printf(`Counts:
udp: %[2]d | >%[1]v left: %[3]d (%[4]d%%)
tcp: %[5]d | >%[1]v left: %[6]d (%[7]d%%)
icmp: %[8]d | >%[1]v left: %[9]d (%[10]d%%)
`,
long_time,
len(udp_nats), len(long_udp_nats), len(long_udp_nats)*100/len(udp_nats),
len(tcp_nats), len(long_tcp_nats), len(long_tcp_nats)*100/len(tcp_nats),
len(icmp_nats), len(long_icmp_nats), len(long_icmp_nats)*100/len(icmp_nats))
}