netmaker/netclient/wireguard/unix.go

129 lines
3.2 KiB
Go
Raw Normal View History

2021-09-20 02:03:47 +08:00
package wireguard
import (
2022-01-04 08:09:19 +08:00
"fmt"
2021-11-13 02:46:55 +08:00
"log"
2021-11-13 02:03:31 +08:00
"os"
2021-11-13 02:46:55 +08:00
"regexp"
2021-11-13 00:24:29 +08:00
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/ncutils"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
2021-10-09 03:07:12 +08:00
// SetWGKeyConfig - sets the wg conf with a new private key
2021-09-20 02:03:47 +08:00
func SetWGKeyConfig(network string, serveraddr string) error {
cfg, err := config.ReadConfig(network)
if err != nil {
return err
}
node := cfg.Node
privatekey, err := wgtypes.GeneratePrivateKey()
if err != nil {
return err
}
privkeystring := privatekey.String()
publickey := privatekey.PublicKey()
node.PublicKey = publickey.String()
err = StorePrivKey(privkeystring, network)
if err != nil {
return err
}
if node.Action == models.NODE_UPDATE_KEY {
node.Action = models.NODE_NOOP
}
err = config.ModConfig(&node)
if err != nil {
return err
}
err = SetWGConfig(network, false)
if err != nil {
return err
}
return err
}
2021-10-09 03:07:12 +08:00
// ApplyWGQuickConf - applies wg-quick commands if os supports
2021-09-20 02:03:47 +08:00
func ApplyWGQuickConf(confPath string) error {
2022-01-20 04:00:03 +08:00
_, err := os.Stat(confPath)
if err != nil {
ncutils.Log(confPath + " does not exist " + err.Error())
return err
}
_, err = ncutils.RunCmd("wg-quick down "+confPath, false)
if err != nil {
ncutils.Log("err runing wg-quick down " + confPath + err.Error())
}
_, err = ncutils.RunCmd("wg-quick up "+confPath, false)
if err != nil {
ncutils.Log("err runing wg-quick up " + confPath + err.Error())
}
2021-11-13 00:24:29 +08:00
return err
}
2022-01-12 07:28:41 +08:00
// ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
func ApplyMacOSConf(node models.Node, ifacename string, confPath string) error {
var err error
2022-01-12 23:03:23 +08:00
_ = WgQuickDownMac(node, ifacename)
2022-01-12 07:28:41 +08:00
err = WgQuickUpMac(node, ifacename, confPath)
return err
}
2021-11-13 02:46:55 +08:00
// SyncWGQuickConf - formats config file and runs sync command
func SyncWGQuickConf(iface string, confPath string) error {
2021-11-13 02:03:31 +08:00
var tmpConf = confPath + ".sync.tmp"
2022-01-12 07:28:41 +08:00
var confCmd = "wg-quick strip "
if ncutils.IsMac() {
confCmd = "grep -v -e Address -e MTU -e PostUp -e PostDown "
}
confRaw, err := ncutils.RunCmd(confCmd+confPath, false)
2021-11-13 02:03:31 +08:00
if err != nil {
return err
}
2021-11-13 02:46:55 +08:00
regex := regexp.MustCompile(".*Warning.*\n")
conf := regex.ReplaceAllString(confRaw, "")
2022-01-07 04:05:38 +08:00
err = os.WriteFile(tmpConf, []byte(conf), 0644)
2021-11-13 02:03:31 +08:00
if err != nil {
return err
}
2021-11-13 02:46:55 +08:00
_, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
2021-11-13 02:03:31 +08:00
if err != nil {
2021-11-13 02:46:55 +08:00
log.Println(err.Error())
2021-11-13 02:03:31 +08:00
ncutils.Log("error syncing conf, resetting")
err = ApplyWGQuickConf(confPath)
}
errN := os.Remove(tmpConf)
if errN != nil {
ncutils.Log(errN.Error())
}
2021-11-13 00:24:29 +08:00
return err
2021-09-20 02:03:47 +08:00
}
2021-10-09 03:07:12 +08:00
// RemoveWGQuickConf - calls wg-quick down
2021-09-20 02:03:47 +08:00
func RemoveWGQuickConf(confPath string, printlog bool) error {
2022-01-04 08:09:19 +08:00
_, err := ncutils.RunCmd(fmt.Sprintf("wg-quick down %s", confPath), printlog)
2021-11-13 00:24:29 +08:00
return err
2021-09-20 02:03:47 +08:00
}
2021-10-09 03:07:12 +08:00
// StorePrivKey - stores wg priv key on disk locally
2021-09-20 02:03:47 +08:00
func StorePrivKey(key string, network string) error {
2021-10-13 03:44:19 +08:00
var err error
2021-09-20 02:03:47 +08:00
d1 := []byte(key)
2022-01-07 04:05:38 +08:00
err = os.WriteFile(ncutils.GetNetclientPathSpecific()+"wgkey-"+network, d1, 0644)
2021-09-20 02:03:47 +08:00
return err
}
2021-10-09 03:07:12 +08:00
// RetrievePrivKey - reads wg priv key from local disk
2021-09-20 02:03:47 +08:00
func RetrievePrivKey(network string) (string, error) {
2022-01-07 04:05:38 +08:00
dat, err := os.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
2021-09-20 02:03:47 +08:00
return string(dat), err
}