From d9f2b21812aa7f7d2a3863e2220b0a97b770cf71 Mon Sep 17 00:00:00 2001 From: "Prad Nukala (aider)" Date: Sun, 8 Dec 2024 19:45:14 -0500 Subject: [PATCH] refactor: Update PeerInfo to extract and store comprehensive device information --- pkg/gateway/internal/session/context.go | 12 +++-- pkg/gateway/internal/session/middleware.go | 61 +++++++++++++++------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/pkg/gateway/internal/session/context.go b/pkg/gateway/internal/session/context.go index d42ee6abe..85d02bba0 100644 --- a/pkg/gateway/internal/session/context.go +++ b/pkg/gateway/internal/session/context.go @@ -48,11 +48,15 @@ func (s *HTTPContext) InitSession() error { result := s.db.Where("id = ?", sessionID).First(&sess) if result.Error != nil { // Create new session if not found - bn, bv := extractBrowserInfo(s.Context) + bn, bv, arch, plat, platVer, model := extractBrowserInfo(s.Context) sess = database.Session{ - ID: sessionID, - BrowserName: bn, - BrowserVersion: bv, + ID: sessionID, + BrowserName: bn, + BrowserVersion: bv, + UserArchitecture: arch, + Platform: plat, + PlatformVersion: platVer, + DeviceModel: model, } if err := s.db.Create(&sess).Error; err != nil { return err diff --git a/pkg/gateway/internal/session/middleware.go b/pkg/gateway/internal/session/middleware.go index df9df1afd..3b4308726 100644 --- a/pkg/gateway/internal/session/middleware.go +++ b/pkg/gateway/internal/session/middleware.go @@ -22,30 +22,51 @@ func Middleware(db *gorm.DB) echo.MiddlewareFunc { } } -func extractBrowserInfo(c echo.Context) (string, string) { - userAgent := common.HeaderRead(c, common.UserAgent) - if userAgent == "" { - return "N/A", "-1" +func extractBrowserInfo(c echo.Context) (string, string, string, string, string, string) { + // Extract all relevant headers + browserName := common.HeaderRead(c, common.UserAgent) + arch := common.HeaderRead(c, common.Architecture) + platform := common.HeaderRead(c, common.Platform) + platformVer := common.HeaderRead(c, common.PlatformVersion) + model := common.HeaderRead(c, common.Model) + fullVersionList := common.HeaderRead(c, common.FullVersionList) + + // Default values if headers are empty + if browserName == "" { + browserName = "N/A" + } + if arch == "" { + arch = "unknown" + } + if platform == "" { + platform = "unknown" + } + if platformVer == "" { + platformVer = "unknown" + } + if model == "" { + model = "unknown" } - var name, ver string - entries := strings.Split(strings.TrimSpace(userAgent), ",") - for _, entry := range entries { - entry = strings.TrimSpace(entry) - re := regexp.MustCompile(`"([^"]+)";v="([^"]+)"`) - matches := re.FindStringSubmatch(entry) + // Extract browser version from full version list + version := "-1" + if fullVersionList != "" { + entries := strings.Split(strings.TrimSpace(fullVersionList), ",") + for _, entry := range entries { + entry = strings.TrimSpace(entry) + re := regexp.MustCompile(`"([^"]+)";v="([^"]+)"`) + matches := re.FindStringSubmatch(entry) - if len(matches) == 3 { - browserName := matches[1] - version := matches[2] - - if browserName != common.BrowserNameUnknown.String() && - browserName != common.BrowserNameChromium.String() { - name = browserName - ver = version - break + if len(matches) == 3 { + browserName = matches[1] + version = matches[2] + if browserName != "Not.A/Brand" && + browserName != "Chromium" { + break + } } } } - return name, ver + + return browserName, version, arch, platform, platformVer, model }