-
Notifications
You must be signed in to change notification settings - Fork 0
/
matomo.go
277 lines (243 loc) · 6.82 KB
/
matomo.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* A log agent for Matomo.
*
* Copyright (C) 2024 Digitalist Open Cloud <cloud@digitalist.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
func validateTokenAuth(config *Config) error {
data := url.Values{
"module": {"API"},
"method": {"API.getMatomoVersion"},
"format": {"JSON"},
"token_auth": {config.Matomo.TokenAuth},
}
validationURL := fmt.Sprintf("%sindex.php", config.Matomo.URL)
resp, err := http.PostForm(validationURL, data)
if err != nil {
return fmt.Errorf("error validating token: %v", err)
}
defer resp.Body.Close()
// Check if the response was successful
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("invalid token_auth, received status: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response: %v", err)
}
// Define a struct to unmarshal the JSON response
type VersionResponse struct {
Value string `json:"value"`
}
var versionResp VersionResponse
err = json.Unmarshal(body, &versionResp)
if err != nil {
return fmt.Errorf("error parsing JSON: %v", err)
}
// Log the extracted version
logger.Infof("Auth token ok, Matomo version is %s", versionResp.Value)
return nil
}
func InitializeAgentURL(config *Config) {
// Ensure the Matomo URL ends with a '/', if not, add it.
if !strings.HasSuffix(config.Matomo.URL, "/") {
config.Matomo.URL += "/"
}
config.Matomo.AgentURL = config.Matomo.URL + "index.php?module=API&method=Agent.postLogData"
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if strings.Contains(item, s) {
return true
}
}
return false
}
// Matomo Tracking API call
func sendToMatomo(logData *LogData, config *Config) {
if len(logData.Host) > 0 {
logData.URL = "https://" + logData.Host + logData.URL
} else {
logData.URL = config.Matomo.WebSite + logData.URL
}
//logData.URL = config.Matomo.WebSite + logData.URL
var targetURL string
InitializeAgentURL(config)
if len(config.Log.UserAgents) > 0 && !contains(config.Log.UserAgents, logData.UserAgent) {
logger.Debugf("User agent '%s' not tracked. Skipping log.", logData.UserAgent)
return
}
// Check if the request URL contains an ignored media file extension
if isIgnored(logData.URL) {
logger.Debugf("Skipping media file request: %s", logData.URL)
return
}
if !shouldSendURL(logData.URL, config.Log.ExcludedURLs) {
logger.Debugf("URL %s is excluded, not sending to Matomo.", logData.URL)
return
}
var isDownload bool
if config.Matomo.Downloads && isDownloadableFile(logData.URL) {
isDownload = true
logger.Debugf("Downloadable file detected: %s", logData.URL)
}
var isTitleEnabled bool
if config.Title.Collect {
isTitleEnabled = true
logger.Debugf("Collect title tags from HTML")
}
var titleDomain string
if len(config.Title.Domain) > 0 {
titleDomain = config.Title.Domain
logger.Debugf("Using %s as domain to get title from HTML", titleDomain)
}
formattedTime, err := formatTimestamp(logData.Timestamp)
if err != nil {
logger.Warnf("Failed to format timestamp: %v", err)
return
}
var pageTitle string
if isTitleEnabled {
cacheFilePath := getTitleCacheFilePath(config)
err := loadCache(cacheFilePath)
if err != nil {
logger.Warnf("Failed to load title cache: %v", err)
}
// @todo: fix parameter for title domain.
//domain := titleDomain
// if domain == "" && logData.Host != "" {
// domain = logData.Host
// }
// Build full URL with domain
fullURL := logData.URL
// Load title cache and check for existing title
pageTitle, err = collectTitle(fullURL, cacheFilePath)
if err != nil {
logger.Warnf("Failed to fetch title for %s: %v", fullURL, err)
} else {
logger.Debugf("Fetched title for %s: %s", fullURL, pageTitle)
logger.Debugf("Title is: %s", pageTitle)
}
}
data := url.Values{
"idsite": {config.Matomo.SiteID},
"rec": {"1"},
"send_image": {"0"},
"cip": {logData.IP},
"ua": {logData.UserAgent},
"url": {logData.URL},
"urlref": {logData.Referrer},
"token_auth": {config.Matomo.TokenAuth},
"status_code": {logData.Status},
"cdt": {formattedTime},
}
if !isDownload {
if len(pageTitle) > 0 {
data.Set("action_name", pageTitle)
logger.Debugf("Page title is: %s", pageTitle)
} else {
logger.Warnf("No page title found for URL: %s", logData.URL)
}
}
if isDownload {
data.Set("download", logData.URL)
}
errorStatuses := map[string]bool{
"400": true,
"401": true,
"402": true,
"403": true,
"404": true,
"405": true,
"406": true,
"407": true,
"408": true,
"409": true,
"410": true,
"411": true,
"412": true,
"413": true,
"414": true,
"415": true,
"416": true,
"417": true,
"418": true,
"421": true,
"425": true,
"426": true,
"428": true,
"429": true,
"431": true,
"451": true,
"500": true,
"501": true,
"502": true,
"503": true,
"504": true,
"505": true,
"506": true,
"510": true,
"511": true,
}
// Code that is only executed if you have set plugin = true in config.
if config.Matomo.Plugin {
if errorStatuses[logData.Status] {
targetURL = config.Matomo.AgentURL
resp, err := http.PostForm(targetURL, data)
if err != nil {
logger.Error("Error sending data to Matomo:", err)
return
} else {
var Site string
if len(logData.Host) > 0 {
Site = logData.Host
} else {
Site = config.Matomo.WebSite
}
logger.Debugf("Error log sent for host %s site %s: %s, Status: %s", Site, config.Matomo.SiteID, logData.URL, resp.Status)
}
defer resp.Body.Close()
}
}
// Ensure the Matomo URL ends with a '/', if not, add it.
if !strings.HasSuffix(config.Matomo.TrackerURL, "/") {
config.Matomo.TrackerURL += "/"
}
targetURL = config.Matomo.TrackerURL
// Post to Tracker API.
resp, err := http.PostForm(targetURL+"matomo.php", data)
if err != nil {
logger.Error("Error sending data to Matomo:", err)
return
} else {
var Site string
if len(logData.Host) > 0 {
Site = logData.Host
} else {
Site = config.Matomo.WebSite
}
logger.Debugf("Log sent host %s and site %s: %s, Status: %s", Site, config.Matomo.SiteID, logData.URL, resp.Status)
}
defer resp.Body.Close()
}