-
Notifications
You must be signed in to change notification settings - Fork 28
/
get_clients_status.go
63 lines (57 loc) · 1.79 KB
/
get_clients_status.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
package platform
import (
"context"
"time"
"github.com/VaalaCat/frp-panel/common"
"github.com/VaalaCat/frp-panel/pb"
"github.com/VaalaCat/frp-panel/rpc"
"github.com/sirupsen/logrus"
)
func GetClientsStatus(c context.Context, req *pb.GetClientsStatusRequest) (*pb.GetClientsStatusResponse, error) {
userInfo := common.GetUserInfo(c)
if !userInfo.Valid() || req == nil || len(req.GetClientIds()) == 0 || req.GetClientType() == pb.ClientType_CLIENT_TYPE_UNSPECIFIED {
return &pb.GetClientsStatusResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_INVALID, Message: "request invalid"},
}, nil
}
var (
clientIDs = req.GetClientIds()
resps = map[string]*pb.ClientStatus{}
)
for _, clientID := range clientIDs {
conn := rpc.GetClientsManager().Get(clientID)
if conn == nil {
resps[clientID] = &pb.ClientStatus{
ClientType: req.GetClientType(),
ClientId: clientID,
Status: pb.ClientStatus_STATUS_OFFLINE,
Ping: -1,
}
continue
}
startTime := time.Now()
tresp, err := rpc.CallClient(c, clientID, pb.Event_EVENT_PING, &pb.CommonRequest{})
endTime := time.Now()
pingTime := endTime.Sub(startTime).Milliseconds()
if err != nil || tresp == nil {
logrus.WithError(err).Errorf("get client status error, client id: [%s]", clientID)
resps[clientID] = &pb.ClientStatus{
ClientType: req.GetClientType(),
ClientId: clientID,
Status: pb.ClientStatus_STATUS_ERROR,
Ping: int32(pingTime),
}
continue
}
resps[clientID] = &pb.ClientStatus{
ClientType: req.GetClientType(),
ClientId: clientID,
Status: pb.ClientStatus_STATUS_ONLINE,
Ping: int32(pingTime),
}
}
return &pb.GetClientsStatusResponse{
Status: &pb.Status{Code: pb.RespCode_RESP_CODE_SUCCESS, Message: "ok"},
Clients: resps,
}, nil
}