diff options
| author | Rikki <i@rikki.moe> | 2025-04-15 10:46:39 +0800 |
|---|---|---|
| committer | Rikki <i@rikki.moe> | 2025-04-15 10:46:39 +0800 |
| commit | 7a00af46de206b9d38f22c955e8820faedeedc31 (patch) | |
| tree | 21429dff30c3478ea0a19e2a774e801773d0155a /def.go | |
| parent | fb886134a635a632f128a48e891631de566b6baa (diff) | |
restructure project
Diffstat (limited to 'def.go')
| -rw-r--r-- | def.go | 66 |
1 files changed, 66 insertions, 0 deletions
@@ -0,0 +1,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 +}
\ No newline at end of file |
