Esempio n. 1
0
        public void Close()
        {
            lock (SendLock)
            {
                if (listener != null)
                {
                    try
                    {
                        listener.Shutdown(SocketShutdown.Both);
                    }
                    finally
                    {
                        listener.Close();
                        listener = null;
                    }
                }

                StopThread();

                SocketList.Clear();
                Packets.Clear();
                ToPeer.Clear();
                SocketToKey.Clear();
                clientcheck.Clear();
                OnListenClient.Clear();

                GetMessage = null;

                RSAkey = new EncryptAndCompress.RSAKeyPair();
            }
        }
Esempio n. 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);
        }
Esempio n. 3
0
        public void Disconnect(int timeout)
        {
            if (socket != null)
            {
                try
                {
                    socket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception) { }
                finally
                {
                    if (timeout == -1)
                    {
                        socket.Close();
                    }
                    else
                    {
                        socket.Close(timeout);
                    }
                    socket = null;
                }
            }

            GetMessage = null;

            Packets.Clear();
            AESkey = null;
            RSAkey = new EncryptAndCompress.RSAKeyPair();
        }
Esempio n. 4
0
        public void Close()
        {
            if (listener != null)
            {
                try
                {
                    listener.Shutdown(SocketShutdown.Both);
                }
                catch (Exception) { }
                finally
                {
                    listener.Close();
                    listener = null;
                }
            }

            SocketList.Clear();
            Packets.Clear();
            ToPeer.Clear();
            SocketToEndPoint.Clear();
            SocketToKey.Clear();

            GetMessage = null;

            RSAkey = new EncryptAndCompress.RSAKeyPair();
        }
Esempio n. 5
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);
        }
Esempio n. 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);
            }
        }