/// <summary>
        /// DoCheckForReplay method implementation
        /// </summary>
        public bool DoCheckForReplay(NamedPipeReplayRecord requestor)
        {
            try
            {
                if (OnDecrypt == null)
                {
                    OnDecrypt += PipeClientOnDecrypt;
                }
                if (OnEncrypt == null)
                {
                    OnEncrypt += PipeClientOnEncrypt;
                }

                Task <bool> task = null;

                NamedPipeClientStream ClientStream = new NamedPipeClientStream(_servers[0], "adfsmfaconfig", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);
                task = Task <bool> .Factory.StartNew(() =>
                {
                    try
                    {
                        ClientStream.Connect();
                        PipeStreamData ss = new PipeStreamData(ClientStream);
                        ss.WriteString(OnEncrypt(Proofkey));
                        if (OnDecrypt(ss.ReadString()) == Proofkey)
                        {
                            NamedPipeNotificationReplayRecord xdata = new NamedPipeNotificationReplayRecord(requestor);
                            ss.WriteData(ObjectToByteArray(xdata));
                            return(ByteArrayToObject <bool>(ss.ReadData()));
                        }
                    }
                    catch (IOException e)
                    {
                        LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8888);
                        ClientStream.Close();
                    }
                    finally
                    {
                        ClientStream.Close();
                    }
                    return(false);
                });

                task.Wait();
                return(task.Result);
            }
            catch (Exception e)
            {
                LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8888);
                return(false);
            }
        }
        /// <summary>
        /// DoCheckForRemoteReplay method implementation
        /// </summary>
        public bool DoCheckForRemoteReplay(NamedPipeNotificationReplayRecord record)
        {
            try
            {
                if (_servers.Count > 0)
                {
                    if (OnDecrypt == null)
                    {
                        OnDecrypt += PipeClientOnDecrypt;
                    }
                    if (OnEncrypt == null)
                    {
                        OnEncrypt += PipeClientOnEncrypt;
                    }

                    record.MustDispatch = false;

                    Task <bool>[] tasks = new Task <bool> [_servers.Count];
                    for (int i = 0; i <= _servers.Count - 1; i++)
                    {
                        string servername = _servers[i];
                        tasks[i] = Task <bool> .Factory.StartNew((svr) =>
                        {
                            NamedPipeClientStream ClientStream = new NamedPipeClientStream(svr.ToString(), "adfsmfaconfig", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);
                            try
                            {
                                ClientStream.Connect();
                                PipeStreamData ss = new PipeStreamData(ClientStream);
                                ss.WriteString(OnEncrypt(Proofkey));
                                if (OnDecrypt(ss.ReadString()) == Proofkey)
                                {
                                    ss.WriteData(ObjectToByteArray(record));
                                    return(ByteArrayToObject <bool>(ss.ReadData()));
                                }
                            }
                            catch (IOException e)
                            {
                                LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8888);
                                ClientStream.Close();
                            }
                            finally
                            {
                                ClientStream.Close();
                            }
                            return(false);
                        }, servername);
                    }
                    Task.WaitAll(tasks);
                    foreach (Task <bool> ts in tasks)
                    {
                        if (!ts.Result)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8888);
                return(false);
            }
        }
        /// <summary>
        /// PipeServerConfigThread method implmentation
        /// </summary>
        private void PipeServerConfigThread(object data)
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            try
            {
                while (!MustExit)
                {
                    ConfigPipeServer.WaitForConnection();
                    try
                    {
                        PipeStreamData ss = new PipeStreamData(ConfigPipeServer);
                        if (OnDecrypt(ss.ReadString()) == this.Proofkey)
                        {
                            ss.WriteString(this.OnEncrypt(Proofkey));
                            object obj = ByteArrayToObject <object>(ss.ReadData());

                            if (obj is NamedPipeReloadConfigRecord)
                            {
                                NamedPipeReloadConfigRecord encrypted = (NamedPipeReloadConfigRecord)obj;
                                bool b = false;
                                if (OnReloadConfiguration != null)
                                {
                                    b = OnReloadConfiguration(encrypted.Requestor, OnDecrypt(encrypted.Message));
                                    ss.WriteData(ObjectToByteArray <bool>(b));
                                }
                            }
                            else if (obj is NamedPipeServerConfigRecord)
                            {
                                NamedPipeServerConfigRecord encrypted = (NamedPipeServerConfigRecord)obj;
                                NamedPipeRegistryRecord     reg;
                                if (OnRequestServerConfiguration != null)
                                {
                                    reg = OnRequestServerConfiguration(encrypted.Requestor);
                                    ss.WriteData(ObjectToByteArray <NamedPipeRegistryRecord>(reg));
                                }
                            }
                            else if (obj is NamedPipeNotificationReplayRecord)
                            {
                                NamedPipeNotificationReplayRecord encrypted = (NamedPipeNotificationReplayRecord)obj;
                                bool b = false;
                                if (OnCheckForReplay != null)
                                {
                                    b = OnCheckForReplay(encrypted);
                                    if ((b) && (encrypted.MustDispatch))
                                    {
                                        bool c = false;
                                        if (OnCheckForRemoteReplay != null)
                                        {
                                            c = OnCheckForRemoteReplay(encrypted);
                                            ss.WriteData(ObjectToByteArray <bool>(c));
                                        }
                                        else
                                        {
                                            ss.WriteData(ObjectToByteArray <bool>(b));
                                        }
                                    }
                                    else if ((b) && (!encrypted.MustDispatch))
                                    {
                                        ss.WriteData(ObjectToByteArray <bool>(true));
                                    }
                                    else
                                    {
                                        ss.WriteData(ObjectToByteArray <bool>(false));
                                    }
                                }
                            }
                        }
                    }
                    catch (IOException e)
                    {
                        LogForSlots.WriteEntry("PipeServer Error : " + e.Message, EventLogEntryType.Error, 8888);
                        ConfigPipeServer.Close();
                    }
                    finally
                    {
                        ConfigPipeServer.WaitForPipeDrain();
                        ConfigPipeServer.Disconnect();
                    }
                }
            }
            finally
            {
                ConfigPipeServer.Close();
            }
        }
        /// <summary>
        /// DoReloadConfiguration method implementation
        /// </summary>
        public bool DoReloadConfiguration(string requestor, string value)
        {
            try
            {
                if (OnDecrypt == null)
                {
                    OnDecrypt += PipeClientOnDecrypt;
                }
                if (OnEncrypt == null)
                {
                    OnEncrypt += PipeClientOnEncrypt;
                }

                Task <bool>[] taskArray = new Task <bool> [_servers.Count];

                for (int i = 0; i < taskArray.Length; i++)
                {
                    NamedPipeClientStream ClientStream = new NamedPipeClientStream(_servers[i], "adfsmfaconfig", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);

                    taskArray[i] = Task <bool> .Factory.StartNew(() =>
                    {
                        try
                        {
                            ClientStream.Connect();
                            PipeStreamData ss = new PipeStreamData(ClientStream);
                            ss.WriteString(OnEncrypt(Proofkey));
                            if (OnDecrypt(ss.ReadString()) == Proofkey)
                            {
                                NamedPipeReloadConfigRecord xdata = new NamedPipeReloadConfigRecord(requestor, OnEncrypt(value));
                                ss.WriteData(ObjectToByteArray(xdata));
                                return(ByteArrayToObject <bool>(ss.ReadData()));
                            }
                        }
                        catch (IOException e)
                        {
                            LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8888);
                            ClientStream.Close();
                        }
                        finally
                        {
                            ClientStream.Close();
                        }
                        return(false);
                    });
                }
                Task.WaitAll(taskArray);
                for (int i = 0; i < taskArray.Length; i++)
                {
                    Task <bool> tsk = taskArray[i];
                    if (tsk == null)
                    {
                        return(false);
                    }
                    if (!tsk.Result)
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8888);
                return(false);
            }
            return(true);
        }
