-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
67 lines (55 loc) · 1.69 KB
/
api.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
// package asn2pf produces pf firewall files from iptoasn.com tables
package asn2pf
import (
"os"
"strings"
"time"
"paepcke.de/asn2pf/asnfetch"
)
// GenerateTables ...
func GenerateTables(file, store string, fetch, noip4, noip6 bool, cmd_terms []string, skiplist string) {
// setup
t0 := time.Now()
// setup header
now := string(t0.Format(time.RFC3339))
tables := []byte(_H1 + _H2 + _H3 + "[ " + now + " ]" + _LF + _H1 + _H4 + "\"" + file + "\"" + _LF + _H5 + _LF)
// verify, fetch, report sources
srcdb := asnfetch.GetSRCDB(store, fetch, noip4, noip6)
// expander | sanitizer for commandline options
querys, table_opts, rule, workmode, tables := parseOptions(cmd_terms, tables, srcdb)
// report
if workmode == _none {
info(pad("file", 30) + file)
}
if table_opts != "" {
info(pad("table options", 30) + table_opts)
}
if rule != "" {
info(pad("firewall rules", 30) + rule)
}
// quick header only exit
if len(querys) < 1 && workmode == _none {
info("no [valid] query(s) defined -> no tables")
time.Sleep(10 * time.Millisecond) // todo: ctx need for logsec daemon shutdown
return
}
// parse sourcefiles
tables = append(tables, parser_tsv5(srcdb, table_opts, querys)...)
if skiplist != "" {
t := string(tables)
info(pad("Skiplist", 30))
for _, skip := range strings.Split(skiplist, "#") {
t = strings.ReplaceAll(t, " "+skip+" ", " ")
info("[" + skip + "] ")
}
tables = []byte(t)
info(_empty)
}
// write
if err := os.WriteFile(file, tables, 0o660); err != nil {
info("unable to write table [" + err.Error() + "]")
}
// report
info(pad("time needed", 30) + time.Since(t0).String())
time.Sleep(10 * time.Millisecond) // todo: ctx need for logsec daemon shutdown
}