示例#1
0
        public NetClient(Rijndael128 crypto, IPAddress target, short port, OnMessageRecieved handler, OnClientConnect onConn, int bufSize = 16384)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            if (target.AddressFamily == AddressFamily.InterNetwork && target.Address == 16777343)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                IPAddress addr = Dns.GetHostEntry(Dns.GetHostName()).GetIPV4();
                if (addr != null)
                {
                    target = addr;
                }
            }
            this.target = target;
            Crypto      = crypto;
            if (crypto != null)
            {
                CBC = new PCBC(crypto, rp);
            }
            this.bufSize = bufSize;
            this.handler = handler;
            this.onConn  = onConn;
            Port         = port;
            ServerSide   = false;
        }
示例#2
0
        protected internal bool SyncListener(ref bool cryptoEstablished, ref int mLen, out bool acceptedData, Queue <byte> ibuf, byte[] buffer)
        {
            if (cryptoEstablished)
            {
                lock (messageBuffer)
                {
                    foreach (byte[] message in messageBuffer)
                    {
                        Connection.Send(NetSupport.WithHeader(message));
                    }
                    messageBuffer.Clear();
                }
            }
            if (acceptedData = Connection.Available > 0)
            {
                int read = Connection.Receive(buffer);
                ibuf.EnqueueAll(buffer, 0, read);
            }
            if (mLen == 0 && ibuf.Count >= 4)
            {
                mLen = Support.ReadInt(ibuf.Dequeue(4), 0);
            }
            if (mLen != 0 && ibuf.Count >= mLen)
            {
                // Got a full message. Parse!
                byte[] message = ibuf.Dequeue(mLen);

                if (!cryptoEstablished)
                {
                    if (ServerSide)
                    {
                        try
                        {
                            if (Crypto == null)
                            {
                                Crypto = Rijndael128.Deserialize(decrypt.Decrypt(message), out int _);
                            }
                            else
                            {
                                CBC = new PCBC(Crypto, decrypt.Decrypt(message));
                            }
                        }
                        catch (Exception) {
                            Console.WriteLine("A fatal error ocurrd when attempting to establish a secure channel! Stopping...");
                            Thread.Sleep(5000);
                            Environment.Exit(-1);
                        }
                    }
                    else
                    {
                        // Reconstruct RSA object from remote public keys and use it to encrypt our serialized AES key/iv
                        RSA asymm = RSA.Deserialize(message, out int _);
                        Connection.Send(NetSupport.WithHeader(asymm.Encrypt(Crypto.Serialize())));
                        Connection.Send(NetSupport.WithHeader(asymm.Encrypt(CBC.IV)));
                    }
                    if (CBC != null)
                    {
                        cryptoEstablished = true;
                        onConn(this);
                    }
                }
                else
                {
                    // Decrypt the incoming message
                    byte[] read = Crypto.Decrypt(message);

                    // Read the decrypted message length
                    int mlenInner = Support.ReadInt(read, 0);

                    // Send the message to the handler and get a response
                    string response = handler(read.SubArray(4, 4 + mlenInner).ToUTF8String(), out bool live);

                    // Send the response (if given one) and drop the connection if the handler tells us to
                    if (response != null)
                    {
                        Connection.Send(NetSupport.WithHeader(Crypto.Encrypt(NetSupport.WithHeader(response.ToUTF8Bytes()))));
                    }
                    if (!live)
                    {
                        Running = false;
                        try
                        {
                            Connection.Close();
                        }
                        catch (Exception) { }
                        return(true);
                    }
                }

                // Reset expexted message length
                mLen = 0;
            }
            return(false);
        }