Skip to content

Commit 9d30cee

Browse files
committed
feat: stop and start client
1 parent 8293ef4 commit 9d30cee

31 files changed

+1446
-130
lines changed

biz/client/rpc_handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ func HandleServerMessage(req *pb.ServerMessage) *pb.ClientMessage {
1313
return common.WrapperServerMsg(req, UpdateFrpcHander)
1414
case pb.Event_EVENT_REMOVE_FRPC:
1515
return common.WrapperServerMsg(req, RemoveFrpcHandler)
16+
case pb.Event_EVENT_START_FRPC:
17+
return common.WrapperServerMsg(req, StartFRPCHandler)
18+
case pb.Event_EVENT_STOP_FRPC:
19+
return common.WrapperServerMsg(req, StopFRPCHandler)
1620
case pb.Event_EVENT_PING:
1721
return &pb.ClientMessage{
1822
Event: pb.Event_EVENT_PONG,

biz/client/start_client.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/VaalaCat/frp-panel/pb"
7+
"github.com/VaalaCat/frp-panel/tunnel"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
func StartFRPCHandler(ctx context.Context, req *pb.StartFRPCRequest) (*pb.StartFRPCResponse, error) {
12+
logrus.Infof("client get a start client request, origin is: [%+v]", req)
13+
14+
tunnel.GetClientController().Run(req.GetClientId())
15+
16+
return &pb.StartFRPCResponse{
17+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
18+
}, nil
19+
}

biz/client/stop_client.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/VaalaCat/frp-panel/pb"
7+
"github.com/VaalaCat/frp-panel/tunnel"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
func StopFRPCHandler(ctx context.Context, req *pb.StopFRPCRequest) (*pb.StopFRPCResponse, error) {
12+
logrus.Infof("client get a stop client request, origin is: [%+v]", req)
13+
14+
tunnel.GetClientController().Stop(req.GetClientId())
15+
16+
return &pb.StopFRPCResponse{
17+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
18+
}, nil
19+
}

biz/master/client/get_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func GetClientHandler(ctx context.Context, req *pb.GetClientRequest) (*pb.GetCli
4242
Secret: lo.ToPtr(client.ConnectSecret),
4343
Config: lo.ToPtr(string(client.ConfigContent)),
4444
ServerId: lo.ToPtr(client.ServerID),
45+
Stopped: lo.ToPtr(client.Stopped),
4546
},
4647
}, nil
4748
}

biz/master/client/list_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func ListClientsHandler(ctx context.Context, req *pb.ListClientsRequest) (*pb.Li
4242
Secret: lo.ToPtr(c.ConnectSecret),
4343
Config: lo.ToPtr(string(c.ConfigContent)),
4444
ServerId: lo.ToPtr(c.ServerID),
45+
Stopped: lo.ToPtr(c.Stopped),
4546
}
4647
})
4748

biz/master/client/rpc_pull_config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/VaalaCat/frp-panel/models"
78
"github.com/VaalaCat/frp-panel/pb"
@@ -18,6 +19,10 @@ func RPCPullConfig(ctx context.Context, req *pb.PullClientConfigReq) (*pb.PullCl
1819
return nil, err
1920
}
2021

