From e4a2efb527fb09b548c14b13d28d0780941ca72d Mon Sep 17 00:00:00 2001
From: kali <kali@win>
Date: Fri, 9 Oct 2020 12:39:27 +0200
Subject: [PATCH] [pkg] windows fixes

Several fixes for missing paths. Refactor qt installer; fetch pre-built
binaries from an existing installation on build system (needs to be
automated); add windows icon.

- Resolves: #362
---
 Makefile                                      | 82 ++++++++-----------
 bitmask.pro                                   |  3 +
 branding/scripts/gen-qtinstaller              | 15 +++-
 .../templates/qtinstaller/config/config.xml   |  2 +-
 .../packages/bitmaskvpn/meta/install.js       |  5 --
 .../packages/bitmaskvpn/meta/package.xml      |  2 +-
 gui/build.sh                                  | 11 ++-
 pkg/config/logger.go                          |  6 ++
 pkg/helper/windows.go                         |  1 -
 9 files changed, 67 insertions(+), 60 deletions(-)

diff --git a/Makefile b/Makefile
index 59751ff..695edc2 100644
--- a/Makefile
+++ b/Makefile
@@ -19,14 +19,17 @@ GOPATH = $(shell go env GOPATH)
 TARGET_GOLIB=lib/libgoshim.a
 SOURCE_GOLIB=gui/backend.go
 
-# detect OS, we use it for dependencies
+# detect OS
+ifeq ($(OS), Windows_NT)
+PLATFORM = windows
+else
 UNAME = $(shell uname -s)
 PLATFORM ?= $(shell echo ${UNAME} | awk "{print tolower(\$$0)}")
+endif
 
 QTBUILD = build/qt
 INSTALLER = build/installer
-WININST_DATA = branding/qtinstaller/packages/root.win_x86_64/data/
-OSX_DATA = ${INSTALLER}/packages/bitmaskvpn/data/
+INST_DATA = ${INSTALLER}/packages/bitmaskvpn/data/
 OSX_CERT="Developer ID Installer: LEAP Encryption Access Project"
 MACDEPLOYQT_OPTS = -appstore-compliant -qmldir=gui/qml -always-overwrite
 # XXX expired cert -codesign="${OSX_CERT}"
@@ -34,7 +37,11 @@ MACDEPLOYQT_OPTS = -appstore-compliant -qmldir=gui/qml -always-overwrite
 SCRIPTS = branding/scripts
 TEMPLATES = branding/templates
 
+ifeq ($(PLATFORM), windows)
+HAS_QTIFW := $(shell which binarycreator.exe)
+else
 HAS_QTIFW := $(shell PATH=$(PATH) which binarycreator)
+endif
 OPENVPN_BIN = "$(HOME)/openvpn_build/sbin/$(shell grep OPENVPN branding/thirdparty/openvpn/build_openvpn.sh | head -n 1 | cut -d = -f 2 | tr -d '"')"
 
 #########################################################################
@@ -78,22 +85,7 @@ build: golib build_helper build_openvpn
 build_helper:
 	@echo "PLATFORM: ${PLATFORM}"
 	@mkdir -p build/bin/${PLATFORM}
-	go build -o build/bin/${PLATFORM}/bitmask-helper -ldflags "-X main.AppName=${APPNAME} -X main.Version=${VERSION}" ./cmd/bitmask-helper/
-
-build_old:
-ifeq (${XBUILD}, yes)
-	$(MAKE) build_cross_win
-	$(MAKE) build_cross_osx
-	$(MAKE) _build_xbuild_done
-else ifeq (${XBUILD}, win)
-	$(MAKE) build_cross_win
-	$(MAKE) _build_done
-else ifeq (${XBUILD}, osx)
-	$(MAKE) build_cross_osx
-	$(MAKE) _build_done
-else
-	@gui/build.sh
-endif
+	go build -o build/bin/${PLATFORM}/bitmask-helper -ldflags "-X main.AppName=${APPNAME} -X main.Version=${VERSION} -H windowsgui" ./cmd/bitmask-helper/
 
 build_openvpn:
 	@[ -f $(OPENVPN_BIN) ] && echo "OpenVPN already built at" $(OPENVPN_BIN) || ./branding/thirdparty/openvpn/build_openvpn.sh
@@ -101,51 +93,51 @@ build_openvpn:
 debug_installer:
 	@VERSION=${VERSION} ${SCRIPTS}/gen-qtinstaller osx ${INSTALLER}
 
-build_installer: check_qtifw build
+build_installer: # TODO re-add check_qtifw build
 	echo "mkdir osx data"	
