Debian Codegraph @

session/session_json.go

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