From 5d102497e80f17056ce97bcc58a4906cf8a05c28 Mon Sep 17 00:00:00 2001
From: kali <kali@win>
Date: Fri, 25 Sep 2020 17:46:50 +0200
Subject: [PATCH] [bug] workaround for helper path in windows

---
 Makefile                                       | 11 +++++++----
 branding/qtinstaller/config/config.xml         |  9 +++++++--
 .../root.win_x86_64/meta/installscript.js      |  6 ++++--
 docs/build.windows.rst                         |  9 +++++++++
 build.sh => gui/build.sh                       |  0
 pkg/helper/helper.go                           |  4 ++--
 pkg/helper/windows.go                          | 18 +++++++++++++-----
 7 files changed, 42 insertions(+), 15 deletions(-)
 create mode 100644 docs/build.windows.rst
 rename build.sh => gui/build.sh (100%)
 mode change 100755 => 100644

diff --git a/Makefile b/Makefile
index e1af715f..000b7556 100644
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,7 @@
 XBUILD ?= no
 SKIP_CACHECK ?= no
 PROVIDER ?= $(shell grep ^'provider =' branding/config/vendor.conf | cut -d '=' -f 2 | tr -d "[:space:]")
+TARGET ?= bitmask
 PROVIDER_CONFIG ?= branding/config/vendor.conf
 DEFAULT_PROVIDER = branding/assets/default/
 VERSION ?= $(shell git describe)
@@ -20,11 +21,12 @@ SOURCE_GOLIB=gui/backend.go
 # detect OS, we use it for dependencies
 UNAME = $(shell uname -s)
 PLATFORM ?= $(shell echo ${UNAME} | awk "{print tolower(\$$0)}")
+WININST_DATA = branding/qtinstaller/packages/root.win_x86_64/data/
 
 TEMPLATES = branding/templates
 SCRIPTS = branding/scripts
 
-all: icon locales build
+all: icon locales helper build
 
 
 #########################################################################
@@ -66,7 +68,7 @@ else ifeq (${XBUILD}, osx)
 	$(MAKE) build_cross_osx
 	$(MAKE) _build_done
 else
-	$(MAKE) _buildparts
+	@gui/build.sh
 endif
 
 
@@ -224,8 +226,9 @@ package_deb:
 	@make -C build/${PROVIDER} pkg_deb
 
 installer_win:
-	cp qtbuild/release/bitmask.exe branding/qtinstaller/packages/root.win_x86_64/data/${PROVIDER}-vpn.exe
-	windeployqt --qmldir gui/qml branding/qtinstaller/packages/root.win_x86_64/data/${PROVIDER}-vpn.exe
+	cp helper.exe ${WININST_DATA}
+	cp qtbuild/release/${TARGET}.exe ${WININST_DATA}${PROVIDER}-vpn.exe
+	windeployqt --qmldir gui/qml ${WININST_DATA}${PROVIDER}-vpn.exe
 	"/c/Qt/QtIFW-3.2.2/bin/binarycreator.exe" -c ./branding/qtinstaller/config/config.xml -p ./branding/qtinstaller/packages build/${PROVIDER}-vpn-${VERSION}-installer.exe
 
 # FIXME --- old nsis installer. deprecate, but probably we need something similar to sign all the binaries (helper, main app, installer...)
diff --git a/branding/qtinstaller/config/config.xml b/branding/qtinstaller/config/config.xml
index 4469d65a..8ce9b2e0 100644
--- a/branding/qtinstaller/config/config.xml
+++ b/branding/qtinstaller/config/config.xml
@@ -1,9 +1,14 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <Installer>
-	<Name>DemoLibVPN</Name>
+	<Name>DemoLibVPN-0.0.1</Name>
 	<Version>0.0.1</Version>
 	<Title>DemoLibVPN Installer</Title>
 	<Publisher>LEAP Encryption Access Project</Publisher>
-	<StartMenuDir>DemoLibVPN</StartMenuDir>
 	<TargetDir>@ApplicationsDir@/DemoLibVPN</TargetDir>
+	<RunProgram>@TargetDir@/demolib-vpn.exe</RunProgram>
+	<RunProgramArguments>
+	</RunProgramArguments>
+        <StartMenuDir>DemoLibVPN</StartMenuDir>
+        <MaintenanceToolName>Uninstall-DemoLibVPN</MaintenanceToolName>
+        <AllowNonAsciiCharacters>false</AllowNonAsciiCharacters>
 </Installer>
