Debian Codegraph @

network/wifi_ap.go

raw symbols SCIP
1 package network
2
3 import (
4 "encoding/json"
5 "sync"
6 "time"
7
8 "github.com/evilsocket/islazy/data"
9 )
10
11 type AccessPoint struct {
12 *Station
13 sync.RWMutex
14
15 aliases *data.UnsortedKV
16 clients map[string]*Station
17 withKeyMaterial bool
18 }
19
20 type apJSON struct {
21 *Station
22 Clients []*Station `json:"clients"`
23 Handshake bool `json:"handshake"`
24 }
25
26 func NewAccessPoint(essid, bssid string, frequency int, rssi int8, aliases *data.UnsortedKV) *AccessPoint {
27 return &AccessPoint{
28 Station: NewStation(essid, bssid, frequency, rssi),
29 aliases: aliases,
30 clients: make(map[string]*Station),
31 }
32 }
33
34 func (ap *AccessPoint) MarshalJSON() ([]byte, error) {
35 ap.RLock()
36 defer ap.RUnlock()
37
38 doc := apJSON{
39 Station: ap.Station,
40 Clients: make([]*Station, 0, len(ap.clients)),
41 Handshake: ap.withKeyMaterial,
42 }
43
44 for _, c := range ap.clients {
45 doc.Clients = append(doc.Clients, c)
46 }
47
48 return json.Marshal(doc)
49 }
50
51 func (ap *AccessPoint) Get(bssid string) (*Station, bool) {
52 ap.RLock()
53 defer ap.RUnlock()
54
55 bssid = NormalizeMac(bssid)
56 if s, found := ap.clients[bssid]; found {
57 return s, true
58 }
59 return nil, false
60 }
61
62 func (ap *AccessPoint) RemoveClient(mac string) {
63 ap.Lock()
64 defer ap.Unlock()
65
66 bssid := NormalizeMac(mac)
67 delete(ap.clients, bssid)
68 }
69
70 func (ap *AccessPoint) AddClientIfNew(bssid string, frequency int, rssi int8) (*Station, bool) {
71 ap.Lock()
72 defer ap.Unlock()
73
74 bssid = NormalizeMac(bssid)
75 alias := ap.aliases.GetOr(bssid, "")
76
77 if s, found := ap.clients[bssid]; found {
78 // update
79 s.Frequency = frequency
80 s.RSSI = rssi
81 s.LastSeen = time.Now()
82
83 if alias != "" {
84 s.Alias = alias
85 }
86
87 return s, false
88 }
89
90 s := NewStation("", bssid, frequency, rssi)
91 s.Alias = alias
92 ap.clients[bssid] = s
93
94 return s, true
95 }
96
97 func (ap *AccessPoint) NumClients() int {
98 ap.RLock()
99 defer ap.RUnlock()
100 return len(ap.clients)
101 }
102
103 func (ap *AccessPoint) Clients() (list []*Station) {
104 ap.RLock()
105 defer ap.RUnlock()
106
107 list = make([]*Station, 0, len(ap.clients))
108 for _, c := range ap.clients {
109 list = append(list, c)
110 }
111 return
112 }
113
114 func (ap *AccessPoint) EachClient(cb func(mac string, station *Station)) {
115 ap.Lock()
116 defer ap.Unlock()
117
118 for m, station := range ap.clients {
119 cb(m, station)
120 }
121 }
122
123 func (ap *AccessPoint) WithKeyMaterial(state bool) {
124 ap.Lock()
125 defer ap.Unlock()
126
127 ap.withKeyMaterial = state
128 }
129
130 func (ap *AccessPoint) HasKeyMaterial() bool {
131 ap.RLock()
132 defer ap.RUnlock()
133
134 return ap.withKeyMaterial
135 }
136
137 func (ap *AccessPoint) NumHandshakes() int {
138 ap.RLock()
139 defer ap.RUnlock()
140
141 sum := 0
142
143 for _, c := range ap.clients {
144 if c.Handshake.Complete() {
145 sum++
146 }
147 }
148
149 return sum
150 }
151
152 func (ap *AccessPoint) HasHandshakes() bool {
153 return ap.NumHandshakes() > 0
154 }
155
156 func (ap *AccessPoint) HasPMKID() bool {
157 ap.RLock()
158 defer ap.RUnlock()
159
160 for _, c := range ap.clients {
161 if c.Handshake.HasPMKID() {
162 return true
163 }
164 }
165
166 return false
167 }