From 239bc469651efa75322131f7e572ae23010e1e45 Mon Sep 17 00:00:00 2001 From: Calcium-Ion <61247483+Calcium-Ion@users.noreply.github.com> Date: Sun, 5 Jan 2025 21:55:25 +0800 Subject: [PATCH 1/4] Fix channel model length issue Fixes #691 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Calcium-Ion/new-api/issues/691?shareId=XXXX-XXXX-XXXX-XXXX). --- controller/channel.go | 11 +++ model/ability.go | 2 +- model/cache.go | 180 +++++++++++++++++++++--------------------- 3 files changed, 102 insertions(+), 91 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index 1551369e1..37c7ba325 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -274,6 +274,17 @@ func AddChannel(c *gin.Context) { } localChannel := channel localChannel.Key = key + // Validate the length of the model name + models := strings.Split(localChannel.Models, ",") + for _, model := range models { + if len(model) > 256 { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": fmt.Sprintf("模型名称过长: %s", model), + }) + return + } + } channels = append(channels, localChannel) } err = model.BatchInsertChannels(channels) diff --git a/model/ability.go b/model/ability.go index bb8ae8064..e5fce3217 100644 --- a/model/ability.go +++ b/model/ability.go @@ -12,7 +12,7 @@ import ( type Ability struct { Group string `json:"group" gorm:"type:varchar(64);primaryKey;autoIncrement:false"` - Model string `json:"model" gorm:"type:varchar(64);primaryKey;autoIncrement:false"` + Model string `json:"model" gorm:"type:varchar(256);primaryKey;autoIncrement:false"` ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"` Enabled bool `json:"enabled"` Priority *int64 `json:"priority" gorm:"bigint;default:0;index"` diff --git a/model/cache.go b/model/cache.go index b6102200f..0af7664ad 100644 --- a/model/cache.go +++ b/model/cache.go @@ -14,102 +14,102 @@ import ( //func CacheGetUserGroup(id int) (group string, err error) { // if !common.RedisEnabled { // return GetUserGroup(id) -// } -// group, err = common.RedisGet(fmt.Sprintf("user_group:%d", id)) -// if err != nil { -// group, err = GetUserGroup(id) -// if err != nil { -// return "", err -// } -// err = common.RedisSet(fmt.Sprintf("user_group:%d", id), group, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) -// if err != nil { -// common.SysError("Redis set user group error: " + err.Error()) -// } -// } -// return group, err -//} + // } + // group, err = common.RedisGet(fmt.Sprintf("user_group:%d", id)) + // if err != nil { + // group, err = GetUserGroup(id) + // if err != nil { + // return "", err + // } + // err = common.RedisSet(fmt.Sprintf("user_group:%d", id), group, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) + // if err != nil { + // common.SysError("Redis set user group error: " + err.Error()) + // } + // } + // return group, err + //} // -//func CacheGetUsername(id int) (username string, err error) { -// if !common.RedisEnabled { -// return GetUsernameById(id) -// } -// username, err = common.RedisGet(fmt.Sprintf("user_name:%d", id)) -// if err != nil { -// username, err = GetUsernameById(id) -// if err != nil { -// return "", err -// } -// err = common.RedisSet(fmt.Sprintf("user_name:%d", id), username, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) -// if err != nil { -// common.SysError("Redis set user group error: " + err.Error()) -// } -// } -// return username, err -//} + //func CacheGetUsername(id int) (username string, err error) { + // if !common.RedisEnabled { + // return GetUsernameById(id) + // } + // username, err = common.RedisGet(fmt.Sprintf("user_name:%d", id)) + // if err != nil { + // username, err = GetUsernameById(id) + // if err != nil { + // return "", err + // } + // err = common.RedisSet(fmt.Sprintf("user_name:%d", id), username, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) + // if err != nil { + // common.SysError("Redis set user group error: " + err.Error()) + // } + // } + // return username, err + //} // -//func CacheGetUserQuota(id int) (quota int, err error) { -// if !common.RedisEnabled { -// return GetUserQuota(id) -// } -// quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id)) -// if err != nil { -// quota, err = GetUserQuota(id) -// if err != nil { -// return 0, err -// } -// return quota, nil -// } -// quota, err = strconv.Atoi(quotaString) -// return quota, nil -//} + //func CacheGetUserQuota(id int) (quota int, err error) { + // if !common.RedisEnabled { + // return GetUserQuota(id) + // } + // quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id)) + // if err != nil { + // quota, err = GetUserQuota(id) + // if err != nil { + // return 0, err + // } + // return quota, nil + // } + // quota, err = strconv.Atoi(quotaString) + // return quota, nil + //} // -//func CacheUpdateUserQuota(id int) error { -// if !common.RedisEnabled { -// return nil -// } -// quota, err := GetUserQuota(id) -// if err != nil { -// return err -// } -// return cacheSetUserQuota(id, quota) -//} + //func CacheUpdateUserQuota(id int) error { + // if !common.RedisEnabled { + // return nil + // } + // quota, err := GetUserQuota(id) + // if err != nil { + // return err + // } + // return cacheSetUserQuota(id, quota) + //} // -//func cacheSetUserQuota(id int, quota int) error { -// err := common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second) -// return err -//} + //func cacheSetUserQuota(id int, quota int) error { + // err := common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second) + // return err + //} // -//func CacheDecreaseUserQuota(id int, quota int) error { -// if !common.RedisEnabled { -// return nil -// } -// err := common.RedisDecrease(fmt.Sprintf("user_quota:%d", id), int64(quota)) -// return err -//} + //func CacheDecreaseUserQuota(id int, quota int) error { + // if !common.RedisEnabled { + // return nil + // } + // err := common.RedisDecrease(fmt.Sprintf("user_quota:%d", id), int64(quota)) + // return err + //} // -//func CacheIsUserEnabled(userId int) (bool, error) { -// if !common.RedisEnabled { -// return IsUserEnabled(userId) -// } -// enabled, err := common.RedisGet(fmt.Sprintf("user_enabled:%d", userId)) -// if err == nil { -// return enabled == "1", nil -// } + //func CacheIsUserEnabled(userId int) (bool, error) { + // if !common.RedisEnabled { + // return IsUserEnabled(userId) + // } + // enabled, err := common.RedisGet(fmt.Sprintf("user_enabled:%d", userId)) + // if err == nil { + // return enabled == "1", nil + // } // -// userEnabled, err := IsUserEnabled(userId) -// if err != nil { -// return false, err -// } -// enabled = "0" -// if userEnabled { -// enabled = "1" -// } -// err = common.RedisSet(fmt.Sprintf("user_enabled:%d", userId), enabled, time.Duration(constant.UserId2StatusCacheSeconds)*time.Second) -// if err != nil { -// common.SysError("Redis set user enabled error: " + err.Error()) -// } -// return userEnabled, err -//} + // userEnabled, err := IsUserEnabled(userId) + // if err != nil { + // return false, err + // } + // enabled = "0" + // if userEnabled { + // enabled = "1" + // } + // err = common.RedisSet(fmt.Sprintf("user_enabled:%d", userId), enabled, time.Duration(constant.UserId2StatusCacheSeconds)*time.Second) + // if err != nil { + // common.SysError("Redis set user enabled error: " + err.Error()) + // } + // return userEnabled, err + //} var group2model2channels map[string]map[string][]*Channel var channelsIDM map[int]*Channel @@ -181,7 +181,7 @@ func CacheGetRandomSatisfiedChannel(group string, model string, retry int) (*Cha } // if memory cache is disabled, get channel directly from database - if !common.MemoryCacheEnabled { + if (!common.MemoryCacheEnabled) { return GetRandomSatisfiedChannel(group, model, retry) } channelSyncLock.RLock() From 4fa7fefe619b6ae081051b553d09f989d2c9589a Mon Sep 17 00:00:00 2001 From: Calcium-Ion <61247483+Calcium-Ion@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:01:36 +0800 Subject: [PATCH 2/4] --- model/ability.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/model/ability.go b/model/ability.go index e5fce3217..527203075 100644 --- a/model/ability.go +++ b/model/ability.go @@ -12,7 +12,7 @@ import ( type Ability struct { Group string `json:"group" gorm:"type:varchar(64);primaryKey;autoIncrement:false"` - Model string `json:"model" gorm:"type:varchar(256);primaryKey;autoIncrement:false"` + Model string `json:"model" gorm:"type:varchar(255);primaryKey;autoIncrement:false"` ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"` Enabled bool `json:"enabled"` Priority *int64 `json:"priority" gorm:"bigint;default:0;index"` @@ -278,7 +278,6 @@ func FixAbility() (int, error) { return 0, err } var channels []Channel - if len(abilityChannelIds) == 0 { err = DB.Find(&channels).Error } else { From 4cf9d0787e4601659a50b02fe2a1489d97d91894 Mon Sep 17 00:00:00 2001 From: Calcium-Ion <61247483+Calcium-Ion@users.noreply.github.com> Date: Sun, 5 Jan 2025 22:02:46 +0800 Subject: [PATCH 3/4] Fix model name length validation limit --- controller/channel.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/channel.go b/controller/channel.go index 37c7ba325..b1fdd0c3e 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -277,7 +277,7 @@ func AddChannel(c *gin.Context) { // Validate the length of the model name models := strings.Split(localChannel.Models, ",") for _, model := range models { - if len(model) > 256 { + if len(model) > 255 { c.JSON(http.StatusOK, gin.H{ "success": false, "message": fmt.Sprintf("模型名称过长: %s", model), From 08a89a50d79bfbbbc56d5ef839c9722a4065f131 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 5 Jan 2025 22:12:39 +0800 Subject: [PATCH 4/4] revert cache.go --- model/cache.go | 180 ++++++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/model/cache.go b/model/cache.go index 0af7664ad..b6102200f 100644 --- a/model/cache.go +++ b/model/cache.go @@ -14,102 +14,102 @@ import ( //func CacheGetUserGroup(id int) (group string, err error) { // if !common.RedisEnabled { // return GetUserGroup(id) - // } - // group, err = common.RedisGet(fmt.Sprintf("user_group:%d", id)) - // if err != nil { - // group, err = GetUserGroup(id) - // if err != nil { - // return "", err - // } - // err = common.RedisSet(fmt.Sprintf("user_group:%d", id), group, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) - // if err != nil { - // common.SysError("Redis set user group error: " + err.Error()) - // } - // } - // return group, err - //} +// } +// group, err = common.RedisGet(fmt.Sprintf("user_group:%d", id)) +// if err != nil { +// group, err = GetUserGroup(id) +// if err != nil { +// return "", err +// } +// err = common.RedisSet(fmt.Sprintf("user_group:%d", id), group, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) +// if err != nil { +// common.SysError("Redis set user group error: " + err.Error()) +// } +// } +// return group, err +//} // - //func CacheGetUsername(id int) (username string, err error) { - // if !common.RedisEnabled { - // return GetUsernameById(id) - // } - // username, err = common.RedisGet(fmt.Sprintf("user_name:%d", id)) - // if err != nil { - // username, err = GetUsernameById(id) - // if err != nil { - // return "", err - // } - // err = common.RedisSet(fmt.Sprintf("user_name:%d", id), username, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) - // if err != nil { - // common.SysError("Redis set user group error: " + err.Error()) - // } - // } - // return username, err - //} +//func CacheGetUsername(id int) (username string, err error) { +// if !common.RedisEnabled { +// return GetUsernameById(id) +// } +// username, err = common.RedisGet(fmt.Sprintf("user_name:%d", id)) +// if err != nil { +// username, err = GetUsernameById(id) +// if err != nil { +// return "", err +// } +// err = common.RedisSet(fmt.Sprintf("user_name:%d", id), username, time.Duration(constant.UserId2GroupCacheSeconds)*time.Second) +// if err != nil { +// common.SysError("Redis set user group error: " + err.Error()) +// } +// } +// return username, err +//} // - //func CacheGetUserQuota(id int) (quota int, err error) { - // if !common.RedisEnabled { - // return GetUserQuota(id) - // } - // quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id)) - // if err != nil { - // quota, err = GetUserQuota(id) - // if err != nil { - // return 0, err - // } - // return quota, nil - // } - // quota, err = strconv.Atoi(quotaString) - // return quota, nil - //} +//func CacheGetUserQuota(id int) (quota int, err error) { +// if !common.RedisEnabled { +// return GetUserQuota(id) +// } +// quotaString, err := common.RedisGet(fmt.Sprintf("user_quota:%d", id)) +// if err != nil { +// quota, err = GetUserQuota(id) +// if err != nil { +// return 0, err +// } +// return quota, nil +// } +// quota, err = strconv.Atoi(quotaString) +// return quota, nil +//} // - //func CacheUpdateUserQuota(id int) error { - // if !common.RedisEnabled { - // return nil - // } - // quota, err := GetUserQuota(id) - // if err != nil { - // return err - // } - // return cacheSetUserQuota(id, quota) - //} +//func CacheUpdateUserQuota(id int) error { +// if !common.RedisEnabled { +// return nil +// } +// quota, err := GetUserQuota(id) +// if err != nil { +// return err +// } +// return cacheSetUserQuota(id, quota) +//} // - //func cacheSetUserQuota(id int, quota int) error { - // err := common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second) - // return err - //} +//func cacheSetUserQuota(id int, quota int) error { +// err := common.RedisSet(fmt.Sprintf("user_quota:%d", id), fmt.Sprintf("%d", quota), time.Duration(constant.UserId2QuotaCacheSeconds)*time.Second) +// return err +//} // - //func CacheDecreaseUserQuota(id int, quota int) error { - // if !common.RedisEnabled { - // return nil - // } - // err := common.RedisDecrease(fmt.Sprintf("user_quota:%d", id), int64(quota)) - // return err - //} +//func CacheDecreaseUserQuota(id int, quota int) error { +// if !common.RedisEnabled { +// return nil +// } +// err := common.RedisDecrease(fmt.Sprintf("user_quota:%d", id), int64(quota)) +// return err +//} // - //func CacheIsUserEnabled(userId int) (bool, error) { - // if !common.RedisEnabled { - // return IsUserEnabled(userId) - // } - // enabled, err := common.RedisGet(fmt.Sprintf("user_enabled:%d", userId)) - // if err == nil { - // return enabled == "1", nil - // } +//func CacheIsUserEnabled(userId int) (bool, error) { +// if !common.RedisEnabled { +// return IsUserEnabled(userId) +// } +// enabled, err := common.RedisGet(fmt.Sprintf("user_enabled:%d", userId)) +// if err == nil { +// return enabled == "1", nil +// } // - // userEnabled, err := IsUserEnabled(userId) - // if err != nil { - // return false, err - // } - // enabled = "0" - // if userEnabled { - // enabled = "1" - // } - // err = common.RedisSet(fmt.Sprintf("user_enabled:%d", userId), enabled, time.Duration(constant.UserId2StatusCacheSeconds)*time.Second) - // if err != nil { - // common.SysError("Redis set user enabled error: " + err.Error()) - // } - // return userEnabled, err - //} +// userEnabled, err := IsUserEnabled(userId) +// if err != nil { +// return false, err +// } +// enabled = "0" +// if userEnabled { +// enabled = "1" +// } +// err = common.RedisSet(fmt.Sprintf("user_enabled:%d", userId), enabled, time.Duration(constant.UserId2StatusCacheSeconds)*time.Second) +// if err != nil { +// common.SysError("Redis set user enabled error: " + err.Error()) +// } +// return userEnabled, err +//} var group2model2channels map[string]map[string][]*Channel var channelsIDM map[int]*Channel @@ -181,7 +181,7 @@ func CacheGetRandomSatisfiedChannel(group string, model string, retry int) (*Cha } // if memory cache is disabled, get channel directly from database - if (!common.MemoryCacheEnabled) { + if !common.MemoryCacheEnabled { return GetRandomSatisfiedChannel(group, model, retry) } channelSyncLock.RLock()