netstat.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package netstat
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "imuslab.com/zoraxy/mod/info/logger"
  14. "imuslab.com/zoraxy/mod/utils"
  15. )
  16. // Float stat store the change of RX and TX
  17. type FlowStat struct {
  18. RX int64
  19. TX int64
  20. }
  21. // A new type of FloatStat that save the raw value from rx tx
  22. type RawFlowStat struct {
  23. RX int64
  24. TX int64
  25. }
  26. type NetStatBuffers struct {
  27. StatRecordCount int //No. of record number to keep
  28. PreviousStat *RawFlowStat //The value of the last instance of netstats
  29. Stats []*FlowStat //Statistic of the flow
  30. StopChan chan bool //Channel to stop the ticker
  31. EventTicker *time.Ticker //Ticker for event logging
  32. logger *logger.Logger
  33. }
  34. // Get a new network statistic buffers
  35. func NewNetStatBuffer(recordCount int, systemWideLogger *logger.Logger) (*NetStatBuffers, error) {
  36. //Flood fill the stats with 0
  37. initialStats := []*FlowStat{}
  38. for i := 0; i < recordCount; i++ {
  39. initialStats = append(initialStats, &FlowStat{
  40. RX: 0,
  41. TX: 0,
  42. })
  43. }
  44. //Setup a timer to get the value from NIC accumulation stats
  45. ticker := time.NewTicker(time.Second)
  46. //Setup a stop channel
  47. stopCh := make(chan bool)
  48. currnetNetSpec := RawFlowStat{
  49. RX: 0,
  50. TX: 0,
  51. }
  52. thisNetBuffer := NetStatBuffers{
  53. StatRecordCount: recordCount,
  54. PreviousStat: &currnetNetSpec,
  55. Stats: initialStats,
  56. StopChan: stopCh,
  57. EventTicker: ticker,
  58. logger: systemWideLogger,
  59. }
  60. //Get the initial measurements of netstats
  61. rx, tx, err := thisNetBuffer.GetNetworkInterfaceStats()
  62. if err != nil {
  63. systemWideLogger.PrintAndLog("netstat", "Unable to get NIC stats: ", err)
  64. }
  65. retryCount := 0
  66. for rx == 0 && tx == 0 && retryCount < 10 {
  67. //Strange. Retry
  68. systemWideLogger.PrintAndLog("netstat", "NIC stats return all 0. Retrying...", nil)
  69. rx, tx, err = thisNetBuffer.GetNetworkInterfaceStats()
  70. if err != nil {
  71. systemWideLogger.PrintAndLog("netstat", "Unable to get NIC stats: ", err)
  72. }
  73. retryCount++
  74. }
  75. thisNetBuffer.PreviousStat = &RawFlowStat{
  76. RX: rx,
  77. TX: tx,
  78. }
  79. // Update the buffer every second
  80. go func(n *NetStatBuffers) {
  81. for {
  82. select {
  83. case <-n.StopChan:
  84. systemWideLogger.PrintAndLog("netstat", "Netstats listener stopped", nil)
  85. return
  86. case <-ticker.C:
  87. if n.PreviousStat.RX == 0 && n.PreviousStat.TX == 0 {
  88. //Initiation state is still not done. Ignore request
  89. systemWideLogger.PrintAndLog("netstat", "No initial states. Waiting", nil)
  90. return
  91. }
  92. // Get the latest network interface stats
  93. rx, tx, err := thisNetBuffer.GetNetworkInterfaceStats()
  94. if err != nil {
  95. // Log the error, but don't stop the buffer
  96. systemWideLogger.PrintAndLog("netstat", "Failed to get network interface stats", err)
  97. continue
  98. }
  99. //Calculate the difference between this and last values
  100. drx := rx - n.PreviousStat.RX
  101. dtx := tx - n.PreviousStat.TX
  102. // Push the new stats to the buffer
  103. newStat := &FlowStat{
  104. RX: drx,
  105. TX: dtx,
  106. }
  107. //Set current rx tx as the previous rxtx
  108. n.PreviousStat = &RawFlowStat{
  109. RX: rx,
  110. TX: tx,
  111. }
  112. newStats := n.Stats[1:]
  113. newStats = append(newStats, newStat)
  114. n.Stats = newStats
  115. }
  116. }
  117. }(&thisNetBuffer)
  118. return &thisNetBuffer, nil
  119. }
  120. func (n *NetStatBuffers) HandleGetBufferedNetworkInterfaceStats(w http.ResponseWriter, r *http.Request) {
  121. arr, _ := utils.GetPara(r, "array")
  122. if arr == "true" {
  123. //Restructure it into array
  124. rx := []int{}
  125. tx := []int{}
  126. for _, state := range n.Stats {
  127. rx = append(rx, int(state.RX))
  128. tx = append(tx, int(state.TX))
  129. }
  130. type info struct {
  131. Rx []int
  132. Tx []int
  133. }
  134. js, _ := json.Marshal(info{
  135. Rx: rx,
  136. Tx: tx,
  137. })
  138. utils.SendJSONResponse(w, string(js))
  139. } else {
  140. js, _ := json.Marshal(n.Stats)
  141. utils.SendJSONResponse(w, string(js))
  142. }
  143. }
  144. func (n *NetStatBuffers) Close() {
  145. n.StopChan <- true
  146. time.Sleep(300 * time.Millisecond)
  147. n.EventTicker.Stop()
  148. }
  149. func (n *NetStatBuffers) HandleGetNetworkInterfaceStats(w http.ResponseWriter, r *http.Request) {
  150. rx, tx, err := n.GetNetworkInterfaceStats()
  151. if err != nil {
  152. utils.SendErrorResponse(w, err.Error())
  153. return
  154. }
  155. currnetNetSpec := struct {
  156. RX int64
  157. TX int64
  158. }{
  159. rx,
  160. tx,
  161. }
  162. js, _ := json.Marshal(currnetNetSpec)
  163. utils.SendJSONResponse(w, string(js))
  164. }
  165. // Get network interface stats, return accumulated rx bits, tx bits and error if any
  166. func (n *NetStatBuffers) GetNetworkInterfaceStats() (int64, int64, error) {
  167. if runtime.GOOS == "windows" {
  168. //Windows wmic sometime freeze and not respond.
  169. //The safer way is to make a bypass mechanism
  170. //when timeout with channel
  171. type wmicResult struct {
  172. RX int64
  173. TX int64
  174. Err error
  175. }
  176. callbackChan := make(chan wmicResult)
  177. cmd := exec.Command("wmic", "path", "Win32_PerfRawData_Tcpip_NetworkInterface", "Get", "BytesReceivedPersec,BytesSentPersec,BytesTotalPersec")
  178. //Execute the cmd in goroutine
  179. go func() {
  180. out, err := cmd.Output()
  181. if err != nil {
  182. callbackChan <- wmicResult{0, 0, err}
  183. return
  184. }
  185. //Filter out the first line
  186. lines := strings.Split(strings.ReplaceAll(string(out), "\r\n", "\n"), "\n")
  187. if len(lines) >= 2 && len(lines[1]) >= 0 {
  188. dataLine := lines[1]
  189. for strings.Contains(dataLine, " ") {
  190. dataLine = strings.ReplaceAll(dataLine, " ", " ")
  191. }
  192. dataLine = strings.TrimSpace(dataLine)
  193. info := strings.Split(dataLine, " ")
  194. if len(info) != 3 {
  195. callbackChan <- wmicResult{0, 0, errors.New("invalid wmic results length")}
  196. }
  197. rxString := info[0]
  198. txString := info[1]
  199. rx := int64(0)
  200. tx := int64(0)
  201. if s, err := strconv.ParseInt(rxString, 10, 64); err == nil {
  202. rx = s
  203. }
  204. if s, err := strconv.ParseInt(txString, 10, 64); err == nil {
  205. tx = s
  206. }
  207. time.Sleep(100 * time.Millisecond)
  208. callbackChan <- wmicResult{rx * 4, tx * 4, nil}
  209. } else {
  210. //Invalid data
  211. callbackChan <- wmicResult{0, 0, errors.New("invalid wmic results")}
  212. }
  213. }()
  214. go func() {
  215. //Spawn a timer to terminate the cmd process if timeout
  216. time.Sleep(3 * time.Second)
  217. if cmd != nil && cmd.Process != nil {
  218. cmd.Process.Kill()
  219. callbackChan <- wmicResult{0, 0, errors.New("wmic execution timeout")}
  220. }
  221. }()
  222. result := wmicResult{}
  223. result = <-callbackChan
  224. cmd = nil
  225. if result.Err != nil {
  226. n.logger.PrintAndLog("netstat", "Unable to extract NIC info from wmic", result.Err)
  227. }
  228. return result.RX, result.TX, result.Err
  229. } else if runtime.GOOS == "linux" {
  230. allIfaceRxByteFiles, err := filepath.Glob("/sys/class/net/*/statistics/rx_bytes")
  231. if err != nil {
  232. //Permission denied
  233. return 0, 0, errors.New("Access denied")
  234. }
  235. if len(allIfaceRxByteFiles) == 0 {
  236. return 0, 0, errors.New("No valid iface found")
  237. }
  238. rxSum := int64(0)
  239. txSum := int64(0)
  240. for _, rxByteFile := range allIfaceRxByteFiles {
  241. rxBytes, err := os.ReadFile(rxByteFile)
  242. if err == nil {
  243. rxBytesInt, err := strconv.Atoi(strings.TrimSpace(string(rxBytes)))
  244. if err == nil {
  245. rxSum += int64(rxBytesInt)
  246. }
  247. }
  248. //Usually the tx_bytes file is nearby it. Read it as well
  249. txByteFile := filepath.Join(filepath.Dir(rxByteFile), "tx_bytes")
  250. txBytes, err := os.ReadFile(txByteFile)
  251. if err == nil {
  252. txBytesInt, err := strconv.Atoi(strings.TrimSpace(string(txBytes)))
  253. if err == nil {
  254. txSum += int64(txBytesInt)
  255. }
  256. }
  257. }
  258. //Return value as bits
  259. return rxSum * 8, txSum * 8, nil
  260. } else if runtime.GOOS == "darwin" {
  261. cmd := exec.Command("netstat", "-ib") //get data from netstat -ib
  262. out, err := cmd.Output()
  263. if err != nil {
  264. return 0, 0, err
  265. }
  266. outStrs := string(out) //byte array to multi-line string
  267. for _, outStr := range strings.Split(strings.TrimSuffix(outStrs, "\n"), "\n") { //foreach multi-line string
  268. if strings.HasPrefix(outStr, "en") { //search for ethernet interface
  269. if strings.Contains(outStr, "<Link#") { //search for the link with <Link#?>
  270. outStrSplit := strings.Fields(outStr) //split by white-space
  271. rxSum, errRX := strconv.Atoi(outStrSplit[6]) //received bytes sum
  272. if errRX != nil {
  273. return 0, 0, errRX
  274. }
  275. txSum, errTX := strconv.Atoi(outStrSplit[9]) //transmitted bytes sum
  276. if errTX != nil {
  277. return 0, 0, errTX
  278. }
  279. return int64(rxSum) * 8, int64(txSum) * 8, nil
  280. }
  281. }
  282. }
  283. return 0, 0, nil //no ethernet adapters with en*/<Link#*>
  284. }
  285. return 0, 0, errors.New("Platform not supported")
  286. }