コード例 #1
0
ファイル: ProtocolIdTests.cs プロジェクト: tabrath/cs-libp2p
        public void Cast_GivenProtocolId_ReturnsString()
        {
            var pid  = new ProtocolId("/test/pid");
            var pids = (string)pid;

            Assert.That(pids, Is.EqualTo(pid.ToString()));
        }
コード例 #2
0
        public void Equals_GivenTwoEqualIds_ReturnsEquality()
        {
            var pid1 = new ProtocolId("/test/pid");
            var pid2 = new ProtocolId("/test/pid");

            Assert.Equal(pid1, pid2);
        }
コード例 #3
0
        public SMBHeader(Stream s)
        {
            using (var reader = new BinaryReader(s, Encoding.Unicode, leaveOpen: true))
            {
                ProtocolId = reader.ReadBytes(4);
                if (!ProtocolId.SequenceEqual(StandardProcolId))
                {
                    throw new ArgumentException("protocol started with " + BitConverter.ToString(ProtocolId));
                }

                StructureSize   = reader.ReadUInt16();
                CreditCharge    = reader.ReadUInt16();
                ChannelSequence = reader.ReadUInt16();

                reader.ReadUInt16(); //reserved
                Command = (SMB2Command)reader.ReadUInt16();
                CreditRequestResponse = reader.ReadUInt16();
                Flags       = (SMB2Flags)reader.ReadUInt32();
                NextCommand = reader.ReadUInt32();
                MessageId   = reader.ReadUInt64();
                reader.ReadUInt32(); //reserved
                TreeId    = reader.ReadUInt32();
                SessionId = reader.ReadUInt64();
                Signature = reader.ReadBytes(16);
            }
        }
コード例 #4
0
        public static Func <string, bool> SemverMatcher(ProtocolId @base)
        {
            var        parts = @base.ToString().Split('/');
            SemVersion version;

            if (!SemVersion.TryParse(parts.Last(), out version))
            {
                throw new ArgumentException("Not a valid semantic version in protocol id", nameof(@base));
            }

            return(check =>
            {
                var chparts = check.Split('/');
                if (chparts.Length != parts.Length)
                {
                    return false;
                }

                for (var i = 0; i < chparts.Length - 1; i++)
                {
                    if (parts[i] != chparts[i])
                    {
                        return false;
                    }
                }

                SemVersion chvers;
                if (!SemVersion.TryParse(chparts.Last(), out chvers))
                {
                    return false;
                }

                return version.Major == chvers.Major && version.Minor >= chvers.Minor;
            });
        }
コード例 #5
0
ファイル: ProtocolIdTests.cs プロジェクト: tabrath/cs-libp2p
        public void Equals_GivenTwoNotEqualIds_ReturnsNoEquality()
        {
            var pid1 = new ProtocolId("/test/pid1");
            var pid2 = new ProtocolId("/test/pid2");

            Assert.That(pid1, Is.Not.EqualTo(pid2));
        }
コード例 #6
0
        public void Save(Stream s, ProtocolId ptype)
        {
            switch (ptype)
            {
            case ProtocolId.Binary: SaveBin(s); break;

            case ProtocolId.Json: SaveJson(s); break;

            default: throw new NotSupportedException("Unsupported protocol");
            }
        }
コード例 #7
0
        public void SetStreamHandler(ProtocolId pid, StreamHandler handler)
        {
            Mux.AddHandler(pid, (p, rwc) =>
            {
                var ns      = (INetworkStream)rwc;
                ns.Protocol = p;
                handler(ns);

                return(true);
            });
        }
コード例 #8
0
 public void SetStreamHandlerMatch(ProtocolId pid, Func <string, bool> match, StreamHandler handler)
 {
     /*Mux.AddHandler(pid, match, (p, rwc) =>
      * {
      *  var ns = (INetworkStream) rwc;
      *  ns.Protocol = p;
      *  handler(ns);
      *
      *  return true;
      * });*/
 }
コード例 #9
0
 public void Store(IMessage msg, ISymmetricCryptoProvider cryptoProvider, ProtocolId protocol)
 {
     using (var src = new MemoryStream())
     {
         var srcWrap = src.AsUnclosable();
         msg.Save(srcWrap, protocol);
         srcWrap.Seek(0, SeekOrigin.Begin);
         cdataWrap.Seek(0, SeekOrigin.Begin);
         cdataWrap.SetLength(0);
         cryptoProvider.Encrypt(cdataWrap, srcWrap, (int)src.Length);
         iv = cryptoProvider.IV;
     }
 }
