Exemplo n.º 1
0
        /// <summary>
        /// Handler for received messages
        /// </summary>
        /// <param name="ar"></param>
        private static void ReceiveCallback(IAsyncResult ar)
        {
            string          err;
            ConnectedObject client;
            int             bytesRead;

            // Check for null values
            if (!CheckState(ar, out err, out client))
            {
                Console.WriteLine(err);
                return;
            }

            // Read message from the client socket
            try
            {
                bytesRead = client.Socket.EndReceive(ar);
            }
            catch (SocketException)
            {
                // Client was forcebly closed on the client side
                CloseClient(client);
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            // Check message
            if (bytesRead > 0)
            {
                // Build message as it comes in
                client.BuildIncomingMessage(bytesRead);

                // Check if we received the full message
                if (client.MessageReceived())
                {
                    // Print message to the console
                    client.PrintMessage();

                    string incomming_message = client.getIncommingMessage().Replace("<END>", "");


                    string flag = client.getIncommingMessage().Split(',')[0];
                    if (flag == "Exit")
                    {
                        MySqlCommand mySqlCommand = new MySqlCommand(MySqlCommandType.UPDATE);
                        mySqlCommand.Update("logins").Set("online", (long)0).Where("username", client.getIncommingMessage().Split(',')[1]).Execute();
                        return;
                    }



                    string HDDSerial = incomming_message.Split(',')[0];
                    string Key       = incomming_message.Split(',')[1];
                    string nistTime  = incomming_message.Split(',')[2];

                    // Get Incomming message from Client

                    using (MySqlCommand mySqlCommand1 = (new MySqlCommand(MySqlCommandType.SELECT)).Select("logins").Where("password", Key))
                    {
                        using (MySqlReader mySqlReader1 = new MySqlReader(mySqlCommand1))
                        {
                            if (mySqlReader1.Read())
                            {
                                databaseusername = mySqlReader1.ReadString("username");
                                Console.WriteLine("DatabaseUsername:"******"password");
                                Console.WriteLine("DatabasePassword:"******"hddserial");
                                Console.WriteLine("DatabaseHDD:" + databasehddserial);
                                databaseonline = mySqlReader1.ReadString("online");
                                Console.WriteLine("DatabaseOnline:" + databaseonline);
                                databaseexpired = mySqlReader1.ReadString("expired");
                                Console.WriteLine("DatabaseExpire:" + databaseexpired);
                                databaseexpiredate = mySqlReader1.ReadDates("expiredate");
                                Console.WriteLine("DatabaseExpireDate:" + databaseexpiredate);
                                databasehddbanned = mySqlReader1.ReadString("hddbanned");
                                Console.WriteLine("DatabaseHDDBanned:" + databasehddbanned);
                                databasestarted = mySqlReader1.ReadString("started");
                                Console.WriteLine("DatabaseStarted:" + databasestarted);
                                databaseperiod = mySqlReader1.ReadInt16("period");
                                Console.WriteLine("DatabasePeriod:" + databaseperiod);
                                mySqlReader1.Dispose();
                            }
                        }
                    }
                    if (HDDSerial == databasehddbanned)
                    {
                        // Reset message
                        client.ClearIncomingMessage();
                        // Acknowledge message - This Key Got Banned!
                        SendReply(client, 1);
                    }
                    else if (databasepassword != Key)
                    {
                        // Reset message
                        client.ClearIncomingMessage();
                        // Acknowledge message - Password Wrong!
                        SendReply(client, 2);
                    }
                    else if (databaseonline == "1")
                    {
                        // Reset message
                        client.ClearIncomingMessage();
                        // Acknowledge message - Key Already Online!
                        SendReply(client, 3);
                    }
                    else
                    {
                        if (databasestarted == "0")
                        {
                            nistTime = GetNistTime().AddDays(databaseperiod).ToString("yyyy-MM-dd HH:mm:ss");
                            Console.WriteLine("Nist Time:" + nistTime);
                            string str = nistTime;
                            new MySqlCommand(MySqlCommandType.UPDATE).Update("logins").Set("expiredate", str).Where("password", databasepassword).Execute();
                            new MySqlCommand(MySqlCommandType.UPDATE).Update("logins").Set("started", "1").Where("password", databasepassword).Execute();
                            //nistTime = GetNistTime();
                            DateTime NistTime = GetNistTime();
                            databaseexpiredate = NistTime.AddDays(databaseperiod);

                            // Reset message
                            client.ClearIncomingMessage();
                            // Acknowledge message - Key Binded Click Login Again!
                            SendReply(client, 4);
                        }
                        if (databaseexpired == "1")
                        {
                            // Reset message
                            client.ClearIncomingMessage();
                            // Acknowledge message - Key Deleted Because Expired!
                            SendReply(client, 5);
                        }
                        else if (databaseexpiredate <= GetNistTime())
                        {
                            new MySqlCommand(MySqlCommandType.UPDATE).Update("logins").Set("expired", "1").Where("password", databasepassword).Execute();

                            // Reset message
                            client.ClearIncomingMessage();
                            // Acknowledge message - Your Key Expired!
                            SendReply(client, 6);
                        }
                        else if (databasehddserial == "")
                        {
                            new MySqlCommand(MySqlCommandType.UPDATE).Update("logins").Set("hddserial", HDDSerial).Where("password", databasepassword).Execute();
                        }

                        else if (databasehddserial != HDDSerial)
                        {
                            // Reset message
                            client.ClearIncomingMessage();
                            // Acknowledge message - You Can't Use On This PC!
                            SendReply(client, 7);
                        }
                        else if (databasehddbanned == HDDSerial)
                        {
                            // Reset message
                            client.ClearIncomingMessage();
                            // Acknowledge message - You Have Been Banned!
                            SendReply(client, 8);
                        }
                        else if (databasestarted == "1" || databaseexpired != "1" || databasehddbanned != HDDSerial || databaseexpiredate >= GetNistTime())
                        {
                            using (MySqlCommand mySqlCommand = (new MySqlCommand(MySqlCommandType.SELECT)).Select("versionupdate").Where("id", (long)1))
                            {
                                using (MySqlReader mySqlReader = new MySqlReader(mySqlCommand))
                                {
                                    if (mySqlReader.Read())
                                    {
                                        databaseversion = mySqlReader.ReadString("version");
                                        Console.WriteLine("Database Version:" + databaseversion);
                                    }
                                }
                            }
                            if (currentversion != databaseversion)
                            {
                                // Reset message
                                client.ClearIncomingMessage();
                                // Acknowledge message - VERSION OUTDATED!
                                SendReply(client, 9);
                            }
                            else
                            {
                                new MySqlCommand(MySqlCommandType.UPDATE).Update("logins").Set("online", "1").Where("password", databasepassword).Execute();
                                // Reset message
                                client.ClearIncomingMessage();
                                // Acknowledge message - Main!
                                SendReply(client, 10, databaseusername + "," + databaseexpiredate.ToString("yyyy-MM-dd HH:mm:ss"));
                            }
                        }
                    }
                }
            }

            // Listen for more incoming messages
            try
            {
                client.Socket.BeginReceive(client.Buffer, 0, client.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), client);
            }
            catch (SocketException)
            {
                // Client was forcebly closed on the client side
                CloseClient(client);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }