mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
fixing windows
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/servercfg"
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
@@ -190,42 +190,6 @@ PersistentKeepAlive = %s
|
|||||||
return peersString, nil
|
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
|
// GetLocalIP - gets local ip of machine
|
||||||
func GetLocalIP(localrange string) (string, error) {
|
func GetLocalIP(localrange string) (string, error) {
|
||||||
_, localRange, err := net.ParseCIDR(localrange)
|
_, localRange, err := net.ParseCIDR(localrange)
|
||||||
|
@@ -19,3 +19,38 @@ func RunCmd(command string, printerr bool) (string, error) {
|
|||||||
return string(out), err
|
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
|
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
|
package ncutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunCmd - runs a local command
|
// RunCmd - runs a local command
|
||||||
@@ -11,6 +17,24 @@ func RunCmd(command string, printerr bool) (string, error) {
|
|||||||
args := strings.Fields(command)
|
args := strings.Fields(command)
|
||||||
cmd := exec.Command(args[0], args[1:]...)
|
cmd := exec.Command(args[0], args[1:]...)
|
||||||
cmd.Wait()
|
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()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil && printerr {
|
if err != nil && printerr {
|
||||||
log.Println("error running command:", command)
|
log.Println("error running command:", command)
|
||||||
@@ -18,3 +42,33 @@ func RunCmd(command string, printerr bool) (string, error) {
|
|||||||
}
|
}
|
||||||
return string(out), err
|
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)
|
ncutils.PrintLog("failed to create wireguard interface", 1)
|
||||||
return err
|
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 {
|
} else {
|
||||||
ipExec, err := exec.LookPath("ip")
|
ipExec, err := exec.LookPath("ip")
|
||||||
|
@@ -1,9 +1,14 @@
|
|||||||
package wireguard
|
package wireguard
|
||||||
|
|
||||||
import "github.com/gravitl/netmaker/netclient/ncutils"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
|
)
|
||||||
|
|
||||||
func ApplyWindowsConf(confPath string) error {
|
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 err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@@ -24,6 +24,11 @@ new-module -name netclient-install -scriptblock {
|
|||||||
Invoke-WebRequest -Uri $url -OutFile $outpath
|
Invoke-WebRequest -Uri $url -OutFile $outpath
|
||||||
$args = @("Comma","Separated","Arguments")
|
$args = @("Comma","Separated","Arguments")
|
||||||
$procWG = Start-Process -Filepath "$env:userprofile\Downloads\wireguard-installer.exe" -ArgumentList $args
|
$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()
|
$procWG.WaitForExit()
|
||||||
Start-Sleep -Seconds 5
|
Start-Sleep -Seconds 5
|
||||||
$software = "WireGuard";
|
$software = "WireGuard";
|
||||||
@@ -48,7 +53,11 @@ new-module -name netclient-install -scriptblock {
|
|||||||
}
|
}
|
||||||
$NetArgs = @("join","-t",$token)
|
$NetArgs = @("join","-t",$token)
|
||||||
$procNC = Start-Process -Filepath $outpath -ArgumentList $NetArgs
|
$procNC = Start-Process -Filepath $outpath -ArgumentList $NetArgs
|
||||||
|
if ($procNC -eq $null) {}
|
||||||
|
Start-Sleep -Seconds 5
|
||||||
|
} else {
|
||||||
$procNC.WaitForExit()
|
$procNC.WaitForExit()
|
||||||
|
}
|
||||||
Add-MpPreference -ExclusionPath "C:\ProgramData\Netclient"
|
Add-MpPreference -ExclusionPath "C:\ProgramData\Netclient"
|
||||||
|
|
||||||
if ((Get-Command "netclient.exe" -ErrorAction SilentlyContinue) -eq $null) {
|
if ((Get-Command "netclient.exe" -ErrorAction SilentlyContinue) -eq $null) {
|
||||||
|
Reference in New Issue
Block a user