mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-06 21:24:16 +08:00
fixing windows
This commit is contained in:
parent
20a939b188
commit
ccbd6f90f7
8 changed files with 162 additions and 48 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/gravitl/netmaker/servercfg"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
|
|
@ -190,42 +190,6 @@ PersistentKeepAlive = %s
|
|||
return peersString, nil
|
||||
}
|
||||
|
||||
// CreateUserSpaceConf - creates a user space WireGuard conf
|
||||
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
|
||||
peersString, err := parsePeers(perskeepalive, peers)
|
||||
var listenPortString string
|
||||
var fwmarkString string
|
||||
if mtu <= 0 {
|
||||
mtu = 1280
|
||||
}
|
||||
if listenPort != "" {
|
||||
listenPortString += "ListenPort = " + listenPort
|
||||
}
|
||||
if fwmark != 0 {
|
||||
fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
config := fmt.Sprintf(`[Interface]
|
||||
Address = %s
|
||||
PrivateKey = %s
|
||||
MTU = %s
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
|
||||
`,
|
||||
address+"/32",
|
||||
privatekey,
|
||||
strconv.Itoa(int(mtu)),
|
||||
listenPortString,
|
||||
fwmarkString,
|
||||
peersString)
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// GetLocalIP - gets local ip of machine
|
||||
func GetLocalIP(localrange string) (string, error) {
|
||||
_, localRange, err := net.ParseCIDR(localrange)
|
||||
|
|
|
@ -8,14 +8,49 @@ import (
|
|||
|
||||
// RunCmd - runs a local command
|
||||
func RunCmd(command string, printerr bool) (string, error) {
|
||||
args := strings.Fields(command)
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Wait()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil && printerr {
|
||||
log.Println("error running command:", command)
|
||||
log.Println(strings.TrimSuffix(string(out), "\n"))
|
||||
}
|
||||
return string(out), err
|
||||
args := strings.Fields(command)
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Wait()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil && printerr {
|
||||
log.Println("error running command:", command)
|
||||
log.Println(strings.TrimSuffix(string(out), "\n"))
|
||||
}
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
// CreateUserSpaceConf - creates a user space WireGuard conf
|
||||
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
|
||||
peersString, err := parsePeers(perskeepalive, peers)
|
||||
var listenPortString string
|
||||
var fwmarkString string
|
||||
if mtu <= 0 {
|
||||
mtu = 1280
|
||||
}
|
||||
if listenPort != "" {
|
||||
listenPortString += "ListenPort = " + listenPort
|
||||
}
|
||||
if fwmark != 0 {
|
||||
fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
config := fmt.Sprintf(`[Interface]
|
||||
Address = %s
|
||||
PrivateKey = %s
|
||||
MTU = %s
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
|
||||
`,
|
||||
address+"/32",
|
||||
privatekey,
|
||||
strconv.Itoa(int(mtu)),
|
||||
listenPortString,
|
||||
fwmarkString,
|
||||
peersString)
|
||||
return config, nil
|
||||
}
|
||||
|
|
|
@ -18,3 +18,39 @@ func RunCmd(command string, printerr bool) (string, error) {
|
|||
}
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
// CreateUserSpaceConf - creates a user space WireGuard conf
|
||||
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
|
||||
peersString, err := parsePeers(perskeepalive, peers)
|
||||
var listenPortString string
|
||||
var fwmarkString string
|
||||
if mtu <= 0 {
|
||||
mtu = 1280
|
||||
}
|
||||
if listenPort != "" {
|
||||
listenPortString += "ListenPort = " + listenPort
|
||||
}
|
||||
if fwmark != 0 {
|
||||
fwmarkString += "FWMark = " + strconv.Itoa(int(fwmark))
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
config := fmt.Sprintf(`[Interface]
|
||||
Address = %s
|
||||
PrivateKey = %s
|
||||
MTU = %s
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
|
||||
`,
|
||||
address+"/32",
|
||||
privatekey,
|
||||
strconv.Itoa(int(mtu)),
|
||||
listenPortString,
|
||||
fwmarkString,
|
||||
peersString)
|
||||
return config, nil
|
||||
}
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
package ncutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
// RunCmd - runs a local command
|
||||
|
@ -11,6 +17,24 @@ func RunCmd(command string, printerr bool) (string, error) {
|
|||
args := strings.Fields(command)
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Wait()
|
||||
//cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/C \"" + command + "\""}
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil && printerr {
|
||||
log.Println("error running command:", command)
|
||||
log.Println(strings.TrimSuffix(string(out), "\n"))
|
||||
}
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
// RunCmd - runs a local command
|
||||
func RunCmdFormatted(command string, printerr bool) (string, error) {
|
||||
var comSpec = os.Getenv("COMSPEC")
|
||||
if comSpec == "" {
|
||||
comSpec = os.Getenv("SystemRoot") + "\\System32\\cmd.exe"
|
||||
}
|
||||
cmd := exec.Command(comSpec)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/C \"" + command + "\""}
|
||||
cmd.Wait()
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil && printerr {
|
||||
log.Println("error running command:", command)
|
||||
|
@ -18,3 +42,33 @@ func RunCmd(command string, printerr bool) (string, error) {
|
|||
}
|
||||
return string(out), err
|
||||
}
|
||||
|
||||
// CreateUserSpaceConf - creates a user space WireGuard conf
|
||||
func CreateUserSpaceConf(address string, privatekey string, listenPort string, mtu int32, fwmark int32, perskeepalive int32, peers []wgtypes.PeerConfig) (string, error) {
|
||||
peersString, err := parsePeers(perskeepalive, peers)
|
||||
var listenPortString string
|
||||
if mtu <= 0 {
|
||||
mtu = 1280
|
||||
}
|
||||
if listenPort != "" {
|
||||
listenPortString += "ListenPort = " + listenPort
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
config := fmt.Sprintf(`[Interface]
|
||||
Address = %s
|
||||
PrivateKey = %s
|
||||
MTU = %s
|
||||
%s
|
||||
|
||||
%s
|
||||
|
||||
`,
|
||||
address+"/32",
|
||||
privatekey,
|
||||
strconv.Itoa(int(mtu)),
|
||||
listenPortString,
|
||||
peersString)
|
||||
return config, nil
|
||||
}
|
||||
|
|
|
@ -217,6 +217,16 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
|
|||
ncutils.PrintLog("failed to create wireguard interface", 1)
|
||||
return err
|
||||
}
|
||||
if ncutils.IsWindows() {
|
||||
var output string
|
||||
starttime := time.Now()
|
||||
ncutils.PrintLog("waiting for interface...", 1)
|
||||
for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Minute))) {
|
||||
output, _ = ncutils.RunCmd("wg", false)
|
||||
time.Sleep(time.Second >> 1)
|
||||
err = ApplyConf(confPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ipExec, err := exec.LookPath("ip")
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
package wireguard
|
||||
|
||||
import "github.com/gravitl/netmaker/netclient/ncutils"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
)
|
||||
|
||||
func ApplyWindowsConf(confPath string) error {
|
||||
if _, err := ncutils.RunCmd("wireguard.exe /installtunnelservice "+confPath, false); err != nil {
|
||||
var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
|
||||
if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -24,6 +24,11 @@ new-module -name netclient-install -scriptblock {
|
|||
Invoke-WebRequest -Uri $url -OutFile $outpath
|
||||
$args = @("Comma","Separated","Arguments")
|
||||
$procWG = Start-Process -Filepath "$env:userprofile\Downloads\wireguard-installer.exe" -ArgumentList $args
|
||||
if ($procWG -eq $null) {}
|
||||
Start-Sleep -Seconds 5
|
||||
} else {
|
||||
$procWG.WaitForExit()
|
||||
}
|
||||
$procWG.WaitForExit()
|
||||
Start-Sleep -Seconds 5
|
||||
$software = "WireGuard";
|
||||
|
@ -48,7 +53,11 @@ new-module -name netclient-install -scriptblock {
|
|||
}
|
||||
$NetArgs = @("join","-t",$token)
|
||||
$procNC = Start-Process -Filepath $outpath -ArgumentList $NetArgs
|
||||
$procNC.WaitForExit()
|
||||
if ($procNC -eq $null) {}
|
||||
Start-Sleep -Seconds 5
|
||||
} else {
|
||||
$procNC.WaitForExit()
|
||||
}
|
||||
Add-MpPreference -ExclusionPath "C:\ProgramData\Netclient"
|
||||
|
||||
if ((Get-Command "netclient.exe" -ErrorAction SilentlyContinue) -eq $null) {
|
||||
|
|
Loading…
Add table
Reference in a new issue