2021-09-20 02:03:47 +08:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
2022-02-06 03:20:50 +08:00
|
|
|
"fmt"
|
2021-09-20 02:03:47 +08:00
|
|
|
"log"
|
2022-02-03 11:00:59 +08:00
|
|
|
"net"
|
2021-09-20 02:03:47 +08:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-09-22 05:50:09 +08:00
|
|
|
"time"
|
2021-09-20 02:03:47 +08:00
|
|
|
|
2022-03-20 23:12:05 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/netclient/config"
|
|
|
|
"github.com/gravitl/netmaker/netclient/local"
|
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2022-01-05 23:05:51 +08:00
|
|
|
"gopkg.in/ini.v1"
|
2021-09-20 02:03:47 +08:00
|
|
|
)
|
|
|
|
|
2022-01-05 23:32:20 +08:00
|
|
|
const (
|
|
|
|
section_interface = "Interface"
|
|
|
|
section_peers = "Peer"
|
|
|
|
)
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// SetPeers - sets peers on a given WireGuard interface
|
2022-02-21 22:45:42 +08:00
|
|
|
func SetPeers(iface string, node *models.Node, peers []wgtypes.PeerConfig) error {
|
2021-11-11 05:08:29 +08:00
|
|
|
var devicePeers []wgtypes.Peer
|
2022-02-21 22:45:42 +08:00
|
|
|
var keepalive = node.PersistentKeepalive
|
2022-06-15 23:18:49 +08:00
|
|
|
var oldPeerAllowedIps = make(map[string]bool, len(peers))
|
|
|
|
|
2021-11-11 05:08:29 +08:00
|
|
|
var err error
|
2022-04-26 04:30:18 +08:00
|
|
|
devicePeers, err = GetDevicePeers(iface)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-11-11 05:08:29 +08:00
|
|
|
}
|
2022-04-26 04:30:18 +08:00
|
|
|
|
2021-09-20 02:03:47 +08:00
|
|
|
if len(devicePeers) > 1 && len(peers) == 0 {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "no peers pulled")
|
2021-09-20 02:03:47 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-02-08 03:43:06 +08:00
|
|
|
for _, peer := range peers {
|
2022-05-20 04:04:47 +08:00
|
|
|
// make sure peer has AllowedIP's before comparison
|
|
|
|
hasPeerIP := len(peer.AllowedIPs) > 0
|
2022-02-08 03:43:06 +08:00
|
|
|
for _, currentPeer := range devicePeers {
|
2022-05-20 04:04:47 +08:00
|
|
|
// make sure currenPeer has AllowedIP's before comparison
|
|
|
|
hascurrentPeerIP := len(currentPeer.AllowedIPs) > 0
|
|
|
|
|
|
|
|
if hasPeerIP && hascurrentPeerIP &&
|
|
|
|
currentPeer.AllowedIPs[0].String() == peer.AllowedIPs[0].String() &&
|
2022-02-08 03:43:06 +08:00
|
|
|
currentPeer.PublicKey.String() != peer.PublicKey.String() {
|
|
|
|
_, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error removing peer", peer.Endpoint.String())
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 03:43:06 +08:00
|
|
|
udpendpoint := peer.Endpoint.String()
|
|
|
|
var allowedips string
|
|
|
|
var iparr []string
|
|
|
|
for _, ipaddr := range peer.AllowedIPs {
|
2022-06-17 03:42:32 +08:00
|
|
|
if hasPeerIP {
|
2022-04-23 01:04:34 +08:00
|
|
|
iparr = append(iparr, ipaddr.String())
|
|
|
|
}
|
2022-02-08 03:43:06 +08:00
|
|
|
}
|
2022-06-17 03:42:32 +08:00
|
|
|
if len(iparr) > 0 {
|
2022-05-11 03:18:29 +08:00
|
|
|
allowedips = strings.Join(iparr, ",")
|
|
|
|
}
|
2022-02-08 03:43:06 +08:00
|
|
|
keepAliveString := strconv.Itoa(int(keepalive))
|
|
|
|
if keepAliveString == "0" {
|
|
|
|
keepAliveString = "15"
|
|
|
|
}
|
2022-06-21 23:19:54 +08:00
|
|
|
if node.IsServer == "yes" || peer.Endpoint == nil {
|
2022-02-08 03:43:06 +08:00
|
|
|
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
|
|
|
" persistent-keepalive "+keepAliveString+
|
|
|
|
" allowed-ips "+allowedips, true)
|
|
|
|
} else {
|
|
|
|
_, err = ncutils.RunCmd("wg set "+iface+" peer "+peer.PublicKey.String()+
|
2022-02-21 22:45:42 +08:00
|
|
|
" endpoint "+udpendpoint+
|
2022-02-08 03:43:06 +08:00
|
|
|
" persistent-keepalive "+keepAliveString+
|
|
|
|
" allowed-ips "+allowedips, true)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error setting peer", peer.PublicKey.String())
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 03:42:32 +08:00
|
|
|
if len(devicePeers) > 0 {
|
2022-05-11 03:18:29 +08:00
|
|
|
for _, currentPeer := range devicePeers {
|
|
|
|
shouldDelete := true
|
2022-06-17 03:42:32 +08:00
|
|
|
if len(peers) > 0 {
|
2022-05-11 03:18:29 +08:00
|
|
|
for _, peer := range peers {
|
2022-05-20 04:04:47 +08:00
|
|
|
|
|
|
|
if len(peer.AllowedIPs) > 0 && len(currentPeer.AllowedIPs) > 0 &&
|
|
|
|
peer.AllowedIPs[0].String() == currentPeer.AllowedIPs[0].String() {
|
2022-05-11 03:18:29 +08:00
|
|
|
shouldDelete = false
|
|
|
|
}
|
|
|
|
// re-check this if logic is not working, added in case of allowedips not working
|
|
|
|
if peer.PublicKey.String() == currentPeer.PublicKey.String() {
|
|
|
|
shouldDelete = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldDelete {
|
|
|
|
output, err := ncutils.RunCmd("wg set "+iface+" peer "+currentPeer.PublicKey.String()+" remove", true)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(output, "error removing peer", currentPeer.PublicKey.String())
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 23:18:49 +08:00
|
|
|
for _, ip := range currentPeer.AllowedIPs {
|
|
|
|
oldPeerAllowedIps[ip.String()] = true
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 23:03:23 +08:00
|
|
|
if ncutils.IsMac() {
|
|
|
|
err = SetMacPeerRoutes(iface)
|
|
|
|
return err
|
2022-02-08 03:43:06 +08:00
|
|
|
} else if ncutils.IsLinux() {
|
2022-06-17 03:42:32 +08:00
|
|
|
if len(peers) > 0 {
|
2022-05-11 03:18:29 +08:00
|
|
|
local.SetPeerRoutes(iface, oldPeerAllowedIps, peers)
|
|
|
|
}
|
2022-01-12 23:03:23 +08:00
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// Initializes a WireGuard interface
|
2022-04-22 03:53:44 +08:00
|
|
|
func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig, syncconf bool) error {
|
2021-09-20 02:03:47 +08:00
|
|
|
|
|
|
|
key, err := wgtypes.ParseKey(privkey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wgclient, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-11 04:01:10 +08:00
|
|
|
defer wgclient.Close()
|
2022-06-01 20:00:22 +08:00
|
|
|
//nodecfg := modcfg.Node
|
2021-09-22 02:01:52 +08:00
|
|
|
var ifacename string
|
2022-07-07 03:20:28 +08:00
|
|
|
if node.Interface != "" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ifacename = node.Interface
|
|
|
|
} else {
|
2022-02-08 03:04:07 +08:00
|
|
|
return fmt.Errorf("no interface to configure")
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-04-20 04:18:03 +08:00
|
|
|
if node.PrimaryAddress() == "" {
|
2022-02-08 03:04:07 +08:00
|
|
|
return fmt.Errorf("no address to configure")
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-07-07 03:20:28 +08:00
|
|
|
if err := WriteWgConfig(node, key.String(), peers); err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "error writing wg conf file: ", err.Error())
|
2021-12-30 19:45:45 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// spin up userspace / windows interface + apply the conf file
|
2022-01-25 20:31:50 +08:00
|
|
|
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
|
2022-02-06 03:20:50 +08:00
|
|
|
var deviceiface = ifacename
|
2022-04-26 08:37:05 +08:00
|
|
|
var mErr error
|
2022-02-06 03:20:50 +08:00
|
|
|
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
|
2022-04-26 08:37:05 +08:00
|
|
|
deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
|
|
|
|
if mErr != nil || deviceiface == "" {
|
2021-12-30 19:45:45 +08:00
|
|
|
deviceiface = ifacename
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 03:20:50 +08:00
|
|
|
// ensure you clear any existing interface first
|
2022-03-15 09:03:49 +08:00
|
|
|
RemoveConfGraceful(deviceiface)
|
2022-03-20 23:12:05 +08:00
|
|
|
ApplyConf(node, ifacename, confPath) // Apply initially
|
|
|
|
logger.Log(1, "waiting for interface...") // ensure interface is created
|
2022-02-06 03:20:50 +08:00
|
|
|
output, _ := ncutils.RunCmd("wg", false)
|
|
|
|
starttime := time.Now()
|
2022-02-08 00:30:15 +08:00
|
|
|
ifaceReady := strings.Contains(output, deviceiface)
|
2022-02-06 03:31:58 +08:00
|
|
|
for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
|
2022-02-08 00:30:15 +08:00
|
|
|
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
|
2022-04-26 08:37:05 +08:00
|
|
|
deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
|
|
|
|
if mErr != nil || deviceiface == "" {
|
2022-02-08 00:30:15 +08:00
|
|
|
deviceiface = ifacename
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 03:20:50 +08:00
|
|
|
output, _ = ncutils.RunCmd("wg", false)
|
2022-02-09 08:13:46 +08:00
|
|
|
err = ApplyConf(node, node.Interface, confPath)
|
2022-02-06 03:20:50 +08:00
|
|
|
time.Sleep(time.Second)
|
2022-02-08 00:30:15 +08:00
|
|
|
ifaceReady = strings.Contains(output, deviceiface)
|
2022-02-06 03:20:50 +08:00
|
|
|
}
|
2022-02-07 18:45:24 +08:00
|
|
|
//wgclient does not work well on freebsd
|
|
|
|
if node.OS == "freebsd" {
|
|
|
|
if !ifaceReady {
|
|
|
|
return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, devErr := wgclient.Device(deviceiface)
|
|
|
|
if !ifaceReady || devErr != nil {
|
2022-03-15 23:11:22 +08:00
|
|
|
fmt.Printf("%v\n", devErr)
|
2022-02-07 18:45:24 +08:00
|
|
|
return fmt.Errorf("could not reliably create interface, please check wg installation and retry")
|
|
|
|
}
|
2022-02-06 03:20:50 +08:00
|
|
|
}
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "interface ready - netclient.. ENGAGE")
|
2022-02-06 03:20:50 +08:00
|
|
|
if syncconf { // should never be called really.
|
2022-03-03 05:19:53 +08:00
|
|
|
fmt.Println("why here")
|
2021-12-30 19:45:45 +08:00
|
|
|
err = SyncWGQuickConf(ifacename, confPath)
|
2022-02-06 03:20:50 +08:00
|
|
|
}
|
2022-02-17 09:19:51 +08:00
|
|
|
if !ncutils.HasWgQuick() && ncutils.IsLinux() {
|
2022-02-21 22:45:42 +08:00
|
|
|
err = SetPeers(ifacename, node, peers)
|
2022-02-17 09:19:51 +08:00
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(1, "error setting peers: ", err.Error())
|
2022-02-17 09:19:51 +08:00
|
|
|
}
|
2022-04-26 08:37:05 +08:00
|
|
|
|
2022-02-17 09:19:51 +08:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2022-04-20 04:18:03 +08:00
|
|
|
|
|
|
|
//ipv4
|
|
|
|
if node.Address != "" {
|
2022-07-07 03:20:28 +08:00
|
|
|
_, cidr, cidrErr := net.ParseCIDR(node.NetworkSettings.AddressRange)
|
2022-04-20 04:18:03 +08:00
|
|
|
if cidrErr == nil {
|
|
|
|
local.SetCIDRRoute(ifacename, node.Address, cidr)
|
|
|
|
} else {
|
|
|
|
logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
|
|
|
|
}
|
|
|
|
local.SetCurrentPeerRoutes(ifacename, node.Address, peers)
|
|
|
|
}
|
|
|
|
if node.Address6 != "" {
|
|
|
|
//ipv6
|
2022-07-07 03:20:28 +08:00
|
|
|
_, cidr, cidrErr := net.ParseCIDR(node.NetworkSettings.AddressRange6)
|
2022-04-20 04:18:03 +08:00
|
|
|
if cidrErr == nil {
|
|
|
|
local.SetCIDRRoute(ifacename, node.Address6, cidr)
|
|
|
|
} else {
|
|
|
|
logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
|
|
|
|
}
|
|
|
|
local.SetCurrentPeerRoutes(ifacename, node.Address6, peers)
|
2021-11-15 05:50:20 +08:00
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// SetWGConfig - sets the WireGuard Config of a given network and checks if it needs a peer update
|
2022-04-26 04:30:18 +08:00
|
|
|
func SetWGConfig(network string, peerupdate bool, peers []wgtypes.PeerConfig) error {
|
2021-09-20 02:03:47 +08:00
|
|
|
|
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
privkey, err := RetrievePrivKey(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-20 04:18:03 +08:00
|
|
|
if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
|
|
|
|
var iface string
|
2022-07-07 03:20:28 +08:00
|
|
|
iface = cfg.Node.Interface
|
2022-04-20 04:18:03 +08:00
|
|
|
if ncutils.IsMac() {
|
2022-07-07 03:20:28 +08:00
|
|
|
iface, err = local.GetMacIface(cfg.Node.PrimaryAddress())
|
2022-04-20 04:18:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-07-07 03:20:28 +08:00
|
|
|
err = SetPeers(iface, &cfg.Node, peers)
|
2021-11-13 00:24:29 +08:00
|
|
|
} else if peerupdate {
|
2022-07-07 03:20:28 +08:00
|
|
|
err = InitWireguard(&cfg.Node, privkey, peers, true)
|
2021-09-20 02:03:47 +08:00
|
|
|
} else {
|
2022-07-07 03:20:28 +08:00
|
|
|
err = InitWireguard(&cfg.Node, privkey, peers, false)
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-05-23 22:02:47 +08:00
|
|
|
|
2021-09-20 02:03:47 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// RemoveConf - removes a configuration for a given WireGuard interface
|
2021-09-20 02:03:47 +08:00
|
|
|
func RemoveConf(iface string, printlog bool) error {
|
|
|
|
os := runtime.GOOS
|
2022-03-03 05:19:53 +08:00
|
|
|
if ncutils.IsLinux() && !ncutils.HasWgQuick() {
|
2022-02-17 09:19:51 +08:00
|
|
|
os = "nowgquick"
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
var err error
|
|
|
|
switch os {
|
2022-02-17 09:19:51 +08:00
|
|
|
case "nowgquick":
|
|
|
|
err = RemoveWithoutWGQuick(iface)
|
2021-09-20 02:03:47 +08:00
|
|
|
case "windows":
|
|
|
|
err = RemoveWindowsConf(iface, printlog)
|
2022-01-12 07:28:41 +08:00
|
|
|
case "darwin":
|
2022-01-13 07:40:44 +08:00
|
|
|
err = RemoveConfMac(iface)
|
2021-09-20 02:03:47 +08:00
|
|
|
default:
|
|
|
|
confPath := ncutils.GetNetclientPathSpecific() + iface + ".conf"
|
|
|
|
err = RemoveWGQuickConf(confPath, printlog)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// ApplyConf - applys a conf on disk to WireGuard interface
|
2022-02-06 03:20:50 +08:00
|
|
|
func ApplyConf(node *models.Node, ifacename string, confPath string) error {
|
2021-09-20 02:03:47 +08:00
|
|
|
os := runtime.GOOS
|
2022-02-17 09:19:51 +08:00
|
|
|
if ncutils.IsLinux() && !ncutils.HasWgQuick() {
|
|
|
|
os = "nowgquick"
|
|
|
|
}
|
2021-09-20 02:03:47 +08:00
|
|
|
var err error
|
|
|
|
switch os {
|
|
|
|
case "windows":
|
2022-02-21 00:12:51 +08:00
|
|
|
ApplyWindowsConf(confPath)
|
2022-01-12 07:28:41 +08:00
|
|
|
case "darwin":
|
2022-02-21 00:12:51 +08:00
|
|
|
ApplyMacOSConf(node, ifacename, confPath)
|
2022-03-03 05:19:53 +08:00
|
|
|
case "nowgquick":
|
|
|
|
ApplyWithoutWGQuick(node, ifacename, confPath)
|
2021-09-20 02:03:47 +08:00
|
|
|
default:
|
2022-02-21 00:12:51 +08:00
|
|
|
ApplyWGQuickConf(confPath, ifacename)
|
2021-09-20 02:03:47 +08:00
|
|
|
}
|
2022-02-21 00:12:51 +08:00
|
|
|
|
|
|
|
var nodeCfg config.ClientConfig
|
|
|
|
nodeCfg.Network = node.Network
|
|
|
|
nodeCfg.ReadConfig()
|
2022-04-26 08:37:05 +08:00
|
|
|
if nodeCfg.NetworkSettings.AddressRange != "" {
|
|
|
|
ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange)
|
|
|
|
if err == nil {
|
|
|
|
local.SetCIDRRoute(node.Interface, ip.String(), cidr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nodeCfg.NetworkSettings.AddressRange6 != "" {
|
|
|
|
ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange6)
|
|
|
|
if err == nil {
|
|
|
|
local.SetCIDRRoute(node.Interface, ip.String(), cidr)
|
|
|
|
}
|
2022-02-21 00:12:51 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 02:03:47 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
|
|
|
|
// WriteWgConfig - creates a wireguard config file
|
2022-01-22 02:15:54 +08:00
|
|
|
func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerConfig) error {
|
2022-01-05 23:05:51 +08:00
|
|
|
options := ini.LoadOptions{
|
|
|
|
AllowNonUniqueSections: true,
|
|
|
|
AllowShadows: true,
|
|
|
|
}
|
|
|
|
wireguard := ini.Empty(options)
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
|
2022-01-31 05:26:32 +08:00
|
|
|
if node.ListenPort > 0 && node.UDPHolePunch != "yes" {
|
2022-01-22 02:15:54 +08:00
|
|
|
wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-04-26 09:16:47 +08:00
|
|
|
addrString := node.Address
|
2022-01-22 02:15:54 +08:00
|
|
|
if node.Address6 != "" {
|
2022-04-26 09:16:47 +08:00
|
|
|
if addrString != "" {
|
|
|
|
addrString += ","
|
|
|
|
}
|
|
|
|
addrString += node.Address6
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-04-26 09:16:47 +08:00
|
|
|
wireguard.Section(section_interface).Key("Address").SetValue(addrString)
|
2022-01-22 02:15:54 +08:00
|
|
|
// need to figure out DNS
|
|
|
|
//if node.DNSOn == "yes" {
|
|
|
|
// wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
|
|
|
|
//}
|
|
|
|
if node.PostUp != "" {
|
|
|
|
wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-22 02:15:54 +08:00
|
|
|
if node.PostDown != "" {
|
|
|
|
wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-24 23:58:12 +08:00
|
|
|
if node.MTU != 0 {
|
|
|
|
wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
for i, peer := range peers {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
|
2022-01-22 03:07:28 +08:00
|
|
|
if peer.PresharedKey != nil {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
|
|
|
if peer.AllowedIPs != nil {
|
|
|
|
var allowedIPs string
|
2022-01-20 04:00:03 +08:00
|
|
|
for i, ip := range peer.AllowedIPs {
|
|
|
|
if i == 0 {
|
|
|
|
allowedIPs = ip.String()
|
|
|
|
} else {
|
|
|
|
allowedIPs = allowedIPs + ", " + ip.String()
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
|
|
|
if peer.Endpoint != nil {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-22 03:07:28 +08:00
|
|
|
|
2022-01-22 02:15:54 +08:00
|
|
|
if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
|
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-22 02:15:54 +08:00
|
|
|
if err := wireguard.SaveTo(ncutils.GetNetclientPathSpecific() + node.Interface + ".conf"); err != nil {
|
2022-01-05 23:05:51 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWgPeers - updates the peers of a network
|
2022-08-10 01:51:28 +08:00
|
|
|
func UpdateWgPeers(file string, peers []wgtypes.PeerConfig) (*net.UDPAddr, error) {
|
|
|
|
var internetGateway *net.UDPAddr
|
2022-01-20 04:12:09 +08:00
|
|
|
options := ini.LoadOptions{
|
|
|
|
AllowNonUniqueSections: true,
|
|
|
|
AllowShadows: true,
|
|
|
|
}
|
|
|
|
wireguard, err := ini.LoadSources(options, file)
|
2022-01-05 23:05:51 +08:00
|
|
|
if err != nil {
|
2022-08-10 01:51:28 +08:00
|
|
|
return internetGateway, err
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-20 04:00:03 +08:00
|
|
|
//delete the peers sections as they are going to be replaced
|
|
|
|
wireguard.DeleteSection(section_peers)
|
2022-01-05 23:05:51 +08:00
|
|
|
for i, peer := range peers {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
|
2022-01-23 00:38:56 +08:00
|
|
|
if peer.PresharedKey != nil {
|
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
if peer.AllowedIPs != nil {
|
|
|
|
var allowedIPs string
|
2022-01-20 04:00:03 +08:00
|
|
|
for i, ip := range peer.AllowedIPs {
|
|
|
|
if i == 0 {
|
|
|
|
allowedIPs = ip.String()
|
|
|
|
} else {
|
|
|
|
allowedIPs = allowedIPs + ", " + ip.String()
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
|
2022-08-10 01:51:28 +08:00
|
|
|
if strings.Contains(allowedIPs, "0.0.0.0/0") || strings.Contains(allowedIPs, "::/0") {
|
|
|
|
internetGateway = peer.Endpoint
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
|
|
|
if peer.Endpoint != nil {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("Endpoint").SetValue(peer.Endpoint.String())
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-22 02:15:54 +08:00
|
|
|
if peer.PersistentKeepaliveInterval != nil && peer.PersistentKeepaliveInterval.Seconds() > 0 {
|
2022-01-21 22:36:29 +08:00
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepalive").SetValue(strconv.FormatInt((int64)(peer.PersistentKeepaliveInterval.Seconds()), 10))
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
|
|
|
if err := wireguard.SaveTo(file); err != nil {
|
2022-08-10 01:51:28 +08:00
|
|
|
return internetGateway, err
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-08-10 01:51:28 +08:00
|
|
|
return internetGateway, nil
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWgInterface - updates the interface section of a wireguard config file
|
2022-01-23 00:38:56 +08:00
|
|
|
func UpdateWgInterface(file, privateKey, nameserver string, node models.Node) error {
|
|
|
|
options := ini.LoadOptions{
|
|
|
|
AllowNonUniqueSections: true,
|
|
|
|
AllowShadows: true,
|
|
|
|
}
|
|
|
|
wireguard, err := ini.LoadSources(options, file)
|
2022-01-05 23:05:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-31 05:06:07 +08:00
|
|
|
if node.UDPHolePunch == "yes" {
|
|
|
|
node.ListenPort = 0
|
|
|
|
}
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
|
|
|
|
wireguard.Section(section_interface).Key("ListenPort").SetValue(strconv.Itoa(int(node.ListenPort)))
|
2022-04-26 10:06:33 +08:00
|
|
|
addrString := node.Address
|
2022-01-05 23:05:51 +08:00
|
|
|
if node.Address6 != "" {
|
2022-04-26 10:06:33 +08:00
|
|
|
if addrString != "" {
|
|
|
|
addrString += ","
|
|
|
|
}
|
|
|
|
addrString += node.Address6
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-04-26 10:06:33 +08:00
|
|
|
wireguard.Section(section_interface).Key("Address").SetValue(addrString)
|
2022-01-25 04:46:38 +08:00
|
|
|
//if node.DNSOn == "yes" {
|
|
|
|
// wireguard.Section(section_interface).Key("DNS").SetValue(nameserver)
|
|
|
|
//}
|
2022-01-05 23:05:51 +08:00
|
|
|
if node.PostUp != "" {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.Section(section_interface).Key("PostUp").SetValue(node.PostUp)
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
|
|
|
if node.PostDown != "" {
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.Section(section_interface).Key("PostDown").SetValue(node.PostDown)
|
2022-01-05 23:05:51 +08:00
|
|
|
}
|
2022-01-24 23:58:12 +08:00
|
|
|
if node.MTU != 0 {
|
|
|
|
wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))
|
|
|
|
}
|
2022-01-05 23:05:51 +08:00
|
|
|
if err := wireguard.SaveTo(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePrivateKey - updates the private key of a wireguard config file
|
2022-01-23 00:38:56 +08:00
|
|
|
func UpdatePrivateKey(file, privateKey string) error {
|
|
|
|
options := ini.LoadOptions{
|
|
|
|
AllowNonUniqueSections: true,
|
|
|
|
AllowShadows: true,
|
|
|
|
}
|
|
|
|
wireguard, err := ini.LoadSources(options, file)
|
2022-01-05 23:05:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-05 23:32:20 +08:00
|
|
|
wireguard.Section(section_interface).Key("PrivateKey").SetValue(privateKey)
|
2022-01-05 23:05:51 +08:00
|
|
|
if err := wireguard.SaveTo(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-15 09:03:49 +08:00
|
|
|
|
2022-03-21 19:07:50 +08:00
|
|
|
// UpdateKeepAlive - updates the persistentkeepalive of all peers
|
|
|
|
func UpdateKeepAlive(file string, keepalive int32) error {
|
|
|
|
options := ini.LoadOptions{
|
|
|
|
AllowNonUniqueSections: true,
|
|
|
|
AllowShadows: true,
|
|
|
|
}
|
|
|
|
wireguard, err := ini.LoadSources(options, file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
peers, err := wireguard.SectionsByName(section_peers)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newvalue := strconv.Itoa(int(keepalive))
|
|
|
|
for i := range peers {
|
|
|
|
wireguard.SectionWithIndex(section_peers, i).Key("PersistentKeepALive").SetValue(newvalue)
|
|
|
|
}
|
|
|
|
if err := wireguard.SaveTo(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 09:03:49 +08:00
|
|
|
// RemoveConfGraceful - Run remove conf and wait for it to actually be gone before proceeding
|
|
|
|
func RemoveConfGraceful(ifacename string) {
|
|
|
|
// ensure you clear any existing interface first
|
|
|
|
wgclient, err := wgctrl.New()
|
|
|
|
if err != nil {
|
2022-03-20 23:12:05 +08:00
|
|
|
logger.Log(0, "could not create wgclient")
|
2022-03-15 09:03:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer wgclient.Close()
|
|
|
|
d, _ := wgclient.Device(ifacename)
|
|
|
|
startTime := time.Now()
|
|
|
|
for d != nil && d.Name == ifacename {
|
|
|
|
if err = RemoveConf(ifacename, false); err != nil { // remove interface first
|
|
|
|
if strings.Contains(err.Error(), "does not exist") {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second >> 2)
|
|
|
|
d, _ = wgclient.Device(ifacename)
|
|
|
|
if time.Now().After(startTime.Add(time.Second << 4)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 23:11:22 +08:00
|
|
|
time.Sleep(time.Second << 1)
|
2022-03-15 09:03:49 +08:00
|
|
|
}
|
2022-04-26 04:30:18 +08:00
|
|
|
|
|
|
|
// GetDevicePeers - gets the current device's peers
|
|
|
|
func GetDevicePeers(iface string) ([]wgtypes.Peer, error) {
|
|
|
|
if ncutils.IsFreeBSD() {
|
|
|
|
if devicePeers, err := ncutils.GetPeers(iface); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return devicePeers, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, "failed to start wgctrl")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
device, err := client.Device(iface)
|
|
|
|
if err != nil {
|
2022-07-11 20:15:46 +08:00
|
|
|
logger.Log(0, "failed to parse interface", iface)
|
2022-04-26 04:30:18 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return device.Peers, nil
|
|
|
|
}
|
|
|
|
}
|