完善错误处理逻辑,统一使用 BizErr 包装业务错误,提供打印源码跳转并返回合适的 http 状态码
This commit is contained in:
@@ -5,11 +5,15 @@ import (
|
||||
"encoding/base32"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"log/slog"
|
||||
"platform/pkg/u"
|
||||
auth2 "platform/web/auth"
|
||||
proxy2 "platform/web/domains/proxy"
|
||||
g "platform/web/globals"
|
||||
"platform/web/globals/orm"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -22,8 +26,9 @@ type OnlineProxyReq struct {
|
||||
}
|
||||
|
||||
type OnlineProxyResp struct {
|
||||
Id int32 `json:"id"`
|
||||
Secret string `json:"secret"`
|
||||
Id int32 `json:"id"`
|
||||
Secret string `json:"secret"`
|
||||
Permits []ProxyPermit `json:"permits"`
|
||||
}
|
||||
|
||||
func OnlineProxy(c *fiber.Ctx) (err error) {
|
||||
@@ -53,8 +58,8 @@ func OnlineProxy(c *fiber.Ctx) (err error) {
|
||||
var secret = base32.StdEncoding.
|
||||
WithPadding(base32.NoPadding).
|
||||
EncodeToString(secretBytes)
|
||||
|
||||
slog.Debug("生成随机密钥", "ip", ip, "secret", secret)
|
||||
|
||||
var proxy = &m.Proxy{
|
||||
Name: req.Name,
|
||||
Version: int32(req.Version),
|
||||
@@ -63,7 +68,7 @@ func OnlineProxy(c *fiber.Ctx) (err error) {
|
||||
Secret: secret,
|
||||
Status: 1,
|
||||
}
|
||||
err = q.Proxy.Debug().
|
||||
err = q.Proxy.
|
||||
Clauses(clause.OnConflict{
|
||||
UpdateAll: true,
|
||||
Columns: []clause.Column{
|
||||
@@ -75,10 +80,31 @@ func OnlineProxy(c *fiber.Ctx) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
channels, err := q.Channel.Where(
|
||||
q.Channel.ProxyID.Eq(proxy.ID),
|
||||
q.Channel.Expiration.Gt(orm.LocalDateTime(time.Now())),
|
||||
).Find()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var permits []ProxyPermit
|
||||
for _, channel := range channels {
|
||||
permit := ProxyPermit{
|
||||
Id: channel.EdgeID,
|
||||
Expire: time.Time(channel.Expiration),
|
||||
Whitelists: u.P(strings.Split(channel.Whitelists, ",")),
|
||||
Username: &channel.Username,
|
||||
Password: &channel.Password,
|
||||
}
|
||||
permits = append(permits, permit)
|
||||
}
|
||||
|
||||
slog.Debug("注册转发服务", "ip", ip, "id", proxy.ID)
|
||||
return c.JSON(&OnlineProxyResp{
|
||||
Id: proxy.ID,
|
||||
Secret: secret,
|
||||
Id: proxy.ID,
|
||||
Secret: secret,
|
||||
Permits: permits,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -158,3 +184,11 @@ func AssignProxyFwdPort(c *fiber.Ctx) (err error) {
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
type ProxyPermit struct {
|
||||
Id int32 `json:"id"`
|
||||
Expire time.Time `json:"expire"`
|
||||
Whitelists *[]string `json:"whitelists"`
|
||||
Username *string `json:"username"`
|
||||
Password *string `json:"password"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user