diff --git a/branding/qtinstaller/packages/root.win_x86_64/meta/installscript.js b/branding/qtinstaller/packages/root.win_x86_64/meta/installscript.js
index 691d1a4c..1598458c 100644
--- a/branding/qtinstaller/packages/root.win_x86_64/meta/installscript.js
+++ b/branding/qtinstaller/packages/root.win_x86_64/meta/installscript.js
@@ -69,7 +69,9 @@ Component.prototype.createOperations = function()
         if (systemInfo.productType === "windows") {
              console.log("Adding shortcut entries");
              component.addElevatedOperation("Mkdir", "@StartMenuDir@");
-             component.addElevatedOperation("CreateShortcut", "@TargetDir@/demolib-vpn.exe", "@StartMenuDir@/DemoLibVPN.lnk", "workingDirectory=@TargetDir@", "iconPath=%SystemRoot%/system32/SHELL32.dll", "iconId=2", "description=Start DemoLibVPN");
-             component.addElevatedOperation("CreateShortcut", "@TargetDir@/maintenancetool.exe", "@StartMenuDir@/uninstall.lnk", "workingDirectory=@TargetDir@", "iconPath=%SystemRoot%/system32/SHELL32.dll", "iconId=2", "description=Uninstall application");
+             component.addElevatedOperation("CreateShortcut", "@TargetDir@/demolib-vpn.exe", "@StartMenuDir@/DemoLibVPN.lnk", "workingDirectory=@TargetDir@", "iconPath=@TargetDir@/icon.ico", "description=Start DemoLibVPN");
+
+	     // TODO I think this one is not being created because the path doesn't exist yet. We might want to do this by hooking on the installation finished signal instead.
+             component.addElevatedOperation("CreateShortcut", "@TargetDir@/Uninstall-DemoLibVPN.exe", "@StartMenuDir@/Uninstall.lnk");
         }
 }
diff --git a/docs/build.windows.rst b/docs/build.windows.rst
new file mode 100644
index 00000000..a0c7a565
--- /dev/null
+++ b/docs/build.windows.rst
@@ -0,0 +1,9 @@
+windows build notes (still some manual steps needed, this should be further automated).
+=======================================================================================
+
+PROVIDER=DemoLib make helper
+INSTALLER_DATA=branding/qtinstaller/packages/root.win_x86_64/data/
+mkdir -p INSTALLER_DATA
+mv main.exe ${INSTALLER_DATA}/helper.exe
+TARGET=demolib-vpn make build
+TARGET=demolib-vpn make installer_win
diff --git a/build.sh b/gui/build.sh
old mode 100755
new mode 100644
similarity index 100%
rename from build.sh
rename to gui/build.sh
diff --git a/pkg/helper/helper.go b/pkg/helper/helper.go
index 9d0b3304..691fc6e1 100644
--- a/pkg/helper/helper.go
+++ b/pkg/helper/helper.go
@@ -29,9 +29,9 @@ import (
 )
 
 var (
-	AppName = "DemoLibVPN"
+	AppName    = "DemoLibVPN"
 	BinaryName = "bitmask"
-	Version = "git"
+	Version    = "git"
 )
 
 type openvpnT struct {
diff --git a/pkg/helper/windows.go b/pkg/helper/windows.go
index ef4f2719..44ac6f50 100644
--- a/pkg/helper/windows.go
+++ b/pkg/helper/windows.go
@@ -33,8 +33,10 @@ import (
 
 var (
 	svcName          = BinaryName + `-helper-v2`
+
+	// XXX this is set to c:\WINDOWS\system32 on initialization. Do not use it, use a function call instead.
 	appPath          = getExecDir()
-	LogFolder        = appPath
+	LogFolder        = getExecDir()
 	openvpnPath      = path.Join(appPath, "openvpn.exe")
 	chocoOpenvpnPath = `C:\Program Files\OpenVPN\bin\openvpn.exe`
 	platformOpenvpnFlags = []string{
@@ -47,9 +49,11 @@ var (
 func getExecDir() string {
 	ex, err := os.Executable()
 	if err != nil {
-		log.Fatal("Cannot find executable path")
+		log.Println("Cannot find executable path")
+		return ""
 	}
-	return path.Dir(ex)
+	/* XXX filepath.Abs is buggy, maybe because of spaces in the path. fuck it, this is good enough for now */
+	return strings.Replace(ex, "\\helper.exe", "", 1)
 }
 
 type httpConf struct {
@@ -131,11 +135,15 @@ func daemonize() {}
 func runServer(port int) {}
 
 func getOpenvpnPath() string {
-	if _, err := os.Stat(openvpnPath); !os.IsNotExist(err) {
-		return openvpnPath
+	openvpn := path.Join(getExecDir(), "openvpn.exe")
+	if _, err := os.Stat(openvpn); !os.IsNotExist(err) {
+		log.Println("DEBUG: openvpnpath found,", openvpnPath)
+		return openvpn
 	} else if _, err := os.Stat(chocoOpenvpnPath); !os.IsNotExist(err) {
+		log.Println("DEBUG: choco openvpn found,", chocoOpenvpnPath)
 		return chocoOpenvpnPath
 	}
+	log.Println("DEBUG: did not find system-wide openvpn...")
 	return "openvpn.exe"
 }
 
-- 
GitLab