This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
/
dbus.go
374 lines (346 loc) · 11.4 KB
/
dbus.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
374
/*
* Copyright (C) 2017 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package netman
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/godbus/dbus"
)
// Client type to support unit test mock and runtime execution
type Client struct {
dbusClient DbusClient
}
// DbusClient properties for testing & runtime
type DbusClient struct {
test bool
BusObj dbus.BusObject
Connection *dbus.Conn
}
// Objecter allows mocking the godbus Object function
type Objecter interface {
Object(dest string, path dbus.ObjectPath) dbus.BusObject
}
// Object is the mock implementation of the godbus Object function
func (d *DbusClient) Object(dest string, path dbus.ObjectPath) dbus.BusObject {
obj := d.Connection.Object(dest, path)
return obj
}
// DefaultClient is the runtime client object
func DefaultClient() *Client {
conn := getSystemBus()
obj := conn.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
return &Client{
dbusClient: DbusClient{
test: false,
BusObj: obj,
Connection: getSystemBus(),
},
}
}
// NewClient is the mocked client object
func NewClient(myobj dbus.BusObject) *Client {
return &Client{
dbusClient: DbusClient{
test: true,
BusObj: myobj,
},
}
}
// for Non test operation, save the current bus object to the client
func setObject(c *Client, iface string, path dbus.ObjectPath) {
if !c.dbusClient.test {
c.dbusClient.BusObj = getSystemBus().Object(iface, path)
}
}
// GetDevices returns NetMan (NetworkManager) devices
func (c *Client) GetDevices() []string {
if !c.dbusClient.test {
c.dbusClient.Connection = getSystemBus()
}
c.dbusClient.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
setObject(c, "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
var devices []string
err := c.dbusClient.BusObj.Call("org.freedesktop.NetworkManager.GetAllDevices", 0).Store(&devices)
if err != nil {
fmt.Println("== wifi-connect: Error getting devices:", err)
}
return devices
}
// GetWifiDevices returns wifi NetMan devices
func (c *Client) GetWifiDevices(devices []string) []string {
var wifiDevices []string
for _, d := range devices {
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
deviceType, err2 := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.DeviceType")
if err2 != nil {
fmt.Println("== wifi-connect: Error getting wifi devices:", err2)
continue
}
var wifiType uint32
wifiType = 2
if deviceType.Value() == nil {
break
}
if deviceType.Value() != wifiType {
continue
}
wifiDevices = append(wifiDevices, d)
}
return wifiDevices
}
//GetAccessPoints returns NetMan known external APs
func (c *Client) GetAccessPoints(devices []string, ap2device map[string]string) []string {
var APs []string
for _, d := range devices {
var aps []string
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
err := c.dbusClient.BusObj.Call("org.freedesktop.NetworkManager.Device.Wireless.GetAllAccessPoints", 0).Store(&aps)
if err != nil {
fmt.Println("== wifi-connect: Error getting accesspoints:", err)
continue
}
if len(aps) == 0 {
break
}
for _, i := range aps {
APs = append(APs, i)
ap2device[i] = d
}
}
return APs
}
// SSID holds SSID properties
type SSID struct {
Ssid string
ApPath string
}
// getSsids returns known NetMan SSIDs
func (c *Client) getSsids(APs []string, ssid2ap map[string]string) []SSID {
var SSIDs []SSID
for _, ap := range APs {
objPath := dbus.ObjectPath(ap)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
ssid, err := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.AccessPoint.Ssid")
if err != nil {
fmt.Println("== wifi-connect: Error getting accesspoint's ssids:", err)
continue
}
type B []byte
res := B(ssid.Value().([]byte))
ssidStr := string(res)
if len(ssidStr) < 1 {
continue
}
for _, s := range SSIDs {
if s.Ssid == ssidStr {
continue
}
}
Ssid := SSID{Ssid: ssidStr, ApPath: ap}
SSIDs = append(SSIDs, Ssid)
ssid2ap[strings.TrimSpace(ssidStr)] = ap
//TODO: exclude ssid of device's own AP (the wifi-ap one)
}
return SSIDs
}
// ConnectAp attempts to Connect to an external AP
func (c *Client) ConnectAp(ssid string, p string, ap2device map[string]string, ssid2ap map[string]string) error {
inner1 := make(map[string]dbus.Variant)
inner1["security"] = dbus.MakeVariant("802-11-wireless-security")
inner2 := make(map[string]dbus.Variant)
inner2["key-mgmt"] = dbus.MakeVariant("wpa-psk")
inner2["psk"] = dbus.MakeVariant(p)
outer := make(map[string]map[string]dbus.Variant)
outer["802-11-wireless"] = inner1
outer["802-11-wireless-security"] = inner2
c.dbusClient.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
setObject(c, "org.freedesktop.NetworkManager", dbus.ObjectPath("/org/freedesktop/NetworkManager"))
c.dbusClient.BusObj.Call("org.freedesktop.NetworkManager.AddAndActivateConnection", 0, outer, dbus.ObjectPath(ap2device[ssid2ap[ssid]]), dbus.ObjectPath(ssid2ap[ssid]))
// loop until connected or until max loops
trying := true
idx := -1
for trying {
idx++
time.Sleep(1000 * time.Millisecond)
if c.Connected(c.GetWifiDevices(c.GetDevices())) {
return nil
}
if idx == 19 {
return errors.New("wifi-connect: cannot connect to AP")
}
}
return nil
}
func getSystemBus() *dbus.Conn {
conn, err := dbus.SystemBus()
if err != nil {
fmt.Fprintln(os.Stderr, "== wifi-connect: Error: Failed to connect to system bus:", err)
panic(1)
}
return conn
}
// Ssids returns known SSIDs
func (c *Client) Ssids() ([]SSID, map[string]string, map[string]string) {
ap2device := make(map[string]string)
ssid2ap := make(map[string]string)
devices := c.GetDevices()
wifiDevices := c.GetWifiDevices(devices)
APs := c.GetAccessPoints(wifiDevices, ap2device)
SSIDs := c.getSsids(APs, ssid2ap)
return SSIDs, ap2device, ssid2ap
}
// Connected checks if any passed ethernet/wifi devices are connected
func (c *Client) Connected(devices []string) bool {
for _, d := range devices {
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
dType, err := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.DeviceType")
if err != nil {
fmt.Println("== wifi-connect: Error getting device type:", err)
continue
}
state, err2 := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.State")
if err2 != nil {
fmt.Println("== wifi-connect: Error getting device state:", err2)
continue
}
// only handle eth and wifi device type
if dbus.Variant.Value(dType) != uint32(1) && dbus.Variant.Value(dType) != uint32(2) {
continue
}
if dbus.Variant.Value(state) == uint32(100) {
return true
}
}
return false
}
// ConnectedWifi checks if any passed wifi devices are connected
func (c *Client) ConnectedWifi(wifiDevices []string) bool {
for _, d := range wifiDevices {
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
state, err := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.State")
if err != nil {
fmt.Println("== wifi-connect: Error getting device state:", err)
continue
}
if dbus.Variant.Value(state) == uint32(100) {
return true
}
}
return false
}
// DisconnectWifi disconnects every interface passed. return shows number of disconnect calls made
func (c *Client) DisconnectWifi(wifiDevices []string) int {
ran := 0
for _, d := range wifiDevices {
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
c.dbusClient.BusObj.Call("org.freedesktop.NetworkManager.Device.Disconnect", 0)
ran++
}
return ran
}
// SetIfaceManaged sets passed device to be managed/unmanaged by network manager, return iface set, if any
func (c *Client) SetIfaceManaged(iface string, state bool, devices []string) string {
for _, d := range devices {
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
intface, err2 := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.Interface")
if err2 != nil {
fmt.Printf("== wifi-connect: Error in SetIfaceManaged() geting interface: %v\n", err2)
return ""
}
if iface != intface.Value().(string) {
continue
}
managed, err := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.Managed")
if err != nil {
fmt.Printf("== wifi-connect: Error in SetIfaceManaged() fetching device managed: %v\n", err)
return ""
}
switch state {
case true:
if managed.Value().(bool) == true {
return "" //no need to set, already managed
}
case false:
if managed.Value().(bool) == false {
return "" //no need to set, already UNmanaged
}
}
c.dbusClient.BusObj.Call("org.freedesktop.DBus.Properties.Set", 0, "org.freedesktop.NetworkManager.Device", "Managed", dbus.MakeVariant(state))
// loop until interface is in desired managed state or max iters reached
idx := -1
for {
idx++
time.Sleep(1000 * time.Millisecond)
managedState, err := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.State")
if err == nil {
switch state {
case true:
if managedState.Value() == uint32(30) { //NM_DEVICE_STATE_DISCONNECTED
return iface
}
case false:
if managedState.Value() == uint32(10) { //NM_DEVICE_STATE_UNMANAGED
return iface
}
}
}
if idx == 59 { //give it 60 iters ~= one minute
break
}
}
}
return "" //no iface state changed
}
// WifisManaged returns map[iface]device of wifi iterfaces that are managed by network manager
func (c *Client) WifisManaged(wifiDevices []string) (map[string]string, error) {
ifaces := make(map[string]string)
for _, d := range wifiDevices {
objPath := dbus.ObjectPath(d)
c.dbusClient.Object("org.freedesktop.NetworkManager", objPath)
setObject(c, "org.freedesktop.NetworkManager", objPath)
managed, err := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.Managed")
if err != nil {
fmt.Printf("== wifi-connect: Error in WifisManaged() getting device managed : %v\n", err)
return ifaces, err
}
iface, err2 := c.dbusClient.BusObj.GetProperty("org.freedesktop.NetworkManager.Device.Interface")
if err2 != nil {
fmt.Printf("== wifi-connect: Error in WifisManaged() getting device interface: %v\n", err)
return ifaces, err2
}
if managed.Value().(bool) == true {
ifaces[iface.Value().(string)] = d
}
}
return ifaces, nil
}