-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path50_Setup-MATLABProxy.ps1
85 lines (66 loc) · 3.07 KB
/
50_Setup-MATLABProxy.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
<#
.SYNOPSIS
Enables matlab-proxy to enable browser access to MATLAB.
.DESCRIPTION
Creates self-signed certificates for secure communication via matlab-proxy.
Sets up matlab-proxy's authentication token to expect the user's VM password.
Adds a firewall rule to allow matlab-proxy traffic.
Invokes a task that starts matlab-proxy in the background.
.PARAMETER EnableBrowserAccess
(Required) Check to determine if the user has enabled browser access.
.PARAMETER AuthToken
(Required) Used to set the auth-token required to access MATLAB in a browser. Value is equivalent to the system password.
.EXAMPLE
Set-MATLABProxy -EnableBrowserAccess "Yes" -AuthToken "<AUTH-TOKEN>"
.NOTES
Copyright 2024 The MathWorks, Inc.
#>
function Set-MATLABProxy {
param(
[Parameter(Mandatory = $true)]
[string] $EnableBrowserAccess,
[Parameter(Mandatory = $true)]
[string] $AuthToken
)
Write-Output 'Starting Set-MATLABProxy...'
# Generate the SSL certificate for matlab-proxy to use TLS
$MatlabProxyDataPath = "$Env:ProgramData\MathWorks\matlab-proxy"
New-Item $MatlabProxyDataPath -Type Directory
$CertPath = "$MatlabProxyDataPath\certificate.pem"
$KeyPath = "$MatlabProxyDataPath\private_key.pem"
if ((-not (Test-Path $CertPath)) -or (-not (Test-Path $KeyPath))) {
if (Test-Path $CertPath) {
Remove-Item -Path $CertPath
}
if (Test-Path $KeyPath) {
Remove-Item -Path $KeyPath
}
py "$Env:ProgramFiles\MathWorks\matlab-proxy\generate-certificate.py"
Move-Item -Path "$Env:ProgramFiles\MathWorks\matlab-proxy\private_key.pem" -Destination $KeyPath
Move-Item -Path "$Env:ProgramFiles\MathWorks\matlab-proxy\certificate.pem" -Destination $CertPath
}
# Set up token-authentication for matlab-proxy
if ($AuthToken) {
$LaunchFile = "$Env:ProgramFiles\MathWorks\matlab-proxy\Start-MatlabProxy.ps1"
$PasswordDec = "[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('$AuthToken'))"
(Get-Content $LaunchFile -Raw) -Replace '# \$Env:MWI_AUTH_TOKEN=', ('$Env:MWI_AUTH_TOKEN=' + $PasswordDec) | Set-Content $LaunchFile
}
# Open a firewall rule for matlab-proxy (only required on Windows)
$RuleName = "matlab-proxy - Allow TCP"
if (-not (Get-NetFirewallRule -DisplayName $RuleName -ErrorAction SilentlyContinue)) {
New-NetFirewallRule -DisplayName $RuleName -Direction Inbound -Protocol TCP -LocalPort 8123 -Action Allow
}
# Start matlab-proxy
if ($EnableBrowserAccess -eq "Yes") {
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"C:\Program Files\MathWorks\matlab-proxy\Start-MATLABProxy.ps1`"" -WindowStyle Hidden
}
Write-Output 'Done with Set-MATLABProxy.'
}
try {
Set-MATLABProxy -EnableBrowserAccess $Env:EnableMATLABProxy -AuthToken $Env:Password
}
catch {
$ScriptPath = $MyInvocation.MyCommand.Path
Write-Output "ERROR - An error occurred while running script: $ScriptPath. Error: $_"
throw
}