2024-03-13 01:30:45 -04:00
|
|
|
|
package handler
|
2023-09-17 03:52:37 -04:00
|
|
|
|
|
|
|
|
|
import (
|
2023-09-20 21:08:02 -04:00
|
|
|
|
"errors"
|
2023-09-22 20:48:45 -04:00
|
|
|
|
"io"
|
2023-09-17 04:59:02 -04:00
|
|
|
|
"net/http"
|
2023-09-22 20:48:45 -04:00
|
|
|
|
"strconv"
|
2023-09-20 21:08:02 -04:00
|
|
|
|
"strings"
|
2024-04-23 02:47:53 -04:00
|
|
|
|
"sub2clash/common"
|
|
|
|
|
"sub2clash/common/database"
|
2023-09-17 04:59:02 -04:00
|
|
|
|
"sub2clash/config"
|
2023-09-22 20:48:45 -04:00
|
|
|
|
"sub2clash/logger"
|
2023-09-17 03:52:37 -04:00
|
|
|
|
"sub2clash/model"
|
|
|
|
|
"sub2clash/validator"
|
|
|
|
|
"time"
|
2023-11-02 14:35:30 -04:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"gorm.io/gorm"
|
2023-09-17 03:52:37 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ShortLinkGenHandler(c *gin.Context) {
|
|
|
|
|
// 从请求中获取参数
|
|
|
|
|
var params validator.ShortLinkGenValidator
|
|
|
|
|
if err := c.ShouldBind(¶ms); err != nil {
|
|
|
|
|
c.String(400, "参数错误: "+err.Error())
|
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
|
if strings.TrimSpace(params.Url) == "" {
|
|
|
|
|
c.String(400, "参数错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 生成hash
|
2024-04-23 02:47:53 -04:00
|
|
|
|
hash := common.RandomString(config.Default.ShortLinkLength)
|
2023-09-20 21:08:02 -04:00
|
|
|
|
var item model.ShortLink
|
|
|
|
|
result := database.FindShortLinkByUrl(params.Url, &item)
|
|
|
|
|
if result.Error == nil {
|
2023-09-22 20:48:45 -04:00
|
|
|
|
if item.Password != params.Password {
|
2023-09-22 13:55:43 -04:00
|
|
|
|
item.Password = params.Password
|
|
|
|
|
database.SaveShortLink(&item)
|
|
|
|
|
c.String(200, item.Hash+"?password="+params.Password)
|
|
|
|
|
} else {
|
|
|
|
|
c.String(200, item.Hash)
|
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
c.String(500, "数据库错误: "+result.Error.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果记录存在则重新生成hash,直到记录不存在
|
|
|
|
|
result = database.FindShortLinkByHash(hash, &item)
|
|
|
|
|
for result.Error == nil {
|
2024-04-23 02:47:53 -04:00
|
|
|
|
hash = common.RandomString(config.Default.ShortLinkLength)
|
2023-09-20 21:08:02 -04:00
|
|
|
|
result = database.FindShortLinkByHash(hash, &item)
|
|
|
|
|
}
|
|
|
|
|
// 创建记录
|
|
|
|
|
database.FirstOrCreateShortLink(
|
2023-09-17 03:52:37 -04:00
|
|
|
|
&model.ShortLink{
|
|
|
|
|
Hash: hash,
|
|
|
|
|
Url: params.Url,
|
|
|
|
|
LastRequestTime: -1,
|
2023-09-22 11:43:26 -04:00
|
|
|
|
Password: params.Password,
|
2023-09-17 03:52:37 -04:00
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
// 返回短链接
|
2023-09-22 11:43:26 -04:00
|
|
|
|
if params.Password != "" {
|
2023-09-22 13:55:43 -04:00
|
|
|
|
hash += "?password=" + params.Password
|
2023-09-22 11:43:26 -04:00
|
|
|
|
}
|
2023-09-17 03:52:37 -04:00
|
|
|
|
c.String(200, hash)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 04:28:40 -04:00
|
|
|
|
func ShortLinkGetUrlHandler(c *gin.Context) {
|
|
|
|
|
var params validator.ShortLinkGetValidator
|
|
|
|
|
if err := c.ShouldBindQuery(¶ms); err != nil {
|
|
|
|
|
c.String(400, "参数错误: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(params.Hash) == "" {
|
|
|
|
|
c.String(400, "参数错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var shortLink model.ShortLink
|
|
|
|
|
result := database.FindShortLinkByHash(params.Hash, &shortLink)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
c.String(404, "未找到短链接")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if shortLink.Password != "" && shortLink.Password != params.Password {
|
|
|
|
|
c.String(403, "密码错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.String(200, shortLink.Url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ShortLinkGetConfigHandler(c *gin.Context) {
|
2023-09-17 03:52:37 -04:00
|
|
|
|
// 获取动态路由
|
|
|
|
|
hash := c.Param("hash")
|
2023-09-22 11:43:26 -04:00
|
|
|
|
password := c.Query("password")
|
2023-09-20 21:08:02 -04:00
|
|
|
|
if strings.TrimSpace(hash) == "" {
|
|
|
|
|
c.String(400, "参数错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-17 03:52:37 -04:00
|
|
|
|
// 查询数据库
|
|
|
|
|
var shortLink model.ShortLink
|
2023-09-20 21:08:02 -04:00
|
|
|
|
result := database.FindShortLinkByHash(hash, &shortLink)
|
2023-09-17 03:52:37 -04:00
|
|
|
|
// 重定向
|
|
|
|
|
if result.Error != nil {
|
2023-09-22 13:55:43 -04:00
|
|
|
|
c.String(404, "未找到短链接或密码错误")
|
2023-09-17 03:52:37 -04:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-22 11:43:26 -04:00
|
|
|
|
if shortLink.Password != "" && shortLink.Password != password {
|
2023-09-22 13:55:43 -04:00
|
|
|
|
c.String(404, "未找到短链接或密码错误")
|
2023-09-22 11:43:26 -04:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
|
// 更新最后访问时间
|
|
|
|
|
shortLink.LastRequestTime = time.Now().Unix()
|
|
|
|
|
database.SaveShortLink(&shortLink)
|
2024-04-23 02:47:53 -04:00
|
|
|
|
get, err := common.Get("http://localhost:" + strconv.Itoa(config.Default.Port) + "/" + shortLink.Url)
|
2023-09-22 20:48:45 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Debug("get short link data failed", zap.Error(err))
|
|
|
|
|
c.String(500, "请求错误: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
all, err := io.ReadAll(get.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.Debug("read short link data failed", zap.Error(err))
|
|
|
|
|
c.String(500, "读取错误: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.String(http.StatusOK, string(all))
|
2023-09-17 03:52:37 -04:00
|
|
|
|
}
|
2024-03-13 01:30:45 -04:00
|
|
|
|
|
|
|
|
|
func ShortLinkUpdateHandler(c *gin.Context) {
|
|
|
|
|
var params validator.ShortLinkUpdateValidator
|
|
|
|
|
if err := c.ShouldBind(¶ms); err != nil {
|
|
|
|
|
c.String(400, "参数错误: "+err.Error())
|
|
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(params.Url) == "" {
|
|
|
|
|
c.String(400, "参数错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var shortLink model.ShortLink
|
|
|
|
|
result := database.FindShortLinkByHash(params.Hash, &shortLink)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
c.String(404, "未找到短链接")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if shortLink.Password == "" {
|
|
|
|
|
c.String(403, "无法修改无密码短链接")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if shortLink.Password != params.Password {
|
|
|
|
|
c.String(403, "密码错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
shortLink.Url = params.Url
|
|
|
|
|
database.SaveShortLink(&shortLink)
|
|
|
|
|
c.String(200, "更新成功")
|
|
|
|
|
}
|