Example #5
0
        /// <summary>
        /// DoRequestServerConfiguration method implementation
        /// </summary>
        public NamedPipeRegistryRecord DoRequestServerConfiguration(string requestor)
        {
            try
            {
                if (OnDecrypt == null)
                {
                    OnDecrypt += PipeClientOnDecrypt;
                }
                if (OnEncrypt == null)
                {
                    OnEncrypt += PipeClientOnEncrypt;
                }
                if (OnDecryptBytes == null)
                {
                    OnDecryptBytes += PipeClientOnDecryptBytes;
                }
                if (OnEncryptBytes == null)
                {
                    OnEncryptBytes += PipeClientOnEncryptBytes;
                }

                string servername = _servers[0];
                Task <NamedPipeRegistryRecord> task = Task <NamedPipeRegistryRecord> .Factory.StartNew((svr) =>
                {
                    NamedPipeClientStream ClientStream = new NamedPipeClientStream(svr.ToString(), "adfsmfaconfig", PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);
                    try
                    {
                        ClientStream.Connect(10000);
                        PipeStreamData ss = new PipeStreamData(ClientStream);
                        ss.WriteString(OnEncrypt(Proofkey));
                        if (OnDecrypt(ss.ReadString()) == Proofkey)
                        {
                            NamedPipeServerConfigRecord xdata = new NamedPipeServerConfigRecord(requestor);
                            ss.WriteData(OnEncryptBytes(ObjectToByteArray(xdata)));
                            return(ByteArrayToObject <NamedPipeRegistryRecord>(OnDecryptBytes(ss.ReadData())));
                        }
                    }
                    catch (IOException e)
                    {
                        LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8870);
                    }
                    catch (TimeoutException e)
                    {
                        LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8871);
                    }
                    finally
                    {
                        ClientStream.Close();
                    }
                    return(default(NamedPipeRegistryRecord));
                }, servername);

                task.Wait();
                return(task.Result);
            }
            catch (Exception e)
            {
                LogForSlots.WriteEntry("PipeClient Error : " + e.Message, EventLogEntryType.Error, 8889);
                return(default(NamedPipeRegistryRecord));
            }
        }