forked from theojulienne/go-wireless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
286 lines (239 loc) · 6.14 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
package wireless
import (
"fmt"
"io"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
)
// WPAConn is an interface to the connection
type WPAConn interface {
SendCommand(...string) (string, error)
SendCommandBool(...string) error
SendCommandInt(...string) (int, error)
io.Closer
Subscribe(...string) *Subscription
}
// Client represents a wireless client
type Client struct {
conn WPAConn
mu sync.Mutex
}
// 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 nil, err
}
return c, nil
}
// NewClientFromConn returns a new client from an already established connection
func NewClientFromConn(conn WPAConn) (c *Client) {
c.conn = conn
return c
}
// Close will close the client connection
func (cl *Client) Close() {
cl.conn.Close()
}
// 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) {
cl.mu.Lock()
defer cl.mu.Unlock()
data, err := cl.conn.SendCommand(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) {
cl.mu.Lock()
defer cl.mu.Unlock()
err = cl.conn.SendCommandBool(CmdScan)
if err != nil {
return nets, err
}
results := cl.conn.Subscribe(EventScanResults)
defer results.Unsubscribe()
failed := cl.conn.Subscribe(EventScanFailed)
defer failed.Unsubscribe()
timer := time.NewTimer(time.Second * 2).C
func() {
for {
select {
case <-failed.Next():
err = ErrScanFailed
return
case <-results.Next():
return
case <-timer:
return
}
}
}()
scanned, err := cl.conn.SendCommand(CmdScanResults)
if err != nil {
return nets, err
}
return parseAP([]byte(scanned))
}
// Networks lists the known networks
func (cl *Client) Networks() (nets Networks, err error) {
cl.mu.Lock()
defer cl.mu.Unlock()
data, err := cl.conn.SendCommand(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
}
// Connect to a new or existing network
func (cl *Client) Connect(net Network) (Network, error) {
net, err := cl.AddOrUpdateNetwork(net)
if err != nil {
return net, err
}
sub := cl.conn.Subscribe(EventNetworkNotFound, EventAuthReject, EventConnected, EventDisconnected, EventAssocReject)
defer sub.Unsubscribe()
if net.IsDisabled() {
fmt.Printf("Enabling network %d\n", net.ID)
if err := cl.EnableNetwork(net.ID); err != nil {
return net, err
}
} else {
fmt.Printf("Selecting network %d\n", net.ID)
if err := cl.SelectNetwork(net.ID); err != nil {
return net, err
}
}
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
}
return net, errors.New("failed to catch event " + ev.Name)
}
func (cl *Client) Disconnect() (err error) {
_, err = cl.conn.SendCommand(CmdDisconnect)
return err
}
// AddOrUpdateNetwork will add or, if the network has IDStr set, update it
func (cl *Client) AddOrUpdateNetwork(net Network) (Network, error) {
if len(net.IDStr) == 0 {
nets, err := cl.Networks()
if err != nil {
return net, err
}
_, ok := nets.FindByIDStr(net.IDStr)
if ok {
return cl.UpdateNetwork(net)
}
}
// I don't think it's right for the same SSID to have
// multiple passwords. Check for a matching SSID.
if len(net.SSID) != 0 {
nets, err := cl.Networks()
if err != nil {
return net, err
}
found, ok := nets.FindBySSID(net.SSID)
if ok {
net.ID = found.ID
net.IDStr = found.IDStr
return cl.UpdateNetwork(net)
}
}
return cl.AddNetwork(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) {
if net.IDStr == "" {
return net, ErrNoIdentifier
}
for _, cmd := range setCmds(net) {
if err := cl.conn.SendCommandBool(cmd); err != nil {
return net, errors.Wrap(err, cmd)
}
}
return net, nil
}
// AddNetwork will add a new network
func (cl *Client) AddNetwork(net Network) (Network, error) {
i, err := cl.conn.SendCommandInt(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.SendCommandBool(cmd); err != nil {
return net, err
}
}
net.Known = true
return net, nil
}
// RemoveNetwork will RemoveNetwork
func (cl *Client) RemoveNetwork(id int) error {
return cl.conn.SendCommandBool(CmdRemoveNetwork, strconv.Itoa(id))
}
// EnableNetwork will EnableNetwork
func (cl *Client) EnableNetwork(id int) error {
return cl.conn.SendCommandBool(CmdEnableNetwork + " " + strconv.Itoa(id))
}
func (cl *Client) SelectNetwork(id int) error {
return cl.conn.SendCommandBool(CmdSelectNetwork + " " + strconv.Itoa(id))
}
// DisableNetwork will DisableNetwork
func (cl *Client) DisableNetwork(id int) error {
return cl.conn.SendCommandBool(CmdDisableNetwork + " " + strconv.Itoa(id))
}
// SaveConfig will SaveConfig
func (cl *Client) SaveConfig() error {
return cl.conn.SendCommandBool(CmdSaveConfig)
}
// LoadConfig will LoadConfig
func (cl *Client) LoadConfig() error {
return cl.conn.SendCommandBool(CmdReconfigure)
}
// GetNetworkAttr will get the given attribute of the given network
func (cl *Client) GetNetworkAttr(id int, attr string) (string, error) {
s, err := cl.conn.SendCommand(CmdGetNetwork, strconv.Itoa(id), attr)
if err != nil {
return s, err
}
return strings.TrimSpace(s), nil
}