-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2ray.go
72 lines (65 loc) · 1.59 KB
/
v2ray.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
70
71
72
package main
import (
"context"
"time"
v2stats "github.com/v2fly/v2ray-core/v4/app/stats/command"
"google.golang.org/grpc"
)
type Client struct {
conn *grpc.ClientConn
v2stats.StatsServiceClient
}
func NewClient(addr string) (*Client, error) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, addr, grpc.WithInsecure())
if err != nil {
return nil, err
}
client := v2stats.NewStatsServiceClient(conn)
return &Client{
conn: conn,
StatsServiceClient: client,
}, nil
}
func (c *Client) Stats(pt string) ([]*v2stats.Stat, error) {
start := time.Now()
resp, err := c.QueryStats(context.Background(), &v2stats.QueryStatsRequest{
Pattern: "",
Reset_: false,
})
if err != nil {
return nil, err
}
logger.Debugln("QueryStats from v2ray", "duration", time.Since(start))
return resp.Stat, nil
}
// SysStats
// NumGoroutine:17
// NumGC:12
// Alloc:3195136
// TotalAlloc:17653480
// Sys:71500024
// Mallocs:369427
// Frees:347892
// LiveObjects:21535
// PauseTotalNs:400819
// Uptime:1057
func (c *Client) SysStats() (*v2stats.SysStatsResponse, error) {
start := time.Now()
resp, err := c.GetSysStats(context.Background(), &v2stats.SysStatsRequest{})
if err != nil {
return nil, err
}
logger.Debugln("GetSysStats from v2ray", "duration", time.Since(start))
return resp, nil
}
func (c *Client) Close() {
logger.Warn("v2ray grpc connection closing")
err := c.conn.Close()
if err != nil {
logger.Warnln("close v2ray connection", err.Error())
}
logger.Warn("v2ray grpc connection closed")
}