/// <inheritdoc />
        public Type Decode(IMessageEnvelope storage)
        {
            storage = Arguments.EnsureNotNull(storage, nameof(storage));

            var header = storage.Headers["AssemblyQualifiedName"];

            string aqn;

            if (header is byte[] headerBytes)
            {
                aqn = Encoding.ASCII.GetString(headerBytes);
            }
            else
            {
                aqn = (string)header;
            }

            if (aqn == null)
            {
                throw new AbbotwareException("Message header is missing Assembly Qualified Name");
            }

            var type = Type.GetType(aqn, false);

            if (type == null)
            {
                throw AbbotwareException.Create("unable to load assembly containing type info:{0}", aqn);
            }

            return(type);
        }
예제 #2
0
        public static ushort?ToUInt16(char[] buffer, int start, int end)
        {
            if (buffer == null)
            {
                return(null);
            }

            if (start == end)
            {
                return(null);
            }

            ushort output  = 0;
            var    basePos = 0;

            for (var j = end; j >= start; --j)
            {
                var digit = buffer[j] - 48;

                if (digit > 9 || digit < 0)
                {
                    throw AbbotwareException.Create("Parse Error:buffer[{0}]={1}", j, buffer[j]);
                }

                output += (ushort)(digit * UInt16MultiplicationTable[basePos]);

                ++basePos;
            }

            return(output);
        }
예제 #3
0
        /// <summary>
        ///     Reboot implementation on UNIX Operating System
        /// </summary>
        public void Reboot()
        {
            var pathToShutdown = Path.Combine(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), "sbin", "shutdown");

            var shutdownArgs = string.Format(CultureInfo.InvariantCulture, "{0} -r now", pathToShutdown);

            var cfg = new ShellCommandOptions("sudo", shutdownArgs);

            using var process = new AbbotwareShellCommand(cfg, NullLogger.Instance);

            var result = process.Execute();

            // If there is data from standard error then the command
            // didn't work because we are not executing as root
            if (!result.StartInfo.Started)
            {
                throw AbbotwareException.Create("Error during shell command {0}", result.ErrorOutput.FirstOrDefault().Message);
            }

            if (!result.Exited)
            {
                throw AbbotwareException.Create("Error during shell command {0}", result.ErrorOutput.FirstOrDefault().Message);
            }

            if (result.ExitCode != 0)
            {
                throw AbbotwareException.Create("Error during shell command {0}", result.ErrorOutput.FirstOrDefault().Message);
            }
        }
예제 #4
0
        /// <inheritdoc />
        public virtual TMessage Decode <TMessage>(IMessageEnvelope storage)
        {
            storage = Arguments.EnsureNotNull(storage, nameof(storage));

            var type = this.TypeEncoder.Decode(storage);

            if (type != null && type != typeof(TMessage))
            {
                throw AbbotwareException.Create($"Message type Mismatch! Message Contains:{type.AssemblyQualifiedName}  Caller Expects:{typeof(TMessage).AssemblyQualifiedName}  maybe you should call the non generic decode, or use a MessageGetter / Cosumer that supports callback's per message type");
            }

            return(this.BinaryEncoder.Decode <TMessage>(storage.Body.ToArray()));
        }
예제 #5
0
파일: Hash.cs 프로젝트: abbotware/abbotware
        /// <summary>
        /// Initializes a new instance of the <see cref="Hash"/> class.
        /// </summary>
        /// <param name="hashType">hash algorithm to use</param>
        /// <param name="data">binary data to hash</param>
        public Hash(HashAlgorithm hashType, byte[] data)
        {
            Arguments.NotNull(data, nameof(data));

            switch (hashType)
            {
            case HashAlgorithm.Sha1:
            {
#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms
                using var sha1 = SHA1.Create();
#pragma warning restore CA5350 // Do Not Use Weak Cryptographic Algorithms

                this.data = sha1.ComputeHash(data);
                break;
            }

            case HashAlgorithm.MD5:
            {
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
                using var md5 = MD5.Create();
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms

                this.data = md5.ComputeHash(data);
                break;
            }

            case HashAlgorithm.OnesCompliment:
            {
                byte val = 0xff;

                foreach (var b in data)
                {
                    val = (byte)(val ^ b);
                }

                this.data = new byte[1]
                {
                    val,
                };

                break;
            }

            default:
            {
                throw AbbotwareException.Create("Unexpected Switch Case:{0} Should not reach this code", hashType);
            }
            }
        }
