summaryrefslogtreecommitdiff
path: root/reader/bunny.go
blob: 4a94913baa10eb44f51adef561726be033cefa15 (plain)
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
package reader

import (
	"fmt"
	"io"

	"github.com/ohler55/ojg/oj"

	"src.eltr.ac/eltrac/audidi/data"
)

type bunnyJSON struct {
	Timestamp       string `json:"timestamp"`
	PullZoneID      int    `json:"pullZoneId"`
	RequestID       string `json:"requestId"`
	CacheStatus     string `json:"cacheStatus"`
	StatusCode      int    `json:"statusCode"`
	BytesSent       int    `json:"bytesSent"`
	RemoteIP        string `json:"remoteIp"`
	CountryCode     string `json:"countryCode"`
	EdgeLocation    string `json:"edgeLocation"`
	Scheme          string `json:"scheme"`
	Host            string `json:"host"`
	Path            string `json:"path"`
	URL             string `json:"url"`
	UserAgent       string `json:"userAgent"`
	Referer         string `json:"referer"`
	JA4Fingerprint  string `json:"ja4Fingerprint"`
	ASN             int    `json:"asn"`
	ASNOrganization string `json:"asnOrganization"`
}

type bunnyError struct {
	Code    string   `json:"code"`
	Message string   `json:"message"`
	Details []string `json:"details"`
}

type bunnyResponse struct {
	Data  []bunnyJSON `json:"data"`
	Error *bunnyError `json:"error"`
}

// BunnyReader reads log entries from the Bunny.net Logging API v2.
type BunnyReader struct{}

// Read parses Bunny.net log records from input.
func (r *BunnyReader) Read(input io.Reader) ([]data.LogEntry, error) {
	b, err := io.ReadAll(input)
	if err != nil {
		return nil, fmt.Errorf("error reading from input")
	}

	var parser oj.Parser
	var response bunnyResponse
	parser.Unmarshal(b, &response)

	if err := response.Error; err != nil {
		return nil, fmt.Errorf("API response error %s: %s", err.Code, err.Message)
	}

	var result []data.LogEntry
	if logdata := response.Data; len(logdata) > 0 {
		for _, entry := range logdata {
			result = append(result, data.LogEntry{
				Host:        entry.Host,
				Path:        entry.Path,
				URL:         entry.URL,
				Status:      entry.StatusCode,
				CountryCode: entry.CountryCode,
				IP:          entry.RemoteIP,
				ASN:         entry.ASN,
				UserAgent:   entry.UserAgent,
				JA4:         entry.JA4Fingerprint,
				JA3:         "",
			})
		}
	}

	return result, nil
}