-
Notifications
You must be signed in to change notification settings - Fork 32
/
client.go
373 lines (313 loc) · 8.71 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package wireless
import (
"context"
"errors"
"io"
"log"
"strconv"
"strings"
"time"
)
// WPAConn is an interface to the connection
type WPAConn interface {
SendCommand(...string) (string, error)
SendCommandBool(...string) error
SendCommandInt(...string) (int, error)
SendCommandWithContext(context.Context, ...string) (string, error)
SendCommandBoolWithContext(context.Context, ...string) error
SendCommandIntWithContext(context.Context, ...string) (int, error)
io.Closer
Subscribe(...string) *Subscription
}
// Client represents a wireless client
type Client struct {
conn WPAConn
ScanTimeout time.Duration
CmdTimeout time.Duration
ctx context.Context
}
// NewClient will create a new client by connecting to the
// given interface in WPA
func NewClient(iface string) (c *Client, err error) {
c = new(Client)
c.conn, err = Dial(iface)
if err != nil {
return
}
c.CmdTimeout = time.Second
return
}
// NewClientFromConn returns a new client from an already established connection
func NewClientFromConn(conn WPAConn) (c *Client) {
c = new(Client)
c.conn = conn
return
}
func (cl *Client) getContext() (context.Context, func()) {
if cl.ctx != nil {
return cl.ctx, func() {}
}
return context.WithTimeout(context.Background(), cl.CmdTimeout)
}
func (cl *Client) WithContext(ctx context.Context, ops ...func(wc *Client)) {
if len(ops) == 0 {
cl.ctx = ctx
return
}
wc := NewClientFromConn(cl.conn)
wc.ctx = ctx
for _, op := range ops {
op(wc)
}
}
// Close will close the client connection
func (cl *Client) Close() {
err := cl.conn.Close()
if err != nil {
log.Println("ERROR: client failed to close conn:", err)
}
}
// Conn will return the underlying connection
func (cl *Client) Conn() *Conn {
return cl.conn.(*Conn)
}
// Subscribe will subscribe to certain events that happen in WPA
func (cl *Client) Subscribe(topics ...string) *Subscription {
return cl.conn.Subscribe(topics...)
}
// Status will return the current state of the WPA
func (cl *Client) Status() (State, error) {
ctx, cancel := cl.getContext()
defer cancel()
data, err := cl.conn.SendCommandWithContext(ctx, CmdStatus)
if err != nil {
return State{}, err
}
s := NewState(data)
return s, nil
}
// Scan will scan for networks and return the APs it finds
func (cl *Client) Scan() (nets APs, err error) {
timeout := cl.ScanTimeout
if timeout == 0 {
timeout = 2 * time.Second
}
err = cl.conn.SendCommandBool(CmdScan)
if err != nil {
return
}
results := cl.conn.Subscribe(EventScanResults)
failed := cl.conn.Subscribe(EventScanFailed)
defer results.Unsubscribe()
defer failed.Unsubscribe()
func() {
for {
select {
case <-failed.Next():
err = ErrScanFailed
return
case <-results.Next():
return
case <-time.NewTimer(cl.ScanTimeout).C:
return
}
}
}()
scanned, err := cl.conn.SendCommand(CmdScanResults)
if err != nil {
return
}
return parseAP([]byte(scanned))
}
// Networks lists the known networks
func (cl *Client) Networks() (nets Networks, err error) {
ctx, cancel := cl.getContext()
defer cancel()
return cl.NetworksWithContext(ctx)
}
func (cl *Client) NetworksWithContext(ctx context.Context) (nets Networks, err error) {
data, err := cl.conn.SendCommandWithContext(ctx, CmdListNetworks)
if err != nil {
return nil, err
}
nets, err = parseNetwork([]byte(data))
if err != nil {
return nil, err
}
for i := range nets {
nets[i].Known = true
(&nets[i]).populateAttrs(cl)
}
return nets, nil
}
func (cl *Client) Connect(net Network) (Network, error) {
ctx, cancel := cl.getContext()
defer cancel()
return cl.ConnectWithContext(ctx, net)
}
// Connect to a new or existing network
func (cl *Client) ConnectWithContext(ctx context.Context, net Network) (Network, error) {
net, err := cl.AddOrUpdateNetworkWithContext(ctx, net)
if err != nil {
return net, err
}
sub := cl.conn.Subscribe(EventNetworkNotFound, EventAuthReject, EventConnected, EventDisconnected, EventAssocReject)
defer sub.Unsubscribe()
if err := cl.EnableNetworkWithContext(ctx, net.ID); err != nil {
return net, err
}
if err := cl.conn.SendCommandBoolWithContext(ctx, "REASSOCIATE"); err != nil {
return net, err
}
if err := cl.conn.SendCommandBoolWithContext(ctx, "RECONNECT"); err != nil {
return net, err
}
select {
case ev := <-sub.Next():
switch ev.Name {
case EventConnected:
return net, cl.SaveConfig()
case EventNetworkNotFound:
return net, ErrSSIDNotFound
case EventAuthReject:
return net, ErrAuthFailed
case EventDisconnected:
return net, ErrDisconnected
case EventAssocReject:
return net, ErrAssocRejected
default:
return net, errors.New("failed to catch event " + ev.Name)
}
case <-ctx.Done():
return net, ctx.Err()
}
}
// AddOrUpdateNetwork will add or, if the network has IDStr set, update it
func (cl *Client) AddOrUpdateNetwork(net Network) (Network, error) {
ctx, cancel := cl.getContext()
defer cancel()
return cl.AddNetworkWithContext(ctx, net)
}
func (cl *Client) AddOrUpdateNetworkWithContext(ctx context.Context, net Network) (Network, error) {
if net.IDStr != "" {
nets, err := cl.NetworksWithContext(ctx)
if err != nil {
return net, err
}
for _, n := range nets {
if n.IDStr == net.IDStr {
return cl.UpdateNetworkWithContext(ctx, net)
}
}
}
return cl.AddNetworkWithContext(ctx, net)
}
// UpdateNetwork will update the given network, an error will be thrown
// if the network doesn't have IDStr specified
func (cl *Client) UpdateNetwork(net Network) (Network, error) {
ctx, cancel := cl.getContext()
defer cancel()
return cl.UpdateNetworkWithContext(ctx, net)
}
func (cl *Client) UpdateNetworkWithContext(ctx context.Context, net Network) (Network, error) {
if net.IDStr == "" {
return net, ErrNoIdentifier
}
for _, cmd := range setCmds(net) {
if err := cl.conn.SendCommandBoolWithContext(ctx, cmd); err != nil {
return net, err
}
}
return net, nil
}
// AddNetwork will add a new network
func (cl *Client) AddNetwork(net Network) (Network, error) {
ctx, cancel := cl.getContext()
defer cancel()
return cl.AddNetworkWithContext(ctx, net)
}
// AddNetwork will add a new network
func (cl *Client) AddNetworkWithContext(ctx context.Context, net Network) (Network, error) {
nets, err := cl.Networks()
if err == nil {
if nw, found := nets.FindBySSID(net.SSID); found {
return nw, nil
}
}
i, err := cl.conn.SendCommandIntWithContext(ctx, CmdAddNetwork)
if err != nil {
return net, err
}
net.ID = i
if net.IDStr == "" {
net.IDStr = net.SSID
}
for _, cmd := range setCmds(net) {
if err := cl.conn.SendCommandBoolWithContext(ctx, cmd); err != nil {
return net, err
}
}
net.Known = true
return net, nil
}
// RemoveNetwork will RemoveNetwork
func (cl *Client) RemoveNetwork(id int) error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.conn.SendCommandBoolWithContext(ctx, CmdRemoveNetwork, strconv.Itoa(id))
}
// EnableNetwork will EnableNetwork
func (cl *Client) EnableNetwork(id int) error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.EnableNetworkWithContext(ctx, id)
}
func (cl *Client) EnableNetworkWithContext(ctx context.Context, id int) error {
return cl.conn.SendCommandBoolWithContext(ctx, CmdEnableNetwork, strconv.Itoa(id))
}
// SelectNetwork will SelectNetwork
func (cl *Client) SelectNetwork(id int) error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.SelectNetworkWithContext(ctx, id)
}
func (cl *Client) SelectNetworkWithContext(ctx context.Context, id int) error {
return cl.conn.SendCommandBoolWithContext(ctx, CmdSelectNetwork, strconv.Itoa(id))
}
// Disconnect will Disconnect
func (cl *Client) Disconnect() error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.DisconnectWithContext(ctx)
}
func (cl *Client) DisconnectWithContext(ctx context.Context) error {
return cl.conn.SendCommandBoolWithContext(ctx, CmdDisconnect)
}
// DisableNetwork will DisableNetwork
func (cl *Client) DisableNetwork(id int) error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.conn.SendCommandBoolWithContext(ctx, CmdDisableNetwork+" "+strconv.Itoa(id))
}
// SaveConfig will SaveConfig
func (cl *Client) SaveConfig() error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.conn.SendCommandBoolWithContext(ctx, CmdSaveConfig)
}
// LoadConfig will LoadConfig
func (cl *Client) LoadConfig() error {
ctx, cancel := cl.getContext()
defer cancel()
return cl.conn.SendCommandBoolWithContext(ctx, CmdReconfigure)
}
// GetNetworkAttr will get the given attribute of the given network
func (cl *Client) GetNetworkAttr(id int, attr string) (string, error) {
ctx, cancel := cl.getContext()
defer cancel()
s, err := cl.conn.SendCommandWithContext(ctx, CmdGetNetwork, strconv.Itoa(id), attr)
if err != nil {
return s, err
}
return strings.TrimSpace(s), nil
}