Exemple #1
0
        /// <summary>
        /// Initialize a new SSPServer
        /// </summary>
        /// <param name="serverProperties">The properties for the server</param>
        public SSPServer(ServerProperties serverProperties)
        {
            if (serverProperties == null)
            {
                throw new ArgumentNullException("serverProperties");
            }

            this.serverProperties = serverProperties;
            this.Clients          = new SortedList <decimal, SSPClient>();
            this.randomDecimal    = new RandomDecimal(DateTime.Now.Millisecond);

            SysLogger.Log("Starting server", SysLogType.Debug);

            this.ClientAcceptProcessor4 = new ClientAcceptProcessor();
            this.ClientAcceptProcessor6 = new ClientAcceptProcessor();


            //start the server for IPv4
            this.TcpServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.TcpServer.Bind(new IPEndPoint(IPAddress.Parse(serverProperties.ListenIp), serverProperties.ListenPort));
            this.TcpServer.Listen(100);
            this.TcpServer.BeginAccept(AcceptClientCallback, null);

            if (serverProperties.UseIPv4AndIPv6)
            {
                //start the server for IPv6
                this.TcpServer6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                this.TcpServer6.Bind(new IPEndPoint(IPAddress.Parse(serverProperties.ListenIp6), serverProperties.ListenPort));
                this.TcpServer6.Listen(100);
                this.TcpServer6.BeginAccept(AcceptClient6Callback, null);
            }

            SysLogger.Log("Started server", SysLogType.Debug);
        }
        public void Dispose()
        {
            try
            {
                Handle.Shutdown(SocketShutdown.Both);
                Handle.Close();
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);
            }

            this.Connection = null;
            this.Properties = null;
            this.Handle     = null;
            this.Server     = null;
            this.clientHS   = null;
            this.serverHS   = null;

            if (this.KeepAliveTimer != null)
            {
                this.KeepAliveTimer.Enabled = false;
            }

            this.IsDisposed = true;

            try
            {
                onDisconnect(DisconnectReason.UserDisconnection);
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);
            }
        }
Exemple #3
0
        internal bool serverHS_onFindKeyInDatabase(string EncryptedHash, ref byte[] Key, ref byte[] Salt, ref byte[] PublicKey, ref string Username)
        {
            lock (FindKeyLock)
            {
                try
                {
                    User.UserDbInfo user = onFindUser(EncryptedHash);

                    if (user == null)
                    {
                        return(false);
                    }

                    Key       = user.Key.getBytes();
                    Salt      = user.PrivateSalt.getBytes();
                    PublicKey = user.PublicKey;
                    Username  = user.UsernameStr;
                    return(true);
                }
                catch (Exception ex)
                {
                    SysLogger.Log(ex.Message, SysLogType.Error);
                    return(false);
                }
            }
        }
        public override MazeErrorCode onReceiveData(byte[] Data, ref byte[] ResponseData)
        {
            ResponseData = new byte[0];
            switch (base.Step)
            {
            case 1:
            {
                if (Data.Length != 32)
                {
                    SysLogger.Log("[MazeHandShake][Server] Receive Length missmatch", SysLogType.Debug);
                    return(MazeErrorCode.Error);
                }

                wopEx = base.GetWopEncryption();
                wopEx.Decrypt(Data, 0, Data.Length);

                BigInteger server_prime = new BigInteger(Data);
                if (server_prime.isProbablePrime())
                {
                    //verify the prime from the server
                    BigInteger server_Prime_test = BigInteger.genPseudoPrime(256, 50, new Random(BitConverter.ToInt32(wopEx.Key, 0)));

                    if (server_prime != server_Prime_test)
                    {
                        //Attacker detected ?
                        SysLogger.Log("[MazeHandShake][Server] Man-In-The-Middle detected", SysLogType.Debug);
                        return(MazeErrorCode.Error);
                    }

                    //successful
                    //generate another prime and send it back
                    BigInteger client_Prime = BigInteger.genPseudoPrime(256, 50, new Random(server_prime.IntValue()));

                    byte[] primeData = client_Prime.getBytes();
                    wopEx.Encrypt(primeData, 0, primeData.Length);
                    ResponseData = primeData;

                    BigInteger key = base.ModKey(server_prime, client_Prime);
                    //apply key to encryption
                    ApplyKey(wopEx, key);

                    base.FinalKey  = wopEx.Key;
                    base.FinalSalt = wopEx.Salt;

                    Step++;
                    return(MazeErrorCode.Finished);
                }
                else
                {
                    //connection failed, using old keys ?
                    SysLogger.Log("[MazeHandShake][Server] Invalid received data", SysLogType.Debug);
                    return(MazeErrorCode.Error);
                }
            }
            }

            return(MazeErrorCode.Success);
        }
