public void Cast_GivenProtocolId_ReturnsString() { var pid = new ProtocolId("/test/pid"); var pids = (string)pid; Assert.That(pids, Is.EqualTo(pid.ToString())); }
public void Equals_GivenTwoEqualIds_ReturnsEquality() { var pid1 = new ProtocolId("/test/pid"); var pid2 = new ProtocolId("/test/pid"); Assert.Equal(pid1, pid2); }
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); } }
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; }); }
public void Equals_GivenTwoNotEqualIds_ReturnsNoEquality() { var pid1 = new ProtocolId("/test/pid1"); var pid2 = new ProtocolId("/test/pid2"); Assert.That(pid1, Is.Not.EqualTo(pid2)); }
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"); } }
public void SetStreamHandler(ProtocolId pid, StreamHandler handler) { Mux.AddHandler(pid, (p, rwc) => { var ns = (INetworkStream)rwc; ns.Protocol = p; handler(ns); return(true); }); }
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; * });*/ }
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; } }
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())); }
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)); }
// 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"); }
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(); }
public Result SetProtocol(ProtocolId protocol) { return(ActiveProtocol.SetProtocol(protocol)); }
public ProtocolHeader(ProtocolId id, AmqpVersion version) { this.protocolId = id; this.version = version; }
public void SetStreamHandlerMatch(ProtocolId pid, Func <string, bool> match, StreamHandler handler) => _host.SetStreamHandlerMatch(pid, match, handler);
public void RemoveStreamHandler(ProtocolId pid) { Mux.RemoveHandler((string)pid); }
public void RemoveStreamHandler(ProtocolId pid) => _host.RemoveStreamHandler(pid);
public void Load(Stream s, ProtocolId ptype) { }
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); } }
public ProtocolRes(ProtocolId protocolId) { ProtocolId = protocolId; }
public Protocol(ProtocolId protocolId) { ProtocolId = protocolId; }
public void SetStreamHandler(ProtocolId pid, StreamHandler handler) => _host.SetStreamHandler(pid, handler);
public void Save(Stream s, ProtocolId ptype) { }
public void Constructor_GivenProtocol_ReturnsValidProtocolId() { var pid = new ProtocolId("/test/pid"); Assert.That(pid.ToString(), Is.EqualTo("/test/pid")); }
public ProtocolHeader(ProtocolId id, AmqpVersion version) { this.protocolId = id; this.version = version; this.InitializePacket(); }
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); } }
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) { } }
public Result SetProtocol(ProtocolId pid) { return(Comm.SetProtocol(pid)); }
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); } }