Update 'devices.go'
This commit is contained in:
parent
b0ba5a7563
commit
35c2edde40
1 changed files with 29 additions and 1 deletions
30
devices.go
30
devices.go
|
@ -33,7 +33,7 @@ func DevicesList() ([]Device, error) {
|
|||
for _, d := range devices {
|
||||
dev, err := convertToDevice(d)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Cant convertt wgtypes device to gowgpkg device: %w", err)
|
||||
return nil, fmt.Errorf("Cant convert wgtypes device to gowgpkg device: %w", err)
|
||||
}
|
||||
devList = append(devList, dev)
|
||||
}
|
||||
|
@ -41,6 +41,25 @@ func DevicesList() ([]Device, error) {
|
|||
return devList, nil
|
||||
}
|
||||
|
||||
func DevicesGet(name string) (Device, error) {
|
||||
client, err := wgctrl.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can't create wgctrl Client: %w", err)
|
||||
}
|
||||
|
||||
device, err := client.Device(name)
|
||||
if err != nil {
|
||||
return Device{}, fmt.Errorf("Cant get device: %w", err)
|
||||
}
|
||||
|
||||
d, err := convertToDevice(device)
|
||||
if err != nil {
|
||||
return Device{}, err
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func DevicesCreate(name string, ip netlink.Addr, privateKey string, listenPort int) (error) {
|
||||
// Create IP Device
|
||||
la := netlink.NewLinkAttrs()
|
||||
|
@ -107,10 +126,19 @@ func DeviceDelete(name string) (error) {
|
|||
|
||||
func convertToDevice(device *wgtypes.Device) (Device, error) {
|
||||
d := Device{}
|
||||
o, err := netlink.LinkByName(device.Name)
|
||||
if err != nil {
|
||||
return Device{}, fmt.Errorf("Cant get device: %w", err)
|
||||
}
|
||||
ips, err := netlink.AddrList(o, 0)
|
||||
if err != nil {
|
||||
return Device{}, fmt.Errorf("Cant get ip: %w", err)
|
||||
}
|
||||
d.Name = device.Name
|
||||
d.PublicKey = device.PublicKey.String()
|
||||
d.PrivateKey = device.PrivateKey.String()
|
||||
d.ListenPort = device.ListenPort
|
||||
d.IP = ips[0]
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue