-
Notifications
You must be signed in to change notification settings - Fork 465
/
statsServiceClient.go
46 lines (37 loc) · 1.16 KB
/
statsServiceClient.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
package v2ray_ssrpanel_plugin
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"strings"
statsservice "v2ray.com/core/app/stats/command"
)
type StatsServiceClient struct {
statsservice.StatsServiceClient
}
func NewStatsServiceClient(client *grpc.ClientConn) *StatsServiceClient {
return &StatsServiceClient{
StatsServiceClient: statsservice.NewStatsServiceClient(client),
}
}
func (s *StatsServiceClient) getUserUplink(email string) (uint64, error) {
return s.getUserTraffic(fmt.Sprintf("user>>>%s>>>traffic>>>uplink", email), true)
}
func (s *StatsServiceClient) getUserDownlink(email string) (uint64, error) {
return s.getUserTraffic(fmt.Sprintf("user>>>%s>>>traffic>>>downlink", email), true)
}
func (s *StatsServiceClient) getUserTraffic(name string, reset bool) (uint64, error) {
req := &statsservice.GetStatsRequest{
Name: name,
Reset_: reset,
}
res, err := s.GetStats(context.Background(), req)
if err != nil {
if status, ok := status.FromError(err); ok && strings.HasSuffix(status.Message(), fmt.Sprintf("%s not found.", name)) {
return 0, nil
}
return 0, err
}
return uint64(res.Stat.Value), nil
}