2024-03-13 01:30:45 -04:00
|
|
|
package handler
|
2023-09-17 03:52:37 -04:00
|
|
|
|
|
|
|
import (
|
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-24 00:51:37 -04:00
|
|
|
"time"
|
|
|
|
|
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-17 03:52:37 -04:00
|
|
|
"sub2clash/model"
|
|
|
|
"sub2clash/validator"
|
2023-11-02 14:35:30 -04:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-09-17 03:52:37 -04:00
|
|
|
)
|
|
|
|
|
2024-04-24 00:51:37 -04:00
|
|
|
func respondWithError(c *gin.Context, code int, message string) {
|
|
|
|
c.String(code, message)
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateLinkHandler(c *gin.Context) {
|
2023-09-17 03:52:37 -04:00
|
|
|
var params validator.ShortLinkGenValidator
|
|
|
|
if err := c.ShouldBind(¶ms); err != nil {
|
2024-04-24 00:51:37 -04:00
|
|
|
respondWithError(c, http.StatusBadRequest, "参数错误: "+err.Error())
|
|
|
|
return
|
2023-09-17 03:52:37 -04:00
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
if strings.TrimSpace(params.Url) == "" {
|
2024-04-24 00:51:37 -04:00
|
|
|
respondWithError(c, http.StatusBadRequest, "URL 不能为空")
|
2023-09-20 21:08:02 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
|
|
|
|
hash, err := generateUniqueHash()
|
|
|
|
if err != nil {
|
|
|
|
respondWithError(c, http.StatusInternalServerError, "生成短链接失败")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shortLink := model.ShortLink{
|
|
|
|
Hash: hash,
|
|
|
|
Url: params.Url,
|
|
|
|
Password: params.Password,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.SaveShortLink(&shortLink); err != nil {
|
|
|
|
respondWithError(c, http.StatusInternalServerError, "数据库错误")
|
2023-09-20 21:08:02 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51: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
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
c.String(http.StatusOK, hash)
|
2023-09-17 03:52:37 -04:00
|
|
|
}
|
|
|
|
|
2024-04-24 00:51:37 -04:00
|
|
|
func generateUniqueHash() (string, error) {
|
|
|
|
for {
|
|
|
|
hash := common.RandomString(config.Default.ShortLinkLength)
|
|
|
|
exists, err := database.CheckShortLinkHashExists(hash)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return hash, nil
|
|
|
|
}
|
2024-03-13 04:28:40 -04:00
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateLinkHandler(c *gin.Context) {
|
|
|
|
var params validator.ShortLinkUpdateValidator
|
|
|
|
if err := c.ShouldBindJSON(¶ms); err != nil {
|
|
|
|
respondWithError(c, http.StatusBadRequest, "参数错误: "+err.Error())
|
2024-03-13 04:28:40 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
shortLink := model.ShortLink{
|
|
|
|
Hash: params.Hash,
|
|
|
|
Url: params.Url,
|
|
|
|
Password: params.Password,
|
2024-03-13 04:28:40 -04:00
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
if err := database.SaveShortLink(&shortLink); err != nil {
|
|
|
|
respondWithError(c, http.StatusInternalServerError, "数据库错误")
|
2024-03-13 04:28:40 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
|
|
|
|
c.String(http.StatusOK, "短链接更新成功")
|
2024-03-13 04:28:40 -04:00
|
|
|
}
|
|
|
|
|
2024-04-24 00:51:37 -04:00
|
|
|
func GetRawConfHandler(c *gin.Context) {
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2023-09-17 03:52:37 -04:00
|
|
|
hash := c.Param("hash")
|
2023-09-22 11:43:26 -04:00
|
|
|
password := c.Query("password")
|
2024-04-24 00:51:37 -04:00
|
|
|
|
2023-09-20 21:08:02 -04:00
|
|
|
if strings.TrimSpace(hash) == "" {
|
2024-04-24 00:51:37 -04:00
|
|
|
c.String(http.StatusBadRequest, "参数错误")
|
2023-09-20 21:08:02 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
|
|
|
|
shortLink, err := database.FindShortLinkByHash(hash)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusNotFound, "未找到短链接或密码错误")
|
2023-09-17 03:52:37 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
|
2023-09-22 11:43:26 -04:00
|
|
|
if shortLink.Password != "" && shortLink.Password != password {
|
2024-04-24 00:51:37 -04:00
|
|
|
c.String(http.StatusNotFound, "未找到短链接或密码错误")
|
2023-09-22 11:43:26 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
|
2023-09-20 21:08:02 -04:00
|
|
|
shortLink.LastRequestTime = time.Now().Unix()
|
2024-04-24 00:51:37 -04:00
|
|
|
err = database.SaveShortLink(shortLink)
|
2023-09-22 20:48:45 -04:00
|
|
|
if err != nil {
|
2024-04-24 00:51:37 -04:00
|
|
|
respondWithError(c, http.StatusInternalServerError, "数据库错误")
|
2023-09-22 20:48:45 -04:00
|
|
|
return
|
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2024-04-24 00:51:37 -04:00
|
|
|
response, err := http.Get("http://localhost:" + strconv.Itoa(config.Default.Port) + "/" + shortLink.Url)
|
2023-09-22 20:48:45 -04:00
|
|
|
if err != nil {
|
2024-04-24 00:51:37 -04:00
|
|
|
respondWithError(c, http.StatusInternalServerError, "请求错误: "+err.Error())
|
2023-09-22 20:48:45 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
defer response.Body.Close()
|
2024-03-13 01:30:45 -04:00
|
|
|
|
2024-04-24 00:51:37 -04:00
|
|
|
all, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
respondWithError(c, http.StatusInternalServerError, "读取错误: "+err.Error())
|
2024-03-13 01:30:45 -04:00
|
|
|
return
|
|
|
|
}
|
2024-04-24 00:51:37 -04:00
|
|
|
|
|
|
|
c.String(http.StatusOK, string(all))
|
2024-03-13 01:30:45 -04:00
|
|
|
}
|
2024-05-09 00:58:33 -04:00
|
|
|
|
|
|
|
func GetRawConfUriHandler(c *gin.Context) {
|
2024-08-11 11:55:47 -04:00
|
|
|
|
2024-05-09 00:58:33 -04:00
|
|
|
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)
|
|
|
|
}
|