Beispiel #1
0
        /// <summary>
        ///   Starts the session.
        /// </summary>
        /// <param name="clientId"> The client id. </param>
        /// <param name="sessionId"> The session id. </param>
        /// <param name="serverPipe"> The server pipe. </param>
        /// <param name="responsePipe"> The response pipe. </param>
        /// <remarks>
        /// </remarks>
        public static void Start(string clientId, string sessionId, NamedPipeServerStream serverPipe, NamedPipeServerStream responsePipe)
        {
            var isElevated = false;
            var userId     = string.Empty;

            serverPipe.RunAsClient(() => {
                var identity = WindowsIdentity.GetCurrent();
                if (identity != null)
                {
                    userId     = identity.Name;
                    isElevated = AdminPrivilege.IsProcessElevated();
                }
            });

            var existingSessions = (from session in ActiveSessions
                                    where session._clientId == clientId && session._sessionId == sessionId && isElevated == session._isElevated && session._userId == userId
                                    select session).ToList();

            if (existingSessions.Any())
            {
                if (existingSessions.Count() > 1)
                {
                    // multiple matching sessions? This isn't good. Shut em all down to be safe
                    foreach (var each in existingSessions)
                    {
                        each.End();
                    }
                }
                else
                {
                    var session = existingSessions.FirstOrDefault();
                    if (session != null)
                    {
                        // found just one session.
                        session._serverPipe   = serverPipe;
                        session._responsePipe = responsePipe;
                        Logger.Message("Rejoining existing session...");
                        // session.SendSessionStarted(sessionId);
                        session.Connected = true;
                        session.SendQueuedMessages();
                    }
                    return;
                }
            }
            else
            {
                // if the exact session isn't there, find any that are partial matches, and shut em down.
                foreach (
                    var each in (from session in ActiveSessions where session._clientId == clientId && session._sessionId == sessionId select session).ToList()
                    )
                {
                    each.End();
                }
            }
            // no viable matching session.
            // Let's start a new one.
            Add(new Session(clientId, sessionId, serverPipe, responsePipe, userId, isElevated));
            Logger.Message("Starting new session...");
        }