Merge pull request #201 from lanrenwo/useractlog_limitchar

修复PlatformVersion字段溢出的问题
This commit is contained in:
bjdgyc 2022-11-25 16:22:25 +08:00 committed by GitHub
commit 9809b407d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -50,7 +50,7 @@ type UserActLog struct {
Client uint8 `json:"client" xorm:"not null default 0 Int"` Client uint8 `json:"client" xorm:"not null default 0 Int"`
Version string `json:"version" xorm:"varchar(15)"` Version string `json:"version" xorm:"varchar(15)"`
DeviceType string `json:"device_type" xorm:"varchar(128) not null default ''"` DeviceType string `json:"device_type" xorm:"varchar(128) not null default ''"`
PlatformVersion string `json:"platform_version" xorm:"varchar(15) not null default ''"` PlatformVersion string `json:"platform_version" xorm:"varchar(128) not null default ''"`
Status uint8 `json:"status" xorm:"not null default 0 Int"` Status uint8 `json:"status" xorm:"not null default 0 Int"`
Info string `json:"info" xorm:"varchar(255) not null default ''"` // 详情 Info string `json:"info" xorm:"varchar(255) not null default ''"` // 详情
CreatedAt time.Time `json:"created_at" xorm:"DateTime created"` CreatedAt time.Time `json:"created_at" xorm:"DateTime created"`

View File

@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/bjdgyc/anylink/base"
"github.com/ivpusic/grpool" "github.com/ivpusic/grpool"
"github.com/spf13/cast" "github.com/spf13/cast"
"xorm.io/xorm" "xorm.io/xorm"
@ -85,8 +86,17 @@ func (ua *UserActLogProcess) Add(u UserActLog, userAgent string) {
u.Info = u.Info[2:] u.Info = u.Info[2:]
} }
} }
// limit the max length of char
u.Version = substr(u.Version, 0, 15)
u.DeviceType = substr(u.DeviceType, 0, 128)
u.PlatformVersion = substr(u.PlatformVersion, 0, 128)
u.Info = substr(u.Info, 0, 255)
UserActLogIns.Pool.JobQueue <- func() { UserActLogIns.Pool.JobQueue <- func() {
_ = Add(u) err := Add(u)
if err != nil {
base.Error("Add UserActLog error: ", err)
}
} }
} }
@ -117,6 +127,7 @@ func (ua *UserActLogProcess) GetInfoOpsById(id uint8) string {
return ua.InfoOps[id] return ua.InfoOps[id]
} }
// 解析user agent
func (ua *UserActLogProcess) ParseUserAgent(userAgent string) (os_idx, client_idx uint8, ver string) { func (ua *UserActLogProcess) ParseUserAgent(userAgent string) (os_idx, client_idx uint8, ver string) {
// Unknown // Unknown
if len(userAgent) == 0 { if len(userAgent) == 0 {
@ -187,3 +198,13 @@ func (ua *UserActLogProcess) GetSession(values url.Values) *xorm.Session {
} }
return session return session
} }
// 截取字符串
func substr(s string, pos, length int) string {
runes := []rune(s)
l := pos + length
if l > len(runes) {
l = len(runes)
}
return string(runes[pos:l])
}