Repository navigation
Expand file tree
/
Copy pathstatic_scripts.go
More file actions
128 lines (121 loc) · 3.47 KB
/
Copy pathstatic_scripts.go
File metadata and controls
128 lines (121 loc) · 3.47 KB
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
package main
import (
"embed"
"encoding/json"
"net/http"
"os"
"path/filepath"
"strings"
)
// Embed universal client scripts so single-binary deployments on ch1-3 / x-1
// always serve `/tr.js` and `/cmp.js` even if StaticDirectory lacks them.
// Disk files in StaticDirectory or ./static take precedence when present.
//
//go:embed static/tr.js static/cmp.js
var embeddedStaticScripts embed.FS
func isTrackerScriptPath(p string) (string, bool) {
switch p {
case "/tr.js", "/tr/" + apiVersion + "/tr.js":
return "tr.js", true
case "/cmp.js", "/sfpl-cmp.js",
"/tr/" + apiVersion + "/cmp.js",
"/tr/" + apiVersion + "/sfpl-cmp.js":
return "cmp.js", true
default:
return "", false
}
}
func readTrackerScriptBytes(staticDir, filename string) ([]byte, error) {
if staticDir != "" {
if b, err := os.ReadFile(filepath.Join(staticDir, filename)); err == nil && len(b) > 0 {
return b, nil
}
}
if b, err := os.ReadFile(filepath.Join("static", filename)); err == nil && len(b) > 0 {
return b, nil
}
return embeddedStaticScripts.ReadFile("static/" + filename)
}
func resolveSiteConfigForScript(r *http.Request) (SiteConfig, bool) {
if gTlsCertMgr == nil {
return SiteConfig{}, false
}
if qOid := strings.TrimSpace(r.URL.Query().Get("oid")); qOid != "" {
if s, ok := gTlsCertMgr.ResolveOid(qOid); ok {
return s, true
}
}
host := getHost(r)
if h := strings.TrimSpace(r.Header.Get("X-Forwarded-Host")); h != "" {
host = strings.Split(h, ",")[0]
}
if idx := strings.IndexByte(host, ':'); idx > 0 {
host = host[:idx]
}
return gTlsCertMgr.ResolveHost(host)
}
func buildTenantScriptPrelude(r *http.Request, filename string) string {
siteCfg, ok := resolveSiteConfigForScript(r)
if !ok || siteCfg.OID == "" {
return ""
}
thost := strings.TrimSpace(siteCfg.TrackerHost)
if thost == "" {
thost = getHost(r)
}
turl := ""
if thost != "" {
turl = "https://" + thost
}
sfplDefaults := map[string]interface{}{
"oid": siteCfg.OID,
"org": siteCfg.Org,
"trackerHost": thost,
"trackerUrl": turl,
}
if siteCfg.DomainKey != "" {
sfplDefaults["domainKey"] = siteCfg.DomainKey
}
if siteCfg.RoutePrefix != "" {
sfplDefaults["routePrefix"] = siteCfg.RoutePrefix
}
raw, err := json.Marshal(sfplDefaults)
if err != nil {
return ""
}
prelude := "window._sfpl=Object.assign(" + string(raw) + ",window._sfpl||{});\n"
if filename == "cmp.js" {
cmpDefaults := map[string]interface{}{
"oid": siteCfg.OID,
"site": siteCfg.Org,
"tracker": turl,
"thost": thost,
"dkey": siteCfg.DomainKey,
}
if cmpRaw, cerr := json.Marshal(cmpDefaults); cerr == nil {
prelude += "window.__SFPL_CMP_CONFIG__=Object.assign(" + string(cmpRaw) + ",window.__SFPL_CMP_CONFIG__||{});\n"
}
}
return prelude
}
func serveTrackerScript(cfg *Configuration, w http.ResponseWriter, r *http.Request, filename string) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Vary", "Host")
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS")
w.WriteHeader(http.StatusOK)
return
}
scriptBytes, err := readTrackerScriptBytes(cfg.StaticDirectory, filename)
if err != nil || len(scriptBytes) == 0 {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=300")
prelude := buildTenantScriptPrelude(r, filename)
if prelude != "" {
_, _ = w.Write([]byte(prelude))
}
_, _ = w.Write(scriptBytes)
}