-	@mkdir -p ${OSX_DATA}
+	@mkdir -p ${INST_DATA}
 	@cp -r ${TEMPLATES}/qtinstaller/packages ${INSTALLER}
 	@cp -r ${TEMPLATES}/qtinstaller/installer.pro ${INSTALLER}
 	@cp -r ${TEMPLATES}/qtinstaller/config ${INSTALLER}
 ifeq (${PLATFORM}, darwin)
-	@mkdir -p ${OSX_DATA}/helper
+	@mkdir -p ${INST_DATA}/helper
 	# TODO need to write this
 	@VERSION=${VERSION} ${SCRIPTS}/gen-qtinstaller osx ${INSTALLER}
-	@cp "${TEMPLATES}/osx/bitmask.pf.conf" ${OSX_DATA}/helper/bitmask.pf.conf
-	@cp "${TEMPLATES}/osx/client.up.sh" ${OSX_DATA}/
-	@cp "${TEMPLATES}/osx/client.down.sh" ${OSX_DATA}/
-	@cp "${TEMPLATES}/qtinstaller/osx-data/post-install.py" ${OSX_DATA}/
-	@cp "${TEMPLATES}/qtinstaller/osx-data/uninstall.py" ${OSX_DATA}/
-	@cp "${TEMPLATES}/qtinstaller/osx-data/se.leap.bitmask-helper.plist" ${OSX_DATA}/
-	@cp build/bin/${PLATFORM}/bitmask-helper ${OSX_DATA}/
+	@cp "${TEMPLATES}/osx/bitmask.pf.conf" ${INST_DATA}helper/bitmask.pf.conf
+	@cp "${TEMPLATES}/osx/client.up.sh" ${INST_DATA}
+	@cp "${TEMPLATES}/osx/client.down.sh" ${INST_DATA}
+	@cp "${TEMPLATES}/qtinstaller/osx-data/post-install.py" ${INST_DATA}
+	@cp "${TEMPLATES}/qtinstaller/osx-data/uninstall.py" ${INST_DATA}
+	@cp "${TEMPLATES}/qtinstaller/osx-data/se.leap.bitmask-helper.plist" ${INST_DATA}
+	@cp build/bin/${PLATFORM}/bitmask-helper ${INST_DATA}
 	# FIXME our static openvpn build fails with an "Assertion failed at crypto.c". Needs to be fixed!!! - kali
 	# a working (old) version:
 	#@curl -L https://downloads.leap.se/thirdparty/osx/openvpn/openvpn -o build/${PROVIDER}/staging/openvpn-osx
-	#@cp $(OPENVPN_BIN) ${OSX_DATA}/openvpn.leap
+	#@cp $(OPENVPN_BIN) ${INST_DATA}/openvpn.leap
 	@echo "WARNING: workaround for broken static build. Shipping homebrew dynamically linked instead"
-	@rm -f ${OSX_DATA}openvpn.leap && cp /usr/local/bin/openvpn ${OSX_DATA}openvpn.leap
+	@rm -f ${INST_DATA}openvpn.leap && cp /usr/local/bin/openvpn ${OSX_DATA}openvpn.leap
 	@echo "[+] Running macdeployqt"
 	@macdeployqt ${QTBUILD}/release/${PROVIDER}-vpn.app ${MACDEPLOYQT_OPTS}
 	@cp -r "${QTBUILD}/release/${TARGET}.app"/ ${OSX_DATA}/
 endif
 ifeq (${PLATFORM}, windows)
-	@${SCRIPTS}/gen-qtinstaller windows ${INSTALLER}
+	@VERSION=${VERSION} ${SCRIPTS}/gen-qtinstaller windows ${INSTALLER}
+	@cp build/bin/${PLATFORM}/bitmask-helper ${INST_DATA}helper.exe
+	@cp branding/assets/${PROVIDER}/icon.ico ${INST_DATA}/icon.ico
+	@cp ${QTBUILD}/release/${TARGET}.exe ${INST_DATA}${TARGET}.exe
+	# FIXME get the signed binaries with curl from openvpn downloads page - see if we have to adapt the openvpn-build to install tap drivers etc from our installer.
+	@cp "/c/Program Files/OpenVPN/bin/openvpn.exe" ${INST_DATA}
+	@cp "/c/Program Files/OpenVPN/bin/"*.dll ${INST_DATA}
+	# XXX add sign options
+	@windeployqt --qmldir gui/qml ${INST_DATA}${TARGET}.exe
 endif
 ifeq (${PLATFORM}, linux)
