-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
179 lines (164 loc) · 4.89 KB
/
event.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
package plausiblefeeder
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"strconv"
"time"
)
// PlausibleEvent represents an event for plausible and holds all data necessary to report it.
type PlausibleEvent struct {
// Header Data.
userAgent string `json:"-"` // Will marshal as "XuserAgent" if not explicitly ignored. WHY?
remoteIP net.IP `json:"-"` // Will marshal as "XremoteIP" if not explicitly ignored. WHY?
// Payload Data.
Domain string `json:"domain"`
Name string `json:"name"`
URL string `json:"url"`
StatusCode string `json:"plausible-event-statuscode"`
}
func (pef *PlausibleEventFeeder) submitToFeed(r *http.Request, statusCode int) {
// Get and check host.
plausibleDomain := r.Host
if !sliceContainsString(pef.config.Domains, plausibleDomain) {
if pef.config.ReportAnyHost {
plausibleDomain = pef.config.Domains[0]
} else {
// Ignore request to unlisted domain.
if pef.config.DebugLogging {
fmt.Fprintf(os.Stdout, "debug: ignoring request to unconfigured domain %q\n", plausibleDomain)
}
return
}
}
// Get remote IP.
var remoteIP net.IP
if pef.config.RemoteIPFromHeader != "" {
headerVal := r.Header.Get(pef.config.RemoteIPFromHeader)
if headerVal == "" {
// Ignore request if remote IP header is missing.
if pef.config.DebugLogging {
fmt.Fprintf(os.Stdout, "debug: required remote IP header field %q is missing\n", pef.config.RemoteIPFromHeader)
}
return
}
remoteIP = net.ParseIP(headerVal)
if remoteIP == nil {
// Ignore request if remote IP is invalid.
if pef.config.DebugLogging {
fmt.Fprintf(os.Stdout, "debug: remote IP from header field is invalid: %q\n", headerVal)
}
return
}
} else {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
// Ignore request if remote address is invalid.
if pef.config.DebugLogging {
fmt.Fprintf(os.Stdout, "debug: failed to parse remote address %q: %s\n", r.RemoteAddr, err)
}
return
}
remoteIP = net.ParseIP(host)
if remoteIP == nil {
// Ignore request if remote IP is invalid.
if pef.config.DebugLogging {
fmt.Fprintf(os.Stdout, "debug: remote IP from remote address is invalid: %q\n", host)
}
return
}
}
// Create event and submit to queue.
event := &PlausibleEvent{
// Header Data.
userAgent: r.UserAgent(),
remoteIP: remoteIP,
// Payload Data.
Domain: plausibleDomain,
Name: "pageview",
URL: r.RequestURI,
StatusCode: strconv.Itoa(statusCode),
}
select {
case pef.queue <- event:
default:
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q failed to submit event: queue full\n", pef.name)
}
}
func (pef *PlausibleEventFeeder) startWorker(ctx context.Context) {
for {
err := pef.plausibleEventFeeder(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q feed worker failed: %s\n", pef.name, err)
} else {
return
}
}
}
func (pef *PlausibleEventFeeder) plausibleEventFeeder(ctx context.Context) (err error) {
defer func() {
// Eecover from panic.
panicVal := recover()
if panicVal != nil {
err = fmt.Errorf("panic: %v", panicVal)
}
}()
client := &http.Client{
Timeout: 1 * time.Second,
}
for {
// Wait for event.
select {
case <-ctx.Done():
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q feed worker shutting down (canceled)\n", pef.name)
return nil
case event := <-pef.queue:
// Report event to plausible.
pef.reportEventToPlausible(ctx, client, event)
}
}
}
func (pef *PlausibleEventFeeder) reportEventToPlausible(ctx context.Context, client *http.Client, event *PlausibleEvent) {
// Create body.
payload, err := json.Marshal(event)
if err != nil {
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q failed to marshal event: %s\n", pef.name, err)
return
}
// Create request.
request, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
pef.config.EventEndpoint,
bytes.NewReader(payload),
)
if err != nil {
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q failed to create http request: %s\n", pef.name, err)
return
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("User-Agent", event.userAgent)
request.Header.Set("X-Forwarded-For", event.remoteIP.String())
// Send to plausible.
resp, err := client.Do(request)
if err != nil {
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q failed to send http request to plausible endpoint: %s\n", pef.name, err)
return
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusAccepted {
fmt.Fprintf(os.Stderr, "plausiblefeeder plugin %q got unexpected status code from plausible endpoint: %s\n", pef.name, resp.Status)
return
}
if pef.config.DebugLogging {
fmt.Fprintf(os.Stderr, "debug: sent event to %s:\n", pef.config.EventEndpoint)
fmt.Fprintf(os.Stderr, "debug: sent headers: %+v\n", request.Header)
fmt.Fprintf(os.Stderr, "debug: sent payload: %s\n", string(payload))
}
}