1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 20:14:42 -05:00
This commit is contained in:
Nite07 2023-09-23 01:55:43 +08:00
parent 67e4121fa6
commit be9df16b61

View File

@ -26,11 +26,16 @@ func ShortLinkGenHandler(c *gin.Context) {
} }
// 生成hash // 生成hash
hash := utils.RandomString(config.Default.ShortLinkLength) hash := utils.RandomString(config.Default.ShortLinkLength)
// 存入数据库
var item model.ShortLink var item model.ShortLink
result := database.FindShortLinkByUrl(params.Url, &item) result := database.FindShortLinkByUrl(params.Url, &item)
if result.Error == nil { if result.Error == nil {
c.String(200, item.Hash) if params.Password != "" && item.Password != params.Password {
item.Password = params.Password
database.SaveShortLink(&item)
c.String(200, item.Hash+"?password="+params.Password)
} else {
c.String(200, item.Hash)
}
return return
} else { } else {
if !errors.Is(result.Error, gorm.ErrRecordNotFound) { if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
@ -55,7 +60,7 @@ func ShortLinkGenHandler(c *gin.Context) {
) )
// 返回短链接 // 返回短链接
if params.Password != "" { if params.Password != "" {
hash += "/?password=" + params.Password hash += "?password=" + params.Password
} }
c.String(200, hash) c.String(200, hash)
} }
@ -73,11 +78,11 @@ func ShortLinkGetHandler(c *gin.Context) {
result := database.FindShortLinkByHash(hash, &shortLink) result := database.FindShortLinkByHash(hash, &shortLink)
// 重定向 // 重定向
if result.Error != nil { if result.Error != nil {
c.String(404, "未找到短链接") c.String(404, "未找到短链接或密码错误")
return return
} }
if shortLink.Password != "" && shortLink.Password != password { if shortLink.Password != "" && shortLink.Password != password {
c.String(403, "密码错误") c.String(404, "未找到短链接或密码错误")
return return
} }
// 更新最后访问时间 // 更新最后访问时间