コード例 #10
0
        private async Task <INetworkStream> NewStream(PeerId peer, ProtocolId pid, CancellationToken cancellationToken)
        {
            var s = await Network.NewStreamAsync(peer, cancellationToken);

            if (s == null)
            {
                return(null);
            }

            s.Protocol = pid;
            //wrap reporter

            var con = Multistream.CreateSelect(((IReadWriter)s).AsSystemStream(), pid);

            return(new StreamWrapper(s, con.AsReadWriter()));
        }
コード例 #11
0
ファイル: ProtocolHeader.cs プロジェクト: Azure/azure-amqp
        public void Decode(ByteBuffer buffer)
        {
            if (buffer.Length < this.EncodeSize)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, this.EncodeSize, buffer.Length));
            }

            uint prefix = AmqpBitConverter.ReadUInt(buffer);
            if (prefix != ProtocolHeader.AmqpPrefix)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, "ProtocolName" + prefix.ToString("X8"));
            }

            this.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer);

            this.version = new AmqpVersion(
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer));
        }
コード例 #12
0
ファイル: ProtocolHeader.cs プロジェクト: yvgopal/azure-amqp
        public void Decode(ByteBuffer buffer)
        {
            if (buffer.Length < this.EncodeSize)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, this.EncodeSize, buffer.Length));
            }

            uint prefix = AmqpBitConverter.ReadUInt(buffer);

            if (prefix != ProtocolHeader.AmqpPrefix)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, "ProtocolName" + prefix.ToString("X8"));
            }

            this.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer);

            this.version = new AmqpVersion(
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer));
        }
コード例 #13
0
        // ISdmSerializable
        public void Load(Stream s, ProtocolId ptype)
        {
            try
            {
                switch (ptype)
                {
                case ProtocolId.Binary: LoadBin(s); return;

                case ProtocolId.Json: LoadJson(s); return;
                }
            }
            catch (MessageLoadException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new MessageLoadException(e.Message, e);
            }
            throw new NotSupportedException("Unsupported protocol");
        }
コード例 #14
0
        public Server(ServerConfig cfg)
        {
            this.cfg = cfg;
            users    = new UserList();
            users.Load();
            clients      = new SortedList <ClientId, SocketClientBase>();
            newClients   = new ConcurrentQueue <SocketClientBase>();
            delClients   = new ConcurrentQueue <SocketClientBase>();
            iclients     = new List <IClient>();
            roClients    = iclients.AsReadOnly();
            nameToClient = new Dictionary <string, SocketClientBase>();
            ftsContainer = new FileTransferSessionContainer();
            int minBufSize = Math.Min(cfg.SocketReceiveBufferSize, cfg.SocketSendBufferSize);

            MaxBlockSize       = minBufSize / 2;
            svcMessages        = new ConcurrentQueue <IMessage>();
            semAcceptingThread = new SemaphoreSlim(0, 1);
            Protocol           = cfg.Protocol;
            asymCp             = CryptoProviderFactory.Instance.CreateAsymmetric(cfg.AsymAlgorithm);
            asymCp.KeySize     = cfg.AsymAlgorithmKeySize;
            symCp         = CryptoProviderFactory.Instance.CreateSymmetric(cfg.SymAlgorithm);
            symCp.KeySize = cfg.SymAlgorithmKeySize;
            rng           = new RNGCryptoServiceProvider();
        }
コード例 #15
0
 public Result SetProtocol(ProtocolId protocol)
 {
     return(ActiveProtocol.SetProtocol(protocol));
 }
コード例 #16
0
ファイル: ProtocolHeader.cs プロジェクト: yvgopal/azure-amqp
 public ProtocolHeader(ProtocolId id, AmqpVersion version)
 {
     this.protocolId = id;
     this.version    = version;
 }
コード例 #17
0
 public void SetStreamHandlerMatch(ProtocolId pid, Func <string, bool> match, StreamHandler handler) => _host.SetStreamHandlerMatch(pid, match, handler);
コード例 #18
0
 public void RemoveStreamHandler(ProtocolId pid)
 {
     Mux.RemoveHandler((string)pid);
 }
コード例 #19
0
 public void RemoveStreamHandler(ProtocolId pid) => _host.RemoveStreamHandler(pid);
コード例 #20
0
 public void Load(Stream s, ProtocolId ptype)
 {
 }
