新增代理服务的离线接口,优化认证逻辑,代理服务表添加状态字段

This commit is contained in:
2025-05-13 15:26:40 +08:00
parent 60df1548f0
commit 0d40c5aa09
8 changed files with 88 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ package handlers
import (
"github.com/gofiber/fiber/v2"
"gorm.io/gorm/clause"
"log/slog"
auth2 "platform/web/auth"
proxy2 "platform/web/domains/proxy"
g "platform/web/globals"
@@ -10,7 +11,9 @@ import (
q "platform/web/queries"
)
type RegisterProxyReq struct {
// region OnlineProxy
type OnlineProxyReq struct {
Name string `json:"name" validate:"required"`
Version int `json:"version" validate:"required"`
}
@@ -26,13 +29,14 @@ func OnlineProxy(c *fiber.Ctx) (err error) {
}
// 验证请求参数
var req = new(RegisterProxyReq)
var req = new(OnlineProxyReq)
err = g.Validator.Validate(c, req)
if err != nil {
return err
}
// 创建代理
slog.Debug("注册转发服务", "ip", c.IP())
err = q.Proxy.
Clauses(clause.OnConflict{
UpdateAll: true,
@@ -45,6 +49,7 @@ func OnlineProxy(c *fiber.Ctx) (err error) {
Version: int32(req.Version),
Host: c.IP(),
Type: int32(proxy2.TypeSelfHosted),
Status: 1,
})
if err != nil {
return err
@@ -52,3 +57,40 @@ func OnlineProxy(c *fiber.Ctx) (err error) {
return nil
}
// endregion
// region OfflineProxy
type OfflineProxyReq struct {
Name string `json:"name" validate:"required"`
}
func OfflineProxy(c *fiber.Ctx) (err error) {
// 检查接口权限
_, err = auth2.NewProtect(c).Payload(
auth2.PayloadInternalServer,
).Do()
if err != nil {
return err
}
// 验证请求参数
var req = new(OfflineProxyReq)
err = g.Validator.Validate(c, req)
if err != nil {
return err
}
// 下线转发服务
_, err = q.Proxy.
Where(q.Proxy.Name.Eq(req.Name)).
UpdateSimple(q.Proxy.Status.Value(0))
if err != nil {
return err
}
return nil
}
// endregion