package session import ( "regexp" "strings" "github.com/labstack/echo/v4" "github.com/onsonr/sonr/pkg/common" "gorm.io/gorm" ) // Middleware creates a new session middleware func Middleware(db *gorm.DB) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { cc := NewHTTPContext(c, db) if err := cc.InitSession(); err != nil { return err } return next(cc) } } } 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" } // 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 != "Not.A/Brand" && browserName != "Chromium" { break } } } } return browserName, version, arch, platform, platformVer, model }