diff --git a/.vscode/launch.json b/.vscode/launch.json index 1c3564c..172928b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "type": "go", "request": "launch", "mode": "debug", - "program": "${workspaceFolder}/main.go", + "program": "${workspaceFolder}", "output": "${workspaceFolder}/dist/main.exe", "buildFlags": "-ldflags '-X sub2clash/constant.Version=dev'" } diff --git a/api/handler/short_link.go b/api/handler/short_link.go index 0e87a19..1eb865a 100644 --- a/api/handler/short_link.go +++ b/api/handler/short_link.go @@ -135,3 +135,29 @@ func GetRawConfHandler(c *gin.Context) { // 返回响应内容 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) +} diff --git a/api/route.go b/api/route.go index 256ea9d..0861945 100644 --- a/api/route.go +++ b/api/route.go @@ -44,4 +44,5 @@ func SetRoute(r *gin.Engine) { r.GET("/s/:hash", handler.GetRawConfHandler) r.POST("/short", handler.GenerateLinkHandler) r.PUT("/short", handler.UpdateLinkHandler) + r.GET("/short", handler.GetRawConfUriHandler) }