MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Management.Automation.Remoting;5 6using Dbg = System.Management.Automation.Diagnostics;7 8namespace System.Management.Automation9{10 internal sealed class RemoteSessionNegotiationEventArgs : EventArgs11 {12 #region Constructors13 14 internal RemoteSessionNegotiationEventArgs(RemoteSessionCapability remoteSessionCapability)15 {16 Dbg.Assert(remoteSessionCapability != null, "caller should validate the parameter");17 18 if (remoteSessionCapability == null)19 {20 throw PSTraceSource.NewArgumentNullException(nameof(remoteSessionCapability));21 }22 23 RemoteSessionCapability = remoteSessionCapability;24 }25 26 #endregion Constructors27 28 /// <summary>29 /// Data from network converted to type RemoteSessionCapability.30 /// </summary>31 internal RemoteSessionCapability RemoteSessionCapability { get; }32 33 /// <summary>34 /// Actual data received from the network.35 /// </summary>36 internal RemoteDataObject<PSObject> RemoteData { get; set; }37 }38 39 /// <summary>40 /// This event arg is designed to contain generic data received from the other side of the connection.41 /// It can be used for both the client side and for the server side.42 /// </summary>43 internal sealed class RemoteDataEventArgs : EventArgs44 {45 #region Constructors46 47 internal RemoteDataEventArgs(RemoteDataObject<PSObject> receivedData)48 {49 Dbg.Assert(receivedData != null, "caller should validate the parameter");50 51 if (receivedData == null)52 {53 throw PSTraceSource.NewArgumentNullException(nameof(receivedData));54 }55 56 ReceivedData = receivedData;57 }58 59 #endregion Constructors60 61 /// <summary>62 /// Received data.63 /// </summary>64 public RemoteDataObject<PSObject> ReceivedData { get; }65 }66 67 /// <summary>68 /// This event arg contains data received and is used to pass information69 /// from a data structure handler to its object.70 /// </summary>71 /// <typeparam name="T">type of data that's associated</typeparam>72 internal sealed class RemoteDataEventArgs<T> : EventArgs73 {74 #region Private Members75 76 #endregion Private Members77 78 #region Properties79 80 /// <summary>81 /// The data contained within this event.82 /// </summary>83 internal T Data { get; }84 85 #endregion Properties86 87 #region Constructor88 89 internal RemoteDataEventArgs(object data)90 {91 // Dbg.Assert(data != null, "data passed should not be null");92 93 Data = (T)data;94 }95 96 #endregion Constructor97 }98 99 /// <summary>100 /// This defines the various states a remote connection can be in.101 /// </summary>102 internal enum RemoteSessionState103 {104 /// <summary>105 /// Undefined state.106 /// </summary>107 UndefinedState = 0,108 109 /// <summary>110 /// This is the state a connect start with. When a connection is closed,111 /// the connection will eventually come back to this Idle state.112 /// </summary>113 Idle = 1,114 115 /// <summary>116 /// A connection operation has been initiated.117 /// </summary>118 Connecting = 2,119 120 /// <summary>121 /// A connection operation has completed successfully.122 /// </summary>123 Connected = 3,124 125 /// <summary>126 /// The capability negotiation message is in the process being sent on a create operation.127 /// </summary>128 NegotiationSending = 4,129 130 /// <summary>131 /// The capability negotiation message is in the process being sent on a connect operation.132 /// </summary>133 NegotiationSendingOnConnect = 5,134 135 /// <summary>136 /// The capability negotiation message is sent successfully from a sender point of view.137 /// </summary>138 NegotiationSent = 6,139 140 /// <summary>141 /// A capability negotiation message is received.142 /// </summary>143 NegotiationReceived = 7,144 145 /// <summary>146 /// Used by server to wait for negotiation from client.147 /// </summary>148 NegotiationPending = 8,149 150 /// <summary>151 /// The connection is in the progress of getting closed.152 /// </summary>153 ClosingConnection = 9,154 155 /// <summary>156 /// The connection is closed completely.157 /// </summary>158 Closed = 10,159 160 /// <summary>161 /// The capability negotiation has been successfully completed.162 /// </summary>163 Established = 11,164 165 /// <summary>166 /// Have sent a public key to the remote end,167 /// awaiting a response.168 /// </summary>169 /// <remarks>Applicable only to client</remarks>170 EstablishedAndKeySent = 12,171 172 /// <summary>173 /// Have received a public key from the remote174 /// end, need to send a response.175 /// </summary>176 /// <remarks>Applicable only to server</remarks>177 EstablishedAndKeyReceived = 13,178 179 /// <summary>180 /// For Server - Have sent a request to the remote end to181 /// send a public key182 /// for Client - have received a PK request from server.183 /// </summary>184 /// <remarks>Applicable to both client and server</remarks>185 EstablishedAndKeyRequested = 14,186 187 /// <summary>188 /// Key exchange complete. This can mean189 /// (a) Sent an encrypted session key to the190 /// remote end in response to receiving191 /// a public key - this is for the server192 /// (b) Received an encrypted session key from193 /// remote end after sending a public key -194 /// this is for the client.195 /// </summary>196 EstablishedAndKeyExchanged = 15,197 198 /// <summary>199 /// </summary>200 Disconnecting = 16,201 202 /// <summary>203 /// </summary>204 Disconnected = 17,205 206 /// <summary>207 /// </summary>208 Reconnecting = 18,209 210 /// <summary>211 /// A disconnect operation initiated by the WinRM robust connection212 /// layer and *not* by the user.213 /// </summary>214 RCDisconnecting = 19,215 216 /// <summary>217 /// Number of states.218 /// </summary>219 MaxState = 20220 }221 222 /// <summary>223 /// This defines the internal events that the finite state machine for the connection224 /// uses to take action and perform state transitions.225 /// </summary>226 internal enum RemoteSessionEvent227 {228 InvalidEvent = 0,229 CreateSession = 1,230 ConnectSession = 2,231 NegotiationSending = 3,232 NegotiationSendingOnConnect = 4,233 NegotiationSendCompleted = 5,234 NegotiationReceived = 6,235 NegotiationCompleted = 7,236 NegotiationPending = 8,237 Close = 9,238 CloseCompleted = 10,239 CloseFailed = 11,240 ConnectFailed = 12,241 NegotiationFailed = 13,242 NegotiationTimeout = 14,243 SendFailed = 15,244 ReceiveFailed = 16,245 FatalError = 17,246 MessageReceived = 18,247 KeySent = 19,248 KeySendFailed = 20,249 KeyReceived = 21,250 KeyReceiveFailed = 22,251 KeyRequested = 23,252 KeyRequestFailed = 24,253 DisconnectStart = 25,254 DisconnectCompleted = 26,255 DisconnectFailed = 27,256 ReconnectStart = 28,257 ReconnectCompleted = 29,258 ReconnectFailed = 30,259 RCDisconnectStarted = 31,260 MaxEvent = 32261 }262 263 /// <summary>264 /// This is a wrapper class for RemoteSessionState.265 /// </summary>266 internal class RemoteSessionStateInfo267 {268 #region Constructors269 270 internal RemoteSessionStateInfo(RemoteSessionState state)271 : this(state, null)272 {273 }274 275 internal RemoteSessionStateInfo(RemoteSessionState state, Exception reason)276 {277 State = state;278 Reason = reason;279 }280 281 internal RemoteSessionStateInfo(RemoteSessionStateInfo sessionStateInfo)282 {283 State = sessionStateInfo.State;284 Reason = sessionStateInfo.Reason;285 }286 287 #endregion Constructors288 289 #region Public_Properties290 291 /// <summary>292 /// State of the connection.293 /// </summary>294 internal RemoteSessionState State { get; }295 296 /// <summary>297 /// If the connection is closed, this provides reason why it had happened.298 /// </summary>299 internal Exception Reason { get; }300 301 #endregion Public_Properties302 }303 304 /// <summary>305 /// This is the event arg that contains the state information.306 /// </summary>307 internal class RemoteSessionStateEventArgs : EventArgs308 {309 #region Constructors310 311 internal RemoteSessionStateEventArgs(RemoteSessionStateInfo remoteSessionStateInfo)312 {313 Dbg.Assert(remoteSessionStateInfo != null, "caller should validate the parameter");314 315 if (remoteSessionStateInfo == null)316 {317 PSTraceSource.NewArgumentNullException(nameof(remoteSessionStateInfo));318 }319 320 SessionStateInfo = remoteSessionStateInfo;321 }322 323 #endregion Constructors324 325 #region Public_Properties326 327 /// <summary>328 /// State information about the connection.329 /// </summary>330 public RemoteSessionStateInfo SessionStateInfo { get; }331 332 #endregion Public_Properties333 }334 335 internal class RemoteSessionStateMachineEventArgs : EventArgs336 {337 #region Constructors338 339 internal RemoteSessionStateMachineEventArgs(RemoteSessionEvent stateEvent)340 : this(stateEvent, null)341 {342 }343 344 internal RemoteSessionStateMachineEventArgs(RemoteSessionEvent stateEvent, Exception reason)345 {346 StateEvent = stateEvent;347 Reason = reason;348 }349 350 #endregion Constructors351 352 internal RemoteSessionEvent StateEvent { get; }353 354 internal Exception Reason { get; }355 356 internal RemoteSessionCapability RemoteSessionCapability { get; set; }357 358 internal RemoteDataObject<PSObject> RemoteData { get; set; }359 }360 361 /// <summary>362 /// Defines the various types of remoting behaviour that a cmdlet may363 /// desire when used in a context that supports ambient / automatic remoting.364 /// </summary>365 public enum RemotingCapability366 {367 /// <summary>368 /// In the presence of ambient remoting, this command should369 /// still be run locally.370 /// </summary>371 None,372 373 /// <summary>374 /// In the presence of ambient remoting, this command should375 /// be run on the target computer using PowerShell Remoting.376 /// </summary>377 PowerShell,378 379 /// <summary>380 /// In the presence of ambient remoting, this command supports381 /// its own form of remoting which can be used instead to target382 /// the remote computer.383 /// </summary>384 SupportedByCommand,385 386 /// <summary>387 /// In the presence of ambient remoting, the command assumes388 /// all responsibility for targeting the remote computer;389 /// PowerShell Remoting is not supported.390 /// </summary>391 OwnedByCommand392 }393 394 /// <summary>395 /// Controls or overrides the remoting behavior, during invocation, of a396 /// command that supports ambient remoting.397 /// </summary>398 public enum RemotingBehavior399 {400 /// <summary>401 /// In the presence of ambient remoting, run this command locally.402 /// </summary>403 None,404 405 /// <summary>406 /// In the presence of ambient remoting, run this command on the target407 /// computer using PowerShell Remoting.408 /// </summary>409 PowerShell,410 411 /// <summary>412 /// In the presence of ambient remoting, and a command that declares413 /// 'SupportedByCommand' remoting capability, run this command on the414 /// target computer using the command's custom remoting facilities.415 /// </summary>416 Custom417 }418}419 