first commit

This commit is contained in:
Oleh Astappiev 2024-10-16 15:57:59 +02:00
commit 3ca46c9ea2
No known key found for this signature in database
GPG key ID: 37D7AC22589E9632
19 changed files with 958 additions and 0 deletions

72
umami_send.go Normal file
View file

@ -0,0 +1,72 @@
package traefik_umami_feeder
import (
"net/http"
"regexp"
"strings"
)
// Copied from https://github.com/1cedsoda/traefik-umami-plugin/blob/master/umami_tracking.go
// Licensed as Apache-2.0 license
type SendPayload struct {
Website string `json:"website"`
Hostname string `json:"hostname"`
Language string `json:"language"`
Url string `json:"url"`
Referer string `json:"referer"`
Name string `json:"name"`
Data map[string]interface{} `json:"data"`
}
type SendBody struct {
Payload SendPayload `json:"payload"`
Type string `json:"type"`
}
func buildPayload(req *http.Request, websiteId string) SendPayload {
return SendPayload{
Website: websiteId,
Hostname: parseDomainFromHost(req.Host),
Language: parseAcceptLanguage(req.Header.Get("Accept-Language")),
Url: req.URL.String(),
Referer: req.Referer(),
Name: "traefik",
Data: map[string]interface{}{},
}
}
// opts the port from the host.
func parseDomainFromHost(host string) string {
// check if the host has a port
if strings.Contains(host, ":") {
host = strings.Split(host, ":")[0]
}
return host
}
const parseAcceptLanguagePattern = `([a-zA-Z\-]+)(?:;q=\d\.\d)?(?:,\s)?`
var parseAcceptLanguageRegexp = regexp.MustCompile(parseAcceptLanguagePattern)
func parseAcceptLanguage(acceptLanguage string) string {
matches := parseAcceptLanguageRegexp.FindAllStringSubmatch(acceptLanguage, -1)
if len(matches) == 0 {
return ""
}
return matches[0][1]
}
func buildSendBody(clientReq *http.Request, websiteId string) (SendBody, http.Header) {
body := SendBody{
Payload: buildPayload(clientReq, websiteId),
Type: "event",
}
var headers = make(http.Header)
headers.Set("Content-Type", "application/json")
copyHeaders(headers, clientReq.Header)
removeHeaders(headers, hopHeaders...)
writeXForwardedHeaders(headers, clientReq)
return body, headers
}