2021-09-20 02:03:47 +08:00
|
|
|
package wireguard
|
|
|
|
|
|
|
|
import (
|
2022-01-04 08:09:19 +08:00
|
|
|
"fmt"
|
2021-09-20 02:03:47 +08:00
|
|
|
"io/ioutil"
|
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 {
|
2021-11-10 07:44:39 +08:00
|
|
|
_, _ = ncutils.RunCmd("wg-quick down "+confPath, false)
|
2021-11-13 00:24:29 +08:00
|
|
|
_, err := ncutils.RunCmd("wg-quick up "+confPath, false)
|
|
|
|
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"
|
2021-11-13 02:46:55 +08:00
|
|
|
confRaw, err := ncutils.RunCmd("wg-quick strip "+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, "")
|
2021-11-13 02:03:31 +08:00
|
|
|
err = ioutil.WriteFile(tmpConf, []byte(conf), 0644)
|
|
|
|
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)
|
2021-10-13 03:44:19 +08:00
|
|
|
err = ioutil.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) {
|
|
|
|
dat, err := ioutil.ReadFile(ncutils.GetNetclientPathSpecific() + "wgkey-" + network)
|
|
|
|
return string(dat), err
|
|
|
|
}
|