Update 'devices.go'
This commit is contained in:
parent
7b4efbc697
commit
b0ba5a7563
1 changed files with 61 additions and 4 deletions
65
devices.go
65
devices.go
|
@ -12,6 +12,7 @@ type Device struct {
|
|||
PublicKey string
|
||||
PrivateKey string
|
||||
ListenPort int
|
||||
IP netlink.Addr
|
||||
}
|
||||
|
||||
func DevicesList() ([]Device, error) {
|
||||
|
@ -24,7 +25,7 @@ func DevicesList() ([]Device, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("Can't get list of devices: %w", err)
|
||||
}
|
||||
|
||||
|
||||
fmt.Println(devices)
|
||||
|
||||
var devList []Device
|
||||
|
@ -40,10 +41,66 @@ func DevicesList() ([]Device, error) {
|
|||
return devList, nil
|
||||
}
|
||||
|
||||
func DevicesCreate(device Device) (error) {
|
||||
func DevicesCreate(name string, ip netlink.Addr, privateKey string, listenPort int) (error) {
|
||||
// Create IP Device
|
||||
la := netlink.NewLinkAttrs()
|
||||
la.Name = device.Name
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -56,4 +113,4 @@ func convertToDevice(device *wgtypes.Device) (Device, error) {
|
|||
d.ListenPort = device.ListenPort
|
||||
|
||||
return d, nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue