|
| 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 | +} |
0 commit comments