net.go 7.9 KB

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