-
Notifications
You must be signed in to change notification settings - Fork 27
/
update_tunnel.go
69 lines (57 loc) · 1.96 KB
/
update_tunnel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package server
import (
"context"
"fmt"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/conf"
"github.com/VaalaCat/frp-panel/dao"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
"github.com/VaalaCat/frp-panel/utils"
v1 "github.com/fatedier/frp/pkg/config/v1"
"github.com/sirupsen/logrus"
)
func UpdateFrpsHander(c context.Context, req *pb.UpdateFRPSRequest) (*pb.UpdateFRPSResponse, error) {
logrus.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.GetServerByServerID(userInfo, serverID)
if srv == nil || err != nil {
logrus.WithError(err).Errorf("cannot get server, id: [%s]", serverID)
return nil, err
}
srvCfg, err := utils.LoadServerConfig(configStr, true)
if srvCfg == nil || err != nil {
logrus.WithError(err).Errorf("cannot load server config")
return nil, err
}
srvCfg.HTTPPlugins = []v1.HTTPPluginOptions{conf.FRPsAuthOption(common.DefaultServerID == serverID)}
if err := srv.SetConfigContent(srvCfg); err != nil {
logrus.WithError(err).Errorf("cannot set server config")
return nil, err
}
srv.Comment = req.GetComment()
if err := dao.UpdateServer(userInfo, srv); err != nil {
logrus.WithError(err).Errorf("cannot update server, id: [%s]", serverID)
return nil, err
}
go func() {
resp, err := rpc.CallClient(context.Background(), req.GetServerId(), pb.Event_EVENT_UPDATE_FRPS, req)
if err != nil {
logrus.WithError(err).Errorf("update event send to server error, server id: [%s]", req.GetServerId())
}
if resp == nil {
logrus.Errorf("cannot get response, server id: [%s]", req.GetServerId())
}
}()
logrus.Infof("update frps success, id: [%s]", serverID)
return &pb.UpdateFRPSResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
}, nil
}