Exemple #1
0
        public void startConnection()
        {
            var recv_func = new Action(() =>
            {
                AsyncCallback receiveCallback = new AsyncCallback(this.receiveDataFromClient);

                while (this.socket.Connected)
                {
                    if (this.canListen)
                    {
                        // Avoid creating new AysncCallback for receive data each time.
                        try
                        {
                            this.socket.BeginReceive(this.buffer, 0, this.buffer.Length, 0, receiveCallback, this.socket);
                        } catch { break; }
                        this.RecvDataEvent.WaitOne();
                    }
                    else
                    {
                        this._listen__Event.WaitOne();
                        continue;
                    }
                }

                Airtower.disposeClient(false, this.socket);
            });

            this.canListen = true;
            this.canSend   = true;

            new Thread(() => recv_func()).Start();
            Task.Run(() => recv_func());
        }
Exemple #2
0
        public void HandlePenguinBanned(MySqlDataReader reader, Penguin client, AutoResetEvent evnt)
        {
            if (!reader.HasRows)
            {
                client.banned = false;
                evnt.Set();
                return;
            }

            var  details     = reader.GetDictFromReader();
            long timestamp   = long.Parse(details["till"]);
            long currentTmsp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;

            if (timestamp > currentTmsp)
            {
                client.banned = true;
                string hoursban = (Math.Round((timestamp - currentTmsp) / 3600.0)).ToString();
                client.send("e", "601", hoursban);
                Airtower.disposeClient(false, client.getSocket());
            }
            else
            {
                client.banned = false;
                MySQLStatement stmt = new MySQLStatement("UPDATE `banned` SET `till` = '0' AND `b_by`='' AND `reason`='' WHERE `ID` = @id", new Dictionary <string, dynamic> {
                    { "@id", client.id }
                });
                Server.Utils.MySQL.MySQL.getCurrentMySQLObject().MySQLCallback(stmt);
            }

            evnt.Set();
        }
Exemple #3
0
        public void CompleteLogin(MySqlDataReader reader, Penguin client, dynamic body)
        {
            var details = reader.GetDictFromReader();
            //"101|OP1-{AB-ERTS-972H}-09-K|valid22|f54fb87b5b1e6120e28c5bbf1a6ca862|NULL|45|2"
            var nick = ((string)body.login.nick.Value).Split(char.Parse("|"));

            client.swid     = nick[1];
            client.username = client.nickname = ((string)nick[2]).preetify();
            string password = nick[3];

            if (client.username.ToLower() != details["Username"].ToLower() || password != details["ConfirmationHash"] ||
                client.swid != details["SWID"] || password == "")
            {
                client.send("e", "101", "#2");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            var pword = body.login.pword.Value.Split(char.Parse("#"));

            if (pword[0] != String.Format("{0}{1}", (password + GetPenguinRandomKey(client)).md5().swap(16), password) || pword[1] != details["LoginKey"] || pword[0] == "" ||
                pword[1] == "")
            {
                client.send("e", "101", "#3");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            client.send("l", "$loggedin");
            return;
        }
Exemple #4
0
        public void ContinuePrimaryLogin(MySqlDataReader reader, Penguin client, dynamic data)
        {
            if (!reader.HasRows || GetPenguinRandomKey(client) == null)
            {
                client.send("e", "101");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            var penguin_details = reader.GetDictFromReader();

            string password = Cryptography.GenerateLoginHash(penguin_details["Password"], GetPenguinRandomKey(client));

            if (password != client.password)
            {
                client.send("e", "101");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            client.id = penguin_details["ID"];
            // Handle Banned
            MySQLStatement stmt = new MySQLStatement("");

            stmt.parameters = new Dictionary <string, dynamic> {
                { "@id", client.id }
            };
            AutoResetEvent evnt = new AutoResetEvent(false);

            stmt.statement = "SELECT `till` FROM `banned` WHERE `ID` = @id";
            Server.Utils.MySQL.MySQL.getCurrentMySQLObject().MySQLCallback(stmt, Server.Utils.Events.EventDelegate.create(this, "HandlePenguinBanned"), client, evnt);

            evnt.WaitOne();

            if (client.banned)
            {
                return;
            }

            string ConfHash = GetPenguinRandomKey(client).md5() + "-" + penguin_details["Password"].GetHashCode().ToString().md5();

            Shell.getCurrentShell().getCurmbs("Login")["HashKey"][client][1] = ConfHash;

            stmt.statement         = "UPDATE `penguins` SET `LoginKey` = @lk, `ConfirmationHash` = @ch WHERE `ID` = @id";
            stmt.parameters["@lk"] = ConfHash;
            stmt.parameters["@ch"] = password;

            Server.Utils.MySQL.MySQL.getCurrentMySQLObject().MySQLCallback(stmt);

            string users_bar = Math.Floor((double)(Airtower.Clients.Values.Where(i => i.PORT == 9875).ToList().Count * 5 / 550)).ToString();

            client.send("l",
                        string.Format("{0}|{1}|{2}|{3}|NULL|45|2", new object[] { client.id, penguin_details["SWID"],
                                                                                  client.username, password }), ConfHash, ConfHash.md5(),
                        string.Format("100,{0}", users_bar), penguin_details["Email"]);

            Airtower.disposeClient(false, client.getSocket());
        }
Exemple #5
0
        public void ClientAPIVersionCheck(Penguin client, dynamic body)
        {
            string ver = body.ver["v"];

            if (ver == "153")
            {
                client.send("<msg t='sys'><body action='apiOK' r='0'></body></msg>");
                return;
            }
            else
            {
                client.send("<msg t='sys'><body action='apiKO' r='0'></body></msg>");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }
        }
Exemple #6
0
        public static void HandleXTPacket(Penguin peng)
        {
            try
            {
                if (peng.clientData == null)
                {
                    return;
                }
                var category = peng.clientData[0];
                var handler  = peng.clientData[1];

                var vars = new List <string> {
                };
                for (var i = 2; i < peng.clientData.length; i++)
                {
                    vars.Add(peng.clientData[i]);
                }

                var callbackHandlerName = String.Format("#xt{0}-{1}/", category, handler);
                Airtower.getCurrentAirtower().updateListeners(callbackHandlerName, new { client = peng }, vars.ToArray());
            }
            catch (Exception) { }
        }
Exemple #7
0
        protected void receiveDataFromClient(IAsyncResult datas)
        {
            int bytesReceived;

            try { bytesReceived = ((Socket)datas.AsyncState).EndReceive(datas); }
            catch { return; }

            if (bytesReceived > 0)
            {
                string  data      = ASCIIEncoding.ASCII.GetString(this.buffer, 0, bytesReceived);
                Boolean isPartial = this.checkForPartialData(data);

                if (!isPartial)
                {
                    // \x00 Null byte in last index, remove it.
                    string   partialdata = this.partialBuffer.ToString();
                    string[] packet      = partialdata.Split(char.Parse("\x00"));

                    this.partialBuffer.Clear();

                    for (int i = 0; i < packet.Length - 1; i++)
                    {
                        string _loc1_ = packet[i];
                        var    parse  = Packets.Parse(_loc1_, this);

                        if (parse == Packets.InvalidPacket)
                        {
                            // Better to remove that penguin
                            Log.Debugger.CallEvent(Airtower.ERROR_EVENT, "Incorrect Packet : " + _loc1_);
                            Airtower.disposeClient(false, this.socket);
                            break;
                        }

                        // handle packets acc to received, if xt or xml
                        if (parse == Packets.XT_DATA)
                        {
                            Log.Debugger.CallEvent(Airtower.XT_EVENT, _loc1_);
                            Packets.HandleXTPacket(this);
                        }
                        else
                        if (parse == Packets.XML_DATA)
                        {
                            Log.Debugger.CallEvent(Airtower.XML_EVENT, _loc1_);
                            if (_loc1_ == "<policy-file-request/>")
                            {
                                this.send(String.Format("<cross-domain-policy><allow-access-from domain='*'" +
                                                        " to-ports='{0}' /></cross-domain-policy>", Airtower.getCurrentAirtower().PORT));
                                return;
                            }

                            Packets.HandleXMLPacket(this);
                        }
                    }
                }

                this.RecvDataEvent.Set();
            }
            else
            {
                Airtower.disposeClient(false, this.socket);
            }
        }
Exemple #8
0
        public void JoinPenguinToServer(Penguin client, string[] data)
        {
            string id   = data[0];
            string hash = data[1];

            if (id != client.id)
            {
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            MySQLStatement stmt = new MySQLStatement("SELECT `ConfirmationHash` FROM `penguins` WHERE `ID` = @id", new Dictionary <string, dynamic> {
                { "@id", client.id }
            });
            string confirmationHash = (MySQL.getCurrentMySQLObject().ExecuteAndFetch(stmt).GetDictFromReader())["ConfirmationHash"];

            stmt = null;

            if (hash != confirmationHash)
            {
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            AutoResetEvent get_details = new AutoResetEvent(false);

            client.LoadPenguinDetails(get_details);
            get_details.WaitOne();
            client.GenerateString();
            client.send(Airtower.JOIN_WORLD, "1", client.epf[0], (client.moderator ? 1 : 0).ToString());

            client.doXT = true;

            var puffle_data = client.PuffleData();

            client.send(Airtower.GET_MY_PLAYER_PUFFLES, puffle_data.join("%"));

            /* TO ADD STAMPS*/
            client.send(Airtower.GET_PLAYER + "s", client.epf[0], "");

            client.send(Airtower.LOAD_PLAYER,
                        (new List <string> {
                client.ToString(),
                client.coins.ToString(),
                "0",                                                                                           // Safe-Game mode ? 0 -> no, 1 -> yes
                "1024",                                                                                        // Egg timer
                ((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds - 2 * 60 * 60).ToString(), // Penguin Standard Time
                client.age.ToString(),
                "0",                                                                                           // Banned age
                client.age.ToString(),                                                                         // Penguin's days old
                client.membershipDays.ToString(),
                "",
                client.Cache["PlayerWidget"],     // Has opened PlayerWidget - Cache
                client.Cache["MapCategory"],
                client.Cache["Igloo"],
            }).join("%"));

            client.send("glr", "");
            client.send("spts", client.id, client.avatar, client.avatarAttributes);

            EventDispatcher._dispatcher.updateListeners("JS#{JOINED}", client);

            ((GeneralRoom)(CacheHandler.Rooms["100"])).Add(client, true);
        }
Exemple #9
0
        public void ContinueWorldLogin(MySqlDataReader reader, Penguin client, dynamic body)
        {
            if (!reader.HasRows || GetPenguinRandomKey(client) == null)
            {
                client.send("e", "101", "#1");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            if (body == null)
            {
                return;
            }
            if (body.login == null)
            {
                return;
            }
            if (body.login.pword == null)
            {
                return;
            }
            if (body.login.nick == null)
            {
                return;
            }
            if (GetPenguinRandomKey(client) == null)
            {
                return;
            }

            string[] userInfo = body.login.nick.Value.Split(char.Parse("|"));
            if (userInfo[0].userInServer(9875))
            {
                client.send("e", "3");
                Airtower.disposeClient(false, client.getSocket());
                return;
            }

            client.id = userInfo[0];

            MySQLStatement stmt = new MySQLStatement("");

            AutoResetEvent banEvent = new AutoResetEvent(false);

            stmt.statement  = "SELECT `till` FROM `banned` WHERE `ID` = @id";
            stmt.parameters = new Dictionary <string, dynamic> {
                { "@id", client.id }
            };
            Server.Utils.MySQL.MySQL.getCurrentMySQLObject().MySQLCallback(stmt, Server.Utils.Events.EventDelegate.create(this, "HandlePenguinBanned"), client, banEvent);

            banEvent.WaitOne();

            if (client.banned)
            {
                return;
            }

            stmt.statement = "SELECT `ID`, `Username`, `Password`, `Email`, `SWID`, `LoginKey`, `ConfirmationHash` FROM `Penguins` WHERE `ID` = @id";
            Server.Utils.MySQL.MySQL.getCurrentMySQLObject().MySQLCallback(stmt, Server.Utils.Events.EventDelegate.create(this, "CompleteLogin"), client, body);
            stmt = null;
        }