-	@${SCRIPTS}/gen-qtinstaller windows ${INSTALLER}
+	@VERSION=${VERSION} ${SCRIPTS}/gen-qtinstaller linux ${INSTALLER}
 endif
 	@echo "[+] All templates, binaries and libraries copied to build/installer."
 	@echo "[+] Now building the installer."
 	@cd build/installer && qmake INSTALLER=${APPNAME}-installer-${VERSION} && make
 
-installer_win:
-	# XXX refactor with build_installer
-	cp helper.exe ${WININST_DATA}
-	cp ${QTBUILD}/release/${TARGET}.exe ${WININST_DATA}${TARGET}.exe
-	# XXX add sign step here
-	windeployqt --qmldir gui/qml ${WININST_DATA}${TARGET}.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
-
 check_qtifw: 
 ifdef HAS_QTIFW
 	@echo "[+] Found QTIFW"
@@ -170,14 +162,6 @@ PLATFORM_OSX = PLATFORM=darwin
 build_cross_osx:
 	$(CROSS_OSX_FLAGS) $(PLATFORM_OSX) $(MAKE) _buildparts
 
-_build_done:
-	@echo
-	@echo 'Done. You can build your package now.'
-
-_build_xbuild_done:
-	@echo
-	@echo 'Done. You can do "make packages" now.'
-
 # --------- FIXME -----------------------------------------------------------------------
 
 clean:
diff --git a/bitmask.pro b/bitmask.pro
index 29b13ee..111ff04 100644
--- a/bitmask.pro
+++ b/bitmask.pro
@@ -11,6 +11,9 @@ macx {
     # TODO -- pass the vendor icon here from Makefile.
     ICON = branding/assets/riseup/icon.icns
 }
+win32 {
+    RC_ICONS = branding/assets/riseup/icon.ico
+}
 
 QT += qml quick widgets
 
diff --git a/branding/scripts/gen-qtinstaller b/branding/scripts/gen-qtinstaller
index 419d958..58ab3f9 100755
--- a/branding/scripts/gen-qtinstaller
+++ b/branding/scripts/gen-qtinstaller
@@ -25,7 +25,7 @@ OS_CONFIG = {
     <!-- end osx -->
 """,
     'windows': """
-    <TargetDir>@ApplicationsDir@/DemoLibVPN</TargetDir>
+    <TargetDir>@ApplicationsDir@/$APPNAME</TargetDir>
     <StartMenuDir>$APPNAME</StartMenuDir>
 
     <RunProgram>@TargetDir@/$BINNAME.exe</RunProgram>
@@ -69,12 +69,23 @@ def generateConfig(data, platform, outDir):
          data=templateData)
 
 def generatePackageMetadata(data, platform, outDir):
+    appname = 'applicationName'
     templateData = {
-        'APPNAME': data['applicationName'],
+        'APPNAME': data[appname],
         'BINNAME': data['binaryName'],
         'VERSION': getVersion(),
         'TIMESTAMP': time.strftime('%Y-%m-%d')
     }
+    if platform == "windows":
+        p = data[appname] + " for Windows"
+    elif platform == "osx":
+        p = data[appname] + " for OSX"
+    elif platform == "linux":
+        p = data[appname] + " for GNU/Linux"
+    else:
+        p = data[appname]
+    templateData['APPNAME_PLATFORM'] = p
+
     renderTemplate(
          templatePath='../templates/qtinstaller/packages/bitmaskvpn/meta/package.xml',
          outPath=os.path.join(outDir, 'packages/bitmaskvpn/meta/package.xml'),
diff --git a/branding/templates/qtinstaller/config/config.xml b/branding/templates/qtinstaller/config/config.xml
index c02b0a6..70ce9f6 100644
--- a/branding/templates/qtinstaller/config/config.xml
+++ b/branding/templates/qtinstaller/config/config.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <Installer>
-    <Name>$APPNAME-$VERSION</Name>
+    <Name>$APPNAME</Name>
     <Version>$VERSION</Version>
     <Title>$APPNAME Installer ($VERSION)</Title>
     <Publisher>LEAP Encryption Access Project</Publisher>
diff --git a/branding/templates/qtinstaller/packages/bitmaskvpn/meta/install.js b/branding/templates/qtinstaller/packages/bitmaskvpn/meta/install.js
index 361e14d..d69f262 100644
--- a/branding/templates/qtinstaller/packages/bitmaskvpn/meta/install.js
+++ b/branding/templates/qtinstaller/packages/bitmaskvpn/meta/install.js
@@ -52,13 +52,8 @@ function Component() {
         cancelInstaller("Installation on " + systemInfo.prettyProductName + " is not supported");
         return;
     }
-
     console.log("CPU Architecture: " +  systemInfo.currentCpuArchitecture);
 
-    if ( systemInfo.kernelType === "winnt") {
-        installer.componentByName("root.win_x86_64").setValue("Default", "true");
-        installer.componentByName("root.win_x86_64").setValue("Virtual", "false");
-    }
 }
 
 Component.prototype.createOperations = function ()
