2021-05-14 14:27:10 +00:00
|
|
|
package gowgpkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
2021-05-14 14:56:17 +00:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
"github.com/vishvananda/netlink"
|
2021-05-14 14:27:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Device struct {
|
2021-05-14 14:56:17 +00:00
|
|
|
Name string
|
|
|
|
PublicKey string
|
|
|
|
PrivateKey string
|
|
|
|
ListenPort int
|
2021-05-14 14:27:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:56:17 +00:00
|
|
|
func DevicesList() ([]Device, error) {
|
2021-05-14 14:27:10 +00:00
|
|
|
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)
|
|
|
|
|
2021-05-14 14:56:17 +00:00
|
|
|
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
|
2021-05-14 15:08:19 +00:00
|
|
|
|
|
|
|
return nil
|
2021-05-14 14:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-05-14 14:27:10 +00:00
|
|
|
}
|