summaryrefslogtreecommitdiff
path: root/def.go
blob: e9951ad4469a148f77a8c73c59aac424653d79b6 (plain)
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
package v2stat

import (
	"strings"
)

type TrafficDirection int

const (
	DirectionDownlink TrafficDirection = iota
	DirectionUplink
)

type ConnectionType int

const (
	ConnTypeUser ConnectionType = iota
	ConnTypeInbound
	ConnTypeOutbound
)

type ConnInfo struct {
	Type ConnectionType `json:"type"`
	Name string         `json:"name"`
}

type TrafficStat struct {
	Time     string `json:"time"`
	Downlink int64  `json:"downlink"`
	Uplink   int64  `json:"uplink"`
}

func (ci *ConnInfo) String() string {
	switch ci.Type {
	case ConnTypeUser:
		return "user:" + ci.Name
	case ConnTypeInbound:
		return "inbound:" + ci.Name
	case ConnTypeOutbound:
		return "outbound:" + ci.Name
	default:
		return "unknown:" + ci.Name
	}
}

func ParseConnInfo(s string) (ConnInfo, bool) {
	parts := strings.Split(s, ":")
	if len(parts) != 2 {
		return ConnInfo{}, false
	}
	var connType ConnectionType
	switch parts[0] {
	case "user":
		connType = ConnTypeUser
	case "inbound":
		connType = ConnTypeInbound
	case "outbound":
		connType = ConnTypeOutbound
	default:
		return ConnInfo{}, false
	}
	return ConnInfo{
		Type: connType,
		Name: parts[1],
	}, true
}