35 lines
779 B
Go
35 lines
779 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
"golang.zx2c4.com/wireguard/wgtypes"
|
|
)
|
|
|
|
// TODO: replace this with a config value
|
|
const deviceName = "wg0"
|
|
|
|
func main() {
|
|
client, err := wgctrl.New()
|
|
defer client.Close()
|
|
|
|
if err != nil {
|
|
log.Fatalf("Unable to create wireguard client. Is wireguard installed? %v", err)
|
|
}
|
|
|
|
log.Println(client.Devices())
|
|
|
|
}
|
|
|
|
func addPeer(client *wgctrl.Client, deviceName string, key wgtypes.Key) error {
|
|
// TODO: load from config
|
|
_, allowedNetwork, _ := net.ParseCIDR("172.16.0.1/20")
|
|
allowedIps := []net.IPNet{allowedNetwork}
|
|
peer := wgtypes.PeerConfig{Key: key, AllowedIPs: allowedIps}
|
|
|
|
config := wgtypes.Config{Peers: []PeerConfig{peer}, ReplacePeers: false}
|
|
return client.ConfigureDevice(name, config)
|
|
}
|