2024-02-03 11:05:10 +00:00
|
|
|
package metrics
|
2024-02-02 20:07:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2024-02-09 14:31:04 +00:00
|
|
|
"os"
|
2024-02-03 11:05:10 +00:00
|
|
|
"regexp"
|
2024-02-02 20:07:29 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-02-09 14:31:04 +00:00
|
|
|
type MetricConfig struct {
|
2024-02-03 11:05:10 +00:00
|
|
|
Url string
|
|
|
|
BotUserAgents *[]regexp.Regexp
|
|
|
|
Enabled bool
|
2024-02-02 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 20:02:17 +00:00
|
|
|
// Checks if we should send a metric ping to page-metrics based on the served path.
|
|
|
|
func (c *MetricConfig) ShouldSendMetrics(path, userAgent, dnt, gpc string) bool {
|
2024-02-03 11:05:10 +00:00
|
|
|
if !strings.HasSuffix(path, ".html") || !c.Enabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:02:17 +00:00
|
|
|
// Ignore requests where the user have set "Do-Not-Track" or "Do-Not-Sell-My-Data", even though
|
|
|
|
// there is no user data and we're not selling it.
|
|
|
|
if dnt == "1" || gpc == "1" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:05:10 +00:00
|
|
|
// Filter out bots
|
|
|
|
for _, pattern := range *c.BotUserAgents {
|
|
|
|
if pattern.MatchString(userAgent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2024-02-02 20:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 14:31:04 +00:00
|
|
|
func (c *MetricConfig) SendMetricPing(domain, path, referrer string) {
|
|
|
|
data := map[string]string{
|
|
|
|
"domain": domain,
|
|
|
|
"path": path,
|
|
|
|
"referer": referrer,
|
2024-02-02 20:07:29 +00:00
|
|
|
}
|
|
|
|
jsonData, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2024-02-09 14:31:04 +00:00
|
|
|
log.Errorf("Failed to send metric ping: %v", err)
|
2024-02-02 20:07:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-02 20:28:16 +00:00
|
|
|
log.Debugf("Sending payload %s", string(jsonData))
|
|
|
|
|
2024-02-09 14:31:04 +00:00
|
|
|
// Send the ping to the server
|
2024-02-02 20:07:29 +00:00
|
|
|
go func() {
|
2024-02-02 20:28:16 +00:00
|
|
|
res, err := http.Post(
|
2024-02-02 20:07:29 +00:00
|
|
|
c.Url,
|
|
|
|
"application/json",
|
|
|
|
strings.NewReader(string(jsonData)),
|
|
|
|
)
|
2024-02-02 20:28:16 +00:00
|
|
|
if err != nil {
|
2024-02-09 14:31:04 +00:00
|
|
|
log.Errorf("Failed to send payload to: %v", err)
|
2024-02-02 20:28:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
2024-02-09 14:31:04 +00:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
log.Errorf("Server returned non-200 status code %d", res.StatusCode)
|
2024-02-02 20:28:16 +00:00
|
|
|
}
|
2024-02-02 20:07:29 +00:00
|
|
|
}()
|
|
|
|
}
|
2024-02-03 11:05:10 +00:00
|
|
|
|
|
|
|
// Reads a JSON array of bot user agents from disk and parses them
|
|
|
|
// into regular expressions.
|
|
|
|
func ReadBotPatterns(file string) ([]regexp.Regexp, error) {
|
2024-02-09 14:31:04 +00:00
|
|
|
content, err := os.ReadFile(file)
|
2024-02-03 11:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to read bot metrics file: %v", err)
|
|
|
|
return []regexp.Regexp{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload []string
|
|
|
|
err = json.Unmarshal(content, &payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to unmarshal file: %v", err)
|
|
|
|
return []regexp.Regexp{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
patterns := make([]regexp.Regexp, 0)
|
|
|
|
for _, v := range payload {
|
|
|
|
patterns = append(
|
|
|
|
patterns,
|
|
|
|
*regexp.MustCompile(v),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return patterns, nil
|
|
|
|
}
|