Files
frp-panel/biz/master/client/create_client.go
T
2024-01-13 13:33:22 +08:00

46 lines
1.2 KiB
Go

package client
import (
"context"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/models"
"github.com/VaalaCat/frp-panel/pb"
"github.com/google/uuid"
)
func InitClientHandler(c context.Context, req *pb.InitClientRequest) (*pb.InitClientResponse, error) {
userClientID := req.GetClientId()
userInfo := common.GetUserInfo(c)
if !userInfo.Valid() {
return &pb.InitClientResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid user"},
}, nil
}
if len(userClientID) == 0 {
return &pb.InitClientResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid client id"},
}, nil
}
globalClientID := common.GlobalClientID(userInfo.GetUserName(), userClientID)
if err := dao.CreateClient(userInfo,
&models.ClientEntity{
ClientID: globalClientID,
TenantID: userInfo.GetTenantID(),
UserID: userInfo.GetUserID(),
ConnectSecret: uuid.New().String(),
}); err != nil {
return nil, err
}
return &pb.InitClientResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
ClientId: &globalClientID,
}, nil
}