Exemple #5
0
        public SSPClient ProcessClient(SSPServer Server, Socket TcpServer, IAsyncResult result)
        {
            try
            {
                Socket    AcceptSocket = TcpServer.EndAccept(result); //<- can throw a error
                SSPClient client       = Server.GetNewClient();
                client.Handle = AcceptSocket;

                if (AcceptSocket.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    client.RemoteIp = ((IPEndPoint)AcceptSocket.RemoteEndPoint).Address.ToString();
                }
                else
                {
                    client.RemoteIp = AcceptSocket.RemoteEndPoint.ToString().Split(':')[0];
                }


                client.Server     = Server;
                client.Connection = new Network.Connection(client);
                client.ClientId   = Server.randomDecimal.NextDecimal();

                client.serverHS = new ServerMaze(Server.serverProperties.Handshake_Maze_Size, Server.serverProperties.Handshake_MazeCount, Server.serverProperties.Handshake_StepSize);
                client.serverHS.onFindKeyInDatabase += Server.serverHS_onFindKeyInDatabase;

                SysLogger.Log("Accepted peer " + client.RemoteIp, SysLogType.Debug);

                lock (Server.Clients)
                {
                    while (Server.Clients.ContainsKey(client.ClientId))
                    {
                        client.ClientId = Server.randomDecimal.NextDecimal();
                    }
                    Server.Clients.Add(client.ClientId, client);
                }

                try
                {
                    client.onBeforeConnect();
                }
                catch (Exception ex)
                {
                    SysLogger.Log(ex.Message, SysLogType.Error);
                    client.onException(ex, ErrorType.UserLand);
                }

                client.Connection.StartReceiver();
                return(client);
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);
                return(null);
            }
        }
Exemple #6
0
        public Stream RecalculatePrivateKey(Stream PrivateKeyData)
        {
            if (!PrivateKeyData.CanSeek || !PrivateKeyData.CanWrite)
            {
                throw new Exception("Unable to write to the stream");
            }

            BigInteger InversedInt = 0;

            try
            {
                InversedInt = PrivateSalt.modInverse(this.Username);
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);

                //no inverse could be found
                InversedInt = PrivateSalt + this.Username;
            }

            PatchKey(ref InversedInt); //patch the key to randomize the 0xFF bytes
            byte[] inverseData = InversedInt.getBytes();

            int temp = InversedInt.IntValue();

            for (int j = 0; j <= 1; j++)
            {
                for (int i = 4 * j; i < PrivateKeyData.Length; i += 8)
                {
                    byte[] tempData = new byte[4];
                    int    read     = 0;

                    PrivateKeyData.Position = i;
                    if ((read = PrivateKeyData.Read(tempData, 0, tempData.Length)) <= 0)
                    {
                        break;
                    }

                    int TempKey = BitConverter.ToInt32(tempData, 0) ^ temp;

                    PrivateKeyData.Position -= read;
                    PrivateKeyData.Write(BitConverter.GetBytes(TempKey), 0, read);

                    temp = TempKey;
                }
            }
            return(PrivateKeyData);
        }
Exemple #7
0
        public override void ProcessPayload(SSPClient client, OperationalSocket OpSocket)
        {
            RequestHeader reqHeader = Header as RequestHeader;

            if (reqHeader != null)
            {
                lock (client.Connection.OperationalSockets)
                {
                    if (!client.Connection.OperationalSockets.TryGetValue(ConnectionId, out OpSocket))
                    {
                        return;
                    }
                }


                if (!OpSocket.isConnected)
                {
                    return;
                }

                OpSocket.InternalSendMessage(new MsgOpDisconnectResponse(ConnectionId), new RequestHeader(reqHeader.RequestId, true));
                OpSocket.isConnected = false;

                lock (client.Connection.OperationalSockets)
                {
                    if (client.Connection.OperationalSockets.ContainsKey(OpSocket.ConnectionId))
                    {
                        client.Connection.OperationalSockets.Remove(OpSocket.ConnectionId);
                    }
                }

                try
                {
                    OpSocket.onDisconnect(DisconnectReason.UserDisconnection);
                    client.onOperationalSocket_Disconnected(OpSocket, DisconnectReason.UserDisconnection);
                }
                catch (Exception ex)
                {
                    SysLogger.Log(ex.Message, SysLogType.Error);
                }
            }
        }
