示例#1
0
 /// <summary>
 /// transform everything to SendData
 /// </summary>
 /// <param name="b">binary</param>
 /// <param name="index">transform from</param>
 /// <param name="length">transform length</param>
 /// <param name="key">decrypt key</param>
 public void ByteToAll(byte[] b, int index, out int length, string key)
 {
     if (b.Length <= index)
     {
         length = 0;
         return;
     }
     byte[] a     = EncryptAndCompress.Decompress(b, index, out length);
     byte[] bytes = EncryptAndCompress.UnLock(a, key);
     try
     {
         object[] datas = (object[])new ChuonBinary(bytes).ToObject();
         Code         = (byte)datas[0];
         Parameters   = datas[1];
         ReturnCode   = (short)datas[2];
         DebugMessage = (string)datas[3];
     }
     catch (System.Exception e)
     {
         SendData g = new SendData(0, new Dictionary <byte, object> {
             { 0, "錯誤" }
         }, 0, e.ToString());
         CopyIn((SendData)g);
     }
 }
示例#2
0
        public bool CreateServer(IPAddress ip, int listenPort, out string a)
        {
            IPEndPoint ipe = new IPEndPoint(ip, listenPort);

            try
            {
                RSAkey   = EncryptAndCompress.GenerateRSAKeys(2048);
                listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
                listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                if ((byte)Environment.OSVersion.Platform >= 0 && (byte)Environment.OSVersion.Platform <= 3)
                {
                    listener.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);
                }
                listener.Bind(ipe);
                EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0);
                listener.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, ref remoteEP, Receive, listener);
                StartThread();

                a = ListenerIP.ToString() + " 成功建立伺服器";
            }
            catch (SocketException)
            {
                RSAkey = new EncryptAndCompress.RSAKeyPair();
                a      = ipe.ToString() + "無法建立伺服器";
                return(false);
            }
            return(true);
        }
示例#3
0
        public bool CreateServer(IPAddress ip, int listenPort, out string a)
        {
            IPEndPoint ipe = new IPEndPoint(ip, listenPort);

            try
            {
                RSAkey   = EncryptAndCompress.GenerateRSAKeys(2048);
                listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
                listener.Bind(ipe);
                listener.Listen(MaxConnections);
                listener.BeginAccept(ListenClient, listener);
                a = ipe.ToString() + " 成功建立伺服器";
            }
            catch (SocketException)
            {
                RSAkey = new EncryptAndCompress.RSAKeyPair();
                a      = ipe.ToString() + "無法建立伺服器";
                return(false);
            }
            return(true);
        }
示例#4
0
 /// <summary>
 /// transform everything to binary
 /// </summary>
 /// <param name="key">encrypt key</param>
 /// <param name="_Lock">encrypt type</param>
 /// <returns>binary</returns>
 public byte[] AllToByte(string key, EncryptAndCompress.LockType _Lock = EncryptAndCompress.LockType.None)
 {
     object[] datas = new object[] { Code, Parameters, ReturnCode, DebugMessage };
     return(EncryptAndCompress.Lock(new ChuonBinary(datas).ToArray(), key, _Lock));
 }
示例#5
0
 public void WriteSendDataByte(byte[] response, string key, EncryptAndCompress.LockType _Lock)
 {
     byte[] bs = EncryptAndCompress.Lock(response, key, _Lock);
     writer.Write(bs);
 }
示例#6
0
        private void ConnectionCallback(System.IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;

            try
            {
                socket.EndConnect(ar);

                #region Set def var
                int defReadTimeout = socket.ReceiveTimeout;
                #endregion

                #region Set Read func
                SendData onRead(PacketType checkType, string key, Func <SendData, bool> datacheck)
                {
                    SendData ReadData = new SendData();

                    try
                    {
                        socket.ReceiveTimeout = 1000;

                        byte[] data = new byte[Packet.header_length];
                        if (socket.Receive(data) <= 0)
                        {
                            PushPacket(PacketType.CONNECTION_ATTEMPT_FAILED, "CONNECTION Shutdown");
                            Disconnect();
                            return(new SendData());
                        }

                        socket.ReceiveTimeout = defReadTimeout;

                        int len = BitConverter.ToInt32(data, 0);
                        data = ReadSocketData(len, socket);
                        using (Packet packet = new Packet(socket, data, null, true))
                        {
                            if (packet.BeginRead() != checkType)
                            {
                                PushPacket(PacketType.CONNECTION_ATTEMPT_FAILED, "bad Receive");
                                Disconnect();
                                return(new SendData());
                            }
                            ReadData = packet.ReadSendData(key);
                        }
                    }
                    catch (Exception e)
                    {
                        PushPacket(PacketType.CONNECTION_ATTEMPT_FAILED, e.ToString());
                        Disconnect();
                        return(new SendData());
                    }
                    if (!datacheck(ReadData))
                    {
                        PushPacket(PacketType.CONNECTION_ATTEMPT_FAILED, "bad Receive");
                        Disconnect();
                        return(new SendData());
                    }
                    return(ReadData);
                }
                #endregion

                #region Set Send func
                void onSend(PacketType sendType, string key, EncryptAndCompress.LockType lockType, SendData send)
                {
                    using (Packet packet = new Packet(socket))
                    {
                        packet.BeginWrite(sendType);
                        packet.WriteSendData(send, key, lockType);
                        Send(packet);
                    }
                }
                #endregion

                #region Start Get Public Key
                SendData sendData = onRead(PacketType.RSAKEY, "", (a) => true);
                if (sendData == new SendData())
                {
                    return;
                }

                RSAkey = new EncryptAndCompress.RSAKeyPair((byte[])sendData.Parameters);
                #endregion

                #region Generate And Send AES Key
                AESkey = EncryptAndCompress.GenerateAESKey();
                onSend(PacketType.AESKEY, RSAkey.PublicKey, EncryptAndCompress.LockType.RSA, new SendData(0, AESkey));
                #endregion

                #region Check AES Key
                sendData = onRead(PacketType.AESKEY, AESkey, (a) => a.Parameters.ToString() == "Connect check");
                if (sendData == new SendData())
                {
                    return;
                }
                #endregion

                #region Send CONNECT_SUCCESSFUL
                onSend(PacketType.CONNECT_SUCCESSFUL, AESkey, EncryptAndCompress.LockType.AES, new SendData(0, "Connect successful"));
                #endregion

                #region On CONNECT
                sendData = onRead(PacketType.CONNECT_SUCCESSFUL, AESkey, (a) => a.Parameters.ToString() == "On Connect");
                if (sendData == new SendData())
                {
                    return;
                }

                PushPacket(PacketType.CONNECT_SUCCESSFUL, "");


                byte[] bytes = new byte[Packet.header_length];
                socket.BeginReceive(bytes, 0, Packet.header_length, SocketFlags.None, Receive, new object[] { bytes });

                #endregion
            }
            catch (Exception e)
            {
                PushPacket(PacketType.CONNECTION_ATTEMPT_FAILED, e.ToString());
                DebugMessage(e.ToString());
                //Disconnect(0);
            }
        }