Exemplo n.º 1
0
 internal RemoteSessionStateInfo(RemoteSessionState state, Exception reason)
 {
     State = state;
     Reason = reason;
 }
 internal abstract void SendNegotiationAsync(RemoteSessionState sessionState);
Exemplo n.º 3
0
 internal RemoteSessionStateInfo(RemoteSessionState state)
     : this(state, null)
 {
 }
Exemplo n.º 4
0
 internal RemoteSessionStateInfo(RemoteSessionStateInfo sessionStateInfo)
 {
     this._state = sessionStateInfo.State;
     this._reason = sessionStateInfo.Reason;
 }
Exemplo n.º 5
0
 internal RemoteSessionStateInfo(RemoteSessionState state, Exception reason)
 {
     this._state = state;
     this._reason = reason;
 }
 internal override void SendNegotiationAsync(RemoteSessionState sessionState)
 {
     RemoteSessionStateMachineEventArgs arg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationSendCompleted);
     this._stateMachine.RaiseEvent(arg, false);
     if (sessionState == RemoteSessionState.NegotiationSending)
     {
         this._transportManager.CreateAsync();
     }
     else if (sessionState == RemoteSessionState.NegotiationSendingOnConnect)
     {
         this._transportManager.ConnectCompleted += new EventHandler<EventArgs>(this.HandleConnectComplete);
         this._transportManager.ConnectAsync();
     }
 }
Exemplo n.º 7
0
        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();
        }
Exemplo n.º 8
0
 internal RemoteSessionStateInfo(RemoteSessionState state)
     : this(state, null)
 {
 }
Exemplo n.º 9
0
 internal RemoteSessionStateInfo(RemoteSessionState state, Exception reason)
 {
     State  = state;
     Reason = reason;
 }
Exemplo n.º 10
0
        public bool SetConnectionState(Guid connectionId, string IPAddress, Guid VMGuid, RemoteSessionState state)
        {
            var connection = connections[connectionId] as Connection;

            if (connection != null)
            {
                Trace.TraceInformation("SetConnectionState: {0}, ip={1}, vm={2}, state={3}", connectionId, IPAddress, VMGuid != Guid.Empty ? VMGuid.ToString() : string.Empty, state);
                connection.State = state;
                return(true);
            }

            Trace.TraceInformation("invalid connection: {0}", connectionId);
            return(false);
        }
Exemplo n.º 11
0
 internal abstract void SendNegotiationAsync(RemoteSessionState sessionState);
Exemplo n.º 12
0
        public bool SetConnectionState(Guid connectionId, string IPAddress, Guid vmGuid, RemoteSessionState state)
        {
            // don't resend an unchanged connection state
            var send = true;

            lock (connectionsLock)
            {
                if (!connectionsState.ContainsKey(connectionId))
                {
                    connectionsState.Add(connectionId, state);
                }
                else
                {
                    send = connectionsState[connectionId] != state;
                    connectionsState[connectionId] = state;
                }
            }

            if (!send)
            {
                return(true);
            }

            var restRequest  = new RestRequest($"SetConnectionState?connectionId={connectionId}&IPAddress={IPAddress}&vmGuid={vmGuid}&state={state}", Method.GET);
            var restResponse = restClient.Execute(restRequest);

            if (restResponse.ResponseStatus != ResponseStatus.Completed)
            {
                throw new Exception(string.Format("Failed to call SetConnectionState; response status: {0}, code: {1}, message: {2}", restResponse.ResponseStatus, restResponse.StatusCode, restResponse.ErrorMessage));
            }
            return(JsonConvert.DeserializeObject <bool>(restResponse.Content));
        }
Exemplo n.º 13
0
 internal RemoteSessionStateInfo(RemoteSessionState state, Exception reason)
 {
     this._state  = state;
     this._reason = reason;
 }
