refactor: Update PeerInfo to extract and store comprehensive device information

This commit is contained in:
Prad Nukala 2024-12-08 19:45:14 -05:00
parent fb9f41248f
commit d9f2b21812
2 changed files with 49 additions and 24 deletions

View File

@ -48,11 +48,15 @@ func (s *HTTPContext) InitSession() error {
result := s.db.Where("id = ?", sessionID).First(&sess) result := s.db.Where("id = ?", sessionID).First(&sess)
if result.Error != nil { if result.Error != nil {
// Create new session if not found // Create new session if not found
bn, bv := extractBrowserInfo(s.Context) bn, bv, arch, plat, platVer, model := extractBrowserInfo(s.Context)
sess = database.Session{ sess = database.Session{
ID: sessionID, ID: sessionID,
BrowserName: bn, BrowserName: bn,
BrowserVersion: bv, BrowserVersion: bv,
UserArchitecture: arch,
Platform: plat,
PlatformVersion: platVer,
DeviceModel: model,
} }
if err := s.db.Create(&sess).Error; err != nil { if err := s.db.Create(&sess).Error; err != nil {
return err return err

View File

@ -22,30 +22,51 @@ func Middleware(db *gorm.DB) echo.MiddlewareFunc {
} }
} }
func extractBrowserInfo(c echo.Context) (string, string) { func extractBrowserInfo(c echo.Context) (string, string, string, string, string, string) {
userAgent := common.HeaderRead(c, common.UserAgent) // Extract all relevant headers
if userAgent == "" { browserName := common.HeaderRead(c, common.UserAgent)
return "N/A", "-1" 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 // Extract browser version from full version list
entries := strings.Split(strings.TrimSpace(userAgent), ",") version := "-1"
for _, entry := range entries { if fullVersionList != "" {
entry = strings.TrimSpace(entry) entries := strings.Split(strings.TrimSpace(fullVersionList), ",")
re := regexp.MustCompile(`"([^"]+)";v="([^"]+)"`) for _, entry := range entries {
matches := re.FindStringSubmatch(entry) entry = strings.TrimSpace(entry)
re := regexp.MustCompile(`"([^"]+)";v="([^"]+)"`)
matches := re.FindStringSubmatch(entry)
if len(matches) == 3 { if len(matches) == 3 {
browserName := matches[1] browserName = matches[1]
version := matches[2] version = matches[2]
if browserName != "Not.A/Brand" &&
if browserName != common.BrowserNameUnknown.String() && browserName != "Chromium" {
browserName != common.BrowserNameChromium.String() { break
name = browserName }
ver = version
break
} }
} }
} }
return name, ver
return browserName, version, arch, platform, platformVer, model
} }