-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver_web.go
144 lines (122 loc) · 3.56 KB
/
receiver_web.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
package main
import (
"errors"
"fmt"
"net"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
uuid "github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
)
const (
// fastlyClientIP represents the IP address of the client:
// https://developer.fastly.com/reference/http/http-headers/Fastly-Client-IP/
// (retrieved on 2021-11-29)
fastlyClientIP = "Fastly-Client-IP"
indexPage = "This request is handled by tokenizer."
)
var (
errBadApiVersion = errors.New("invalid ads API version")
errBadWalletFmt = errors.New("wallet ID has bad format")
errNoFastlyHeader = fmt.Errorf("found no %q header", fastlyClientIP)
errBadFastlyAddrFormat = fmt.Errorf("bad IP address format in %q header", fastlyClientIP)
)
// clientRequest represents a client's confirmation token request. It contains
// the client's IP address and wallet ID.
type clientRequest struct {
Addr net.IP `json:"addr"`
Wallet uuid.UUID `json:"wallet"`
}
func (c *clientRequest) bytes() []byte {
return c.Addr
}
// webReceiver implements a receiver that exposes an HTTP API to receive data.
type webReceiver struct {
done chan empty
in chan serializer
router *chi.Mux
port uint16
}
func newWebReceiver() receiver {
w := &webReceiver{
in: make(chan serializer),
done: make(chan empty),
}
w.router = newRouter(w.in)
return w
}
// isValidApiVersion returns true if we're dealing with ads API version 1, 2,
// 3, or 4. As of 2023-05-05, version 1 and 2 are outdated, 3 is live, and 4
// is not yet in the works. For the sake of being future-proof, we do however
// accept version 4 already.
func isValidApiVersion(v string) bool {
num, err := strconv.ParseUint(v, 10, 0)
if err != nil {
return false
}
return num >= 1 && num <= 4
}
func newRouter(inbox chan serializer) *chi.Mux {
r := chi.NewRouter()
r.Get("/v{version}/confirmation/token/{walletID}", getConfTokenHandler(inbox))
r.Get("/", indexHandler)
return r
}
func (w *webReceiver) setConfig(c *config) {
w.port = c.port
}
func (w *webReceiver) inbox() chan serializer {
return w.in
}
func (w *webReceiver) start() {
go func() {
l.Printf("Starting Web server at :%d.", w.port)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", w.port),
Handler: w.router,
}
l.Fatal(srv.ListenAndServe())
}()
}
func (w *webReceiver) stop() {
close(w.done)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, indexPage)
}
func getConfTokenHandler(inbox chan serializer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
errAndReport := func(body string, code int) {
http.Error(w, body, code)
m.webResponses.With(prometheus.Labels{
httpCode: fmt.Sprintf("%d", code),
httpBody: body,
}).Inc()
}
if !isValidApiVersion(chi.URLParam(r, "version")) {
errAndReport(errBadApiVersion.Error(), http.StatusBadRequest)
return
}
// Make sure that the wallet ID is a valid UUID.
rawWalletID := chi.URLParam(r, "walletID")
walletID, err := uuid.Parse(rawWalletID)
if err != nil {
errAndReport(errBadWalletFmt.Error(), http.StatusBadRequest)
return
}
rawAddr := r.Header.Get(fastlyClientIP)
if rawAddr == "" {
errAndReport(errNoFastlyHeader.Error(), http.StatusBadRequest)
return
}
// Fetch the client's IP address from Fastly's proprietary header.
addr := net.ParseIP(rawAddr)
if addr == nil {
errAndReport(errBadFastlyAddrFormat.Error(), http.StatusBadRequest)
return
}
m.webResponses.With(prometheus.Labels{httpCode: "200", httpBody: ""}).Inc()
inbox <- &clientRequest{Addr: addr, Wallet: walletID}
}
}