Exemple #8
0
        /// Override OnPaint method
        protected override void OnPaint(PaintEventArgs e)
        {
            try
            {
                CalcChartHeight();
                base.OnPaint(e);

                // Enable AntiAliasing, if needed
                if (perfChartStyle.AntiAliasing)
                {
                    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                }

                DrawBackgroundAndGrid(e.Graphics);
                DrawChart(e.Graphics);
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);
            }
        }
        /// <summary>
        /// Disconnect the virtual connection
        /// </summary>
        public void Disconnect()
        {
            if (!isConnected)
            {
                return;
            }

            int        RequestId = 0;
            SyncObject syncObj   = Client.Connection.RegisterRequest(ref RequestId);

            Client.Connection.SendMessage(new MsgOpDisconnect(this.ConnectionId), new RequestHeader(RequestId, false));

            MsgOpDisconnectResponse response = syncObj.Wait <MsgOpDisconnectResponse>(null, 5000);

            if (response == null)
            {
                throw new Exception("A time-out occured");
            }

            isConnected = false;

            lock (Client.Connection.OperationalSockets)
            {
                if (Client.Connection.OperationalSockets.ContainsKey(ConnectionId))
                {
                    Client.Connection.OperationalSockets.Remove(ConnectionId);
                }
            }

            try
            {
                onDisconnect(DisconnectReason.UserDisconnection);
                Client.onOperationalSocket_Disconnected(this, DisconnectReason.UserDisconnection);
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);
            }
        }
Exemple #10
0
        public override void ProcessPayload(SSPClient client, OperationalSocket OpSocket)
        {
            SSPClient _client = client as SSPClient;

            if (_client != null)
            {
                byte[]        responseData  = new byte[0];
                MazeErrorCode errorCode     = MazeErrorCode.Error;
                Mazing        mazeHandshake = _client.IsServerSided ? _client.serverHS : _client.clientHS;

                if (mazeHandshake == null)
                {
                    //error could occur on a unexpected disconnect
                    client.Connection.HandShakeCompleted = false;
                    client.Connection.HandshakeSync.Pulse();
                    return;
                }

                errorCode = mazeHandshake.onReceiveData(Data, ref responseData);


                if (errorCode != MazeErrorCode.Finished && errorCode != MazeErrorCode.Success && client.TimingConfiguration.Enable_Timing)
                {
                    //something went wrong, annoy the attacker
                    Thread.Sleep(client.TimingConfiguration.Authentication_WrongPassword);
                }

                if (responseData.Length > 0)
                {
                    client.Connection.SendMessage(new MsgHandshake(responseData), new SystemHeader());
                }

                if (client == null || client.Connection == null || client.Connection.HandshakeSync == null)
                {
                    //error could occur on a unexpected disconnect
                    return;
                }

                client.Connection.HandshakeSync.Value = errorCode;
                if (errorCode != MazeErrorCode.Finished && errorCode != MazeErrorCode.Success)
                {
                    client.Connection.HandshakeSync.Pulse();
                }
                else if (errorCode == MazeErrorCode.Finished)
                {
                    //let's tell it's completed and apply the new key
                    client.Connection.ApplyNewKey(mazeHandshake, mazeHandshake.FinalKey, mazeHandshake.FinalSalt);

                    if (_client.IsServerSided)
                    {
                        if (mazeHandshake as ServerMaze != null)
                        {
                            client.Username = (mazeHandshake as ServerMaze).Username;
                        }

                        client.Connection.HandShakeCompleted = true;

                        /*try
                         * {
                         *  client.onBeforeConnect();
                         * }
                         * catch (Exception ex)
                         * {
                         *  SysLogger.Log(ex.Message, SysLogType.Error);
                         *  client.onException(ex, ErrorType.UserLand);
                         *  return; //don't send that we're ready since we're clearly not at this point
                         * }*/

                        client.Connection.SendMessage(new MsgInitOk(), new SystemHeader());

                        try
                        {
                            client.onConnect();
                        }
                        catch (Exception ex)
                        {
                            SysLogger.Log(ex.Message, SysLogType.Error);
                            client.onException(ex, ErrorType.UserLand);
                            return; //don't send that we're ready since we're clearly not at this point
                        }
                    }
                    else
                    {
                        client.Connection.HandShakeCompleted = true;
                        client.Connection.HandshakeSync.Pulse();
                    }
                }
            }
        }
        public override MazeErrorCode onReceiveData(byte[] Data, ref byte[] ResponseData)
        {
            ResponseData = new byte[0];

            if (LastErrorCode != MazeErrorCode.Success)
            {
                //don't continue if the client/server messed something up
                return(LastErrorCode);
            }

            switch (base.Step)
            {
            case 1:
            {
                //step 2
                if (Data.Length != Mazing.ByteCode.Length)
                {
                    SysLogger.Log("[MazeHandShake][Server] ByteCode Length Missmatch", SysLogType.Debug);
                    return(MazeErrorCode.WrongByteCode);
                }

                for (int i = 0; i < Mazing.ByteCode.Length; i++)
                {
                    if (Mazing.ByteCode[i] != Data[i])
                    {
                        SysLogger.Log("[MazeHandShake][Server] WrongByteCode from client", SysLogType.Debug);
                        return(MazeErrorCode.WrongByteCode);
                    }
                }
                Step++;
                break;
            }

            case 2:
            {
                if (onFindKeyInDatabase == null)     //programmer error
                {
                    SysLogger.Log("[MazeHandShake][Server] onFindKeyInDatabase is null", SysLogType.Debug);
                    ResponseData = GetFailResponseData();     //not encrypted, client knows this will fail
                    return(MazeErrorCode.Error);
                }

                string EncHashedMsg = BitConverter.ToString(SHA512Managed.Create().ComputeHash(Data, 0, Data.Length)).Replace("-", "");
                byte[] _key         = new byte[0];
                byte[] _salt        = new byte[0];
                byte[] _publicKey   = new byte[0];
                string _userName    = "";

                if (onFindKeyInDatabase(EncHashedMsg, ref _key, ref _salt, ref _publicKey, ref _userName))
                {
                    this.PublicKeyData = TrimArray(_publicKey, Mazing.MAX_KEY_SIZE);
                    this.wopEx         = base.GetWopEncryption(_key, _salt);

                    base.FinalKey  = _key;
                    base.FinalSalt = _salt;

                    //let's try to decrypt the data, should go successful
                    wopEx.Decrypt(Data, 0, Data.Length);

                    if (Data.Length != _publicKey.Length)
                    {
                        SysLogger.Log("[MazeHandShake][Server] Public key length missmatch", SysLogType.Debug);
                        //key size not the same... strange
                        ResponseData = GetFailResponseData();
                        return(MazeErrorCode.Error);
                    }

                    for (int i = 0; i < _publicKey.Length; i++)
                    {
                        if (Data[i] != _publicKey[i])
                        {
                            SysLogger.Log("[MazeHandShake][Server] Public key missmatch", SysLogType.Debug);
                            //public key did not match... strange
                            ResponseData = GetFailResponseData();
                            return(MazeErrorCode.Error);
                        }
                    }

                    //encryption / public key went successful for now
                    this.server_Prime = BigInteger.genPseudoPrime(256, 50, new Random(BitConverter.ToInt32(_key, 0)));
                    byte[] primeData = server_Prime.getBytes();
                    wopEx.Encrypt(primeData, 0, primeData.Length);
                    ResponseData = primeData;

                    this.Username = _userName;

                    Step++;
                }
                else
                {
                    SysLogger.Log("[MazeHandShake][Server] No user key found in database", SysLogType.Debug);
                    ResponseData = GetFailResponseData();
                    return(MazeErrorCode.UserKeyNotFound);
                }
                break;
            }

            case 3:
            {
                //response back from client with his prime number
                wopEx.Decrypt(Data, 0, Data.Length);

                this.client_Prime = new BigInteger(Data);
                if (this.client_Prime.isProbablePrime())
                {
                    //verify the prime from the client
                    BigInteger client_Prime_test = BigInteger.genPseudoPrime(256, 50, new Random(this.server_Prime.IntValue()));

                    if (this.client_Prime != client_Prime_test)
                    {
                        //Attacker detected ?
                        SysLogger.Log("[MazeHandShake][Server] Man-In-The-Middle detected", SysLogType.Debug);
                        return(MazeErrorCode.Error);
                    }

                    BigInteger key = base.ModKey(server_Prime, client_Prime);
                    //apply key to encryption
                    ApplyKey(wopEx, key);
                    return(MazeErrorCode.Finished);
                }
                else
                {
                    SysLogger.Log("[MazeHandShake][Server] Invalid response", SysLogType.Debug);
                    return(MazeErrorCode.Error);
                }
            }
            }
            return(MazeErrorCode.Success);
        }
