1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 15:24:42 -05:00

🐛 Parse short link

This commit is contained in:
Nite07 2024-05-09 12:58:33 +08:00
parent 3e720ec14d
commit 036907fba4
3 changed files with 28 additions and 1 deletions

2
.vscode/launch.json vendored
View File

@ -5,7 +5,7 @@
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "debug", "mode": "debug",
"program": "${workspaceFolder}/main.go", "program": "${workspaceFolder}",
"output": "${workspaceFolder}/dist/main.exe", "output": "${workspaceFolder}/dist/main.exe",
"buildFlags": "-ldflags '-X sub2clash/constant.Version=dev'" "buildFlags": "-ldflags '-X sub2clash/constant.Version=dev'"
} }

View File

@ -135,3 +135,29 @@ func GetRawConfHandler(c *gin.Context) {
// 返回响应内容 // 返回响应内容
c.String(http.StatusOK, string(all)) c.String(http.StatusOK, string(all))
} }
func GetRawConfUriHandler(c *gin.Context) {
// 获取动态路由参数
hash := c.Query("hash")
password := c.Query("password")
if strings.TrimSpace(hash) == "" {
c.String(http.StatusBadRequest, "参数错误")
return
}
// 查询数据库中的短链接
shortLink, err := database.FindShortLinkByHash(hash)
if err != nil {
c.String(http.StatusNotFound, "未找到短链接或密码错误")
return
}
// 校验密码
if shortLink.Password != "" && shortLink.Password != password {
c.String(http.StatusNotFound, "未找到短链接或密码错误")
return
}
c.String(http.StatusOK, shortLink.Url)
}

View File

@ -44,4 +44,5 @@ func SetRoute(r *gin.Engine) {
r.GET("/s/:hash", handler.GetRawConfHandler) r.GET("/s/:hash", handler.GetRawConfHandler)
r.POST("/short", handler.GenerateLinkHandler) r.POST("/short", handler.GenerateLinkHandler)
r.PUT("/short", handler.UpdateLinkHandler) r.PUT("/short", handler.UpdateLinkHandler)
r.GET("/short", handler.GetRawConfUriHandler)
} }