22+
if cli.Stopped {
23+
return nil, fmt.Errorf("client is stopped")
24+
}
25+
2126
return &pb.PullClientConfigResp{
2227
Client: &pb.Client{
2328
Id: lo.ToPtr(cli.ClientID),

biz/master/client/start_client.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/VaalaCat/frp-panel/common"
7+
"github.com/VaalaCat/frp-panel/dao"
8+
"github.com/VaalaCat/frp-panel/pb"
9+
"github.com/VaalaCat/frp-panel/rpc"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
func StartFRPCHandler(ctx context.Context, req *pb.StartFRPCRequest) (*pb.StartFRPCResponse, error) {
14+
logrus.Infof("master get a start client request, origin is: [%+v]", req)
15+
16+
userInfo := common.GetUserInfo(ctx)
17+
clientID := req.GetClientId()
18+
19+
if !userInfo.Valid() {
20+
return &pb.StartFRPCResponse{
21+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid user"},
22+
}, nil
23+
}
24+
25+
if len(clientID) == 0 {
26+
return &pb.StartFRPCResponse{
27+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid client id"},
28+
}, nil
29+
}
30+
31+
client, err := dao.GetClientByClientID(userInfo, clientID)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
client.Stopped = false
37+
38+
if err = dao.UpdateClient(userInfo, client); err != nil {
39+
return nil, err
40+
}
41+
42+
go func() {
43+
resp, err := rpc.CallClient(context.Background(), req.GetClientId(), pb.Event_EVENT_START_FRPC, req)
44+
if err != nil {
45+
logrus.WithError(err).Errorf("start client event send to client error, client id: [%s]", req.GetClientId())
46+
}
47+
48+
if resp == nil {
49+
logrus.Errorf("cannot get response, client id: [%s]", req.GetClientId())
50+
}
51+
}()
52+
53+
return &pb.StartFRPCResponse{
54+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
55+
}, nil
56+
}

biz/master/client/stop_client.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/VaalaCat/frp-panel/common"
7+
"github.com/VaalaCat/frp-panel/dao"
8+
"github.com/VaalaCat/frp-panel/pb"
9+
"github.com/VaalaCat/frp-panel/rpc"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
func StopFRPCHandler(ctx context.Context, req *pb.StopFRPCRequest) (*pb.StopFRPCResponse, error) {
14+
logrus.Infof("master get a stop client request, origin is: [%+v]", req)
15+
16+
userInfo := common.GetUserInfo(ctx)
17+
clientID := req.GetClientId()
18+
19+
if !userInfo.Valid() {
20+
return &pb.StopFRPCResponse{
21+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid user"},
22+
}, nil
23+
}
24+
25+
if len(clientID) == 0 {
26+
return &pb.StopFRPCResponse{
27+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "invalid client id"},
28+
}, nil
29+
}
30+
31+
client, err := dao.GetClientByClientID(userInfo, clientID)
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
client.Stopped = true
37+
38+
if err = dao.UpdateClient(userInfo, client); err != nil {
39+
return nil, err
40+
}
41+
42+
go func() {
43+
resp, err := rpc.CallClient(context.Background(), req.GetClientId(), pb.Event_EVENT_STOP_FRPC, req)
44+
if err != nil {
45+
logrus.WithError(err).Errorf("stop client event send to client error, client id: [%s]", req.GetClientId())
46+
}
47+
48+
if resp == nil {
49+
logrus.Errorf("cannot get response, client id: [%s]", req.GetClientId())
50+
}
51+
}()
52+
53+
return &pb.StopFRPCResponse{
54+
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
55+
}, nil
56+
}

biz/master/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ func ConfigureRouter(router *gin.Engine) {
6161
{
6262
frpcRouter.POST("/update", common.Wrapper(client.UpdateFrpcHander))
6363
frpcRouter.POST("/delete", common.Wrapper(client.RemoveFrpcHandler))
64+
frpcRouter.POST("/stop", common.Wrapper(client.StopFRPCHandler))
65+
frpcRouter.POST("/start", common.Wrapper(client.StartFRPCHandler))
6466
}
6567
frpsRouter := v1.Group("/frps", middleware.JWTAuth, middleware.AuthCtx)
6668
{

cmd/frppc/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package main
33
import (
44
"github.com/VaalaCat/frp-panel/conf"
55
"github.com/VaalaCat/frp-panel/rpc"
6+
"github.com/spf13/cobra"
67
)
78

89
func main() {
10+
cobra.MousetrapHelpText = ""
11+
912
initLogger()
1013
initCommand()
1114
conf.InitConfig()

0 commit comments

Comments
 (0)