-
Notifications
You must be signed in to change notification settings - Fork 219
/
CloudShell.ps1
193 lines (157 loc) · 6.84 KB
/
CloudShell.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
# Starts Azure Cloud Shell session
# Sep 8th 2020
function Start-CloudShell
{
<#
.SYNOPSIS
Starts an Azure Cloud Shell session.
.DESCRIPTION
Starts an Azure Cloud Shell session for the given user.
Note: Does not work with VSCode or ISE.
.Parameter AccessToken
The access token used to start the session.
.EXAMPLE
Get-AADIntAccessTokenForCloudShell -SaveToCache
PS\:>Start-AADIntCloudShell
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[ValidateSet('PowerShell','Bash')]
[String]$Shell="PowerShell",
[Parameter(Mandatory=$False)]
[guid]$SubscriptionId,
[Parameter(Mandatory=$False)]
[String]$ResourceGroup,
[Parameter(Mandatory=$False)]
[String]$StorageAccount,
[Parameter(Mandatory=$False)]
[String]$FileShareName
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://management.core.windows.net/" -ClientId "00000006-0000-0ff1-ce00-000000000000"
if(!$host.UI.SupportsVirtualTerminal)
{
Write-Error "PowerShell ISE or VSCode not supported!"
return
}
try
{
# Check the user settings
$userSettings = Get-UserCloudShellSettings -AccessToken $AccessToken
if(!$userSettings)
{
# User has no settings, we need storage account and fileshare information to create new settings
if([string]::IsNullOrEmpty($StorageAccount) -or [string]::IsNullOrEmpty($ResourceGroup) -or [string]::IsNullOrEmpty($FileShareName) -or ($SubscriptionId -eq $null))
{
Write-Warning "User has no cloud shell settings. If connection fails, please provide Storage Account and FileShare details."
}
else
{
# Let's try to create setting
$StorageAccountId = "/subscriptions/$SubscriptionId/resourcegroups/$ResourceGroup/providers/Microsoft.Storage/storageAccounts/$StorageAccount"
$userSettings = Set-UserCloudShellSettings -AccessToken $AccessToken -StorageAccountId $StorageAccountId -fileShareName $fileShareName
}
}
else
{
Write-Verbose "User settings received!"
}
# Get the shell info
$shellInfo = New-CloudShell -AccessToken $AccessToken
Write-Verbose "Created shell $($shellInfo.uri)"
# Get the authorization code
$authToken = Get-CloudShellAuthToken -AccessToken $AccessToken -Url $shellInfo.uri
Write-Verbose "Received auth-token $authToken"
# Get the settings
$settings = Get-CloudShellSettings -AccessToken $AccessToken -Url $shellInfo.uri -Shell $Shell
Write-Verbose "Received cloud shell settings"
}
catch
{
Write-Error "Failed to connect to Cloud Shell $($_.Message)"
return
}
# Save the current setting for Ctrl+C
$CtrlC = [console]::TreatControlCAsInput
Try
{
$url = $settings.socketUri
# Create the socket and keep alive
$socket = New-Object System.Net.WebSockets.ClientWebSocket
# Set the cookies
$cookiec = [System.Net.CookieContainer]::new(1)
$cookie = [System.Net.Cookie]::new("auth-token", $authToken)
$cookie.Domain = ".console.azure.com"
$cookiec.Add($cookie)
$socket.Options.Cookies = $cookiec
# Create the token and open the connection
$token = New-Object System.Threading.CancellationToken
$connection = $socket.ConnectAsync($url, $token)
Write-Verbose "Connecting to socket $($settings.socketUri)"
# Wait 'till the connection is completed
While (!$connection.IsCompleted) { Start-Sleep -Milliseconds 100 }
if($connection.IsFaulted -eq "True")
{
Write-Error $connection.Exception
return
}
Write-Verbose "Connected to socket."
# Buffer for the content
$buffer = New-Object Byte[] 1024
$socket_in = $Socket.ReceiveAsync($buffer, $Token)
# Clear the console and set the Ctlr+C to be used as an input (so that we can stop things running in cloud)
[console]::TreatControlCAsInput = $true
[console]::Clear()
# The main loop
do
{
# If the read is completed, print it to console and start another read
if($socket_in.IsCompleted)
{
$retVal = $buffer[0..$($socket_in.Result.Count-1)]
$text = [text.encoding]::UTF8.GetString($retVal)
[console]::Write($text)
$socket_in = $Socket.ReceiveAsync($buffer, $Token)
}
# Read the key if available
if([console]::KeyAvailable)
{
$key = [console]::ReadKey($True)
# https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
switch($key.Key)
{
"Insert" { $keyBytes = [byte[]]@(27,91,50,126) }
"Delete" { $keyBytes = [byte[]]@(27,91,51,126) }
"PageUp" { $keyBytes = [byte[]]@(27,91,53,126) }
"PageDown" { $keyBytes = [byte[]]@(27,91,54,126) }
"UpArrow" { $keyBytes = [byte[]]@(27,79,65) }
"DownArrow" { $keyBytes = [byte[]]@(27,79,66) }
"RightArrow" { $keyBytes = [byte[]]@(27,79,67) }
"LeftArrow" { $keyBytes = [byte[]]@(27,79,68) }
"Home" { $keyBytes = [byte[]]@(27,79,72) }
"End" { $keyBytes = [byte[]]@(27,79,70) }
default { $keyBytes = [text.encoding]::UTF8.GetBytes($key.KeyChar) }
}
SendToSocket -Socket $socket -Token $token -Bytes $keyBytes
}
} Until (!$connection -or $socket_in.IsFaulted -eq "True")
}
Catch
{
Write-Error $_
}
Finally
{
# Return the original Ctrl+C
[console]::TreatControlCAsInput = $CtrlC
If ($socket) {
Write-Verbose "Closing websocket"
$socket.Dispose()
}
}
}
}