重构通道创建逻辑

This commit is contained in:
2025-05-08 19:02:07 +08:00
parent e2cc318560
commit 623e9652d9
3 changed files with 317 additions and 328 deletions

View File

@@ -97,13 +97,23 @@ type CreateChannelReq struct {
Isp string `json:"isp"`
}
type CreateChannelRespItem struct {
Proto s.ChannelProtocol `json:"-"`
Host string `json:"host"`
Port int32 `json:"port"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
}
func CreateChannel(c *fiber.Ctx) error {
// 检查权限
authContext, err := auth.Protect(c, []auth.PayloadType{auth.PayloadUser}, []string{})
if err != nil {
return err
}
// 获取用户信息
// 检查用户其他权限
user, err := q.User.
Where(q.User.ID.Eq(authContext.Payload.Id)).
Take()
@@ -140,6 +150,7 @@ func CreateChannel(c *fiber.Ctx) error {
isp = "移动"
}
// 创建通道
result, err := s.Channel.CreateChannel(
c.Context(),
authContext,
@@ -157,7 +168,20 @@ func CreateChannel(c *fiber.Ctx) error {
return err
}
return c.JSON(result)
// 返回结果
var resp = make([]*CreateChannelRespItem, len(result))
for i, channel := range result {
resp[i] = &CreateChannelRespItem{
Proto: req.Protocol,
Host: channel.ProxyHost,
Port: channel.ProxyPort,
}
if req.AuthType == s.ChannelAuthTypePass {
resp[i].Username = &channel.Username
resp[i].Password = &channel.Password
}
}
return c.JSON(resp)
}
type CreateChannelResultType string