Ejemplo n.º 1
0
        protected override void Encode(IChannelHandlerContext context,
                                       BaseCommand message,
                                       List <object> output)
        {
            // Let us get the string representation of the message sent
            if (string.IsNullOrEmpty(message?.ToString()))
            {
                return;
            }
            var msg = message.ToString().Trim();

            if (!msg.Trim().EndsWith(MessageEndString, StringComparison.Ordinal))
            {
                msg += MessageEndString;
            }
            if (logger.IsDebugEnabled)
            {
                logger.Debug("Encoded message sent [{0}]",
                             msg.Trim());
            }

            output.Add(ByteBufferUtil.EncodeString(context.Allocator,
                                                   msg,
                                                   encoding));
        }
Ejemplo n.º 2
0
        public override Task WriteAsync(IChannelHandlerContext context, object message)
        {
            var textWriter = JsonConvert.SerializeObject(message);

            return(base.WriteAsync(context,
                                   ByteBufferUtil.EncodeString(context.Allocator, textWriter, Encoding.Default)));
        }
Ejemplo n.º 3
0
        protected override void Encode(IChannelHandlerContext context, string message, List <object> output)
        {
            IByteBuffer buffer = ByteBufferUtil.EncodeString(context.Allocator, message, encoding);

            buffer.WriteBytes(lineSeparator);
            output.Add(buffer);
        }
Ejemplo n.º 4
0
        protected internal override void Encode(IChannelHandlerContext context, string message, List <object> output)
        {
            if (0u >= (uint)message.Length)
            {
                return;
            }

            output.Add(ByteBufferUtil.EncodeString(context.Allocator, message, this.encoding));
        }
Ejemplo n.º 5
0
        protected override void Encode(IChannelHandlerContext context, string message, List <object> output)
        {
            if (message.Length == 0)
            {
                return;
            }

            output.Add(ByteBufferUtil.EncodeString(context.Allocator, message, encoding));
        }
Ejemplo n.º 6
0
        protected internal IByteBuffer Encode(IChannelHandlerContext context, IByteBuffer input)
        {
            var bytes = new byte[input.ReadableBytes];

            input.ReadBytes(bytes);

            var base64String = Convert.ToBase64String(bytes);

            return(ByteBufferUtil.EncodeString(context.Allocator, base64String, Encoding.ASCII));
        }
Ejemplo n.º 7
0
 protected override void Encode(IChannelHandlerContext context, string message, List <object> output)
 {
     if (message.Length == 0)
     {
         return;
     }
     // "#" +  length +  "packetid" + message + "!"
     message = "#" + (message.Length + 9).ToString().PadLeft(4, '0') + "999" + message + "!";
     output.Add(ByteBufferUtil.EncodeString(context.Allocator, message, Encoding.UTF8));
 }
