forked from BellerophonMobile/gonetworkmanager
-
Notifications
You must be signed in to change notification settings - Fork 42
/
DnsManager.go
96 lines (79 loc) · 2.42 KB
/
DnsManager.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
package gonetworkmanager
import (
"errors"
"github.com/godbus/dbus/v5"
)
const (
DnsManagerInterface = NetworkManagerInterface + ".DnsManager"
DnsManagerObjectPath = "/org/freedesktop/NetworkManager/DnsManager"
/* Property */
DnsManagerPropertyMode = DnsManagerInterface + ".Mode" // readable s
DnsManagerPropertyRcManager = DnsManagerInterface + ".RcManager" // readable s
DnsManagerPropertyConfiguration = DnsManagerInterface + ".Configuration" // readable aa{sv}
)
type DnsConfigurationData struct {
Nameservers []string
Priority int32
Interface string
Vpn bool
}
type DnsManager interface {
GetPath() dbus.ObjectPath
GetPropertyMode() (string, error)
GetPropertyRcManager() (string, error)
GetPropertyConfiguration() ([]DnsConfigurationData, error)
}
type dnsManager struct {
dbusBase
}
func NewDnsManager() (DnsManager, error) {
var d dnsManager
return &d, d.init(NetworkManagerInterface, DnsManagerObjectPath)
}
func (d *dnsManager) GetPath() dbus.ObjectPath {
return d.obj.Path()
}
func (d *dnsManager) GetPropertyMode() (string, error) {
return d.getStringProperty(DnsManagerPropertyMode)
}
func (d *dnsManager) GetPropertyRcManager() (string, error) {
return d.getStringProperty(DnsManagerPropertyRcManager)
}
func (d *dnsManager) GetPropertyConfiguration() ([]DnsConfigurationData, error) {
configurations, err := d.getSliceMapStringVariantProperty(DnsManagerPropertyConfiguration)
if err != nil {
return nil, err
}
ret := make([]DnsConfigurationData, len(configurations))
for i, conf := range configurations {
if serversVar, exist := conf["nameservers"]; exist {
servers, ok := serversVar.Value().([]string)
if !ok {
return nil, errors.New("unexpected variant type for nameservers")
}
ret[i].Nameservers = servers
}
if priorityVar, exist := conf["priority"]; exist {
priority, ok := priorityVar.Value().(int32)
if !ok {
return nil, errors.New("unexpected variant type for priority")
}
ret[i].Priority = priority
}
if interfaceVar, exist := conf["interface"]; exist {
iface, ok := interfaceVar.Value().(string)
if !ok {
return nil, errors.New("unexpected variant type for interface")
}
ret[i].Interface = iface
}
if vpnVar, exist := conf["vpn"]; exist {
vpn, ok := vpnVar.Value().(bool)
if !ok {
return nil, errors.New("unexpected variant type for vpn")
}
ret[i].Vpn = vpn
}
}
return ret, nil
}