gowgpkg/devices.go

143 lines
2.9 KiB
Go
Raw Normal View History

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-16 14:41:17 +00:00
IP netlink.Addr
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)
}
2021-05-16 14:41:17 +00:00
2021-05-14 14:56:17 +00:00
var devList []Device
for _, d := range devices {
dev, err := convertToDevice(d)
if err != nil {
2021-05-18 11:30:07 +00:00
return nil, fmt.Errorf("Cant convert wgtypes device to gowgpkg device: %w", err)
2021-05-14 14:56:17 +00:00
}
devList = append(devList, dev)
}
return devList, nil
}
2021-05-18 11:30:07 +00:00
func DevicesGet(name string) (Device, error) {
client, err := wgctrl.New()
if err != nil {
2021-06-01 16:00:09 +00:00
return Device{}, fmt.Errorf("Can't create wgctrl Client: %w", err)
2021-05-18 11:30:07 +00:00
}
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
}
2021-05-16 14:41:17 +00:00
func DevicesCreate(name string, ip netlink.Addr, privateKey string, listenPort int) (error) {
2021-05-14 14:56:17 +00:00
// Create IP Device
la := netlink.NewLinkAttrs()
2021-05-16 14:41:17 +00:00
la.Name = name
wgDev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkAdd(wgDev)
if err != nil {
return fmt.Errorf("Cant create Link to Device: %w", err)
}
err = netlink.AddrAdd(wgDev, &ip)
if err != nil {
return fmt.Errorf("Cant add ip to device: %w", err)
}
privkey, err := wgtypes.ParseKey(privateKey)
if err != nil {
return fmt.Errorf("Cant parse private key: %w", err)
}
config := wgtypes.Config{
PrivateKey: &privkey,
ListenPort: &listenPort,
FirewallMark: nil,
ReplacePeers: false,
Peers: nil,
}
client, err := wgctrl.New()
if err != nil {
return fmt.Errorf("Cant create wireguard client: %w", err)
}
client.ConfigureDevice(name, config)
err = netlink.LinkSetUp(wgDev)
if err != nil {
return fmt.Errorf("Cant start device: %w", err)
}
return nil
}
func DeviceDelete(name string) (error) {
la := netlink.NewLinkAttrs()
la.Name = name
dev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkDel(dev)
if err != nil {
return fmt.Errorf("Cant delete Device: %w", err)
}
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{}
2021-05-18 11:30:07 +00:00
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)
}
2021-05-14 14:56:17 +00:00
d.Name = device.Name
d.PublicKey = device.PublicKey.String()
d.PrivateKey = device.PrivateKey.String()
d.ListenPort = device.ListenPort
2021-05-18 11:30:07 +00:00
d.IP = ips[0]
2021-05-14 14:56:17 +00:00
return d, nil
2021-05-16 14:41:17 +00:00
}