gowgpkg/devices.go
2021-05-14 15:08:19 +00:00

59 lines
No EOL
1.1 KiB
Go

package gowgpkg
import (
"fmt"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"github.com/vishvananda/netlink"
)
type Device struct {
Name string
PublicKey string
PrivateKey string
ListenPort int
}
func DevicesList() ([]Device, error) {
client, err := wgctrl.New()
if err != nil {
return nil, fmt.Errorf("Can't create wgctrl Client: %w", err)
}
devices, err := client.Devices()
if err != nil {
return nil, fmt.Errorf("Can't get list of devices: %w", err)
}
fmt.Println(devices)
var devList []Device
for _, d := range devices {
dev, err := convertToDevice(d)
if err != nil {
return nil, fmt.Errorf("Cant convertt wgtypes device to gowgpkg device: %w", err)
}
devList = append(devList, dev)
}
return devList, nil
}
func DevicesCreate(device Device) (error) {
// Create IP Device
la := netlink.NewLinkAttrs()
la.Name = device.Name
return nil
}
func convertToDevice(device *wgtypes.Device) (Device, error) {
d := Device{}
d.Name = device.Name
d.PublicKey = device.PublicKey.String()
d.PrivateKey = device.PrivateKey.String()
d.ListenPort = device.ListenPort
return d, nil
}