forked from Gerenios/AADInternals
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CloudShell.ps1
164 lines (129 loc) · 5.41 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
# 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"
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://management.core.windows.net/" -ClientId "0c1307d4-29d6-4389-a11c-5cbe7f65d7fa"
if(!$host.UI.SupportsVirtualTerminal)
{
Write-Error "PowerShell ISE or VSCode not supported!"
return
}
try
{
# 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()
}
}
}
}