internal override void RaiseDataReceivedEvent(RemoteDataEventArgs dataArg) { if (dataArg == null) { throw PSTraceSource.NewArgumentNullException("dataArg"); } RemoteDataObject <PSObject> receivedData = dataArg.ReceivedData; RemotingTargetInterface targetInterface = receivedData.TargetInterface; RemotingDataType dataType = receivedData.DataType; switch (dataType) { case RemotingDataType.SessionCapability: { RemoteSessionCapability remoteSessionCapability = null; try { remoteSessionCapability = RemotingDecoder.GetSessionCapability(receivedData.Data); } catch (PSRemotingDataStructureException exception2) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNotFoundCapabilityProperties, new object[] { exception2.Message, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion }); } RemoteSessionStateMachineEventArgs fsmEventArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived) { RemoteSessionCapability = remoteSessionCapability }; this._stateMachine.RaiseEvent(fsmEventArg); if (this.NegotiationReceived != null) { RemoteSessionNegotiationEventArgs eventArgs = new RemoteSessionNegotiationEventArgs(remoteSessionCapability) { RemoteData = receivedData }; this.NegotiationReceived.SafeInvoke <RemoteSessionNegotiationEventArgs>(this, eventArgs); } return; } case RemotingDataType.CloseSession: { PSRemotingDataStructureException reason = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientRequestedToCloseSession); RemoteSessionStateMachineEventArgs args = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reason); this._stateMachine.RaiseEvent(args); return; } case RemotingDataType.CreateRunspacePool: this.CreateRunspacePoolReceived.SafeInvoke <RemoteDataEventArgs>(this, dataArg); return; case RemotingDataType.PublicKey: { string publicKey = RemotingDecoder.GetPublicKey(receivedData.Data); this.PublicKeyReceived.SafeInvoke <RemoteDataEventArgs <string> >(this, new RemoteDataEventArgs <string>(publicKey)); return; } } throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, new object[] { dataType }); }
private void ProcessSessionMessages(RemoteDataEventArgs arg) { using (ClientRemoteSessionDSHandlerImpl._trace.TraceMethod()) { RemoteDataObject <PSObject> remoteDataObject = arg != null && arg.ReceivedData != null ? arg.ReceivedData : throw ClientRemoteSessionDSHandlerImpl._trace.NewArgumentNullException(nameof(arg)); int targetInterface = (int)remoteDataObject.TargetInterface; RemotingDataType dataType = remoteDataObject.DataType; switch (dataType) { case RemotingDataType.SessionCapability: RemoteSessionCapability sessionCapability; try { sessionCapability = RemotingDecoder.GetSessionCapability((object)remoteDataObject.Data); } catch (PSRemotingDataStructureException ex) { throw new PSRemotingDataStructureException(PSRemotingErrorId.ClientNotFoundCapabilityProperties, new object[3] { (object)ex.Message, (object)PSVersionInfo.BuildVersion, (object)RemotingConstants.ProtocolVersion }); } this._stateMachine.RaiseEvent(new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived) { RemoteSessionCapability = sessionCapability }); if (this.NegotiationReceived == null) { break; } this.NegotiationReceived((object)this, new RemoteSessionNegotiationEventArgs(sessionCapability)); break; case RemotingDataType.CloseSession: this._stateMachine.RaiseEvent(new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, (Exception) new PSRemotingDataStructureException(PSRemotingErrorId.ServerRequestedToCloseSession, new object[0]))); break; case RemotingDataType.EncryptedSessionKey: this.EncryptedSessionKeyReceived((object)this, new RemoteDataEventArgs <string>((object)RemotingDecoder.GetEncryptedSessionKey(remoteDataObject.Data))); break; case RemotingDataType.PublicKeyRequest: this.PublicKeyRequestReceived((object)this, new RemoteDataEventArgs <string>((object)string.Empty)); break; default: throw new PSRemotingDataStructureException(PSRemotingErrorId.ReceivedUnsupportedAction, new object[1] { (object)dataType }); } } }
private void ProcessSessionMessages(RemoteDataEventArgs arg) { if ((arg == null) || (arg.ReceivedData == null)) { throw PSTraceSource.NewArgumentNullException("arg"); } RemoteDataObject <PSObject> receivedData = arg.ReceivedData; RemotingTargetInterface targetInterface = receivedData.TargetInterface; RemotingDataType dataType = receivedData.DataType; switch (dataType) { case RemotingDataType.SessionCapability: { RemoteSessionCapability remoteSessionCapability = null; try { remoteSessionCapability = RemotingDecoder.GetSessionCapability(receivedData.Data); } catch (PSRemotingDataStructureException exception2) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNotFoundCapabilityProperties, new object[] { exception2.Message, PSVersionInfo.BuildVersion, RemotingConstants.ProtocolVersion }); } RemoteSessionStateMachineEventArgs args2 = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived) { RemoteSessionCapability = remoteSessionCapability }; this._stateMachine.RaiseEvent(args2, false); RemoteSessionNegotiationEventArgs eventArgs = new RemoteSessionNegotiationEventArgs(remoteSessionCapability); this.NegotiationReceived.SafeInvoke <RemoteSessionNegotiationEventArgs>(this, eventArgs); return; } case RemotingDataType.CloseSession: { PSRemotingDataStructureException reason = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerRequestedToCloseSession); RemoteSessionStateMachineEventArgs args = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reason); this._stateMachine.RaiseEvent(args, false); return; } case RemotingDataType.EncryptedSessionKey: { string encryptedSessionKey = RemotingDecoder.GetEncryptedSessionKey(receivedData.Data); this.EncryptedSessionKeyReceived.SafeInvoke <RemoteDataEventArgs <string> >(this, new RemoteDataEventArgs <string>(encryptedSessionKey)); return; } case RemotingDataType.PublicKeyRequest: this.PublicKeyRequestReceived.SafeInvoke <RemoteDataEventArgs <string> >(this, new RemoteDataEventArgs <string>(string.Empty)); return; } throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, new object[] { dataType }); }
// TODO: If this is not used remove this // internal override event EventHandler<RemoteDataEventArgs> DataReceived; /// <summary> /// This processes the object received from transport which are /// targeted for session /// </summary> /// <param name="arg"> /// argument contains the data object /// </param> private void ProcessSessionMessages(RemoteDataEventArgs arg) { if (arg == null || arg.ReceivedData == null) { throw PSTraceSource.NewArgumentNullException("arg"); } RemoteDataObject <PSObject> rcvdData = arg.ReceivedData; RemotingTargetInterface targetInterface = rcvdData.TargetInterface; Dbg.Assert(targetInterface == RemotingTargetInterface.Session, "targetInterface must be Session"); RemotingDataType dataType = rcvdData.DataType; switch (dataType) { case RemotingDataType.CloseSession: PSRemotingDataStructureException reasonOfClose = new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerRequestedToCloseSession); RemoteSessionStateMachineEventArgs closeSessionArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reasonOfClose); _stateMachine.RaiseEvent(closeSessionArg); break; case RemotingDataType.SessionCapability: RemoteSessionCapability capability = null; try { capability = RemotingDecoder.GetSessionCapability(rcvdData.Data); } catch (PSRemotingDataStructureException dse) { // this will happen if expected properties are not // received for session capability throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientNotFoundCapabilityProperties, dse.Message, PSVersionInfo.GitCommitId, RemotingConstants.ProtocolVersion); } RemoteSessionStateMachineEventArgs capabilityArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived); capabilityArg.RemoteSessionCapability = capability; _stateMachine.RaiseEvent(capabilityArg); RemoteSessionNegotiationEventArgs negotiationArg = new RemoteSessionNegotiationEventArgs(capability); NegotiationReceived.SafeInvoke(this, negotiationArg); break; case RemotingDataType.EncryptedSessionKey: { String encryptedSessionKey = RemotingDecoder.GetEncryptedSessionKey(rcvdData.Data); EncryptedSessionKeyReceived.SafeInvoke(this, new RemoteDataEventArgs <string>(encryptedSessionKey)); } break; case RemotingDataType.PublicKeyRequest: { PublicKeyRequestReceived.SafeInvoke(this, new RemoteDataEventArgs <string>(String.Empty)); } break; default: { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, dataType); } } }
/// <summary> /// This method is used by the input queue dispatching mechanism. /// It examines the data and takes appropriate actions. /// </summary> /// <param name="dataArg"> /// The received client data. /// </param> /// <exception cref="ArgumentNullException"> /// If the parameter is null. /// </exception> internal override void RaiseDataReceivedEvent(RemoteDataEventArgs dataArg) { if (dataArg == null) { throw PSTraceSource.NewArgumentNullException("dataArg"); } RemoteDataObject <PSObject> rcvdData = dataArg.ReceivedData; RemotingTargetInterface targetInterface = rcvdData.TargetInterface; RemotingDataType dataType = rcvdData.DataType; Dbg.Assert(targetInterface == RemotingTargetInterface.Session, "targetInterface must be Session"); switch (dataType) { case RemotingDataType.CreateRunspacePool: { // At this point, the negotiation is complete, so // need to import the clients public key CreateRunspacePoolReceived.SafeInvoke(this, dataArg); } break; case RemotingDataType.CloseSession: PSRemotingDataStructureException reasonOfClose = new PSRemotingDataStructureException(RemotingErrorIdStrings.ClientRequestedToCloseSession); RemoteSessionStateMachineEventArgs closeSessionArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.Close, reasonOfClose); _stateMachine.RaiseEvent(closeSessionArg); break; case RemotingDataType.SessionCapability: RemoteSessionCapability capability = null; try { capability = RemotingDecoder.GetSessionCapability(rcvdData.Data); } catch (PSRemotingDataStructureException dse) { // this will happen if expected properties are not // received for session capability throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerNotFoundCapabilityProperties, dse.Message, PSVersionInfo.GitCommitId, RemotingConstants.ProtocolVersion); } RemoteSessionStateMachineEventArgs capabilityArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationReceived); capabilityArg.RemoteSessionCapability = capability; _stateMachine.RaiseEvent(capabilityArg); if (NegotiationReceived != null) { RemoteSessionNegotiationEventArgs negotiationArg = new RemoteSessionNegotiationEventArgs(capability); negotiationArg.RemoteData = rcvdData; NegotiationReceived.SafeInvoke(this, negotiationArg); } break; case RemotingDataType.PublicKey: { string remotePublicKey = RemotingDecoder.GetPublicKey(rcvdData.Data); PublicKeyReceived.SafeInvoke(this, new RemoteDataEventArgs <string>(remotePublicKey)); } break; default: throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ReceivedUnsupportedAction, dataType); } }
internal void ExecuteConnect(byte[] connectData, out byte[] connectResponseData) { RemoteSessionCapability sessionCapability; connectResponseData = null; Fragmentor fragmentor = new Fragmentor(0x7fffffff, null); Fragmentor defragmentor = fragmentor; int length = connectData.Length; if (length < 0x15) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } FragmentedRemoteObject.GetFragmentId(connectData, 0); bool isStartFragment = FragmentedRemoteObject.GetIsStartFragment(connectData, 0); bool isEndFragment = FragmentedRemoteObject.GetIsEndFragment(connectData, 0); int blobLength = FragmentedRemoteObject.GetBlobLength(connectData, 0); if (blobLength > (length - 0x15)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if (!isStartFragment || !isEndFragment) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } RemoteSessionState state = this.SessionDataStructureHandler.StateMachine.State; if ((state != RemoteSessionState.Established) && (state != RemoteSessionState.EstablishedAndKeyExchanged)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnServerStateValidation); } MemoryStream serializedDataStream = new MemoryStream(); serializedDataStream.Write(connectData, 0x15, blobLength); serializedDataStream.Seek(0L, SeekOrigin.Begin); RemoteDataObject <PSObject> obj2 = RemoteDataObject <PSObject> .CreateFrom(serializedDataStream, defragmentor); if (obj2 == null) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if ((obj2.Destination != (RemotingDestination.InvalidDestination | RemotingDestination.Server)) || (obj2.DataType != RemotingDataType.SessionCapability)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } int num3 = (length - 0x15) - blobLength; if (num3 < 0x15) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } byte[] destinationArray = new byte[num3]; Array.Copy(connectData, 0x15 + blobLength, destinationArray, 0, num3); FragmentedRemoteObject.GetFragmentId(destinationArray, 0); isStartFragment = FragmentedRemoteObject.GetIsStartFragment(destinationArray, 0); isEndFragment = FragmentedRemoteObject.GetIsEndFragment(destinationArray, 0); blobLength = FragmentedRemoteObject.GetBlobLength(destinationArray, 0); if (blobLength != (num3 - 0x15)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if (!isStartFragment || !isEndFragment) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } serializedDataStream = new MemoryStream(); serializedDataStream.Write(destinationArray, 0x15, blobLength); serializedDataStream.Seek(0L, SeekOrigin.Begin); RemoteDataObject <PSObject> obj3 = RemoteDataObject <PSObject> .CreateFrom(serializedDataStream, defragmentor); if (obj3 == null) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnServerStateValidation); } if ((obj3.Destination != (RemotingDestination.InvalidDestination | RemotingDestination.Server)) || (obj3.DataType != RemotingDataType.ConnectRunspacePool)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } try { sessionCapability = RemotingDecoder.GetSessionCapability(obj2.Data); } catch (PSRemotingDataStructureException) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } try { this.RunServerNegotiationAlgorithm(sessionCapability, true); } catch (PSRemotingDataStructureException exception) { throw exception; } int minRunspaces = -1; int maxRunspaces = -1; bool flag3 = false; if ((obj3.Data.Properties["MinRunspaces"] != null) && (obj3.Data.Properties["MinRunspaces"] != null)) { try { minRunspaces = RemotingDecoder.GetMinRunspaces(obj3.Data); maxRunspaces = RemotingDecoder.GetMaxRunspaces(obj3.Data); flag3 = true; } catch (PSRemotingDataStructureException) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } } if (flag3 && (((minRunspaces == -1) || (maxRunspaces == -1)) || (minRunspaces > maxRunspaces))) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if (this._runspacePoolDriver == null) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnServerStateValidation); } if (obj3.RunspacePoolId != this._runspacePoolDriver.InstanceId) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnInputValidation); } if ((flag3 && (this._runspacePoolDriver.RunspacePool.GetMaxRunspaces() != maxRunspaces)) && (this._runspacePoolDriver.RunspacePool.GetMinRunspaces() != minRunspaces)) { throw new PSRemotingDataStructureException(RemotingErrorIdStrings.ServerConnectFailedOnMismatchedRunspacePoolProperties); } RemoteDataObject obj4 = RemotingEncoder.GenerateServerSessionCapability(this._context.ServerCapability, this._runspacePoolDriver.InstanceId); RemoteDataObject obj5 = RemotingEncoder.GenerateRunspacePoolInitData(this._runspacePoolDriver.InstanceId, this._runspacePoolDriver.RunspacePool.GetMaxRunspaces(), this._runspacePoolDriver.RunspacePool.GetMinRunspaces()); SerializedDataStream streamToWriteTo = new SerializedDataStream(0x1000); streamToWriteTo.Enter(); obj4.Serialize(streamToWriteTo, fragmentor); streamToWriteTo.Exit(); streamToWriteTo.Enter(); obj5.Serialize(streamToWriteTo, fragmentor); streamToWriteTo.Exit(); byte[] buffer2 = streamToWriteTo.Read(); streamToWriteTo.Dispose(); connectResponseData = buffer2; ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object s) { RemoteSessionStateMachineEventArgs fsmEventArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.ConnectSession); this._sessionDSHandler.StateMachine.RaiseEvent(fsmEventArg); })); this._runspacePoolDriver.DataStructureHandler.ProcessConnect(); }