-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
527 lines (439 loc) · 16.4 KB
/
Copy pathinstall.ps1
File metadata and controls
527 lines (439 loc) · 16.4 KB
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
#!/usr/bin/env pwsh
# ezvpn installer for Windows
# Downloads latest binary from: https://github.com/flexaccessdev/ezvpn/releases
#
# Usage: .\install.ps1 [RELEASE_TAG] [-PreRelease] [-DownloadOnly]
# Or set $env:RELEASE_TAG environment variable
#
# Installs systemwide to %ProgramData%\ezvpn — run from an elevated (Administrator)
# PowerShell. VPN mode also requires Administrator privileges to run (creates TUN
# devices).
# The wintun.dll driver must be downloaded separately from https://www.wintun.net/ (official WireGuard project)
param(
[Parameter(Position = 0)]
[string]$ReleaseTag,
[Parameter()]
[switch]$PreRelease,
[Parameter()]
[switch]$DownloadOnly
)
# Note: installs systemwide to %ProgramData%\ezvpn; run elevated (Administrator).
$ErrorActionPreference = "Stop"
$REPO_OWNER = "flexaccessdev"
$REPO_NAME = "ezvpn"
# Function to print colored messages
function Print-Info {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor Green
}
function Print-Warn {
param([string]$Message)
Write-Host "[WARN] $Message" -ForegroundColor Yellow
}
function Print-Error {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor Red
}
# Fetch the latest stable release tag (non-prerelease)
function Get-LatestReleaseTag {
$apiUrl = "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest"
try {
$release = Invoke-RestMethod -Uri $apiUrl -Method Get
}
catch {
Print-Error "Failed to fetch latest release from GitHub: $_"
exit 1
}
if (-not $release.tag_name) {
Print-Error "Could not find a latest release on GitHub"
exit 1
}
return $release.tag_name
}
# Fetch the latest prerelease tag
function Get-LatestPrereleaseTag {
$apiUrl = "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases?per_page=30"
try {
$releases = Invoke-RestMethod -Uri $apiUrl -Method Get
}
catch {
Print-Error "Failed to fetch releases from GitHub: $_"
exit 1
}
$latestPrerelease = $releases |
Where-Object { $_.prerelease -eq $true } |
Select-Object -First 1 -ExpandProperty tag_name
if (-not $latestPrerelease) {
Print-Error "Could not find any prerelease on GitHub"
exit 1
}
return $latestPrerelease
}
# Fetch full release info (including asset checksums) from GitHub API
function Get-ReleaseInfo {
param([string]$Tag)
$apiUrl = "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$Tag"
try {
$release = Invoke-RestMethod -Uri $apiUrl -Method Get
return $release
}
catch {
Print-Warn "Could not fetch release info: $_"
return $null
}
}
# Extract SHA-256 checksum from release JSON for a specific binary
function Get-ExpectedChecksum {
param(
[object]$ReleaseInfo,
[string]$BinaryName
)
if (-not $ReleaseInfo -or -not $ReleaseInfo.assets) {
return $null
}
# Find the asset matching the binary name
$asset = $ReleaseInfo.assets | Where-Object { $_.name -eq $BinaryName } | Select-Object -First 1
if (-not $asset) {
return $null
}
# Extract sha256 hash from digest field
if ($asset.digest -match 'sha256:([a-f0-9]+)') {
return $matches[1]
}
return $null
}
# Compute SHA-256 checksum of a file
function Get-FileChecksum {
param([string]$FilePath)
try {
$hash = Get-FileHash -Path $FilePath -Algorithm SHA256
return $hash.Hash.ToLower()
}
catch {
Print-Error "Failed to compute checksum: $_"
return $null
}
}
# Verify file checksum against expected value
function Test-Checksum {
param(
[string]$FilePath,
[string]$ExpectedChecksum
)
Print-Info "Verifying checksum..."
$actualChecksum = Get-FileChecksum -FilePath $FilePath
if (-not $actualChecksum) {
return $false
}
if ($ExpectedChecksum -eq $actualChecksum) {
$shortHash = $actualChecksum.Substring(0, 16)
Print-Info "Checksum verified: $shortHash..."
return $true
}
else {
Print-Error "Checksum verification FAILED!"
Print-Error "Expected: $ExpectedChecksum"
Print-Error "Actual: $actualChecksum"
return $false
}
}
# Detect architecture
function Get-Architecture {
$arch = [System.Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
if ($arch -ne "AMD64") {
Print-Error "Unsupported architecture: $arch"
Print-Error "Only AMD64 (x86_64) is supported for Windows"
exit 1
}
return "amd64"
}
# Get binary name based on architecture
function Get-BinaryName {
param([string]$Arch)
if ($Arch -ne "amd64") {
Print-Error "Unsupported architecture: $Arch"
Print-Error "Only amd64 is supported for Windows"
exit 1
}
return "ezvpn-windows-amd64.exe"
}
# Download binary and verify checksum
function Download-Binary {
param(
[string]$Url,
[string]$OutputPath,
[string]$ExpectedChecksum
)
Print-Info "Downloading from $Url"
# Download the binary
try {
Invoke-WebRequest -Uri $Url -OutFile $OutputPath -UseBasicParsing
}
catch {
Print-Error "Failed to download binary: $_"
exit 1
}
# Verify checksum
if (-not $ExpectedChecksum) {
Print-Error "No checksum available. Aborting."
Remove-Item -Path $OutputPath -Force -ErrorAction SilentlyContinue
exit 1
}
if (-not (Test-Checksum -FilePath $OutputPath -ExpectedChecksum $ExpectedChecksum)) {
Print-Error "Binary integrity check failed. Aborting."
Remove-Item -Path $OutputPath -Force -ErrorAction SilentlyContinue
exit 1
}
}
# Download only - save to current directory
function Download-Only {
param(
[string]$BaseUrl,
[string]$BinaryName,
[string]$ExpectedChecksum
)
$url = "$BaseUrl/$BinaryName"
$outputFile = Join-Path (Get-Location) $BinaryName
Download-Binary -Url $url -OutputPath $outputFile -ExpectedChecksum $ExpectedChecksum
# Test the binary
Print-Info "Testing downloaded binary..."
try {
$versionInfo = & $outputFile --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Binary returned non-zero exit code"
}
Print-Info "Binary test successful: $versionInfo"
}
catch {
Print-Error "Binary test failed. The downloaded file may be corrupted or incompatible."
Print-Error "Output: $_"
Remove-Item -Path $outputFile -Force -ErrorAction SilentlyContinue
exit 1
}
Print-Info "Binary saved to: $outputFile"
Print-Warn ""
Print-Warn "IMPORTANT: Before running, you must manually install the WinTun driver:"
Print-Warn " 1. Download wintun.zip from https://www.wintun.net/ (official WireGuard project)"
Print-Warn " 2. Extract the zip file"
Print-Warn " 3. Copy wintun\bin\amd64\wintun.dll to: $(Get-Location)"
Print-Warn " (or any directory in the system PATH)"
Print-Warn " 4. Run as Administrator"
Print-Warn ""
Print-Warn "If you see 'Failed to create TUN device: LoadLibraryExW failed',"
Print-Warn "the wintun.dll is missing or not in a valid DLL search path."
}
# Download binary to temporary location, test it, and install
function Install-Binary {
param(
[string]$BaseUrl,
[string]$BinaryName,
[string]$ExpectedChecksum
)
$url = "$BaseUrl/$BinaryName"
$tempDir = Join-Path $env:TEMP "ezvpn-install-$(Get-Random)"
$tempBinary = Join-Path $tempDir $BinaryName
# Install systemwide, alongside the config and runtime/lock files the binary
# reads from %ProgramData%\ezvpn (resolved the same way the app resolves them;
# fall back to the C:\ProgramData literal only if the env var is unset).
$programData = if ($env:ProgramData) { $env:ProgramData } else { "C:\ProgramData" }
$installDir = Join-Path $programData "ezvpn"
$finalPath = Join-Path $installDir "ezvpn.exe"
try {
# Create temp directory
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
Download-Binary -Url $url -OutputPath $tempBinary -ExpectedChecksum $ExpectedChecksum
# Test the binary
Print-Info "Testing downloaded binary..."
try {
$versionInfo = & $tempBinary --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Binary returned non-zero exit code"
}
Print-Info "Binary test successful: $versionInfo"
}
catch {
Print-Error "Binary test failed. The downloaded file may be corrupted or incompatible."
Print-Error "Output: $_"
exit 1
}
# Create target directory if it doesn't exist
if (-not (Test-Path $installDir)) {
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
}
# Move the tested binary to final location
try {
Move-Item -Path $tempBinary -Destination $finalPath -Force
}
catch {
Print-Error "Failed to move binary to final location: $_"
exit 1
}
Print-Info "Binary installed successfully to $finalPath"
# Add to the systemwide (Machine) PATH if not already there
# (case-insensitive exact match). Machine scope matches the systemwide
# install location and requires the elevation this installer enforces.
$machinePath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
$normalizedInstallDir = $installDir.TrimEnd('\', '/')
$currentPaths = @()
if ($machinePath) {
$currentPaths = $machinePath -split ';' | ForEach-Object { $_.Trim().TrimEnd('\', '/') } | Where-Object { $_ -ne '' }
}
$isInPath = $currentPaths -contains $normalizedInstallDir
if (-not $isInPath) {
Print-Warn "$installDir is not in the system PATH"
Print-Warn "Adding to machine PATH..."
try {
$newPath = if ($machinePath) { "$machinePath;$installDir" } else { $installDir }
[System.Environment]::SetEnvironmentVariable("Path", $newPath, "Machine")
Print-Info "Added to PATH. You may need to restart your terminal for changes to take effect."
}
catch {
Print-Warn "Failed to add to PATH automatically. Please add manually:"
Print-Warn "$installDir"
}
}
else {
Print-Info "$installDir is already in the system PATH"
}
Print-Warn ""
Print-Warn "IMPORTANT: Before running, you must manually install the WinTun driver:"
Print-Warn " 1. Download wintun.zip from https://www.wintun.net/ (official WireGuard project)"
Print-Warn " 2. Extract the zip file"
Print-Warn " 3. Copy wintun\bin\amd64\wintun.dll to: $installDir"
Print-Warn " (or any directory in the system PATH)"
Print-Warn " 4. Run as Administrator"
Print-Warn ""
Print-Warn "If you see 'Failed to create TUN device: LoadLibraryExW failed',"
Print-Warn "the wintun.dll is missing or not in a valid DLL search path."
}
finally {
# Clean up temp directory
if (Test-Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# Display usage information
function Show-Usage {
Write-Host @"
Usage: .\install.ps1 [OPTIONS] [RELEASE_TAG]
Download and install ezvpn binary
Installs systemwide to %ProgramData%\ezvpn (run from an elevated PowerShell).
Options:
-DownloadOnly Download binary to current directory without installing (no elevation needed)
-PreRelease Use latest prerelease instead of latest stable release
-h, --help Show this help message
Arguments:
RELEASE_TAG GitHub release tag to download (default: latest)
Environment variables:
`$env:RELEASE_TAG Alternative way to specify release tag
Examples:
.\install.ps1 # Install latest release (run elevated)
.\install.ps1 20251210172710 # Install specific release
.\install.ps1 -PreRelease # Install latest prerelease
.\install.ps1 -DownloadOnly # Download latest to current directory
.\install.ps1 -DownloadOnly 20251210172710 # Download specific release
`$env:RELEASE_TAG='latest'; .\install.ps1 # Use environment variable
Supported platforms: Windows (amd64)
Note: installs systemwide to %ProgramData%\ezvpn; VPN mode also requires
Administrator privileges to run (creates TUN devices).
IMPORTANT: You must manually download and install the WinTun driver:
1. Download wintun.zip from https://www.wintun.net/ (official WireGuard project)
2. Extract and copy wintun\bin\amd64\wintun.dll to the executable directory
(or any directory in the system PATH)
If you see 'Failed to create TUN device: LoadLibraryExW failed', wintun.dll is missing or not in a valid DLL search path.
"@
}
# Require administrator privileges. A systemwide install writes the binary to
# %ProgramData%\ezvpn and updates the machine PATH, both of which need elevation
# — and ezvpn itself must run elevated to manage the TUN device.
function Test-AdminPrivileges {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Print-Error "Administrator privileges are required to install systemwide to %ProgramData%\ezvpn."
Print-Error ""
Print-Error "Re-run this installer from an elevated (Administrator) PowerShell."
Print-Error ""
Print-Error "To download the binary without installing (no elevation needed), use -DownloadOnly."
exit 1
}
}
# Main installation function
function Start-Installation {
param(
[string]$Tag,
[bool]$DownloadOnly
)
if ($DownloadOnly) {
Print-Info "ezvpn downloader"
}
else {
Print-Info "ezvpn installer"
}
Print-Info "Release: $Tag"
Print-Info "Repository: $REPO_OWNER/$REPO_NAME"
$arch = Get-Architecture
$binaryName = Get-BinaryName -Arch $arch
Print-Info "Platform detected: windows-$arch"
Print-Info "Binary name: $binaryName"
$baseUrl = "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$Tag"
# Fetch release info for checksum verification
Print-Info "Fetching release information..."
$releaseInfo = Get-ReleaseInfo -Tag $Tag
if (-not $releaseInfo) {
Print-Error "Could not fetch release info from GitHub. Cannot verify binary integrity."
exit 1
}
$expectedChecksum = Get-ExpectedChecksum -ReleaseInfo $releaseInfo -BinaryName $binaryName
if (-not $expectedChecksum) {
Print-Error "No checksum found for $binaryName in release. Cannot verify binary integrity."
exit 1
}
$shortHash = $expectedChecksum.Substring(0, 16)
Print-Info "Expected checksum: $shortHash..."
if ($DownloadOnly) {
Download-Only -BaseUrl $baseUrl -BinaryName $binaryName -ExpectedChecksum $expectedChecksum
Print-Info "Download completed successfully!"
}
else {
Install-Binary -BaseUrl $baseUrl -BinaryName $binaryName -ExpectedChecksum $expectedChecksum
Print-Info "Installation completed successfully!"
Print-Info "You can now run 'ezvpn' from an Administrator terminal."
}
}
# Main execution
function Main {
# Handle help flags via ReleaseTag parameter
if ($ReleaseTag -eq "--help" -or $ReleaseTag -eq "-h" -or $ReleaseTag -eq "-?" -or $ReleaseTag -eq "/?" -or $ReleaseTag -eq "/h") {
Show-Usage
exit 0
}
if ($DownloadOnly) {
Print-Info "Starting ezvpn download..."
}
else {
Print-Info "Starting ezvpn installation..."
}
# Determine release tag
$tag = $ReleaseTag
if (-not $tag) {
$tag = $env:RELEASE_TAG
}
if (-not $tag) {
if ($PreRelease) {
Print-Info "Fetching latest prerelease tag from GitHub..."
$tag = Get-LatestPrereleaseTag
}
else {
Print-Info "Fetching latest release tag from GitHub..."
$tag = Get-LatestReleaseTag
}
}
if (-not $DownloadOnly) {
Test-AdminPrivileges
}
Start-Installation -Tag $tag -DownloadOnly:$DownloadOnly
}
# Run main function
Main