Browse Source

cleaning up powershell

afeiszli 3 years ago
parent
commit
638cb0ec04

+ 3 - 1
netclient/ncutils/netclientutils.go

@@ -243,7 +243,9 @@ func GetNetworkIPMask(networkstring string) (string, string, error) {
 		return "", "", err
 		return "", "", err
 	}
 	}
 	ipstring := ip.String()
 	ipstring := ip.String()
-	maskstring := ipnet.Mask.String()
+	mask := ipnet.Mask
+	maskstring := fmt.Sprintf("%d.%d.%d.%d", mask[0], mask[1], mask[2], mask[3])
+	//maskstring := ipnet.Mask.String()
 	return ipstring, maskstring, err
 	return ipstring, maskstring, err
 }
 }
 
 

BIN
netclient/netclient.syso


+ 12 - 8
netclient/wireguard/common.go

@@ -1,6 +1,7 @@
 package wireguard
 package wireguard
 
 
 import (
 import (
+	"errors"
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
 	"log"
 	"log"
@@ -221,11 +222,20 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 				var output string
 				var output string
 				starttime := time.Now()
 				starttime := time.Now()
 				ncutils.PrintLog("waiting for interface...", 1)
 				ncutils.PrintLog("waiting for interface...", 1)
-				for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Minute))) {
+				for !strings.Contains(output, ifacename) && !(time.Now().After(starttime.Add(time.Duration(10) * time.Second))) {
 					output, _ = ncutils.RunCmd("wg", false)
 					output, _ = ncutils.RunCmd("wg", false)
 					time.Sleep(time.Second >> 1)
 					time.Sleep(time.Second >> 1)
 					err = ApplyConf(confPath)
 					err = ApplyConf(confPath)
 				}
 				}
+				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)
 			}
 			}
 		}
 		}
 	} else {
 	} else {
@@ -290,13 +300,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 	}
 	}
 
 
 	//extra network route setting required for freebsd and windows
 	//extra network route setting required for freebsd and windows
-	if ncutils.IsWindows() {
-		ip, mask, err := ncutils.GetNetworkIPMask(nodecfg.NetworkSettings.AddressRange)
-		if err != nil {
-			return err
-		}
-		_, _ = ncutils.RunCmd("route add "+ip+" mask "+mask+" "+node.Address, true)
-	} else if ncutils.IsFreeBSD() {
+	if ncutils.IsFreeBSD() {
 		_, _ = ncutils.RunCmd("route add -net "+nodecfg.NetworkSettings.AddressRange+" -interface "+ifacename, true)
 		_, _ = ncutils.RunCmd("route add -net "+nodecfg.NetworkSettings.AddressRange+" -interface "+ifacename, true)
 	}
 	}
 
 

+ 4 - 22
scripts/netclient-install.ps1

@@ -25,14 +25,7 @@ new-module -name netclient-install -scriptblock {
         $outpath = "$env:userprofile\Downloads\wireguard-installer.exe"
         $outpath = "$env:userprofile\Downloads\wireguard-installer.exe"
         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
-        if ($procWG -eq $null) {
-            Start-Sleep -Seconds 5
-        } else {
-            $procWG.WaitForExit() 
-        }
-        $procWG.WaitForExit()        
-        Start-Sleep -Seconds 5
+        Start-Process -Filepath "$env:userprofile\Downloads\wireguard-installer.exe" -ArgumentList $args -Wait
         $software = "WireGuard";
         $software = "WireGuard";
         $installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $software }) -ne $null
         $installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $software }) -ne $null
         If(-Not $installed) {
         If(-Not $installed) {
@@ -52,14 +45,11 @@ new-module -name netclient-install -scriptblock {
         Write-Host "https://github.com/gravitl/netmaker/releases/download/$version/netclient.exe";
         Write-Host "https://github.com/gravitl/netmaker/releases/download/$version/netclient.exe";
         $url = "https://github.com/gravitl/netmaker/releases/download/$version/netclient.exe"
         $url = "https://github.com/gravitl/netmaker/releases/download/$version/netclient.exe"
         Invoke-WebRequest -Uri $url -OutFile $outpath
         Invoke-WebRequest -Uri $url -OutFile $outpath
+        $loc = Get-Location
+        Copy-Item -Path "$env:userprofile\Downloads\netclient.exe" -Destination "$loc\netclient.exe"
     }
     }
     $NetArgs = @("join","-t",$token)
     $NetArgs = @("join","-t",$token)
-    $procNC = Start-Process -Filepath $outpath -ArgumentList $NetArgs
-    if ($procNC -eq $null) {
-        Start-Sleep -Seconds 5
-    } else {
-        $procNC.WaitForExit() 
-    }
+    Start-Process -Filepath $outpath -ArgumentList $NetArgs -Wait
     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) { 
@@ -71,15 +61,7 @@ new-module -name netclient-install -scriptblock {
             Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
             Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
             $env:Path += ";C:\ProgramData\Netclient\bin"
             $env:Path += ";C:\ProgramData\Netclient\bin"
         }
         }
-        '''
-        Please add netclient.exe to your path to make it permanently executable from powershell:
-            1. Open "Edit environment variables for your account"
-            2. Double click on "Path"
-            3. On a new line, add the following: C:\ProgramData\Netclient\bin
-            4. Click "Ok"
-        '''
     }
     }
-
     Write-Host "'netclient' is installed."
     Write-Host "'netclient' is installed."
     }
     }
 }
 }