session_json.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package session
  2. import (
  3. "encoding/json"
  4. "net"
  5. "runtime"
  6. "time"
  7. "github.com/bettercap/bettercap/caplets"
  8. "github.com/bettercap/bettercap/core"
  9. "github.com/bettercap/bettercap/network"
  10. "github.com/bettercap/bettercap/packets"
  11. )
  12. var flagNames = []string{
  13. "UP",
  14. "BROADCAST",
  15. "LOOPBACK",
  16. "POINT2POINT",
  17. "MULTICAST",
  18. }
  19. type addrJSON struct {
  20. Address string `json:"address"`
  21. Type string `json:"type"`
  22. }
  23. type ifaceJSON struct {
  24. Index int `json:"index"`
  25. MTU int `json:"mtu"`
  26. Name string `json:"name"`
  27. MAC string `json:"mac"`
  28. Vendor string `json:"vendor"`
  29. Flags []string `json:"flags"`
  30. Addresses []addrJSON `json:"addresses"`
  31. }
  32. type resourcesJSON struct {
  33. NumCPU int `json:"cpus"`
  34. MaxCPU int `json:"max_cpus"`
  35. NumGoroutine int `json:"goroutines"`
  36. Alloc uint64 `json:"alloc"`
  37. Sys uint64 `json:"sys"`
  38. NumGC uint32 `json:"gcs"`
  39. }
  40. type SessionJSON struct {
  41. Version string `json:"version"`
  42. OS string `json:"os"`
  43. Arch string `json:"arch"`
  44. GoVersion string `json:"goversion"`
  45. Resources resourcesJSON `json:"resources"`
  46. Interfaces []ifaceJSON `json:"interfaces"`
  47. Options core.Options `json:"options"`
  48. Interface *network.Endpoint `json:"interface"`
  49. Gateway *network.Endpoint `json:"gateway"`
  50. Env *Environment `json:"env"`
  51. Lan *network.LAN `json:"lan"`
  52. WiFi *network.WiFi `json:"wifi"`
  53. BLE *network.BLE `json:"ble"`
  54. HID *network.HID `json:"hid"`
  55. Queue *packets.Queue `json:"packets"`
  56. StartedAt time.Time `json:"started_at"`
  57. PolledAt time.Time `json:"polled_at"`
  58. Active bool `json:"active"`
  59. GPS GPS `json:"gps"`
  60. Modules ModuleList `json:"modules"`
  61. Caplets []*caplets.Caplet `json:"caplets"`
  62. }
  63. func (s *Session) MarshalJSON() ([]byte, error) {
  64. var m runtime.MemStats
  65. runtime.ReadMemStats(&m)
  66. doc := SessionJSON{
  67. Version: core.Version,
  68. OS: runtime.GOOS,
  69. Arch: runtime.GOARCH,
  70. GoVersion: runtime.Version(),
  71. Resources: resourcesJSON{
  72. NumCPU: runtime.NumCPU(),
  73. MaxCPU: runtime.GOMAXPROCS(0),
  74. NumGoroutine: runtime.NumGoroutine(),
  75. Alloc: m.Alloc,
  76. Sys: m.Sys,
  77. NumGC: m.NumGC,
  78. },
  79. Interfaces: make([]ifaceJSON, 0),
  80. Options: s.Options,
  81. Interface: s.Interface,
  82. Gateway: s.Gateway,
  83. Env: s.Env,
  84. Lan: s.Lan,
  85. WiFi: s.WiFi,
  86. BLE: s.BLE,
  87. HID: s.HID,
  88. Queue: s.Queue,
  89. StartedAt: s.StartedAt,
  90. PolledAt: time.Now(),
  91. Active: s.Active,
  92. GPS: s.GPS,
  93. Modules: s.Modules,
  94. Caplets: caplets.List(),
  95. }
  96. ifaces, err := net.Interfaces()
  97. if err != nil {
  98. return nil, err
  99. }
  100. for _, iface := range ifaces {
  101. mac := network.NormalizeMac(iface.HardwareAddr.String())
  102. ij := ifaceJSON{
  103. Index: iface.Index,
  104. MTU: iface.MTU,
  105. Name: iface.Name,
  106. MAC: mac,
  107. Vendor: network.ManufLookup(mac),
  108. Flags: make([]string, 0),
  109. Addresses: make([]addrJSON, 0),
  110. }
  111. if addrs, err := iface.Addrs(); err == nil {
  112. for _, addr := range addrs {
  113. ij.Addresses = append(ij.Addresses, addrJSON{
  114. Address: addr.String(),
  115. Type: addr.Network(),
  116. })
  117. }
  118. }
  119. for bit, name := range flagNames {
  120. if iface.Flags&(1<<uint(bit)) != 0 {
  121. ij.Flags = append(ij.Flags, name)
  122. }
  123. }
  124. doc.Interfaces = append(doc.Interfaces, ij)
  125. }
  126. return json.Marshal(doc)
  127. }