forked from mullvad/mullvadvpn-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller.nsh
1150 lines (831 loc) · 23.4 KB
/
installer.nsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!include stdutils.nsh
!include winver.nsh
!addplugindir "${BUILD_RESOURCES_DIR}\..\windows\nsis-plugins\bin\Win32-Release"
#
# NOTES
#
# Do not include certain default header files - results in random errors
# Do not define and use functions - broken
# Do not use DetailPrint - any message sent to DetailPrint is lost
# Do not compare variables using the <> operator - broken
#
# "sc" exit code
!define SERVICE_STARTED 0
!define SERVICE_START_PENDING 2
# Generic return codes for Mullvad nsis plugins
!define MULLVAD_GENERAL_ERROR 0
!define MULLVAD_SUCCESS 1
# Return codes from driverlogic
!define DL_GENERAL_SUCCESS 0
!define DL_GENERAL_ERROR 1
# Log targets
!define LOG_INSTALL 0
!define LOG_UNINSTALL 1
!define LOG_VOID 2
# Windows error codes
!define ERROR_SERVICE_DOES_NOT_EXIST 1060
!define ERROR_SERVICE_MARKED_FOR_DELETE 1072
!define ERROR_SERVICE_DEPENDENCY_DELETED 1075
# mullvad-setup status codes
!define MVSETUP_OK 0
!define MVSETUP_ERROR 1
!define MVSETUP_VERSION_NOT_OLDER 2
!define MVSETUP_DAEMON_NOT_RUNNING 3
# Override electron-builder generated application settings key.
# electron-builder uses a GUID here rather than the application name.
!define INSTALL_REGISTRY_KEY "Software\${PRODUCT_NAME}"
!define BLOCK_OUTBOUND_IPV4_FILTER_GUID "{a81c5411-0fd0-43a9-a9be-313f299de64f}"
!define PERSISTENT_BLOCK_OUTBOUND_IPV4_FILTER_GUID "{79860c64-9a5e-48a3-b5f3-d64b41659aa5}"
#
# UnloadPlugins
#
# Ensures that temporary files can be removed by the installer.
#
!macro UnloadPlugins
Push $0
log::SetLogTarget ${LOG_VOID}
# Horrendous hack for unpinning log.dll. Since we do not know the reference count
# it is safest to unload it from here.
UnloadPlugins_free_logger:
System::Call "KERNEL32::GetModuleHandle(t $\"$PLUGINSDIR\log.dll$\")p.r0"
${If} $0 P<> 0
System::Call "KERNEL32::FreeLibrary(pr0)"
Goto UnloadPlugins_free_logger
${EndIf}
# The working directory cannot be deleted, so make sure it's set to $TEMP.
SetOutPath "$TEMP"
# $PLUGINSDIR is deleted for us as long as nothing is in use.
Pop $0
!macroend
!define UnloadPlugins '!insertmacro "UnloadPlugins"'
#
# ExtractDriverlogic
#
# Extract device setup tools to $PLUGINSDIR
#
!macro ExtractDriverlogic
SetOutPath "$PLUGINSDIR"
File "${BUILD_RESOURCES_DIR}\..\windows\driverlogic\bin\$%CPP_BUILD_TARGET%-$%CPP_BUILD_MODE%\driverlogic.exe"
!macroend
!define ExtractDriverlogic '!insertmacro "ExtractDriverlogic"'
#
# ExtractWireGuard
#
# Extract Wintun and WireGuardNT installer into $PLUGINSDIR
#
!macro ExtractWireGuard
SetOutPath "$PLUGINSDIR"
File "${BUILD_RESOURCES_DIR}\binaries\$%TARGET_TRIPLE%\wintun\wintun.dll"
File "${BUILD_RESOURCES_DIR}\binaries\$%TARGET_TRIPLE%\wireguard-nt\mullvad-wireguard.dll"
!macroend
!define ExtractWireGuard '!insertmacro "ExtractWireGuard"'
#
# ExtractMullvadSetup
#
# Extract mullvad-setup into $PLUGINSDIR
#
!macro ExtractMullvadSetup
SetOutPath "$PLUGINSDIR"
File "${BUILD_RESOURCES_DIR}\$%SETUP_SUBDIR%\mullvad-setup.exe"
File "${BUILD_RESOURCES_DIR}\..\windows\winfw\bin\$%CPP_BUILD_TARGET%-$%CPP_BUILD_MODE%\winfw.dll"
!macroend
!define ExtractMullvadSetup '!insertmacro "ExtractMullvadSetup"'
#
# RemoveWintun
#
# Try to remove Wintun
#
!macro RemoveWintun
Push $0
Push $1
log::Log "RemoveWintun()"
nsExec::ExecToStack '"$PLUGINSDIR\driverlogic.exe" wintun-delete-driver'
Pop $0
Pop $1
${If} $0 != ${DL_GENERAL_SUCCESS}
StrCpy $R0 "Failed to remove Wintun driver. It may be in use."
log::LogWithDetails $R0 $1
Goto RemoveWintun_return_only
${EndIf}
log::Log "RemoveWintun() completed successfully"
Push 0
Pop $R0
RemoveWintun_return_only:
Pop $1
Pop $0
!macroend
!define RemoveWintun '!insertmacro "RemoveWintun"'
#
# RemoveWireGuardNt
#
# Try to remove WireGuardNT
#
!macro RemoveWireGuardNt
Push $0
Push $1
log::Log "RemoveWireGuardNt()"
nsExec::ExecToStack '"$PLUGINSDIR\driverlogic.exe" wg-nt-cleanup'
Pop $0
Pop $1
${If} $0 != ${DL_GENERAL_SUCCESS}
IntFmt $0 "0x%X" $0
StrCpy $R0 "Failed to remove WireGuardNT pool: error $0"
log::LogWithDetails $R0 $1
Goto RemoveWireGuardNt_return_only
${EndIf}
log::Log "RemoveWireGuardNt() completed successfully"
Push 0
Pop $R0
RemoveWireGuardNt_return_only:
Pop $1
Pop $0
!macroend
!define RemoveWireGuardNt '!insertmacro "RemoveWireGuardNt"'
#
# RemoveAbandonedWintunAdapter
#
# Removes old Wintun interface, even if it belongs to a different pool.
#
!macro RemoveAbandonedWintunAdapter
Push $0
Push $1
log::Log "RemoveAbandonedWintunAdapter()"
nsExec::ExecToStack '"$PLUGINSDIR\driverlogic.exe" wintun-delete-abandoned-device'
Pop $0
Pop $1
${If} $0 != ${DL_GENERAL_SUCCESS}
IntFmt $0 "0x%X" $0
StrCpy $R0 "Failed to remove network adapter: error $0"
log::LogWithDetails $R0 $1
Goto RemoveAbandonedWintunAdapter_return_only
${EndIf}
log::Log "RemoveAbandonedWintunAdapter() completed successfully"
Push 0
Pop $R0
RemoveAbandonedWintunAdapter_return_only:
Pop $1
Pop $0
!macroend
!define RemoveAbandonedWintunAdapter '!insertmacro "RemoveAbandonedWintunAdapter"'
#
# InstallService
#
# Register the service with Windows and start it
#
# Returns: 0 in $R0 on success, otherwise an error message in $R0
#
!macro InstallService
log::Log "InstallService()"
Push $0
Push $1
Push $2
log::Log "Running $\"mullvad-daemon$\" for it to self-register as a service"
nsExec::ExecToStack '"$INSTDIR\resources\mullvad-daemon.exe" --register-service'
Pop $0
Pop $1
${If} $0 != 0
StrCpy $R0 "Failed to install Mullvad service"
log::LogWithDetails $R0 $1
#
# NSIS documentation indicates that failure to launch the target will return
# the string "error" on the top of the stack ($0 after we pop).
#
# However in practice, the failure code from CreateProcess is used.
# And naturally, comparing to 0xC0000139 fails because... NSIS
#
${If} $0 == -1073741511
log::Log "Failed to launch $\"mullvad-daemon$\" (API issue)"
Goto InstallService_return
${EndIf}
Goto InstallService_return
${EndIf}
log::Log "Starting service"
nsExec::ExecToStack '"$SYSDIR\sc.exe" start mullvadvpn'
Pop $0
Pop $1
${If} $0 != ${SERVICE_STARTED}
${AndIf} $0 != ${SERVICE_START_PENDING}
${If} $0 == ${ERROR_SERVICE_DEPENDENCY_DELETED}
StrCpy $R0 'Failed to start Mullvad service: The firewall service "Base Filtering Engine" is missing or unavailable.'
${Else}
StrCpy $R0 "Failed to start Mullvad service: error $0"
${EndIf}
log::LogWithDetails $R0 $1
Goto InstallService_return
${EndIf}
log::Log "InstallService() completed successfully"
Push 0
Pop $R0
InstallService_return:
Pop $2
Pop $1
Pop $0
!macroend
!define InstallService '!insertmacro "InstallService"'
#
# RemoveSplitTunnelDriver
#
# Reset and remove split tunnel driver
#
!macro RemoveSplitTunnelDriver
log::Log "RemoveSplitTunnelDriver()"
Push $0
Push $1
log::Log "Removing Split Tunneling driver"
nsExec::ExecToStack '"$PLUGINSDIR\driverlogic.exe" st-remove'
Pop $0
Pop $1
${If} $0 != ${DL_GENERAL_SUCCESS}
IntFmt $0 "0x%X" $0
StrCpy $R0 "Failed to remove driver: error $0"
log::LogWithDetails $R0 $1
Goto RemoveSplitTunnelDriver_return
${EndIf}
log::Log "RemoveSplitTunnelDriver() completed successfully"
Push 0
Pop $R0
RemoveSplitTunnelDriver_return:
Pop $1
Pop $0
!macroend
!define RemoveSplitTunnelDriver '!insertmacro "RemoveSplitTunnelDriver"'
#
# InstallTrayIcon
#
# Create or update registry entry for tray icon.
#
!macro InstallTrayIcon
log::Log "InstallTrayIcon()"
Push $0
Push $1
tray::PromoteTrayIcon
Pop $0
Pop $1
${If} $0 != ${MULLVAD_SUCCESS}
log::LogWithDetails "Failed to install Mullvad tray icon" $1
Goto InstallTrayIcon_return
${EndIf}
log::Log "InstallTrayIcon() completed successfully"
InstallTrayIcon_return:
Pop $1
Pop $0
!macroend
!define InstallTrayIcon '!insertmacro "InstallTrayIcon"'
#
# MigrateCache
#
# Move old cache files to the new cache directory.
# This is for upgrades from versions <= 2020.8-beta2.
#
!macro MigrateCache
log::Log "MigrateCache()"
Push $0
Push $1
cleanup::MigrateCache
Pop $0
Pop $1
${If} $0 != ${MULLVAD_SUCCESS}
log::Log "Failed to migrate cache: $1"
Goto MigrateCache_return
${EndIf}
log::Log "MigrateCache() completed successfully"
MigrateCache_return:
Pop $1
Pop $0
!macroend
!define MigrateCache '!insertmacro "MigrateCache"'
#
# RemoveLogsAndCache
#
# Call into helper DLL instructing it to remove all logs and cache
# for the current user, other regular users and the SYSTEM (service) user.
#
!macro RemoveLogsAndCache
Push $0
cleanup::RemoveLogsAndCache
# Discard return value
Pop $0
Pop $0
!macroend
!define RemoveLogsAndCache '!insertmacro "RemoveLogsAndCache"'
#
# RemoveSettings
#
# Call into helper DLL instructing it to remove all settings
# for the current user, other regular users and the SYSTEM (service) user.
#
!macro RemoveSettings
Push $0
cleanup::RemoveSettings
# Discard return value
Pop $0
Pop $0
!macroend
!define RemoveSettings '!insertmacro "RemoveSettings"'
#
# RemoveRelayCache
#
# Call into helper DLL instructing it to remove all relay cache.
# Currently, errors are only logged and not propagated.
#
!macro RemoveRelayCache
log::Log "RemoveRelayCache()"
Push $0
Push $1
cleanup::RemoveRelayCache
Pop $0
Pop $1
${If} $0 != ${MULLVAD_SUCCESS}
log::Log "Failed to remove relay cache: $1"
Goto RemoveRelayCache_return
${EndIf}
log::Log "RemoveRelayCache() completed successfully"
RemoveRelayCache_return:
Pop $1
Pop $0
!macroend
!define RemoveRelayCache '!insertmacro "RemoveRelayCache"'
#
# RemoveApiAddressCache
#
# Call into helper DLL instructing it to remove all API address cache.
# Currently, errors are only logged and not propagated.
#
!macro RemoveApiAddressCache
log::Log "RemoveApiAddressCache()"
Push $0
Push $1
cleanup::RemoveApiAddressCache
Pop $0
Pop $1
${If} $0 != ${MULLVAD_SUCCESS}
log::Log "Failed to remove address cache: $1"
Goto RemoveApiAddressCache_return
${EndIf}
log::Log "RemoveApiAddressCache() completed successfully"
RemoveApiAddressCache_return:
Pop $1
Pop $0
!macroend
!define RemoveApiAddressCache '!insertmacro "RemoveApiAddressCache"'
#
# AddCLIToEnvironPath
#
# Add "$INSTDIR\resources" to system env PATH,
# unless it already exists.
#
!macro AddCLIToEnvironPath
log::Log "AddCLIToEnvironPath()"
Push $0
Push $1
pathedit::AddSysEnvPath "$INSTDIR\resources"
Pop $0
Pop $1
${If} $0 != ${MULLVAD_SUCCESS}
log::LogWithDetails "Failed to add the CLI tools to the system PATH" $1
Goto UpdatePath_return
${EndIf}
log::Log "AddCLIToEnvironPath() completed successfully"
UpdatePath_return:
Pop $1
Pop $0
!macroend
!define AddCLIToEnvironPath '!insertmacro "AddCLIToEnvironPath"'
#
# RemoveCLIFromEnvironPath
#
# Remove "$INSTDIR\resources" to system env PATH
#
!macro RemoveCLIFromEnvironPath
log::Log "RemoveCLIFromEnvironPath()"
Push $0
Push $1
pathedit::RemoveSysEnvPath "$INSTDIR\resources"
Pop $0
Pop $1
${If} $0 != ${MULLVAD_SUCCESS}
log::LogWithDetails "Failed to remove the CLI tools from the system PATH" $1
Goto RemovePath_return
${EndIf}
log::Log "RemoveCLIFromEnvironPath() completed successfully"
RemovePath_return:
Pop $1
Pop $0
!macroend
!define RemoveCLIFromEnvironPath '!insertmacro "RemoveCLIFromEnvironPath"'
#
# ClearFirewallRules
#
# Removes any WFP filters added by the daemon, using mullvad-setup.
# This fails if the daemon is running.
#
!macro ClearFirewallRules
log::Log "ClearFirewallRules()"
Push $0
Push $1
nsExec::ExecToStack '"$PLUGINSDIR\mullvad-setup.exe" reset-firewall'
Pop $0
Pop $1
${If} $0 != ${MVSETUP_OK}
log::LogWithDetails "ClearFirewallRules() failed" $1
${Else}
log::Log "ClearFirewallRules() completed successfully"
${EndIf}
Pop $1
Pop $0
!macroend
!define ClearFirewallRules '!insertmacro "ClearFirewallRules"'
!macro FirewallWarningCheck
Push $0
Push $1
Push $2
nsExec::ExecToStack '"$SYSDIR\netsh.exe" wfp show security FILTER ${BLOCK_OUTBOUND_IPV4_FILTER_GUID}'
Pop $1
Pop $0
nsExec::ExecToStack '"$SYSDIR\netsh.exe" wfp show security FILTER ${PERSISTENT_BLOCK_OUTBOUND_IPV4_FILTER_GUID}'
Pop $2
Pop $0
${If} $1 == 0
${OrIf} $2 == 0
MessageBox MB_ICONEXCLAMATION|MB_OK "Installation failed. Your internet access will be unblocked."
${EndIf}
Pop $2
Pop $1
Pop $0
!macroend
!define FirewallWarningCheck '!insertmacro "FirewallWarningCheck"'
#
# RemoveCurrentDevice
#
# Remove the device from the account, if there is one
#
!macro RemoveCurrentDevice
log::Log "RemoveCurrentDevice()"
Push $0
Push $1
nsExec::ExecToStack '"$PLUGINSDIR\mullvad-setup.exe" remove-device'
Pop $0
Pop $1
${If} $0 != ${MVSETUP_OK}
log::LogWithDetails "RemoveCurrentDevice() failed" $1
${Else}
log::Log "RemoveCurrentDevice() completed successfully"
${EndIf}
Pop $1
Pop $0
!macroend
!define RemoveCurrentDevice '!insertmacro "RemoveCurrentDevice"'
#
# customInit
#
# This macro is activated right when the installer first starts up.
#
# When the installer is starting, take the opportunity to update registry
# keys to use the new app identifier.
#
# This enables subsequent logic in the installer to correctly identify
# that there is a previous version of the app installed.
#
!macro customInit
Push $0
${IfNot} ${AtLeastWin10}
MessageBox MB_ICONSTOP|MB_TOPMOST|MB_OK "Windows versions below 10 are unsupported. The last version to support Windows 7 and 8/8.1 is 2021.6."
Abort
${EndIf}
# Application settings key
# Migrate 2018.(x<6) to current
registry::MoveKey "HKLM\SOFTWARE\8fa2c331-e09e-5709-bc74-c59df61f0c7e" "HKLM\SOFTWARE\${PRODUCT_NAME}"
# Discard return value
Pop $0
Pop $0
# Application uninstall key
# Migrate 2018.(x<6) to current
registry::MoveKey "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\8fa2c331-e09e-5709-bc74-c59df61f0c7e" "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}"
# Discard return value
Pop $0
Pop $0
# Application uninstall key
# Migrate 2018.6 through 2019.7 to current
registry::MoveKey "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Mullvad VPN" "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}"
# Discard return value
Pop $0
Pop $0
# Migrate 2019.8 through 2020.5 to current
registry::MoveKey "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{${APP_GUID}}" "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}"
# Discard return value
Pop $0
Pop $0
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}" "NewVersion" "${VERSION}"
Pop $0
!macroend
#
# customCheckAppRunning
#
# The default behavior may cause the daemon to disconnect.
#
!macro customCheckAppRunning
push $R0
push $R1
# This must be done here for compatibility with <= 2021.2,
# since those versions do not kill the GUI in the uninstaller.
Var /GLOBAL OldVersion
ReadRegStr $OldVersion HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}" "DisplayVersion"
StrCpy $R0 $OldVersion 4 # Major version
StrCpy $R1 $OldVersion 1 5 # Minor version
${If} $R0 > 2021
Goto customCheckAppRunning_skip_kill
${OrIf} $R0 == 2021
${If} $R1 > 2
Goto customCheckAppRunning_skip_kill
${EndIf}
${EndIf}
# Killing without /f will likely cause the daemon to disconnect.
nsExec::Exec `taskkill /f /t /im "${APP_EXECUTABLE_FILENAME}"` $R0
Sleep 500
customCheckAppRunning_skip_kill:
pop $R1
pop $R0
!macroend
#
# customInstall
#
# This macro is activated towards the end of the installation
# after all files are copied, shortcuts created, etc
#
!macro customInstall
Push $R0
log::SetLogTarget ${LOG_INSTALL}
log::Log "Running installer for ${PRODUCT_NAME} ${VERSION}"
log::LogWindowsVersion
#
# The electron-builder NSIS logic, that runs before 'customInstall' is activated,
# makes a copy of the installer file:
# C:\Users\%CURRENTUSER%\AppData\Local\mullvad-vpn-updater\installer.exe
#
# Let's undo this and remove the entire folder in AppData.
#
SetShellVarContext current
RMDir /r "$LOCALAPPDATA\mullvad-vpn-updater"
${MigrateCache}
${RemoveRelayCache}
${RemoveApiAddressCache}
${ExtractDriverlogic}
${RemoveAbandonedWintunAdapter}
${RemoveSplitTunnelDriver}
${If} $R0 != 0
MessageBox MB_OK "$R0"
Goto customInstall_abort_installation
${EndIf}
${InstallService}
${If} $R0 != 0
MessageBox MB_OK "$R0"
Goto customInstall_abort_installation
${EndIf}
${AddCLIToEnvironPath}
${InstallTrayIcon}
Goto customInstall_skip_abort
customInstall_abort_installation:
# Aborting the customization step does not undo previous steps taken
# by the installer (copy files, create shortcut, etc)
#
# Therefore we have to break the installed application to
# prevent users from running a half-installed product
#
Delete "$INSTDIR\mullvad vpn.exe"
${FirewallWarningCheck}
${ExtractMullvadSetup}
${ClearFirewallRules}
Abort
customInstall_skip_abort:
${UnloadPlugins}
Pop $R0
!macroend
#
# customUnInstallCheck
#
# This is called from the installer during an upgrade after the old version
# has been uninstalled or failed to uninstall.
#
# The error flag is set if the uninstaller failed to run. Otherwise, $R0
# contains the exit status.
#
!macro customUnInstallCheck
IfErrors 0 customUnInstallCheck_CheckReturnCode
log::SetLogTarget ${LOG_UNINSTALL}
log::Log "Unable to launch uninstaller for previous ${PRODUCT_NAME} version"
#
# If $INSTDIR is gone or can be removed, proceed anyway
#
IfFileExists $INSTDIR\*.* 0 customUnInstallCheck_Done
ClearErrors
RMDir /r $INSTDIR
IfErrors 0 customUnInstallCheck_Done
log::Log "Aborting since $INSTDIR exists"
Goto customUnInstallCheck_Abort
customUnInstallCheck_CheckReturnCode:
${if} $R0 == 0
Goto customUnInstallCheck_Done
${endif}
customUnInstallCheck_Abort:
${FirewallWarningCheck}
${ExtractMullvadSetup}
${ClearFirewallRules}
MessageBox MB_OK "Failed to uninstall a previous version. Please try restarting your computer and try again. If you still have this issue, please contact support."
SetErrorLevel 5
Abort
customUnInstallCheck_Done:
!macroend
###############################################################################
#
# Uninstaller
#
###############################################################################
#
# StopAndDeleteService
#
# Stops and deletes the service, attempting to forcibly kill it if necessary.
#
# Returns: 0 in $R0 on success. Otherwise, an error message in $R0 is returned.
#
!macro StopAndDeleteService
log::Log "StopAndDeleteService()"
Push $0
Push $1
nsExec::ExecToStack '"$SYSDIR\sc.exe" query mullvadvpn'
Pop $0
Pop $1
${If} $0 == ${ERROR_SERVICE_DOES_NOT_EXIST}
Goto StopAndDeleteService_success
${EndIf}
log::Log "Stopping Mullvad service"
nsExec::ExecToStack '"$SYSDIR\net.exe" stop mullvadvpn'
Pop $0
Pop $1
${If} $0 != 0
log::LogWithDetails "Failed to stop the service: $0" $1
# It may be possible to recover by force-killing the service
# This also "fails" with a generic error if the service isn't running
${EndIf}
# Copy over the daemon log from the old install for debugging purposes
SetShellVarContext all
CopyFiles /SILENT /FILESONLY "$LOCALAPPDATA\Mullvad VPN\daemon.log" "$LOCALAPPDATA\Mullvad VPN\old-install-daemon.log"
log::Log "Removing Mullvad service"
nsExec::ExecToStack '"$SYSDIR\sc.exe" delete mullvadvpn'
Pop $0
Pop $R0
${If} $0 != 0
${AndIf} $0 != ${ERROR_SERVICE_MARKED_FOR_DELETE}
log::Log "Failed to delete the service: $0"
Goto StopAndDeleteService_return_only
${EndIf}
Sleep 1000
#
# Forcibly kill the service (if marked for deletion)
#
Var /GLOBAL DeleteService_Counter
Push 0
Pop $DeleteService_Counter
StopAndDeleteService_check_delete:
nsExec::ExecToStack '"$SYSDIR\sc.exe" query mullvadvpn'
Pop $0
Pop $1
${If} $0 != ${ERROR_SERVICE_DOES_NOT_EXIST}
log::Log "Attempting to forcibly kill Mullvad service"
nsExec::ExecToStack '"$SYSDIR\taskkill.exe" /f /fi "SERVICES eq mullvadvpn"'
Pop $0
Pop $1
# Check again whether it was deleted
IntOp $DeleteService_Counter $DeleteService_Counter + 1
${If} $DeleteService_Counter < 3
Sleep 1000
Goto StopAndDeleteService_check_delete
${EndIf}
StrCpy $R0 "Failed to kill Mullvad service"
log::Log $R0
Goto StopAndDeleteService_return_only
${EndIf}
StopAndDeleteService_success:
Push 0
Pop $R0
StopAndDeleteService_return_only:
${If} $R0 == 0
log::Log "StopAndDeleteService() completed successfully"
${Else}
log::Log "StopAndDeleteService() failed"
${EndIf}
Pop $1
Pop $0
!macroend
!define StopAndDeleteService '!insertmacro "StopAndDeleteService"'
#