-
Notifications
You must be signed in to change notification settings - Fork 6
/
track_srv.go
102 lines (93 loc) · 2.97 KB
/
track_srv.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
package sparkypmtatracking
import (
"encoding/json"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
)
// TransparentGif contains the bytes that should be served back to the client for an open pixel
var TransparentGif = []byte("GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff" +
"\xff\xff\xff\x21\xf9\x04\x01\x0a\x00\x01\x00\x2c\x00\x00\x00\x00" +
"\x01\x00\x01\x00\x00\x02\x02\x4c\x01\x00\x3b\x00")
// XRealIPHeader is a header that will be used, if provided (e.g. from NGINX)
const XRealIPHeader = "X-Real-Ip"
// TrackingServer expects URL paths of the form /xyzzy
// where xyzzy = base64 urlsafe encoded, Zlib compressed, []byte
// These are written to the Redis queue
func TrackingServer(w http.ResponseWriter, req *http.Request) {
// Emulate what SparkPost engagement tracker endpoint does. Necessary only for testing with bouncy sink.
w.Header().Set("Server", "msys-http")
switch req.Method {
case "GET":
break
default:
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
s := strings.Split(req.URL.Path, "/")
if s[0] != "" || len(s) != 2 || len(s[1]) <= 0 {
log.Println("Incoming URL error:", req.URL.Path)
w.WriteHeader(http.StatusBadRequest)
return
}
var e TrackEvent
e.UserAgent = req.UserAgent()
// Look for the original client IP from Nginx, if present - check syntax then use it
if xRealIP := req.Header.Get(XRealIPHeader); xRealIP != "" {
if checkedIP := net.ParseIP(xRealIP); checkedIP != nil {
e.IPAddress = checkedIP.String()
} else {
log.Printf("Invalid %s header found=%s\n", XRealIPHeader, xRealIP)
w.WriteHeader(http.StatusBadRequest)
return
}
} else {
e.IPAddress, _, _ = net.SplitHostPort(req.RemoteAddr)
}
e.TimeStamp = strconv.FormatInt(time.Now().Unix(), 10)
eBytes, err := DecodePath(s[1])
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err = json.Unmarshal(eBytes, &e.WD); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
// Build the composite info ready to push into the Redis queue
eBytes, err = json.Marshal(e)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
// Log information received
log.Printf("Timestamp %s, IPAddress %s, UserAgent %s, Action %s, URL %s, MsgID %s\n", e.TimeStamp, e.IPAddress, e.UserAgent, e.WD.Action, e.WD.TargetLinkURL, e.WD.MessageID)
client := MyRedis()
defer client.Close()
if _, err = client.RPush(RedisQueue, eBytes).Result(); err != nil {
log.Println("Redis error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
switch e.WD.Action {
case "o":
fallthrough
case "i":
w.Header().Set("Content-Type", "image/gif")
w.Header().Set("Cache-Control", "no-cache, max-age=0")
if _, err = w.Write(TransparentGif); err != nil {
log.Println("http.ResponseWriter error", err)
}
case "c":
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Location", e.WD.TargetLinkURL)
w.WriteHeader(http.StatusFound)
}
}