update join command to use wg.conf file in all cases.

This commit is contained in:
Matthew R Kasun 2021-12-30 06:45:45 -05:00
parent 7ed56a85d6
commit 9068792e61
2 changed files with 58 additions and 150 deletions

View file

@ -34,7 +34,7 @@ func GetEmbedded() error {
} }
// CreateUserSpaceConf - creates a user space WireGuard conf // CreateUserSpaceConf - creates a user space WireGuard conf
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) { func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, dns string, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
peersString, err := parsePeers(perskeepalive, peers) peersString, err := parsePeers(perskeepalive, peers)
var listenPortString string var listenPortString string
if mtu <= 0 { if mtu <= 0 {
@ -48,6 +48,7 @@ func CreateUserSpaceConf(address string, privatekey string, listenPort string, m
} }
config := fmt.Sprintf(`[Interface] config := fmt.Sprintf(`[Interface]
Address = %s Address = %s
DNS = %s
PrivateKey = %s PrivateKey = %s
MTU = %s MTU = %s
%s %s
@ -56,6 +57,7 @@ MTU = %s
`, `,
address+"/32", address+"/32",
dns,
privatekey, privatekey,
strconv.Itoa(int(mtu)), strconv.Itoa(int(mtu)),
listenPortString, listenPortString,

View file

@ -2,11 +2,8 @@ package wireguard
import ( import (
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"os/exec"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -140,165 +137,74 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
} }
nameserver := servercfg.CoreDNSAddr nameserver := servercfg.CoreDNSAddr
network := node.Network var newConf string
if nodecfg.Network != "" { if node.UDPHolePunch != "yes" {
network = nodecfg.Network newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, nameserver, node.PersistentKeepalive, peers)
} else if node.Network != "" {
network = node.Network
}
if ncutils.IsKernel() {
setKernelDevice(ifacename, node.Address)
}
nodeport := int(node.ListenPort)
conf := wgtypes.Config{}
if nodecfg.UDPHolePunch == "yes" &&
nodecfg.IsServer == "no" &&
nodecfg.IsIngressGateway != "yes" &&
nodecfg.IsStatic != "yes" {
conf = wgtypes.Config{
PrivateKey: &key,
ReplacePeers: true,
Peers: peers,
}
} else { } else {
conf = wgtypes.Config{ newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, nameserver, node.PersistentKeepalive, peers)
PrivateKey: &key,
ListenPort: &nodeport,
ReplacePeers: true,
Peers: peers,
}
} }
if !ncutils.IsKernel() { confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
var newConf string ncutils.PrintLog("writing wg conf file to: "+confPath, 1)
if node.UDPHolePunch != "yes" { err = ioutil.WriteFile(confPath, []byte(newConf), 0644)
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), strconv.FormatInt(int64(node.ListenPort), 10), node.MTU, node.PersistentKeepalive, peers) if err != nil {
} else { ncutils.PrintLog("error writing wg conf file to "+confPath+": "+err.Error(), 1)
newConf, _ = ncutils.CreateUserSpaceConf(node.Address, key.String(), "", node.MTU, node.PersistentKeepalive, peers) return err
} }
confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf" if ncutils.IsWindows() {
ncutils.PrintLog("writing wg conf file to: "+confPath, 1) wgConfPath := ncutils.GetWGPathSpecific() + ifacename + ".conf"
err = ioutil.WriteFile(confPath, []byte(newConf), 0644) err = ioutil.WriteFile(wgConfPath, []byte(newConf), 0644)
if err != nil { if err != nil {
ncutils.PrintLog("error writing wg conf file to "+confPath+": "+err.Error(), 1) ncutils.PrintLog("error writing wg conf file to "+wgConfPath+": "+err.Error(), 1)
return err return err
} }
if ncutils.IsWindows() { confPath = wgConfPath
wgConfPath := ncutils.GetWGPathSpecific() + ifacename + ".conf" }
err = ioutil.WriteFile(wgConfPath, []byte(newConf), 0644) // spin up userspace / windows interface + apply the conf file
var deviceiface string
if ncutils.IsMac() {
deviceiface, err = local.GetMacIface(node.Address)
if err != nil || deviceiface == "" {
deviceiface = ifacename
}
}
if syncconf {
log.Println("syncing conf")
err = SyncWGQuickConf(ifacename, confPath)
} else {
d, _ := wgclient.Device(deviceiface)
for d != nil && d.Name == deviceiface {
_ = RemoveConf(ifacename, false) // remove interface first
time.Sleep(time.Second >> 2)
d, _ = wgclient.Device(deviceiface)
}
if !ncutils.IsWindows() {
err = ApplyConf(confPath)
if err != nil { if err != nil {
ncutils.PrintLog("error writing wg conf file to "+wgConfPath+": "+err.Error(), 1) ncutils.PrintLog("failed to create wireguard interface", 1)
return err return err
} }
confPath = wgConfPath
}
// spin up userspace / windows interface + apply the conf file
var deviceiface string
if ncutils.IsMac() {
deviceiface, err = local.GetMacIface(node.Address)
if err != nil || deviceiface == "" {
deviceiface = ifacename
}
}
if syncconf {
log.Println("syncing conf")
err = SyncWGQuickConf(ifacename, confPath)
} else { } else {
d, _ := wgclient.Device(deviceiface) var output string
for d != nil && d.Name == deviceiface { starttime := time.Now()
_ = RemoveConf(ifacename, false) // remove interface first RemoveConf(ifacename, false)
time.Sleep(time.Second >> 2) time.Sleep(time.Second >> 2)
d, _ = wgclient.Device(deviceiface) ncutils.PrintLog("waiting for interface...", 1)
} for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Duration(10) * time.Second))) {
if !ncutils.IsWindows() { output, _ = ncutils.RunCmd("wg", false)
err = ApplyConf(confPath) err = ApplyConf(confPath)
if err != nil { time.Sleep(time.Second)
ncutils.PrintLog("failed to create wireguard interface", 1)
return err
}
} else {
var output string
starttime := time.Now()
RemoveConf(ifacename, false)
time.Sleep(time.Second >> 2)
ncutils.PrintLog("waiting for interface...", 1)
for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Duration(10) * time.Second))) {
output, _ = ncutils.RunCmd("wg", false)
err = ApplyConf(confPath)
time.Sleep(time.Second)
}
if !strings.Contains(output, ifacename) {
return errors.New("could not create wg interface for " + ifacename)
}
ip, mask, err := ncutils.GetNetworkIPMask(nodecfg.NetworkSettings.AddressRange)
if err != nil {
log.Println(err.Error())
return err
}
ncutils.RunCmd("route add "+ip+" mask "+mask+" "+node.Address, true)
time.Sleep(time.Second >> 2)
ncutils.RunCmd("route change "+ip+" mask "+mask+" "+node.Address, true)
} }
} if !strings.Contains(output, ifacename) {
} else { return errors.New("could not create wg interface for " + ifacename)
ipExec, err := exec.LookPath("ip")
if err != nil {
return err
}
_, err = wgclient.Device(ifacename)
if err != nil {
if os.IsNotExist(err) {
fmt.Println("Device does not exist: ")
fmt.Println(err)
} else {
log.Fatalf("Unknown config error: %v", err)
} }
} ip, mask, err := ncutils.GetNetworkIPMask(nodecfg.NetworkSettings.AddressRange)
if err != nil {
err = wgclient.ConfigureDevice(ifacename, conf) log.Println(err.Error())
if err != nil { return err
if os.IsNotExist(err) {
fmt.Println("Device does not exist: ")
fmt.Println(err)
} else {
fmt.Printf("This is inconvenient: %v", err)
} }
} ncutils.RunCmd("route add "+ip+" mask "+mask+" "+node.Address, true)
time.Sleep(time.Second >> 2)
//=========DNS Setup==========\\ ncutils.RunCmd("route change "+ip+" mask "+mask+" "+node.Address, true)
if nodecfg.DNSOn == "yes" {
_ = local.UpdateDNS(ifacename, network, nameserver)
}
//=========End DNS Setup=======\\
if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil {
ncutils.Log("attempted to remove interface before editing")
return err
}
if nodecfg.PostDown != "" {
runcmds := strings.Split(nodecfg.PostDown, "; ")
_ = ncutils.RunCmds(runcmds, true)
}
// set MTU of node interface
if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(nodecfg.MTU))+" up dev "+ifacename, true); err != nil {
ncutils.Log("failed to create interface with mtu " + ifacename)
return err
}
if nodecfg.PostUp != "" {
runcmds := strings.Split(nodecfg.PostUp, "; ")
_ = ncutils.RunCmds(runcmds, true)
}
if hasGateway {
for _, gateway := range gateways {
_, _ = ncutils.RunCmd(ipExec+" -4 route add "+gateway+" dev "+ifacename, true)
}
}
if node.Address6 != "" && node.IsDualStack == "yes" {
log.Println("[netclient] adding address: "+node.Address6, 1)
_, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true)
} }
} }