mirror of
https://github.com/VaalaCat/frp-panel.git
synced 2025-09-26 19:31:18 +08:00
79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/VaalaCat/frp-panel/common"
|
|
"github.com/VaalaCat/frp-panel/conf"
|
|
"github.com/VaalaCat/frp-panel/defs"
|
|
"github.com/VaalaCat/frp-panel/pb"
|
|
"github.com/VaalaCat/frp-panel/services/app"
|
|
"github.com/VaalaCat/frp-panel/services/dao"
|
|
"github.com/VaalaCat/frp-panel/services/rpc"
|
|
"github.com/VaalaCat/frp-panel/utils"
|
|
"github.com/VaalaCat/frp-panel/utils/logger"
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
)
|
|
|
|
func UpdateFrpsHander(c *app.Context, req *pb.UpdateFRPSRequest) (*pb.UpdateFRPSResponse, error) {
|
|
logger.Logger(c).Infof("update frps, req: [%+v]", req)
|
|
var (
|
|
serverID = req.GetServerId()
|
|
configStr = req.GetConfig()
|
|
userInfo = common.GetUserInfo(c)
|
|
)
|
|
|
|
if len(configStr) == 0 || len(serverID) == 0 {
|
|
return nil, fmt.Errorf("request invalid")
|
|
}
|
|
|
|
srv, err := dao.NewQuery(c).GetServerByServerID(userInfo, serverID)
|
|
if srv == nil || err != nil {
|
|
logger.Logger(context.Background()).WithError(err).Errorf("cannot get server, id: [%s]", serverID)
|
|
return nil, err
|
|
}
|
|
|
|
srvCfg, err := utils.LoadServerConfig(configStr, true)
|
|
if srvCfg == nil || err != nil {
|
|
logger.Logger(context.Background()).WithError(err).Errorf("cannot load server config")
|
|
return nil, err
|
|
}
|
|
|
|
srvCfg.HTTPPlugins = []v1.HTTPPluginOptions{conf.FRPsAuthOption(c.GetApp().GetConfig(), defs.DefaultServerID == serverID)}
|
|
|
|
if err := srv.SetConfigContent(srvCfg); err != nil {
|
|
logger.Logger(context.Background()).WithError(err).Errorf("cannot set server config")
|
|
return nil, err
|
|
}
|
|
|
|
srv.Comment = req.GetComment()
|
|
if len(req.GetServerIp()) > 0 {
|
|
srv.ServerIP = req.GetServerIp()
|
|
}
|
|
|
|
if len(req.GetFrpsUrls()) > 0 {
|
|
srv.FrpsUrls = req.GetFrpsUrls()
|
|
}
|
|
|
|
if err := dao.NewQuery(c).UpdateServer(userInfo, srv); err != nil {
|
|
logger.Logger(context.Background()).WithError(err).Errorf("cannot update server, id: [%s]", serverID)
|
|
return nil, err
|
|
}
|
|
|
|
go func() {
|
|
resp, err := rpc.CallClient(app.NewContext(context.Background(), c.GetApp()), req.GetServerId(), pb.Event_EVENT_UPDATE_FRPS, req)
|
|
if err != nil {
|
|
logger.Logger(context.Background()).WithError(err).Errorf("update event send to server error, server id: [%s]", req.GetServerId())
|
|
}
|
|
if resp == nil {
|
|
logger.Logger(c).Errorf("cannot get response, server id: [%s]", req.GetServerId())
|
|
}
|
|
}()
|
|
|
|
logger.Logger(c).Infof("update frps success, id: [%s]", serverID)
|
|
return &pb.UpdateFRPSResponse{
|
|
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
|
|
}, nil
|
|
}
|