This commit is contained in:
Kekskurse 2021-06-01 17:22:20 +02:00
parent 35c2edde40
commit c8a240ae35
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
2 changed files with 68 additions and 5 deletions

View file

@ -1,5 +1,11 @@
test #GoWgPKG
Easy to use wireguard abstraction for linux
# Used git.keks.cloud/kekskurse/gowgpkg
* github.com/vishvananda/netlink
* golang.zx2c4.com/wireguard/wgctrl Go Package to configure Wireguard on Linux
* Create Devices
* Configure Device
* Create Wireguard
* Configure wireguard
* Provides Structs with all needed information

57
peer.go Normal file
View file

@ -0,0 +1,57 @@
package gowgpkg
import (
"fmt"
"net"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func PeerAdd(deviceName string, PeerPublicKey string, PeerPresharedKey string, ipList []net.IPNet, endpoint *net.UDPAddr) (error) {
client, err := wgctrl.New()
if err != nil {
return fmt.Errorf("Cant create wgctrl: %w", err)
}
pubKey, err := wgtypes.ParseKey(PeerPublicKey)
if err != nil {
return fmt.Errorf("Cant parse public key: %e", err)
}
var preKey *wgtypes.Key
if PeerPresharedKey != "" {
presharedKey, err := wgtypes.ParseKey(PeerPresharedKey)
if err != nil {
return fmt.Errorf("Cant parse preshared key: %e", err)
}
preKey = &presharedKey
}
peerConfig := wgtypes.PeerConfig{
PublicKey: pubKey,
Remove: false,
UpdateOnly: false,
PresharedKey: preKey,
Endpoint: endpoint,
PersistentKeepaliveInterval: nil,
ReplaceAllowedIPs: true,
AllowedIPs: ipList,
}
var peerConfigs []wgtypes.PeerConfig
peerConfigs = append(peerConfigs, peerConfig)
config := wgtypes.Config{
PrivateKey: nil,
ListenPort: nil,
FirewallMark: nil,
ReplacePeers: false,
Peers: peerConfigs,
}
err = client.ConfigureDevice(deviceName, config)
if err != nil {
return fmt.Errorf("Cant add peer: %e", err)
}
return nil
}