-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtianji_script.go
More file actions
147 lines (133 loc) · 3.91 KB
/
Copy pathtianji_script.go
File metadata and controls
147 lines (133 loc) · 3.91 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package traefik_tianji_plugin
import (
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
"regexp"
"strings"
)
const insertBeforeRegexPattern = `</body>`
var insertBeforeRegex = regexp.MustCompile(insertBeforeRegexPattern)
// injects the tianji script into the response head.
func regexReplaceSingle(bytes []byte, match *regexp.Regexp, replace string) []byte {
rx := match.FindIndex(bytes)
if len(rx) == 0 {
return bytes
}
// insert the script before the head tag
return append(bytes[:rx[0]], append([]byte(replace), bytes[rx[0]:]...)...)
}
// builds the tianji script.
func buildTianjiScript(config *Config) (string, error) {
// check if the script should be injected
if config.ScriptInjection == false {
return "", nil
}
// download the script
var scriptJs string
if config.ScriptInjectionMode == SIModeSource {
_scriptJs, err := downloadScript(config, context.Background())
if err != nil {
return "", err
}
scriptJs = _scriptJs
}
// src url
var src string
if config.ScriptInjectionMode == SIModeTag {
src = fmt.Sprintf(`/%s/tracker.js`, config.ForwardPath)
}
if config.EvadeGoogleTagManager {
return buildTianjiScriptWithEvade(config, scriptJs, src), nil
} else {
return buildTianjiScriptWithoutEvade(config, scriptJs, src), nil
}
}
func buildTianjiScriptWithEvade(config *Config, scriptJs, src string) string {
html := "<script>"
html += "(function () {"
html += "var el = document.createElement('script');"
html += fmt.Sprintf("el.setAttribute('data-host-url', '%s');", config.ForwardPath)
if config.ScriptInjectionMode == SIModeTag {
html += fmt.Sprintf("el.setAttribute('src', '%s');", src)
} else if config.ScriptInjectionMode == SIModeSource {
scriptBase64 := base64.StdEncoding.EncodeToString([]byte(scriptJs))
html += "el.setAttribute('type', 'text/javascript');"
html += fmt.Sprintf("el.innerHTML = atob('%s');", scriptBase64)
}
html += fmt.Sprintf("el.setAttribute('data-website-id', '%s');", config.WebsiteId)
if config.AutoTrack {
html += "el.setAttribute('data-auto-track', 'true');"
} else {
html += "el.setAttribute('data-auto-track', 'false');"
}
if config.DoNotTrack {
html += "el.setAttribute('data-do-not-track', 'true');"
}
if config.Cache {
html += "el.setAttribute('data-cache', 'true');"
}
if len(config.Domains) > 0 {
html += fmt.Sprintf("el.setAttribute('data-domains', '%s');", strings.Join(config.Domains, ","))
}
html += "document.body.appendChild(el);"
html += "})();"
html += "</script>"
return html
}
func buildTianjiScriptWithoutEvade(config *Config, scriptJs, src string) string {
html := "<script"
html += " async"
html += " defer"
html += fmt.Sprintf(" data-host-url='/%s'", config.ForwardPath)
if config.ScriptInjectionMode == SIModeTag {
html += fmt.Sprintf(" src='%s'", src)
}
html += fmt.Sprintf(" data-website-id='%s'", config.WebsiteId)
if config.AutoTrack {
html += " data-auto-track='true'"
} else {
html += " data-auto-track='false'"
}
if config.DoNotTrack {
html += " data-do-not-track='true'"
}
if config.Cache {
html += " data-cache='true'"
}
if len(config.Domains) > 0 {
html += fmt.Sprintf(" data-domains='%s'", strings.Join(config.Domains, ","))
}
html += ">"
if config.ScriptInjectionMode == SIModeSource {
html += scriptJs
}
html += "</script>"
return html
}
func downloadScript(config *Config, ctx context.Context) (string, error) {
// request
url := fmt.Sprintf("%s/tracker.js", config.TianjiHost)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "traefik-tianji-plugin")
req.Header.Set("Accept", "application/javascript")
req.Header.Set("Accept-Encoding", "identity")
// make request
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return "", err
}
// read response
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
// return the script
return string(body), nil
}