Exemplo n.º 14
0
 internal RemoteSessionStateInfo(RemoteSessionState state)
     : this(state, (Exception)null)
 {
 }
        /// <summary>
        /// Sets the state of the state machine. Since only 
        /// one thread can be manipulating the state at a time
        /// the state is not synchronized
        /// </summary>
        /// <param name="newState">new state of the state machine</param>
        /// <param name="reason">reason why the state machine is set
        /// to the new state</param>
        private void SetState(RemoteSessionState newState, Exception reason)
        {
            RemoteSessionState oldState = _state;

            if (newState != oldState)
            {
                _state = newState;
                s_trace.WriteLine("state machine state transition: from state {0} to state {1}", oldState, _state);

                RemoteSessionStateInfo stateInfo = new RemoteSessionStateInfo(_state, reason);
                RemoteSessionStateEventArgs sessionStateEventArg = new RemoteSessionStateEventArgs(stateInfo);

                _clientRemoteSessionStateChangeQueue.Enqueue(sessionStateEventArg);
            }
        }
        /// <summary>
        /// Sends the negotiation package asynchronously
        /// </summary>
        internal override void SendNegotiationAsync(RemoteSessionState sessionState)
        {
            // This state change is made before the call to CreateAsync to ensure the state machine
            // is prepared for a NegotiationReceived response.  Otherwise a race condition can 
            // occur when the transport NegotiationReceived arrives too soon, breaking the session.
            // This race condition was observed for OutOfProc transport when reusing the OutOfProc process.
            //this will change StateMachine to NegotiationSent.
            RemoteSessionStateMachineEventArgs negotiationSendCompletedArg =
                new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.NegotiationSendCompleted);
            _stateMachine.RaiseEvent(negotiationSendCompletedArg);

            if (sessionState == RemoteSessionState.NegotiationSending)
            {
                _transportManager.CreateAsync();
            }
            else if (sessionState == RemoteSessionState.NegotiationSendingOnConnect)
            {
                _transportManager.ConnectCompleted += HandleConnectComplete;
                _transportManager.ConnectAsync();
            }
            else
            {
                Dbg.Assert(false, "SendNegotiationAsync called in unexpected session state");
            }
        }
 private void SetState(RemoteSessionState newState, Exception reason)
 {
     RemoteSessionState state = this._state;
     if (newState != state)
     {
         this._state = newState;
         _trace.WriteLine("state machine state transition: from state {0} to state {1}", new object[] { state, this._state });
         RemoteSessionStateInfo remoteSessionStateInfo = new RemoteSessionStateInfo(this._state, reason);
         RemoteSessionStateEventArgs item = new RemoteSessionStateEventArgs(remoteSessionStateInfo);
         this._clientRemoteSessionStateChangeQueue.Enqueue(item);
     }
 }
        private void SetState(RemoteSessionState newState, Exception reasion)
        {
            RemoteSessionState state = this._state;
            if (newState != state)
            {
				if (newState == RemoteSessionState.ClosingConnection || state == RemoteSessionState.ClosingConnection)
				{
					_trace.WriteLine ("Closing Server Session");
				}
                this._state = newState;
                _trace.WriteLine("state machine state transition: from state {0} to state {1}", new object[] { state, this._state });
            }
        }
 /// <summary>
 /// Set the FSM state to a new state.
 /// </summary>
 /// <param name="newState">
 /// The new state.
 /// </param>
 /// <param name="reasion">
 /// Optional parameter that can provide additional information. This is currently not used.
 /// </param>
 private void SetState(RemoteSessionState newState, Exception reasion)
 {
     RemoteSessionState oldState = _state;
     if (newState != oldState)
     {
         _state = newState;
         s_trace.WriteLine("state machine state transition: from state {0} to state {1}", oldState, _state);
     }
     // TODO: else should we close the session here?
 }
Exemplo n.º 20
0
        public bool SetConnectionState(Guid connectionId, string IPAddress, Guid vmGuid, RemoteSessionState state)
        {
            if (string.IsNullOrWhiteSpace(IPAddress) && vmGuid == Guid.Empty)
            {
                throw new ArgumentException("ipAddress and VmGuid cannot both be null");
            }

            if (!Enum.IsDefined(typeof(RemoteSessionState), state))
            {
                throw new ArgumentException(nameof(state));
            }

            return(_connectionService.SetConnectionState(connectionId, IPAddress, vmGuid, state));
        }