ua-labels: derive source from User-Agent, persist peerID in localStorage
Server now parses the User-Agent header on /api/send when no explicit source is provided, producing labels like 'iphone-safari', 'macos-chrome', 'windows-firefox' so the feed shows where messages came from at a glance. Browser-side peerID is now stored in localStorage instead of being freshly random on each page load — refreshing or reopening the tab keeps the same identity for the same browser profile. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -31,11 +31,45 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/mileusna/useragent"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// labelFromUA returns a short, human-friendly platform label like
|
||||
// "iphone-safari", "macos-chrome", "windows-firefox". Falls back to "web".
|
||||
func labelFromUA(uaStr string) string {
|
||||
ua := useragent.Parse(uaStr)
|
||||
platform := strings.ToLower(ua.OS)
|
||||
switch {
|
||||
case ua.IsIOS() && strings.Contains(strings.ToLower(ua.Device), "ipad"):
|
||||
platform = "ipad"
|
||||
case ua.IsIOS():
|
||||
platform = "iphone"
|
||||
case ua.IsAndroid():
|
||||
platform = "android"
|
||||
case ua.IsMacOS():
|
||||
platform = "macos"
|
||||
case ua.IsWindows():
|
||||
platform = "windows"
|
||||
case ua.IsLinux():
|
||||
platform = "linux"
|
||||
}
|
||||
browser := strings.ToLower(ua.Name)
|
||||
browser = strings.ReplaceAll(browser, " ", "")
|
||||
if platform == "" && browser == "" {
|
||||
return "web"
|
||||
}
|
||||
if browser == "" {
|
||||
return platform
|
||||
}
|
||||
if platform == "" {
|
||||
return browser
|
||||
}
|
||||
return platform + "-" + browser
|
||||
}
|
||||
|
||||
//go:embed web
|
||||
var webFS embed.FS
|
||||
|
||||
@@ -251,7 +285,7 @@ func main() {
|
||||
if m.Source == "" {
|
||||
m.Source = r.Header.Get("X-Tether-Source")
|
||||
if m.Source == "" {
|
||||
m.Source = "web"
|
||||
m.Source = labelFromUA(r.Header.Get("User-Agent"))
|
||||
}
|
||||
}
|
||||
m.TS = time.Now().UnixMilli()
|
||||
|
||||
Reference in New Issue
Block a user