forked from BrowserBox/BrowserBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThunderbird.ps1
executable file
·386 lines (328 loc) · 13.1 KB
/
Thunderbird.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
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
param (
[Parameter(Mandatory = $true)]
[string]$scriptUrlOrPath,
[Parameter(Mandatory = $true)]
[string]$rdpPassword,
[string]$shell = "powershell.exe"
)
function ValidateArguments {
param (
[string]$scriptUrlOrPath,
[string]$rdpPassword
)
if (-not $scriptUrlOrPath -or -not $rdpPassword) {
Write-Host "Missing arguments. Please provide a script URL/path and RDP password."
exit
}
return $true
}
function CheckAdminPermissions {
$scriptPath = $($MyInvocation.ScriptName)
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process "$shell" -Verb RunAs -ArgumentList "-File `"$scriptPath`" `"$scriptUrlOrPath`" `"$rdpPassword`" `"$shell`""
exit
}
}
function DownloadOrCopyScript {
param (
[string]$scriptUrlOrPath,
[string]$destinationPath
)
if ($scriptUrlOrPath -like "http*") {
Invoke-WebRequest -Uri $scriptUrlOrPath -OutFile $destinationPath
} elseif ($scriptUrlOrPath -cne $destinationPath) {
Copy-Item -Path $scriptUrlOrPath -Destination $destinationPath -Force
} else {
Write-Error "Could not copy $scriptUrlOrPath to $destinationPath"
}
}
function SetLogonScript {
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
$scriptPath = $($MyInvocation.ScriptName)
$command = "`"$shell`" -WindowStyle Hidden -File `"$scriptPath`" -scriptUrlOrPath `"$scriptUrlOrPath`" -rdpPassword `"$rdpPassword`" -shell `"$shell`""
Set-ItemProperty -Path $regPath -Name "Thunderbird-SoundBridge" -Value $command
}
function WriteStateFile {
param (
[string]$stateName
)
$stateDirectory = "$env:USERPROFILE\StateDirectory"
# Check if the state directory exists, if not, create it
if (-not (Test-Path -Path $stateDirectory)) {
New-Item -Path $stateDirectory -ItemType Directory
}
$stateFilePath = Join-Path $stateDirectory "$stateName.state"
$timestamp = Get-Date -Format "o"
Set-Content -Path $stateFilePath -Value $timestamp
}
function EnsureWindowsAudioService {
$audioService = Get-Service -Name 'Audiosrv'
# Set service to start automatically
Set-Service -Name 'Audiosrv' -StartupType Automatic
Write-Output "Windows Audio service set to start automatically"
# Start the service if it's not running
if ($audioService.Status -ne 'Running') {
Start-Service -Name 'Audiosrv'
Write-Output "Windows Audio service started"
}
else {
Write-Output "Windows Audio service is already running"
}
}
function SetupRDP {
Param (
[int]$MaxConnections = 10
)
$rdpStatus = Get-ItemProperty 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections"
if ($rdpStatus.fDenyTSConnections -eq 1) {
Set-ItemProperty 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
Write-Output "RDP Enabled"
}
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "MaxConnectionAllowed" -Value $MaxConnections
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name "fSingleSessionPerUser" -Value 0
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" -Name "MaxConnectionCount" -Value 999
Write-Output "Max RDP Connections set to $MaxConnections"
}
function Trust-RDPCertificate {
$userHome = [System.Environment]::GetFolderPath('UserProfile')
$certFolder = Join-Path -Path $userHome -ChildPath "RDP_Certificates"
$certPath = Join-Path -Path $certFolder -ChildPath "rdp_certificate.cer"
# Create the certificate folder if it does not exist
if (-not (Test-Path $certFolder)) {
New-Item -Path $certFolder -ItemType Directory
}
# Export the RDP Certificate
$rdpCert = Get-ChildItem -Path Cert:\LocalMachine\"Remote Desktop" | Select-Object -First 1
if ($rdpCert -ne $null) {
Export-Certificate -Cert $rdpCert -FilePath $certPath
Write-Output "Certificate exported to $certPath"
}
else {
Write-Error "RDP Certificate not found."
return
}
# Import the Certificate into the Trusted Store
if (Test-Path $certPath) {
try {
$certStore = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", "LocalMachine")
$certStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$certStore.Add([System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile($certPath))
$certStore.Close()
Write-Output "Certificate added to the Trusted Root Certification Authorities store."
}
catch {
Write-Error "An error occurred while importing the certificate."
}
}
else {
Write-Error "Exported certificate file not found."
}
}
function InitiateLocalRDPSession {
param (
[string]$Password,
[int]$retryCount = 30,
[int]$retryInterval = 5
)
$username = $env:USERNAME
$localComputerName = [System.Environment]::MachineName
cmdkey /generic:TERMSRV/$localComputerName /user:$username /pass:$Password
Write-Output "Credentials Stored for RDP. Password: $Password"
for ($i = 0; $i -lt $retryCount; $i++) {
mstsc /v:$localComputerName /w:640 /h:480
Start-Sleep -Seconds 5 # Wait a bit for mstsc to possibly initiate
# Get the list of sessions, excluding the current one
$rdpSessions = qwinsta /SERVER:$localComputerName | Where-Object { $_ -notmatch "^\s*$" -and $_ -notmatch "^>" -and $_ -match "\brdp-tcp\b" }
$activeSession = $rdpSessions | Select-String $username
if ($activeSession) {
Write-Output "RDP Session initiated successfully."
return
}
else {
Write-Output "RDP Session failed to initiate. Retrying in $retryInterval seconds..."
Start-Sleep -Seconds $retryInterval
}
}
Write-Error "Failed to initiate RDP Session after $retryCount attempts."
}
function EstablishRDPLoopback {
param (
[string]$Password
)
Write-Output "Establishing RDP Loopback for Windows Audio solution..."
Trust-RDPCertificate
SetupRDP -MaxConnections 10
InitiateLocalRDPSession -Password $Password
}
function InitiateLocalRDPSession {
param (
[Parameter(Mandatory = $true)]
[string]$Password,
[int]$retryCount = 30,
[int]$retryInterval = 5
)
$username = $env:USERNAME
$localComputerName = [System.Environment]::MachineName
cmdkey /generic:TERMSRV/$localComputerName /user:$username /pass:$Password
Write-Output "Credentials Stored for RDP."
for ($i = 0; $i -lt $retryCount; $i++) {
mstsc /v:$localComputerName
Start-Sleep -Seconds 5 # Wait a bit for mstsc to possibly initiate
# Get the list of sessions, excluding the current one
$rdpSessions = qwinsta /SERVER:$localComputerName | Where-Object { $_ -notmatch "^>" -and $_ -match "\brdp-tcp\b" }
$activeSession = $rdpSessions | Select-String $username
if ($activeSession) {
Write-Output "RDP Session initiated successfully."
return
}
else {
Write-Output "RDP Session failed to initiate. Retrying in $retryInterval seconds..."
Start-Sleep -Seconds $retryInterval
}
}
Write-Error "Failed to initiate RDP Session after $retryCount attempts."
}
function Start-TightVNCViewer {
param (
[Parameter(Mandatory = $true)]
[string]$Password
)
$localComputerName = "localhost"
$vncViewerPath = "$env:ProgramFiles\TightVNC\tvnviewer.exe"
if (Test-Path $vncViewerPath) {
Write-Output "Starting TightVNC Viewer for a local test..."
Start-Process $vncViewerPath -ArgumentList "$localComputerName::5900 -password=$Password"
Write-Output "TightVNC Viewer test completed"
}
else {
Write-Error "TightVNC Viewer is not installed."
}
}
function ExecuteProvidedScript {
param (
[string]$scriptPath
)
# Execute the script
Start-Process "$shell" -ArgumentList "-File `"$scriptPath`""
}
function Install-TightVNC {
param (
[string]$downloadUrl = "https://www.tightvnc.com/download/2.8.81/tightvnc-2.8.81-gpl-setup-64bit.msi",
[string]$installerPath = "$env:TEMP\tightvnc-setup.msi",
[Parameter(Mandatory = $true)]
[string]$Password
)
# Download TightVNC Installer
Invoke-WebRequest -Uri $downloadUrl -OutFile $installerPath
Write-Output "TightVNC Installer downloaded"
# Install TightVNC Silently
Start-Process "msiexec.exe" -ArgumentList "/i `"$installerPath`" /quiet /norestart ADDLOCAL=`"Server,Viewer`" SET_USEVNCAUTHENTICATION=1 VALUE_OF_USEVNCAUTHENTICATION=1 SET_PASSWORD=1 VALUE_OF_PASSWORD=`"$Password`" SET_ALLOWLOOPBACK=1 VALUE_OF_ALLOWLOOPBACK=1" -Wait
Write-Output "TightVNC Installed"
# Set TightVNC Password
Add-ToSystemPath "$env:ProgramFiles\TightVNC"
RefreshPath
$installArgs = "-install -silent"
$startArgs = "-start -silent"
Start-Process "tvnserver.exe" -ArgumentList $installArgs -Wait
Start-Process "tvnserver.exe" -ArgumentList $startArgs -Wait
Write-Output "TightVNC setup"
# Modify TightVNC Settings for RDP
Set-ItemProperty -Path "HKLM:\SOFTWARE\TightVNC\Server" -Name "ConnectToRdpSession" -Value 1
Write-Output "TightVNC configured to connect to RDP session"
}
function Add-ToSystemPath {
param ([string]$pathToAdd)
$currentPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if (-not $currentPath.Contains($pathToAdd)) {
$newPath = $currentPath + ";" + $pathToAdd
[Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::Machine)
Write-Output "Added $pathToAdd to system PATH."
}
else {
Write-Output "$pathToAdd is already in system PATH."
}
}
function RefreshPath {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
function EstablishVNCLoopback {
param (
[string]$Password
)
Install-TightVNC -Password $Password
Start-TightVNCViewer -Password $Password
}
function ExecuteTsconRedirection {
$qwinstaOutput = qwinsta
$currentSessionId = $qwinstaOutput | Where-Object { $_ -match "^>.*rdp-tcp" } | ForEach-Object { ($_ -split "\s+")[2] }
if ($currentSessionId) {
tscon $currentSessionId /dest:console
}
}
function WaitForState {
param (
[string]$stateName,
[int]$timeoutSeconds = 480
)
$stateDirectory = "$env:USERPROFILE\StateDirectory"
$endTime = (Get-Date).AddSeconds($timeoutSeconds)
Write-Output "Waiting for state '$stateName'..."
while ((Get-Date) -lt $endTime) {
$stateFilePath = Join-Path $stateDirectory "$stateName.state"
if (Test-Path $stateFilePath) {
Write-Output "State '$stateName' reached."
return
}
Start-Sleep -Seconds 5
}
Write-Error "Timeout reached. State '$stateName' not found."
}
function RestartSelf {
$scriptPath = $($MyInvocation.ScriptName)
Write-Output "Restarting script..."
Start-Process "$shell" -ArgumentList "-File `"$scriptPath`" -scriptUrlOrPath `"$scriptUrlOrPath`" -rdpPassword `"$rdpPassword`" -shell `"$shell`""
exit
}
# Main Script Block
if (-not (ValidateArguments -scriptUrlOrPath $scriptUrlOrPath -rdpPassword $rdpPassword)) {
Write-Error "Need to supply script to run after audio setup and rdp password"
exit
}
CheckAdminPermissions
$currentState = Get-ChildItem -Path "$env:USERPROFILE\StateDirectory" -Filter "*.state" | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty BaseName
if ( $currentState -eq $nil ) {
WriteStateFile -stateName "InitialState"
$currentState="InitialState"
}
Write-Host "Current state: $currentState"
switch ($currentState) {
"InitialState" {
DownloadOrCopyScript -scriptUrlOrPath $scriptUrlOrPath -destinationPath "$env:USERPROFILE\Desktop\ProvidedScript.ps1"
SetLogonScript
EnsureWindowsAudioService
WriteStateFile -stateName "LoopbackConnectionsEstablished"
EstablishRDPLoopback -Password $rdpPassword
EstablishVNCLoopback -Password $rdpPassword
WaitForState -stateName "ProvidedScriptRunning"
RestartSelf
}
"LoopbackConnectionsEstablished" {
ExecuteProvidedScript -scriptPath "$env:USERPROFILE\Desktop\ProvidedScript.ps1"
Start-Sleep -seconds 5
WriteStateFile -stateName "ProvidedScriptRunning"
}
"ProvidedScriptRunning" {
Start-Sleep -Seconds 5
ExecuteTsconRedirection
WriteStateFile -stateName "TsconExecutionCompleted"
}
"TsconExecutionCompleted" {
# Additional actions if required
Write-Output "Process is completed. Audio should persist"
}
default {
Write-Host "Unknown state. Exiting."
exit
}
}