-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogparse.go
95 lines (84 loc) · 1.75 KB
/
logparse.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
package main
import (
"edu.ncsu.lib/logparse/v2/internal/apache"
"edu.ncsu.lib/logparse/v2/internal/util"
//"encoding/csv"
"encoding/json"
"flag"
"fmt"
"os"
"time"
)
type OutputEncoder interface {
Encode(interface{}) error
}
type InterestingRequest struct {
IP string `json:"ipv4"`
Source string `json:"source"`
Range string `json:"range"`
Request string `json:"query_string"`
UserAgent string `json:"user_agent"`
UASummary string `json:"user_agent_summary"`
Time time.Time
}
func showWhoisinfo(line apache.Line) {
whois, err := util.IPV4Search(line.RemoteHost)
if err != nil {
panic(err)
}
wi, err := util.ParseWhois(whois)
if err == nil {
fmt.Println(wi)
} else {
fmt.Println(err)
}
}
func main() {
var ipFile string
format := "json"
flag.StringVar(&ipFile, "b", "", "path to file for blocklist")
flag.StringVar(&format, "f", "json", "output format")
var cl *util.CIDRClassifier
var err error
flag.Parse()
if ipFile == "" {
cl = util.NewDefaultClassifier()
} else {
cl, err = util.NewClassifier(ipFile)
if err != nil {
wrap := fmt.Errorf("Unable to load blocklist file %s: %s",
ipFile,
err)
panic(wrap)
}
}
fn := "/dev/stdin"
if len(flag.Args()) > 0 {
fn = flag.Args()[0]
}
p, err := apache.NewParser()
if err != nil {
panic(err)
}
reader, err := p.Read(fn)
if err != nil {
panic(err)
}
encoder := json.NewEncoder(os.Stdout)
for rec := range reader {
if rec.Error == nil {
line := rec.Line
if ok, ip_range := cl.Contains(line.RemoteHost); ok {
rec := &InterestingRequest{
line.RemoteHost,
ip_range.Owner,
ip_range.Range,
line.URL,
line.UserAgent,
util.SummarizeUserAgent(line.UserAgent),
line.Time}
encoder.Encode(rec)
}
}
}
}