Ejemplo n.º 8
0
        protected override void Encode(IChannelHandlerContext ctx, Person person, List <object> output)
        {
            if (person is null)
            {
                return;
            }

            var message = $"{person.Name}|{person.Age}" + Environment.NewLine;

            output.Add(ByteBufferUtil.EncodeString(ctx.Allocator, message, _encoding));
        }
        private static void EncodePasswordAuthRequest(ISocks5PasswordAuthRequest message, IByteBuffer output)
        {
            output.WriteByte(0x01);

            output.WriteByte(message.Username.Length);
            output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, message.Username,
                                                          Encoding.ASCII));

            output.WriteByte(message.Password.Length);
            output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, message.Password,
                                                          Encoding.ASCII));
        }
        public void EncodeAddress(Socks5AddressType addrType, string addrValue, IByteBuffer output)
        {
            var typeVal = addrType.ByteValue;

            if (typeVal == Socks5AddressType.Pv4.ByteValue)
            {
                if (addrValue != null)
                {
                    output.WriteBytes(NetUtil.CreateByteArrayFromIpAddressString(addrValue));
                }
                else
                {
                    output.WriteInt(0);
                }
            }
            else if (typeVal == Socks5AddressType.Domain.ByteValue)
            {
                if (addrValue != null)
                {
                    output.WriteByte(addrValue.Length);
                    output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, addrValue,
                                                                  Encoding.ASCII));
                }
                else
                {
                    output.WriteByte(1);
                    output.WriteByte(0);
                }
            }
            else if (typeVal == Socks5AddressType.Pv6.ByteValue)
            {
                if (addrValue != null)
                {
                    output.WriteBytes(NetUtil.CreateByteArrayFromIpAddressString(addrValue));
                }
                else
                {
                    output.WriteLong(0);
                    output.WriteLong(0);
                }
            }
            else
            {
                throw new EncoderException("unsupported addrType: " + (addrType.ByteValue & 0xFF));
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Decodes the settings header and returns a <see cref="Http2Settings"/> object.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="settingsHeader"></param>
        /// <returns></returns>
        private Http2Settings DecodeSettingsHeader(IChannelHandlerContext ctx, ICharSequence settingsHeader)
        {
            var header = ByteBufferUtil.EncodeString(ctx.Allocator, settingsHeader.ToString(), Encoding.UTF8);

            try
            {
                // Decode the SETTINGS payload.
                var payload = Base64.Decode(header, Base64Dialect.UrlSafe);

                // Create an HTTP/2 frame for the settings.
                var frame = CreateSettingsFrame(ctx, payload);

                // Decode the SETTINGS frame and return the settings object.
                return(DecodeSettings(ctx, frame));
            }
            finally
            {
                _ = header.Release();
            }
        }
Ejemplo n.º 12
0
        public void Encode_Test()
        {
            var r1           = ByteBufferUtil.EncodeString("hellow");
            var sourceBytes1 = Encoding.UTF8.GetBytes("hellow");
            var dest1        = new byte[4];

            Array.Copy(r1, dest1, dest1.Length);
            Assert.Equal(sourceBytes1.Length, BitConverter.ToInt32(dest1));

            var r2 = ByteBufferUtil.DecodeString(r1, 0, out int len);

            Assert.Equal("hellow", r2);

            var short_1      = BitConverter.GetBytes((short)1);
            var int_1        = BitConverter.GetBytes(2);
            var long_1       = BitConverter.GetBytes(3L);
            var combineByte1 = ByteBufferUtil.Combine(short_1, int_1, long_1);

            var startOffset = 0;
            var short_2     = ByteBufferUtil.DecodeShort(combineByte1, startOffset, out startOffset);
            var int_2       = ByteBufferUtil.DecodeInt(combineByte1, startOffset, out startOffset);
            var long_2      = ByteBufferUtil.DecodeLong(combineByte1, startOffset, out startOffset);

            Assert.Equal(1, short_2);
            Assert.Equal(2, int_2);
            Assert.Equal(3, long_2);

            var d1 = new DateTime(2019, 1, 1, 1, 1, 1);
            var b1 = ByteBufferUtil.EncodeDateTime(d1);
            var d2 = ByteBufferUtil.DecodeDateTime(b1, 0, out int n1);

            Assert.Equal(d1, d2);

            var byte1 = new byte[] { 1, 2, 3 };
            var b2    = ByteBufferUtil.EncodeBytes(byte1);
            var byte2 = ByteBufferUtil.DecodeBytes(b2, 0, out int n2);

            Assert.Equal(byte1, byte2);
        }
Ejemplo n.º 13
0
 protected override void Encode(IChannelHandlerContext context, ISocks4CommandRequest message,
                                IByteBuffer output)
 {
     output.WriteByte((int)message.Version());
     output.WriteByte(message.Type.ByteValue);
     output.WriteShort(message.DstPort);
     if (IPAddress.TryParse(message.DstAddr, out _))
     {
         output.WriteBytes(NetUtil.CreateByteArrayFromIpAddressString(message.DstAddr));
         output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, message.UserId,
                                                       Encoding.ASCII));
         output.WriteByte(0);
     }
     else
     {
         output.WriteBytes(Pv4DomainMarker);
         output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, message.UserId,
                                                       Encoding.ASCII));
         output.WriteByte(0);
         output.WriteBytes(ByteBufferUtil.EncodeString(ByteBufferUtil.DefaultAllocator, message.DstAddr,
                                                       Encoding.ASCII));
         output.WriteByte(0);
     }
 }
Ejemplo n.º 14
0
 protected override void Encode(IChannelHandlerContext context, string message, List <object> output)
 {
     output.Add(ByteBufferUtil.EncodeString(context.Allocator, message, Encoding.UTF8));
 }