-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLaunchWithSsl.ps1
166 lines (145 loc) · 6.12 KB
/
LaunchWithSsl.ps1
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
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string] $proxyport,
[Parameter(Mandatory=$True)]
[string] $webport,
[Parameter(Mandatory=$True)]
[string] $certpath,
[Parameter(Mandatory=$True)]
[string] $keypath,
[Parameter(Mandatory=$True)]
[string] $commonname,
[Parameter(Mandatory=$false)]
[string] $vnc = "localhost:5900"
)
Write-Verbose "proxyport = $proxyport"
Write-Verbose "webport = $webport"
Write-Verbose "certpath = $certpath"
Write-Verbose "keypath = $keypath"
Write-Verbose "commonname = $commonname"
Write-Verbose "vnc = $vnc"
function Find-ListeningPort([int] $port)
{
$ipproperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
$listening = $ipproperties.GetActiveTcpListeners() | where Port -EQ $port
return ($listening -ne $null)
}
function Set-NodeVariables()
{
Write-Verbose "Configuring nodejs installation"
[string] $modulespath = Join-Path $env:APPDATA -ChildPath "\npm\node_modules"
[Environment]::SetEnvironmentVariable("NODE_PATH", $modulespath, "Machine")
$currentpath = [Environment]::GetEnvironmentVariable("Path", "Machine")
[Environment]::SetEnvironmentVariable("Path", ("{0};{1}" -f $currentpath.ToString(), [Environment]::GetEnvironmentVariable("%NODE_PATH%","Machine")), "Machine")
return $currentpath.ToString()
}
function Install-NodeJs()
{
Write-Verbose "Installing nodejs"
choco install nodejs.install -y
Update-SessionEnvironment
Write-Verbose "Installing required nodejs modules"
npm install -g optimist, policyfile, ws, http-server
}
function Get-NoVncSource([string] $tempfolder, [string] $novncfolder)
{
Write-Verbose "Downloading novnc git master repository; $($tempfolder)\novnc.zip"
wget https://github.com/novnc/noVNC/archive/master.zip -OutFile "$($tempfolder)\novnc.zip"
Write-Verbose "Extracting novnc source in $($novncfolder)"
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory("$($tempfolder)\novnc.zip", "$($webfolder)")
}
function Get-WebSockifySource([string] $tempfolder, [string] $websockifyfolder)
{
Write-Verbose "Downloading websockify git master repository; $($tempfolder)\websockify.zip"
wget https://github.com/novnc/websockify/archive/master.zip -OutFile "$($tempfolder)\websockify.zip"
Write-Verbose "Extracting novnc source in $($websockifyfolder)"
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory("$($tempfolder)\websockify.zip", "$($webfolder)")
}
function Start-NoVncWebServer([string] $novncfolder, [string] $webport, [string] $certpath, [string] $keypath, [string] $commonname)
{
[string]$arguments = "http-server $($novncfolder) -a $($commonname) -p $($webport) -S -C $($certpath) -K $($keypath)"
Write-Verbose "Starting novnc web server on port $($webport)"
$novncprocess = Start-Process powershell -ArgumentList $arguments -PassThru
If ($novncprocess -eq $null)
{
throw "Failed to start novnc web server"
}
return $novncprocess
}
function Start-WebSockifyProxy([string] $websockifyfolder, [string] $proxyport, [string] $vnc, [string] $certpath, [string] $keypath, [string] $commonname)
{
[string]$websockifyjs = Join-Path $($websockifyfolder) -ChildPath "other\js\websockify.js"
[string]$arguments = "node $($websockifyjs) --cert $($certpath) --key $($keypath) $($commonname):$($proxyport) $($vnc)"
Write-Verbose "Starting websockify proxy from port $($commonname):$($proxyport) to $($vnc)"
$websockifyprocess = Start-Process powershell -ArgumentList $arguments -PassThru
If ($websockifyprocess -eq $null)
{
throw "Failed to start websockify proxy"
}
return $websockifyprocess
}
function Import-ChocolateyModules()
{
Write-Verbose "Importing chocolatey helper functions for powershell"
Import-Module "$env:ChocolateyInstall\helpers\chocolateyInstaller.psm1"
}
function Save-Path([string] $currentpath)
{
[string] $backuppath = Join-Path $($PWD) -ChildPath "path.back"
("{0} - {1}" -f [datetime]::now.ToString('yyyy-MM-dd'), $currentpath) | Out-File $backuppath -Append
}
Try
{
If (Find-ListeningPort -port $proxyport)
{
throw "Port $proxyport in use. Try -proxyport PORT"
}
If (Find-ListeningPort -port $webport)
{
throw "Port $webport in use. Try -webport PORT"
}
Import-ChocolateyModules
Install-NodeJs
[string] $currentpath = Set-NodeVariables
Save-Path -currentpath $currentpath
Update-SessionEnvironment
$tempfolder = New-Item -ItemType directory "$($PWD)\$(get-date -f yyyyMMddHHmmss)"
[string] $webfolder = Join-Path $tempfolder -ChildPath "\web"
[string] $novncfolder = Join-Path $tempfolder -ChildPath "\web\noVNC-master"
[string] $websockifyfolder = Join-Path $tempfolder -ChildPath "\web\websockify-master"
Get-NoVncSource -tempfolder $tempfolder -novncfolder $novncfolder
Get-WebSockifySource -tempfolder $tempfolder -websockifyfolder $websockifyfolder
$novncprocess = Start-NoVncWebServer -novncfolder $novncfolder -webport $webport -certpath $certpath -keypath $keypath -commonname $commonname
$websockifyprocess = Start-WebSockifyProxy -websockifyfolder $websockifyfolder -proxyport $proxyport -vnc $vnc -certpath $certpath -keypath $keypath -commonname $commonname
[string] $novncclient = "https://$($commonname):$($webport)/vnc.html?host=$($commonname)&port=$($proxyport)"
Start-Process $novncclient
Wait-Process -InputObject $novncprocess, $websockifyprocess
}
Catch
{
Write-Error $_
}
Finally
{
If ($currentpath -ne $null)
{
Write-Verbose "Reverting system path variable value"
[Environment]::SetEnvironmentVariable("Path", $currentpath, "Machine")
}
If (Test-Path $tempfolder)
{
Remove-Item $tempfolder -Recurse
}
If ($novncprocess -ne $null)
{
Stop-Process $novncprocess -ErrorAction SilentlyContinue
}
If ($websockifyprocess -ne $null)
{
Stop-Process $websockifyprocess -ErrorAction SilentlyContinue
}
}