Exemple #12
0
        public ActionResult LeerBalanza(string id)
        {
            try
            {
                var balanza = _db.Dispositivos.SingleOrDefault(x => x.Puesto == id);

                if (balanza == null)
                {
                    _logger.Log(string.IsNullOrEmpty(id) ? "" : id, "Balanza no encontrada", null);

                    return(Json(new LecturaBalanza
                    {
                        Text = "E",
                        Error = true,
                        ErrorText = string.Format("Balanza '{0}' no encontrada", id)
                    }, JsonRequestBehavior.AllowGet));
                }

                var lectura = _deviceQuery.Query(balanza);

                if (lectura == null)
                {
                    _logger.Log(id, "No se pudo realizar la lectura", balanza.Id);

                    return(Json(new LecturaBalanza
                    {
                        Text = "E",
                        Error = true,
                        ErrorText = "No se pudo realizar la lectura"
                    }, JsonRequestBehavior.AllowGet));
                }

                decimal peso;

                if (decimal.TryParse(lectura.Text, out peso))
                {
                    _db.Lecturas.Add(new Lectura
                    {
                        DatoDespachado = peso.ToString(),
                        DatoLeido      = lectura.Text,
                        DispositivoId  = balanza.Id,
                        TimePrint      = DateTime.Now
                    });

                    _db.SaveChanges();

                    return(Json(new LecturaBalanza
                    {
                        Text = lectura.Text,
                        Error = false,
                        ErrorText = "OK",
                        Value = peso
                    }, JsonRequestBehavior.AllowGet));
                }

                _logger.Log(lectura.Text, "No se pudo convertir la lectura a un valor decimal", balanza.Id);

                return(Json(new LecturaBalanza
                {
                    Text = "E",
                    Error = true,
                    ErrorText = "No se pudo convertir la lectura a un valor decimal"
                }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                _logger.Log(string.IsNullOrEmpty(id) ? "" : id,
                            e.ToString().Length > 999 ? e.ToString().Substring(0, 999) : e.ToString(),
                            null);

                return(Json(new LecturaBalanza
                {
                    Text = "E",
                    Error = true,
                    ErrorText = e.ToString()
                }, JsonRequestBehavior.AllowGet));
            }
        }
        /// <summary>
        /// Send data to the established connection
        /// </summary>
        /// <param name="Message">The data to send</param>
        /// <param name="Header">The Header to use for adding additional information</param>
        /// <param name="feature">The Feature that has been used for this Message</param>
        /// <param name="OpSocket">The OperationalSocket that has been used for this Message</param>
        internal int SendMessage(IMessage Message, Header Header, Feature feature = null, OperationalSocket OpSocket = null)
        {
            lock (SendLock)
            {
                if (!Connected)
                {
                    return(-1);
                }

                if (Message == null)
                {
                    throw new ArgumentException("Message cannot be null");
                }
                if (Header == null)
                {
                    throw new ArgumentException("Header cannot be null");
                }

                ushort HeaderId = OpSocket != null?OpSocket.Headers.GetHeaderId(Header) : Headers.GetHeaderId(Header);

                byte[] SerializedHeader = Header.Serialize(Header);

                uint messageId = OpSocket != null?OpSocket.MessageHandler.GetMessageId(Message.GetType()) : messageHandler.GetMessageId(Message.GetType());

                if (SerializedHeader.Length >= MAX_PACKET_SIZE)
                {
                    throw new ArgumentException("Header length cannot be greater then " + MAX_PAYLOAD);
                }

                using (MemoryStream outStream = new MemoryStream())
                    using (PayloadWriter pw = new PayloadWriter(outStream))
                    {
                        pw.WriteBytes(new byte[HEADER_SIZE], 0, HEADER_SIZE); //reserve space

                        pw.WriteBytes(SerializedHeader);
                        pw.WriteUInteger(messageId);

                        int packetSize = messageHandler.EncryptMessage(this, Message, outStream);

                        if (pw.Length > MAX_PACKET_SIZE)
                        {
                            throw new OverflowException("Message size cannot be greater then " + MAX_PACKET_SIZE);
                        }

                        int  PayloadLength = pw.Length - Connection.HEADER_SIZE;
                        byte CurPacketId   = 0;
                        int  FeatureId     = feature != null?feature.GetFeatureId() : -1;

                        ushort ConnectionId = OpSocket != null ? OpSocket.ConnectionId : (ushort)0;

                        byte checksum = 0;
                        checksum += (byte)PayloadLength;
                        checksum += CurPacketId;
                        checksum += (byte)ConnectionId;
                        checksum += (byte)HeaderId;
                        checksum += (byte)FeatureId;

                        pw.Position = 0;
                        pw.WriteThreeByteInteger(PayloadLength); //length
                        pw.WriteByte(CurPacketId);               //cur packet id
                        pw.WriteUShort(ConnectionId);            //Connection Id
                        pw.WriteUShort(HeaderId);                //Header Id
                        pw.WriteByte(checksum);
                        pw.WriteInteger(FeatureId);

                        //encrypt the header
                        lock (HeaderEncryption)
                        {
                            HeaderEncryption.Encrypt(pw.GetBuffer(), 0, HEADER_SIZE);

                            byte[] temp = pw.GetBuffer();
                            headerConfuser.Obfuscate(ref temp, 0);
                        }

                        int SendNum = 0;

                        try
                        {
                            for (int i = 0; i < outStream.Length;)
                            {
                                int len = i + 65535 < outStream.Length ? 65535 : (int)outStream.Length - i;
                                Handle.Send(outStream.GetBuffer(), i, len, SocketFlags.None);
                                i       += len;
                                SendNum += len;
                            }
                        }
                        catch (Exception ex)
                        {
                            Disconnect();
                            return(-1);
                        }

                        SysLogger.Log("Send " + outStream.Length, SysLogType.Network);

                        PacketsOut++;
                        DataOut += (ulong)outStream.Length;
                        this.LastPacketSendSW = Stopwatch.StartNew();
                        return(SendNum);
                    }


                /*using (OptimizedPayloadStream ms = new OptimizedPayloadStream(SerializedHeader, HeaderId, feature, OpSocket))
                 * {
                 *  ms.Write(BitConverter.GetBytes(messageId), 0, 4);
                 *
                 *  MemoryStream stream = ms.PayloadFrames[ms.PayloadFrames.Count - 1];
                 *
                 *  int ReservedPos = (int)stream.Position;
                 *  ms.Write(new byte[3], 0, 3); //reserve space
                 *
                 *  ms.WritingMessage = true;
                 *  Serializer.Serialize(ms, Message);
                 *  ms.WritingMessage = false;
                 *
                 *  using (PayloadWriter pw = new PayloadWriter(new MemoryStream(stream.GetBuffer())))
                 *  {
                 *      pw.Position = ReservedPos; //skip MessageId data
                 *      pw.WriteThreeByteInteger(ms.MessageLength);//Reserved Space + MessageId = 7
                 *  }
                 *  ms.Commit(this);
                 *
                 *  for (int i = 0; i < ms.PayloadFrames.Count; i++)
                 *  {
                 *      stream = ms.PayloadFrames[i];
                 *
                 *      lock (HeaderEncryption)
                 *      {
                 *          HeaderEncryption.Encrypt(stream.GetBuffer(), 0, HEADER_SIZE);
                 *
                 *          byte[] temp = stream.GetBuffer();
                 *          headerConfuser.Obfuscate(ref temp, 0);
                 *      }
                 *      //lock (PayloadEncryption)
                 *      //{
                 *      //    PayloadEncryption.Encrypt(stream.GetBuffer(), HEADER_SIZE, (int)stream.Length - HEADER_SIZE);
                 *      //}
                 *
                 *      Handle.Send(stream.GetBuffer(), 0, (int)stream.Length, SocketFlags.None);
                 *  }
                 * }*/
            }
        }
        private void AynsReceive(IAsyncResult result)
        {
            int BytesTransferred = -1;

            try
            {
                BytesTransferred = Handle.EndReceive(result);

                SysLogger.Log("Received " + BytesTransferred, SysLogType.Network);

                if (BytesTransferred <= 0)
                {
                    Disconnect();
                    return;
                }
            }
            catch (Exception ex)
            {
                SysLogger.Log(ex.Message, SysLogType.Error);
                Disconnect();
                return;
            }

            try
            {
                //let's check the certificate
                if (Client.Server != null && Client.Server.serverProperties != null)
                {
                    if (Client.ConnectionTime > Client.Server.serverProperties.ClientTimeConnected)
                    {
                        //we need to wait till the time is right
                        Disconnect();
                        return;
                    }
                }

                this.LastPacketRecvSW = Stopwatch.StartNew();
                ReadableDataLen      += BytesTransferred;
                DataIn += (ulong)BytesTransferred;
                bool Process = true;

                while (Process)
                {
                    if (ReceiveState == ReceiveType.Header)
                    {
                        Process = ReadableDataLen >= HEADER_SIZE;
                        if (ReadableDataLen >= HEADER_SIZE)
                        {
                            lock (HeaderEncryption)
                            {
                                headerConfuser.Deobfuscate(ref Buffer, ReadOffset);
                                HeaderEncryption.Decrypt(Buffer, ReadOffset, HEADER_SIZE);
                            }

                            using (PayloadReader pr = new PayloadReader(Buffer))
                            {
                                pr.Position    = ReadOffset;
                                PayloadLen     = pr.ReadThreeByteInteger();
                                CurPacketId    = pr.ReadByte();
                                ConnectionId   = pr.ReadUShort();
                                HeaderId       = pr.ReadUShort();
                                HeaderChecksum = pr.ReadByte();
                                FeatureId      = pr.ReadInteger();
                            }

                            byte ReChecksum = 0; //re-calculate the checksum
                            ReChecksum += (byte)PayloadLen;
                            ReChecksum += CurPacketId;
                            ReChecksum += (byte)ConnectionId;
                            ReChecksum += (byte)HeaderId;
                            ReChecksum += (byte)FeatureId;

                            if (ReChecksum != HeaderChecksum ||
                                PayloadLen >= MAX_PACKET_SIZE ||
                                PayloadLen < 0)
                            {
                                Disconnect();
                                return;
                            }

                            if (PayloadLen > Buffer.Length)
                            {
                                ResizeBuffer(PayloadLen);
                            }

                            TotalReceived    = HEADER_SIZE;
                            ReadableDataLen -= HEADER_SIZE;
                            ReadOffset      += HEADER_SIZE;
                            ReceiveState     = ReceiveType.Payload;
                        }
                    }
                    else if (ReceiveState == ReceiveType.Payload)
                    {
                        Process = ReadableDataLen >= PayloadLen;
                        if (ReadableDataLen >= PayloadLen)
                        {
                            byte[] DecryptedBuffer  = null;
                            int    DecryptedBuffLen = 0;

                            messageHandler.DecryptMessage(this, Buffer, ReadOffset, PayloadLen, ref DecryptedBuffer, ref DecryptedBuffLen);

                            if (DecryptedBuffer == null)
                            {
                                //failed to decrypt data
                                Disconnect();
                                return;
                            }

                            TotalReceived += PayloadLen;

                            using (PayloadReader pr = new PayloadReader(DecryptedBuffer))
                            {
                                OperationalSocket OpSocket = null;
                                if (ConnectionId > 0)
                                {
                                    lock (OperationalSockets)
                                    {
                                        if (!OperationalSockets.TryGetValue(ConnectionId, out OpSocket))
                                        {
                                            //strange...
                                            Disconnect();
                                            return;
                                        }
                                    }
                                }

                                Type type = Headers.GetHeaderType(HeaderId);

                                if (type != null)
                                {
                                    Header header = Header.DeSerialize(type, pr);

                                    if (header == null)
                                    {
                                        Disconnect();
                                        return;
                                    }

                                    uint     MessageId = pr.ReadUInteger();
                                    IMessage message   = OpSocket != null?OpSocket.MessageHandler.DeSerialize(pr, MessageId) : messageHandler.DeSerialize(pr, MessageId);

                                    if (message != null)
                                    {
                                        message.RawSize = TotalReceived;
                                        message.Header  = header;

                                        if (!HandShakeCompleted)
                                        {
                                            if (message.GetType() == typeof(MsgHandshake))
                                            {
                                                //process the handshake messages straight away
                                                message.ProcessPayload(Client, null);
                                            }
                                        }
                                        else
                                        {
                                            ProcessMessage(new SystemPacket(header, message, ConnectionId, OpSocket));
                                        }
                                    }
                                }
                                else
                                {
                                    Disconnect();
                                    return;
                                }
                            }
                            TotalReceived = 0;

                            PacketsIn++;
                            ReadOffset      += PayloadLen;
                            ReadableDataLen -= PayloadLen;
                            ReceiveState     = ReceiveType.Header;
                        }
                    }
                }

                int len = ReceiveState == ReceiveType.Header ? HEADER_SIZE : PayloadLen;
                if (ReadOffset + len >= this.Buffer.Length)
                {
                    //no more room for this data size, at the end of the buffer ?

                    //copy the buffer to the beginning
                    Array.Copy(this.Buffer, ReadOffset, this.Buffer, 0, ReadableDataLen);

                    WriteOffset = ReadableDataLen;
                    ReadOffset  = 0;
                }
                else
                {
                    //payload fits in the buffer from the current offset
                    //use BytesTransferred to write at the end of the payload
                    //so that the data is not split
                    WriteOffset += BytesTransferred;
                }

                if (Buffer.Length - WriteOffset > 0)
                {
                    Handle.BeginReceive(this.Buffer, WriteOffset, Buffer.Length - WriteOffset, SocketFlags.None, AynsReceive, null);
                }
                else
                {
                    //Shoudln't be even possible... very strange
                    Disconnect();
                }
            }
            catch (Exception ex)
            {
                //unexpected error, client might have disconnected itself, or else report this error
                SysLogger.Log(ex.Message, SysLogType.Error);
                Disconnect();
                return;
            }
        }
        internal void Connect(ConnectionState State)
        {
            IPAddress[] resolved = Dns.GetHostAddresses(Properties.HostIp);
            string      DnsIp    = "";
            bool        IsIPv6   = false;

            for (int i = 0; i < resolved.Length; i++)
            {
                if (resolved[i].AddressFamily == AddressFamily.InterNetwork ||
                    resolved[i].AddressFamily == AddressFamily.InterNetworkV6)
                {
                    IsIPv6 = resolved[i].AddressFamily == AddressFamily.InterNetworkV6;
                    DnsIp  = resolved[i].ToString();
                    break;
                }
            }

            if (DnsIp == "")
            {
                throw new Exception("Unable to resolve \"" + Properties.HostIp + "\" by using DNS");
            }

            int ConTimeout = Properties.ConnectionTimeout;

            do
            {
                this.Handle = new Socket(IsIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IAsyncResult result = this.Handle.BeginConnect(new IPEndPoint(resolved[0], Properties.Port), (IAsyncResult ar) =>
                {
                    try
                    {
                        this.Handle.EndConnect(ar);
                    }
                    catch (Exception ex)
                    {
                        /* Will throw a error if connection couldn't be made */
                        SysLogger.Log(ex.Message, SysLogType.Error);
                    }
                }, null);

                Stopwatch sw = Stopwatch.StartNew();
                if (ConTimeout > 0)
                {
                    result.AsyncWaitHandle.WaitOne(ConTimeout);
                }
                else
                {
                    result.AsyncWaitHandle.WaitOne();
                }

                sw.Stop();
                ConTimeout -= (int)sw.ElapsedMilliseconds;

                if (!this.Handle.Connected)
                {
                    this.Handle.Close();
                }
            } while (ConTimeout > 0 && !this.Handle.Connected);

            if (!Handle.Connected)
            {
                throw new Exception("Unable to establish a connection with " + Properties.HostIp + ":" + Properties.Port);
            }

            Connection = new Connection(this);
            Connection.StartReceiver();

            onBeforeConnect();

            //let's begin the handshake
            User user = new User(Properties.Username, Properties.Password, new List <Stream>(Properties.PrivateKeyFiles), Properties.PublicKeyFile);

            user.GenKey(this, SessionSide.Client, Properties.Handshake_Maze_Size, Properties.Handshake_MazeCount, Properties.Handshake_StepSize);

            this.clientHS = user.MazeHandshake;
            byte[] encryptedPublicKey = clientHS.GetEncryptedPublicKey();


            byte[] byteCode = clientHS.GetByteCode();
            Connection.SendMessage(new MsgHandshake(byteCode), new SystemHeader());

            //send our encrypted public key
            Connection.SendMessage(new MsgHandshake(encryptedPublicKey), new SystemHeader());

            //and now just wait for the handshake to finish... can't take longer then 60 seconds
            MazeErrorCode errorCode = Connection.HandshakeSync.Wait <MazeErrorCode>(MazeErrorCode.Error, 60000);

            if (errorCode != MazeErrorCode.Finished)
            {
                throw new Exception("Username or Password is incorrect.");
            }

            bool initOk = Connection.InitSync.Wait <bool>(false, 30000);

            if (!initOk)
            {
                throw new Exception("A server time-out occured");
            }

            //re-calculate the private keys
            for (int i = 0; i < Properties.PrivateKeyFiles.Length; i++)
            {
                clientHS.RecalculatePrivateKey(Properties.PrivateKeyFiles[i]);
            }

            this.Username = Properties.Username;

            this.KeepAliveTimer          = new System.Timers.Timer();
            this.KeepAliveTimer.Interval = 5000;
            this.KeepAliveTimer.Elapsed += KeepAliveTimer_Elapsed;
            this.KeepAliveTimer.Enabled  = true;

            onConnect();
        }
        public override void ProcessPayload(SSPClient client, OperationalSocket _OpSocket)
        {
            RequestHeader reqHeader = Header as RequestHeader;

            if (reqHeader != null)
            {
                Type type = null;
                lock (client.Connection.RegisteredOperationalSockets)
                {
                    client.Connection.RegisteredOperationalSockets.TryGetValue(Identifier, out type);
                }

                if (type != null)
                {
                    bool SendedSuccess = false;
                    try
                    {
                        OperationalSocket OpSocket = (OperationalSocket)Activator.CreateInstance(type, client);

                        OpSocket.isConnected = true;

                        lock (client.Connection.OperationalSockets)
                        {
                            FastRandom rnd = new FastRandom();
                            OpSocket.ConnectionId = (ushort)rnd.Next(1, 65535);
                            while (client.Connection.OperationalSockets.ContainsKey(OpSocket.ConnectionId))
                            {
                                OpSocket.ConnectionId = (ushort)rnd.Next(1, 65535);
                            }

                            client.Connection.OperationalSockets.Add(OpSocket.ConnectionId, OpSocket);
                        }


                        try
                        {
                            OpSocket.onBeforeConnect();
                            client.onOperationalSocket_BeforeConnect(OpSocket);
                        }
                        catch (Exception ex)
                        {
                            SysLogger.Log(ex.Message, SysLogType.Error);
                            OpSocket.onException(ex, ErrorType.UserLand);
                        }

                        client.Connection.SendMessage(new MsgCreateConnectionResponse(OpSocket.ConnectionId, true), new RequestHeader(reqHeader.RequestId, true));
                        SendedSuccess = true;
                        OpSocket.onConnect();
                        client.onOperationalSocket_Connected(OpSocket);
                    }
                    catch (Exception ex)
                    {
                        SysLogger.Log(ex.Message, SysLogType.Error);

                        if (!SendedSuccess)
                        {
                            client.Connection.SendMessage(new MsgCreateConnectionResponse(0, false), new RequestHeader(reqHeader.RequestId, true));
                        }
                    }
                }
                else
                {
                    client.Connection.SendMessage(new MsgCreateConnectionResponse(0, false), new RequestHeader(reqHeader.RequestId, true));
                }
            }
        }