-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
140 lines (115 loc) · 3.6 KB
/
plugin.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
package mixpanelproxy
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/tidwall/sjson"
)
var keysToClear = map[string]string{
"$referrer": "https://example.com",
"$referring_domain": "example.com",
"$current_url": "https://example.com:1443/admin#/status/dashboard",
"$initial_referrer": "https://example.com:1443/admin",
"$initial_referring_domain": "example.com",
}
const clearedValue = "CLEARED_BY_MIXPANEL_PROXY"
func init() {
caddy.RegisterModule(MixpanelProxy{})
httpcaddyfile.RegisterHandlerDirective("mixpanel_proxy", parseCaddyfile)
}
// MixpanelProxy implements an HTTP handler that dynamically adds the key to a mixpanel payload
type MixpanelProxy struct {
MixpanelKey string `json:"mixpanel_key"`
}
// CaddyModule returns the Caddy module information.
func (MixpanelProxy) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.mixpanel_proxy",
New: func() caddy.Module { return new(MixpanelProxy) },
}
}
// Provision implements caddy.Provisioner.
func (m *MixpanelProxy) Provision(ctx caddy.Context) error {
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m MixpanelProxy) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
err := m.MassageRequestBody(r)
if err == nil {
return next.ServeHTTP(w, r)
} else {
fmt.Println("Error while massaging the request body", err)
w.WriteHeader(http.StatusInternalServerError)
return nil
}
}
func (m MixpanelProxy) MassageRequestBody(r *http.Request) error {
defer r.Body.Close()
bodyRaw, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("mixpanel_proxy: Unable to read body: %s", err)
}
query, err := url.ParseQuery(string(bodyRaw))
if err != nil {
return fmt.Errorf("mixpanel_proxy: Unable to parse body: %s", err)
}
data := query.Get("data")
if data == "" {
return fmt.Errorf("mixpanel_proxy: Unable to read 'data' in body")
}
data, err = sjson.Set(data, "#.properties.token", m.MixpanelKey)
if err != nil {
return fmt.Errorf("mixpanel_proxy: Unable to set token value: %s", err)
}
for toClear, v := range keysToClear {
data, err = sjson.Set(data, "#.properties."+toClear, v)
if err != nil {
return fmt.Errorf("mixpanel_proxy: Unable to set clear %s key: %s", toClear, err)
}
}
newBody := "data=" + url.QueryEscape(data)
newBody = strings.Replace(newBody, "+", "%20", -1)
newBodyBytes := []byte(newBody)
r.Header.Set("Content-Length", strconv.Itoa(len(newBodyBytes)))
r.ContentLength = int64(len(newBodyBytes))
r.Body = ioutil.NopCloser(bytes.NewBuffer(newBodyBytes))
return nil
}
// UnmarshalCaddyfile - this is a no-op
func (m *MixpanelProxy) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
arg1 := d.NextArg()
arg2 := d.NextArg()
// Parse standalone length
if arg1 && arg2 {
val := d.Val()
m.MixpanelKey = val
if m.MixpanelKey == "" {
return d.Err("empty mixpanel key")
}
} else {
return d.Err("missing mixpanel key")
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
m := new(MixpanelProxy)
err := m.UnmarshalCaddyfile(h.Dispenser)
if err != nil {
return nil, err
}
return m, nil
}
// Interface guards
var (
_ caddy.Provisioner = (*MixpanelProxy)(nil)
_ caddyhttp.MiddlewareHandler = (*MixpanelProxy)(nil)
_ caddyfile.Unmarshaler = (*MixpanelProxy)(nil)
)