diff --git a/branding/templates/qtinstaller/packages/bitmaskvpn/meta/package.xml b/branding/templates/qtinstaller/packages/bitmaskvpn/meta/package.xml
index 0e56f4f..d54cf56 100644
--- a/branding/templates/qtinstaller/packages/bitmaskvpn/meta/package.xml
+++ b/branding/templates/qtinstaller/packages/bitmaskvpn/meta/package.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <Package>
     <DisplayName>$APPNAME</DisplayName>
-    <Description>$APPNAME</Description>
+    <Description>$APPNAME_PLATFORM</Description>
     <Version>$VERSION</Version>
     <ReleaseDate>$TIMESTAMP</ReleaseDate>
     <Default>false</Default>
diff --git a/gui/build.sh b/gui/build.sh
index 080b1fe..8b336fa 100755
--- a/gui/build.sh
+++ b/gui/build.sh
@@ -15,6 +15,7 @@ QTBUILD=build/qt
 RELEASE=$QTBUILD/release
 
 PLATFORM=$(uname -s)
+LDFLAGS=""
 
 if [ "$TARGET" == "" ]
 then
@@ -51,10 +52,14 @@ function buildGoLib {
 	CGO_CFLAGS="-g -O2 -mmacosx-version-min=$OSX_TARGET"
 	CGO_LDFLAGS="-g -O2 -mmacosx-version-min=$OSX_TARGET"
     fi
+    if [ "$PLATFORM" == "MINGW64_NT-10.0" ]
+    then
+	LDFLAGS="-H=windowsgui"
+    fi
     if [ "$XBUILD" == "no" ]
     then
         echo "[+] Building Go library with standard Go compiler"
-        CGO_ENABLED=1 GOOS=$GOOS CC=$CC CGO_CFLAGS=$CGO_CFLAGS CGO_LDFLAGS=$CGO_LDFLAGS go build -buildmode=c-archive -o $TARGET_GOLIB $SOURCE_GOLIB
+        CGO_ENABLED=1 GOOS=$GOOS CC=$CC CGO_CFLAGS=$CGO_CFLAGS CGO_LDFLAGS=$CGO_LDFLAGS go build -ldflags $LDFLAGS -buildmode=c-archive -o $TARGET_GOLIB $SOURCE_GOLIB
     fi
     if [ "$XBUILD" == "$WIN64" ]
     then
@@ -85,6 +90,10 @@ function renameOutput {
     	mv $RELEASE/bitmask.app/ $RELEASE/$TARGET.app/
     	echo "[+] App is in" $RELEASE/$TARGET
     fi
+    if [ "$PLATFORM" == "MINGW64_NT-10.0" ]
+    then
+    	mv $RELEASE/bitmask.exe $RELEASE/$TARGET.exe
+    fi
 }
 
 echo "[+] Building BitmaskVPN"
diff --git a/pkg/config/logger.go b/pkg/config/logger.go
index 09d96b7..cb78349 100644
--- a/pkg/config/logger.go
+++ b/pkg/config/logger.go
@@ -19,10 +19,16 @@ import (
 	"io"
 	"log"
 	"os"
+	"path"
 )
 
 //ConfigureLogger to write logs into a file as well as the stderr
 func ConfigureLogger(logPath string) (io.Closer, error) {
+	dir := path.Dir(logPath)
+	err := os.MkdirAll(dir, 0700)
+	if err == nil {
+		log.Println("ERROR: cannot create data dir")
+	}
 	logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
 	if err == nil {
 		log.SetOutput(io.MultiWriter(logFile, os.Stderr))
diff --git a/pkg/helper/windows.go b/pkg/helper/windows.go
index c33a4bc..43436ad 100644
--- a/pkg/helper/windows.go
+++ b/pkg/helper/windows.go
@@ -39,7 +39,6 @@ var (
 	LogFolder        = getExecDir()
 	openvpnPath      = path.Join(appPath, "openvpn.exe")
 	chocoOpenvpnPath = `C:\Program Files\OpenVPN\bin\openvpn.exe`
-	platformOpenvpnFlags = []string{
 	httpServerConf = &httpConf{}
 )
 
-- 
GitLab