コード例 #21
0
        private void Load(bool saveDefault)
        {
            IniFile cfg;

            try
            { cfg = new IniFile(ConfigFileName, Encoding.UTF8); }
            catch (FileNotFoundException)
            {
                if (saveDefault)
                {
                    Save();
                }
                return;
            }
            catch (IOException)
            { return; }
            string tmp = null;

            if (cfg.ContainsSection("socket"))
            {
                cfg.TryGetBool("socket", "ipv6", ref UseIPv6);
                cfg.TryGetInt32("socket", "send_timeout", ref SocketSendTimeout);
                cfg.TryGetInt32("socket", "send_buffer_size", ref SocketSendBufferSize);
                cfg.TryGetInt32("socket", "recv_buffer_size", ref SocketReceiveBufferSize);
                cfg.TryGetInt32("socket", "backlog", ref SocketBacklog);
            }
            if (cfg.ContainsSection("server"))
            {
                if (cfg.TryGetString("server", "protocol", ref tmp))
                {
                    ProtocolId p;
                    if (Enum.TryParse(tmp, true, out p))
                    {
                        Protocol = p;
                    }
                }
                if (cfg.TryGetString("server", "address", ref tmp))
                {
                    var af = UseIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
                    try
                    {
                        Address = NetUtil.GetHostByName(tmp, af);
                    }
                    catch { }
                }
                int port = 0;
                if (cfg.TryGetInt32("server", "port", ref port))
                {
                    if (0 <= port && port <= ushort.MaxValue)
                    {
                        Port = (ushort)port;
                    }
                }
            }
            if (cfg.ContainsSection("security"))
            {
                if (cfg.TryGetString("security", "sym_algorithm", ref tmp))
                {
                    SdmSymmetricAlgorithm sa;
                    if (Enum.TryParse(tmp, true, out sa))
                    {
                        SymAlgorithm = sa;
                    }
                }
                if (cfg.TryGetString("security", "asym_algorithm", ref tmp))
                {
                    SdmAsymmetricAlgorithm aa;
                    if (Enum.TryParse(tmp, true, out aa))
                    {
                        AsymAlgorithm = aa;
                    }
                }
                cfg.TryGetInt32("security", "sym_key_size", ref SymAlgorithmKeySize);
                cfg.TryGetInt32("security", "asym_key_size", ref AsymAlgorithmKeySize);
            }
            if (cfg.ContainsSection("misc"))
            {
                cfg.TryGetInt32("misc", "update_sleep", ref UpdateSleep);
            }
        }
コード例 #22
0
 public ProtocolRes(ProtocolId protocolId)
 {
     ProtocolId = protocolId;
 }
コード例 #23
0
 public Protocol(ProtocolId protocolId)
 {
     ProtocolId = protocolId;
 }
コード例 #24
0
 public void SetStreamHandler(ProtocolId pid, StreamHandler handler) => _host.SetStreamHandler(pid, handler);
コード例 #25
0
 public void Save(Stream s, ProtocolId ptype)
 {
 }
コード例 #26
0
ファイル: ProtocolIdTests.cs プロジェクト: tabrath/cs-libp2p
        public void Constructor_GivenProtocol_ReturnsValidProtocolId()
        {
            var pid = new ProtocolId("/test/pid");

            Assert.That(pid.ToString(), Is.EqualTo("/test/pid"));
        }
コード例 #27
0
ファイル: ProtocolHeader.cs プロジェクト: modulexcite/IL2JS
 public ProtocolHeader(ProtocolId id, AmqpVersion version)
 {
     this.protocolId = id;
     this.version = version;
     this.InitializePacket();
 }
コード例 #28
0
 public IMessage Extract(MessageId id, ISymmetricCryptoProvider cryptoProvider, ProtocolId protocol)
 {
     using (var dst = new MemoryStream())
     {
         var dstWrap = dst.AsUnclosable();
         cdataWrap.Seek(0, SeekOrigin.Begin);
         cryptoProvider.IV = iv;
         cryptoProvider.Decrypt(dstWrap, cdataWrap, (int)cdata.Length);
         dstWrap.Seek(0, SeekOrigin.Begin);
         var msg = MessageFactory.CreateMessage(id);
         msg.Load(dstWrap, protocol);
         return(msg);
     }
 }
