github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/net/net.go (about) 1 package net 2 3 import ( 4 "context" 5 "encoding/json" 6 "net" 7 8 "github.com/unionj-cloud/go-doudou/toolkit/internal/common" 9 ) 10 11 var invoke common.Invoker = common.Invoke{} 12 13 type IOCountersStat struct { 14 Name string `json:"name"` // interface name 15 BytesSent uint64 `json:"bytesSent"` // number of bytes sent 16 BytesRecv uint64 `json:"bytesRecv"` // number of bytes received 17 PacketsSent uint64 `json:"packetsSent"` // number of packets sent 18 PacketsRecv uint64 `json:"packetsRecv"` // number of packets received 19 Errin uint64 `json:"errin"` // total number of errors while receiving 20 Errout uint64 `json:"errout"` // total number of errors while sending 21 Dropin uint64 `json:"dropin"` // total number of incoming packets which were dropped 22 Dropout uint64 `json:"dropout"` // total number of outgoing packets which were dropped (always 0 on OSX and BSD) 23 Fifoin uint64 `json:"fifoin"` // total number of FIFO buffers errors while receiving 24 Fifoout uint64 `json:"fifoout"` // total number of FIFO buffers errors while sending 25 } 26 27 // Addr is implemented compatibility to psutil 28 type Addr struct { 29 IP string `json:"ip"` 30 Port uint32 `json:"port"` 31 } 32 33 type ConnectionStat struct { 34 Fd uint32 `json:"fd"` 35 Family uint32 `json:"family"` 36 Type uint32 `json:"type"` 37 Laddr Addr `json:"localaddr"` 38 Raddr Addr `json:"remoteaddr"` 39 Status string `json:"status"` 40 Uids []int32 `json:"uids"` 41 Pid int32 `json:"pid"` 42 } 43 44 // System wide stats about different network protocols 45 type ProtoCountersStat struct { 46 Protocol string `json:"protocol"` 47 Stats map[string]int64 `json:"stats"` 48 } 49 50 // NetInterfaceAddr is designed for represent interface addresses 51 type InterfaceAddr struct { 52 Addr string `json:"addr"` 53 } 54 55 // InterfaceAddrList is a list of InterfaceAddr 56 type InterfaceAddrList []InterfaceAddr 57 58 type InterfaceStat struct { 59 Index int `json:"index"` 60 MTU int `json:"mtu"` // maximum transmission unit 61 Name string `json:"name"` // e.g., "en0", "lo0", "eth0.100" 62 HardwareAddr string `json:"hardwareAddr"` // IEEE MAC-48, EUI-48 and EUI-64 form 63 Flags []string `json:"flags"` // e.g., FlagUp, FlagLoopback, FlagMulticast 64 Addrs InterfaceAddrList `json:"addrs"` 65 } 66 67 // InterfaceStatList is a list of InterfaceStat 68 type InterfaceStatList []InterfaceStat 69 70 type FilterStat struct { 71 ConnTrackCount int64 `json:"connTrackCount"` 72 ConnTrackMax int64 `json:"connTrackMax"` 73 } 74 75 // ConntrackStat has conntrack summary info 76 type ConntrackStat struct { 77 Entries uint32 `json:"entries"` // Number of entries in the conntrack table 78 Searched uint32 `json:"searched"` // Number of conntrack table lookups performed 79 Found uint32 `json:"found"` // Number of searched entries which were successful 80 New uint32 `json:"new"` // Number of entries added which were not expected before 81 Invalid uint32 `json:"invalid"` // Number of packets seen which can not be tracked 82 Ignore uint32 `json:"ignore"` // Packets seen which are already connected to an entry 83 Delete uint32 `json:"delete"` // Number of entries which were removed 84 DeleteList uint32 `json:"deleteList"` // Number of entries which were put to dying list 85 Insert uint32 `json:"insert"` // Number of entries inserted into the list 86 InsertFailed uint32 `json:"insertFailed"` // # insertion attempted but failed (same entry exists) 87 Drop uint32 `json:"drop"` // Number of packets dropped due to conntrack failure. 88 EarlyDrop uint32 `json:"earlyDrop"` // Dropped entries to make room for new ones, if maxsize reached 89 IcmpError uint32 `json:"icmpError"` // Subset of invalid. Packets that can't be tracked d/t error 90 ExpectNew uint32 `json:"expectNew"` // Entries added after an expectation was already present 91 ExpectCreate uint32 `json:"expectCreate"` // Expectations added 92 ExpectDelete uint32 `json:"expectDelete"` // Expectations deleted 93 SearchRestart uint32 `json:"searchRestart"` // Conntrack table lookups restarted due to hashtable resizes 94 } 95 96 func NewConntrackStat(e uint32, s uint32, f uint32, n uint32, inv uint32, ign uint32, del uint32, dlst uint32, ins uint32, insfail uint32, drop uint32, edrop uint32, ie uint32, en uint32, ec uint32, ed uint32, sr uint32) *ConntrackStat { 97 return &ConntrackStat{ 98 Entries: e, 99 Searched: s, 100 Found: f, 101 New: n, 102 Invalid: inv, 103 Ignore: ign, 104 Delete: del, 105 DeleteList: dlst, 106 Insert: ins, 107 InsertFailed: insfail, 108 Drop: drop, 109 EarlyDrop: edrop, 110 IcmpError: ie, 111 ExpectNew: en, 112 ExpectCreate: ec, 113 ExpectDelete: ed, 114 SearchRestart: sr, 115 } 116 } 117 118 type ConntrackStatList struct { 119 items []*ConntrackStat 120 } 121 122 func NewConntrackStatList() *ConntrackStatList { 123 return &ConntrackStatList{ 124 items: []*ConntrackStat{}, 125 } 126 } 127 128 func (l *ConntrackStatList) Append(c *ConntrackStat) { 129 l.items = append(l.items, c) 130 } 131 132 func (l *ConntrackStatList) Items() []ConntrackStat { 133 items := make([]ConntrackStat, len(l.items)) 134 for i, el := range l.items { 135 items[i] = *el 136 } 137 return items 138 } 139 140 // Summary returns a single-element list with totals from all list items. 141 func (l *ConntrackStatList) Summary() []ConntrackStat { 142 summary := NewConntrackStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 143 for _, cs := range l.items { 144 summary.Entries += cs.Entries 145 summary.Searched += cs.Searched 146 summary.Found += cs.Found 147 summary.New += cs.New 148 summary.Invalid += cs.Invalid 149 summary.Ignore += cs.Ignore 150 summary.Delete += cs.Delete 151 summary.DeleteList += cs.DeleteList 152 summary.Insert += cs.Insert 153 summary.InsertFailed += cs.InsertFailed 154 summary.Drop += cs.Drop 155 summary.EarlyDrop += cs.EarlyDrop 156 summary.IcmpError += cs.IcmpError 157 summary.ExpectNew += cs.ExpectNew 158 summary.ExpectCreate += cs.ExpectCreate 159 summary.ExpectDelete += cs.ExpectDelete 160 summary.SearchRestart += cs.SearchRestart 161 } 162 return []ConntrackStat{*summary} 163 } 164 165 func (n IOCountersStat) String() string { 166 s, _ := json.Marshal(n) 167 return string(s) 168 } 169 170 func (n ConnectionStat) String() string { 171 s, _ := json.Marshal(n) 172 return string(s) 173 } 174 175 func (n ProtoCountersStat) String() string { 176 s, _ := json.Marshal(n) 177 return string(s) 178 } 179 180 func (a Addr) String() string { 181 s, _ := json.Marshal(a) 182 return string(s) 183 } 184 185 func (n InterfaceStat) String() string { 186 s, _ := json.Marshal(n) 187 return string(s) 188 } 189 190 func (l InterfaceStatList) String() string { 191 s, _ := json.Marshal(l) 192 return string(s) 193 } 194 195 func (n InterfaceAddr) String() string { 196 s, _ := json.Marshal(n) 197 return string(s) 198 } 199 200 func (n ConntrackStat) String() string { 201 s, _ := json.Marshal(n) 202 return string(s) 203 } 204 205 func Interfaces() (InterfaceStatList, error) { 206 return InterfacesWithContext(context.Background()) 207 } 208 209 func InterfacesWithContext(ctx context.Context) (InterfaceStatList, error) { 210 is, err := net.Interfaces() 211 if err != nil { 212 return nil, err 213 } 214 ret := make(InterfaceStatList, 0, len(is)) 215 for _, ifi := range is { 216 217 var flags []string 218 if ifi.Flags&net.FlagUp != 0 { 219 flags = append(flags, "up") 220 } 221 if ifi.Flags&net.FlagBroadcast != 0 { 222 flags = append(flags, "broadcast") 223 } 224 if ifi.Flags&net.FlagLoopback != 0 { 225 flags = append(flags, "loopback") 226 } 227 if ifi.Flags&net.FlagPointToPoint != 0 { 228 flags = append(flags, "pointtopoint") 229 } 230 if ifi.Flags&net.FlagMulticast != 0 { 231 flags = append(flags, "multicast") 232 } 233 234 r := InterfaceStat{ 235 Index: ifi.Index, 236 Name: ifi.Name, 237 MTU: ifi.MTU, 238 HardwareAddr: ifi.HardwareAddr.String(), 239 Flags: flags, 240 } 241 addrs, err := ifi.Addrs() 242 if err == nil { 243 r.Addrs = make(InterfaceAddrList, 0, len(addrs)) 244 for _, addr := range addrs { 245 r.Addrs = append(r.Addrs, InterfaceAddr{ 246 Addr: addr.String(), 247 }) 248 } 249 250 } 251 ret = append(ret, r) 252 } 253 254 return ret, nil 255 } 256 257 func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { 258 r := IOCountersStat{ 259 Name: "all", 260 } 261 for _, nic := range n { 262 r.BytesRecv += nic.BytesRecv 263 r.PacketsRecv += nic.PacketsRecv 264 r.Errin += nic.Errin 265 r.Dropin += nic.Dropin 266 r.BytesSent += nic.BytesSent 267 r.PacketsSent += nic.PacketsSent 268 r.Errout += nic.Errout 269 r.Dropout += nic.Dropout 270 } 271 272 return []IOCountersStat{r}, nil 273 }