예제 #6
0
파일: Hash.cs 프로젝트: abbotware/abbotware
        /// <summary>
        /// Initializes a new instance of the <see cref="Hash"/> class.
        /// </summary>
        /// <param name="hashType">hash algorithm to use</param>
        /// <param name="data">binary data to hash</param>
        /// <param name="key">binary key to use in hash algorithm</param>
        public Hash(HashAlgorithmWithKey hashType, byte[] data, byte[] key)
        {
            Arguments.NotNull(data, nameof(data));
            Arguments.NotNull(key, nameof(key));

            switch (hashType)
            {
            case HashAlgorithmWithKey.HmacSha256:
            {
                using var h = HMAC.Create("System.Security.Cryptography.HMACSHA256") !;

                h.Key = key;

                this.data = h.ComputeHash(data);

                break;
            }

            case HashAlgorithmWithKey.HmacOnesCompliment:
            {
                byte val = 0;

                foreach (var b in key)
                {
                    val = (byte)(val ^ b);
                }

                foreach (var b in data)
                {
                    val = (byte)(val ^ b);
                }

                this.data = new byte[1] {
                    val
                };

                break;
            }

            default:
            {
                throw AbbotwareException.Create("Unexpected Switch Case:{0} Should not reach this code", hashType);
            }
            }
        }
예제 #7
0
        /// <summary>
        ///     callback for Kernel32's SetConsoleCtrlHandler
        /// </summary>
        /// <param name="ctrlType">the control event received</param>
        /// <returns>true if the callback handled the event</returns>
        private bool OnConsoleCtrlEvent(ConsoleCtrlType ctrlType)
        {
            switch (ctrlType)
            {
            case ConsoleCtrlType.CtrlCEvent:
            case ConsoleCtrlType.CtrlBreakEvent:
            case ConsoleCtrlType.CloseEvent:
            case ConsoleCtrlType.LogOffEvent:
            case ConsoleCtrlType.ShutdownEvent:
            {
                this.ShutdownSignal.TrySetResult(true);
                return(false);
            }

            default:
            {
                throw AbbotwareException.Create("Unexpected Shutdown Event Recieved: {0} Should not reach this code", ctrlType);
            }
            }
        }
        /// <inheritdoc />
        public Type Decode(IMessageEnvelope storage)
        {
            storage = Arguments.EnsureNotNull(storage, nameof(storage));

            if (string.IsNullOrWhiteSpace(storage.PublishProperties.RoutingKey))
            {
                throw new AbbotwareException("Topic is empty, can't resolve type info");
            }

            var typeName = storage.PublishProperties.RoutingKey;

            var type = Type.GetType(typeName, false);

            if (type == null)
            {
                throw AbbotwareException.Create("unable to load assembly containing type:{0}", typeName);
            }

            return(type);
        }
        public void Exceptions()
        {
            var a1 = new AbbotwareException();

            Assert.IsNotNull(a1);

            var a2 = new AbbotwareException("test");

            Assert.IsNotNull(a2);

            var a3 = AbbotwareException.Create(new Exception(), "test");

            Assert.IsNotNull(a3);

            var a4 = AbbotwareException.Create("test {0}", "test");

            Assert.AreEqual("test test", a4.Message);

            var a5 = AbbotwareException.Create(new Exception(), "test {0}", "test");

            Assert.AreEqual("test test", a5.Message);
        }