コード例 #29
0
    public async Task Invoke(HttpContext context)
    {
        ProtocolId protocolId = ProtocolId.None;

        try
        {
            var httpRequest  = context.Request;
            var httpResponse = context.Response;

            XXTea       xxtea = null;
            ProtocolReq req   = null;
            byte        encrypted;
            uint        hash;

            int    length   = (int)httpRequest.ContentLength;
            byte[] inBuffer = new byte[length];

            using (var inStream = httpRequest.Body)
                using (var stream = new MemoryStream(inBuffer))
                    using (var reader = new BinaryReader(stream))
                    {
                        await inStream.CopyToAsync(stream);

                        stream.Position = 0;

                        encrypted = reader.ReadByte();
                        hash      = reader.ReadUInt32();

                        if (encrypted == 2)
                        {
                            xxtea = new XXTea(new uint[] { hash ^ ServerConfig.Key1, hash ^ ServerConfig.Key2, hash ^ ServerConfig.Key3, hash ^ ServerConfig.Key4 });
                        }
                        else if (encrypted == 1)
                        {
                            xxtea      = new XXTea(new uint[] { ServerConfig.Key1, ServerConfig.Key2, ServerConfig.Key3, ServerConfig.Key4 });
                            req        = DecryptAndDeserialize(xxtea, inBuffer, 5, length - 5);
                            protocolId = req.Protocol.ProtocolId;

                            if (protocolId == ProtocolId.Auth)
                            {
                            }
                            else
                            {
                                throw new Exception("encrypted == 1 is allowed to only Protocol Auth");
                            }
                        }
                        else if (encrypted == 0)
                        {
                            req        = Serializer.Deserialize <ProtocolReq>(stream);
                            protocolId = req.Protocol.ProtocolId;

                            if (protocolId == ProtocolId.HandShake)
                            {
                            }
                            else
                            {
                                throw new Exception("encrypted == 0 is allowed to Protocol HandShake");
                            }
                        }
                        else
                        {
                            throw new Exception("Encrypted : Unavaiable Value");
                        }
                    }


            byte[]      data     = null;
            ProtocolRes response = await Service.ProcessAsync(context, req.Suid, hash, req.Protocol);

            if (response.ProtocolId == ProtocolId.Error)
            {
                encrypted = 0;
                xxtea     = null;
            }

            using (var stream = new MemoryStream())
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write(encrypted);

                    if (xxtea != null)
                    {
                        writer.Write(SerializeAndEncrypt(xxtea, response));
                    }
                    else
                    {
                        Serializer.Serialize(stream, response);
                    }

                    data = stream.ToArray();

                    httpResponse.ContentLength = data.Length;
                    using (Stream outStream = httpResponse.Body)
                    {
                        await outStream.WriteAsync(data, 0, data.Length);
                    }
                }
        }
        catch (Exception ex)
        {
        }
    }
コード例 #30
0
ファイル: ProtocolHeader.cs プロジェクト: tralivali1234/IL2JS
 public ProtocolHeader(ProtocolId id, AmqpVersion version)
 {
     this.protocolId = id;
     this.version    = version;
     this.InitializePacket();
 }
コード例 #31
0
ファイル: ProtocolHeader.cs プロジェクト: Azure/azure-amqp
 public ProtocolHeader(ProtocolId id, AmqpVersion version)
 {
     this.protocolId = id;
     this.version = version;
 }
コード例 #32
0
 public Result SetProtocol(ProtocolId pid)
 {
     return(Comm.SetProtocol(pid));
 }
コード例 #33
0
ファイル: ClientConfig.cs プロジェクト: Reaper38/SdMessenger
        private void Load()
        {
            IniFile cfg;

            try
            { cfg = new IniFile(ConfigFileName, Encoding.UTF8); }
            catch (IOException)
            { return; }
            string tmp = null;

            if (cfg.ContainsSection("socket"))
            {
                cfg.TryGetBool("socket", "ipv6", ref UseIPv6);
                cfg.TryGetInt32("socket", "send_timeout", ref SocketSendTimeout);
                cfg.TryGetInt32("socket", "send_buffer_size", ref SocketSendBufferSize);
                cfg.TryGetInt32("socket", "recv_buffer_size", ref SocketReceiveBufferSize);
            }
            if (cfg.ContainsSection("client"))
            {
                if (cfg.TryGetString("client", "protocol", ref tmp))
                {
                    ProtocolId p;
                    if (Enum.TryParse(tmp, true, out p))
                    {
                        Protocol = p;
                    }
                }
                if (cfg.TryGetString("client", "host", ref tmp))
                {
                    Host = tmp;
                }
                int port = 0;
                if (cfg.TryGetInt32("client", "port", ref port))
                {
                    if (0 <= port && port <= ushort.MaxValue)
                    {
                        Port = (ushort)port;
                    }
                }
                if (cfg.TryGetString("client", "login", ref tmp))
                {
                    Login = tmp;
                }
                if (cfg.TryGetString("client", "password", ref tmp) && tmp != "")
                {
                    Password = Decrypt(tmp);
                }
                cfg.TryGetBool("client", "remember", ref Remember);
            }
            if (cfg.ContainsSection("security"))
            {
                if (cfg.TryGetString("security", "sym_algorithm", ref tmp))
                {
                    SdmSymmetricAlgorithm sa;
                    if (Enum.TryParse(tmp, true, out sa))
                    {
                        SymAlgorithm = sa;
                    }
                }
                if (cfg.TryGetString("security", "asym_algorithm", ref tmp))
                {
                    SdmAsymmetricAlgorithm aa;
                    if (Enum.TryParse(tmp, true, out aa))
                    {
                        AsymAlgorithm = aa;
                    }
                }
            }
            if (cfg.ContainsSection("misc"))
            {
                cfg.TryGetInt32("misc", "update_sleep", ref UpdateSleep);
            }
        }