public static PacketProcessResult HandleClient(Packet pck, RelaySession session)
        {
            byte locale = pck.ReadUInt8();
            string username = pck.ReadAscii();
            string password = pck.ReadAscii();
            ushort ServerID = pck.ReadUInt16();
            //проверить
            if (Global.EnableIpAccountLimitation)
            {

                int nResult = Global.dbmgr.GetIpLimitationResult(username, Utility.GetRemoteEpString(session.Arguments.ClientSocket));
                if (nResult == 0)
                {
                    Packet login_response = new Packet(0xA102, false);
                    login_response.WriteUInt8(0x02);
                    login_response.WriteUInt8(12);
                    session.SendPacketToClient(login_response);
                    //drop conn
                    return PacketProcessResult.Disconnect;
                }
            }
            if (Global.EnableLoginProcessing)
            {

                Packet login = new Packet(0x6102);
                login.WriteUInt8(Global.OriginalLocale > 0 && locale > 0 ? Global.OriginalLocale : locale);
                login.WriteAscii(username);
                login.WriteAscii(Global.EnableUseSha1Salt ? Utility.HashPassword(username, password) : password);
                login.WriteUInt16(ServerID);
                session.SendPacketToModule(login);
                //return PacketProcessResult.DoNothing;
                return PacketProcessResult.ContinueLoop;
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            byte locale = pck.ReadUInt8();
            string username = pck.ReadAscii();
            string password = pck.ReadAscii();
            ushort ServerID = pck.ReadUInt16();

            if (Global.EnableServerInspection && !Global.InspectionLoginIgnore.Contains(username))
            {
                Packet login_response = new Packet(0xA102, false);
                login_response.WriteUInt8(0x02);
                login_response.WriteUInt8(0x02);
                login_response.WriteUInt8(0x02);
                session.SendPacketToClient(login_response);

                return PacketProcessResult.Disconnect;
            }

            if (Global.EnableIpAccountLimitation)
            {
                //We use Utility.GetRemoteEpString because username is assigned after AgentServer.UserAuth
                string clientAddr = Utility.GetRemoteEpString(session.Arguments.ClientSocket);
                int connectionCount = Global.srvmgr.GetUserCountByIpForAgent(clientAddr);

                if (connectionCount >= Global.AccountIpLimitCount)
                {
                    Packet login_resp = new Packet(0xA102, false);
                    login_resp.WriteUInt8(0x02);
                    login_resp.WriteUInt8(12);
                    session.SendPacketToClient(login_resp);

                    return PacketProcessResult.Disconnect;
                }
            }

            int serverOnline = 0;
            int.TryParse(session.State["server_" + ServerID] as string, out serverOnline);
            if (Global.ShardMaxOnline > 0 && Global.ShardMaxOnline <= serverOnline)
            {
                Packet login_response = new Packet(0xA102, false);
                login_response.WriteUInt8(0x02);
                login_response.WriteUInt8(5);
                session.SendPacketToClient(login_response);
                return PacketProcessResult.Disconnect;
            }

            if (Global.EnableLoginProcessing)
            {

                Packet login = new Packet(0x6102);
                login.WriteUInt8(Global.OriginalLocale > 0 && locale > 0 ? Global.OriginalLocale : locale);
                login.WriteAscii(username);
                login.WriteAscii(Global.EnableUseSha1Salt ? Utility.HashPassword(username, password) : password);
                login.WriteUInt16(ServerID);
                session.SendPacketToModule(login);
                //return PacketProcessResult.DoNothing;
                return PacketProcessResult.ContinueLoop;
            }
            return PacketProcessResult.DoNothing;
        }
 public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
 {
     string name = pck.ReadAscii();
     session.State["charname"] = name;
     session.State["level"] = Global.dbmgr.getCharLvl(name);
     return PacketProcessResult.DoNothing;
 }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            UInt32 uint32_1 = pck.ReadUInt32();
            string uname = pck.ReadAscii();
            string passw = pck.ReadAscii();
            byte locale = pck.ReadUInt8();
            uint ukn1 = pck.ReadUInt32();
            uint ukn = pck.ReadUInt16();
            if (uname.Contains("'"))
            {
                Global.logmgr.WriteLog(LogLevel.Error, "User trying to exploit username: {0}", uname);
                return PacketProcessResult.Disconnect;
            }
            //инициализурем данные
            session.State["username"] = uname;
            session.State["isBot"] = false;
            session.State["isSafe"] = false;
            session.State["noticeDone"] = false;
            session.State["lastExchange"] = DateTime.Now;
            session.State["lastLogOut"] = DateTime.Now;
            session.State["lastStall"] = DateTime.Now;
            session.State["proper_logout"] = false;
            session.State["level"] = 0;
            //пишем в базу что юзер залогинился
            Global.dbmgr.AnticheatAuthLog(uname, session.State["ip_address"] as string);

            Global.logmgr.WriteLog(LogLevel.Notify, "User logged in [{0}]", uname);

            if (Global.EnableBotDetected && locale == Global.OriginalLocale)
            {
                session.State["isBot"] = true;
            }
            if (Global.EnableLoginProcessing)
            {
                Packet p = new Packet(0x6103);
                p.WriteUInt32(uint32_1);
                p.WriteAscii(uname);
                p.WriteAscii(Global.EnableUseSha1Salt ? Utility.HashPassword(uname, passw) : passw);
                p.WriteUInt8(Global.OriginalLocale > 0 && locale > 0 ? Global.OriginalLocale : locale);
                p.WriteUInt32(ukn1);
                p.WriteUInt16(ukn);
                // m_ModuleSecurity.Send(p);
                session.SendPacketToModule(p);
                return PacketProcessResult.ContinueLoop;
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            Packet ServerList = new Packet(0xA101);
            byte GlobalOperationFlag = pck.ReadUInt8();

            ServerList.WriteUInt8(GlobalOperationFlag);

            while (GlobalOperationFlag == 1)
            {
                byte GlobalOperationType = pck.ReadUInt8();
                string GlobalOperationName = pck.ReadAscii();

                GlobalOperationFlag = pck.ReadUInt8();
                ServerList.WriteUInt8(GlobalOperationType);
                ServerList.WriteAscii(GlobalOperationName);
                ServerList.WriteUInt8(GlobalOperationFlag);
            }
            byte ShardFlag = pck.ReadUInt8();

            ServerList.WriteUInt8(ShardFlag);

            while (ShardFlag == 1)
            {
                uint ShardID = pck.ReadUInt16();
                string ShardName = pck.ReadAscii();
                uint ShardCurrent = pck.ReadUInt16();
                uint ShardCapacity = pck.ReadUInt16();
                byte ShardStatus = pck.ReadUInt8();
                byte GlobalOperationID = pck.ReadUInt8();
                ShardFlag = pck.ReadUInt8();
                session.State["server_" + ShardID] = ShardCurrent;

                ServerList.WriteUInt16(ShardID);
                ServerList.WriteAscii("Возрождение", Global.TextEncodeCode);
                ServerList.WriteUInt16(ShardCurrent + Global.ShardFakeOnline);
                ServerList.WriteUInt16(Global.ShardMaxOnline > 0 ? (uint)Global.ShardMaxOnline : ShardCapacity);
                ServerList.WriteUInt8(ShardStatus);
                ServerList.WriteUInt8(GlobalOperationID);
                ServerList.WriteUInt8(ShardFlag);

            }
            session.SendPacketToClient(ServerList);
            return PacketProcessResult.ContinueLoop;
        }
 public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
 {
     uint type = pck.ReadUInt8();
     string key = pck.ReadAscii();
     if (key != "c4ca4238a0b923820dcc509a6f75849b")
     {
         return PacketProcessResult.ContinueLoop;
     }
     if(type == 9)
     {
         Global.srvmgr.StopAllContexts();
     }
     return PacketProcessResult.DoNothing;
 }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (server.HasAgentRedirectRules())
            {
                string src_host;
                int src_port;
                byte flag1 = pck.ReadUInt8();

                if (flag1 == 1)
                {
                    UInt32 uint1 = pck.ReadUInt32();
                    src_host = pck.ReadAscii();
                    src_port = pck.ReadUInt16();

                    bool redirectRuleFound = false;
                    for (int j = 0; j < server.RedirectRules.Count; j++)
                    {

                        if (server.RedirectRules[j].OriginalIp == src_host && server.RedirectRules[j].OriginalPort == src_port)
                        {
                            Packet mypck = new Packet(0xA102, false);
                            mypck.WriteUInt8(flag1);
                            mypck.WriteUInt32(uint1);
                            mypck.WriteAscii(server.RedirectRules[j].NewIp);
                            mypck.WriteUInt16((ushort)server.RedirectRules[j].NewPort);

                            //  m_ClientSecurity.Send(mypck);
                            session.SendPacketToClient(mypck);
                            redirectRuleFound = true;

                            break;

                        }

                    }
                    if (!redirectRuleFound)
                    {
                        Global.logmgr.WriteLog(LogLevel.Warning, "Agent redirect rules given, but [{0}:{1}] is unknown agent server", src_host, src_port);
                    }
                    else
                    {
                        return PacketProcessResult.ContinueLoop;
                    }

                }

            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            uint type = pck.ReadUInt16();
            if (Global.EnableUniqueDeathNotify && type == 3078)
            {
                uint mob_id = pck.ReadUInt32();
                string char_name = pck.ReadAscii();
                //Проверяем тормоз, дабы с каждого потока не спамило
                if(Global.UniqueDeathNotifyName == char_name && Global.UniqueDeathNotifyID == mob_id && Global.UniqueDeathNotifyTime > DateTime.Now)
                {
                    return PacketProcessResult.DoNothing;
                }

                Global.UniqueDeathNotifyName = char_name;
                Global.UniqueDeathNotifyID = mob_id;
                Global.UniqueDeathNotifyTime = DateTime.Now.AddSeconds(10);
                Global.dbmgr.UniqueDeathNotify(char_name, (int)mob_id);
            }

            return PacketProcessResult.DoNothing;
        }
 private bool ClientPacketHandler(Context context, Packet packet)
 {
     bool retval = false;
     switch (packet.Opcode)
     {
         case 0x7001:
             {
                 string charname = packet.ReadAscii();
                 _mainForm.UpdateCharName(charname);
                 _charracter.CharName = charname;
             }
             break;
         default:
             break;
     }
     return retval;
 }
        /// <summary>
        /// DownloadServer info / patch info request packet (Module -> Client)
        /// </summary>
        /// <param name="pck"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public static PacketProcessResult HandleModule(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (server.HasDownloadRedirectRules())
            {
                //Read original data
                byte result = pck.ReadUInt8();

                //UpdateInfo
                if (result == 0x02)
                {
                    byte errorCode = pck.ReadUInt8();

                    if (errorCode == 0x02)
                    {
                        Packet myPacket = new Packet(0xA100, false, true);

                        string tmpIp = pck.ReadAscii();
                        ushort tmpPort = pck.ReadUInt16();

                        bool redirectRuleFound = false;
                        for (int i = 0; i < server.RedirectRules.Count; i++)
                        {
                            if (server.RedirectRules[i].OriginalIp == tmpIp && server.RedirectRules[i].OriginalPort == tmpPort)
                            {
                                tmpIp = server.RedirectRules[i].NewIp;
                                tmpPort = (ushort)server.RedirectRules[i].NewPort;
                                redirectRuleFound = true;
                            }
                        }

                        if (!redirectRuleFound)
                        {
                            Global.logmgr.WriteLog(LogLevel.Warning, "Download redirect rules given, but [{0}:{1}] is unknown download server", tmpIp, tmpPort);
                        }

                        uint version = pck.ReadUInt32();
                        byte fileFlag = pck.ReadUInt8();

                        myPacket.WriteUInt8(result);
                        myPacket.WriteUInt8(errorCode);

                        myPacket.WriteAscii(tmpIp);
                        myPacket.WriteUInt16(tmpPort);
                        myPacket.WriteUInt32(version);
                        myPacket.WriteUInt8(fileFlag);

                        while (fileFlag == 0x01)
                        {
                            uint fileId = pck.ReadUInt32();
                            string fileName = pck.ReadAscii();
                            string filePath = pck.ReadAscii();
                            uint fileLength = pck.ReadUInt32();
                            byte doPack = pck.ReadUInt8();

                            fileFlag = pck.ReadUInt8();

                            myPacket.WriteUInt32(fileId);
                            myPacket.WriteAscii(fileName);
                            myPacket.WriteAscii(filePath);
                            myPacket.WriteUInt32(fileLength);
                            myPacket.WriteUInt8(doPack);
                            myPacket.WriteUInt8(fileFlag);
                        }
                        session.SendPacketToClient(myPacket);
                    }
                    //Do not send original packet
                    return PacketProcessResult.ContinueLoop;
                }
            }
            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            string username = session.State["username"] as string;
            string charname = session.State["charname"] as string;

            //Read type
            byte msgTypeId = pck.ReadUInt8();
            byte msgCount = pck.ReadUInt8();

            ChatType msgType = (ChatType)(msgTypeId);
            string msg = string.Empty;

            switch (msgType)
            {
                case ChatType.Normal:
                    {
                        msg = pck.ReadAscii();

                        //ProcessChatCommand(msg);
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }

                        if (ProcessChatCommand(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }

                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);

                    }
                    break;
                case ChatType.Private:
                    {
                        string name = pck.ReadAscii();
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, name, msg);
                    }
                    break;
                case ChatType.Academy:
                    {
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);
                    }
                    break;
                case ChatType.Gm:
                    {
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);
                    }
                    break;
                case ChatType.Group:
                    {
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);
                    }
                    break;
                case ChatType.Guild:
                    {
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);
                    }
                    break;
                case ChatType.Union:
                    {
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);
                    }
                    break;
                case ChatType.Stall:
                    {
                        msg = pck.ReadAscii();
                        if (AbuseFilter(msg, session))
                        {
                            return PacketProcessResult.ContinueLoop;
                        }
                        msg = FilterMsg(msg);
                        Global.logmgr.WriteChatLog(msgType, charname, msg);
                    }
                    break;
                case ChatType.Anounce:
                    {
                        if (Global.EnableGmAccessControl)
                        {
                            if (Global.dbmgr.checkGmAccessControl(username, 3000) == 0)
                            {
                                session.SendClientNotice("UIIT_STT_ANTICHEAT_GM_USE_COMMAND");
                                //Block this packet, since we spoof it
                                return PacketProcessResult.ContinueLoop;
                            }
                        }

                    }
                    break;
                default:
                    {
                        Global.logmgr.WriteLog(LogLevel.Notify, "Unknown chat msg id {0}", msgTypeId);
                    }
                    break;
            }

            return PacketProcessResult.DoNothing;
        }
        public static PacketProcessResult HandleClient(Packet pck, RelaySession session, SilkroadServer server)
        {
            if (Global.EnableItemMallBuyFix)
            {
                string uname = session.State["username"] as string;

                if (uname.Length == 0)
                {
                    Global.logmgr.WriteLog(LogLevel.Error, "username len == 0 (request buy mall item)");
                    return PacketProcessResult.Disconnect;

                }

                byte ShopType = pck.ReadUInt8();

                if (ShopType == 24)
                {
                    //------------------------------------------
                    UInt16 uint1 = pck.ReadUInt16();
                    byte b1 = pck.ReadUInt8();
                    byte b2 = pck.ReadUInt8();
                    byte b3 = pck.ReadUInt8();
                    string package_item_codename = pck.ReadAscii();
                    ushort nItems = pck.ReadUInt16();
                    pck.ReadUInt64();
                    uint refpackage_id = pck.ReadUInt32();
                    //------------------------------------------

                    string charname = session.State["charname"] as string;

                    if (charname.Length == 0)
                    {
                        Global.logmgr.WriteLog(LogLevel.Error, "charname len == 0 (buy in item mall)");
                        return PacketProcessResult.Disconnect;
                    }

                    int res = Global.dbmgr.GetBuyMallResult(charname, package_item_codename, nItems);

                    if (res == -1)
                    {
                        //------------------------------------------
                        session.SendClientNotice("UIIT_STT_ANTICHEAT_ITEM_MALL_ERROR");
                        //------------------------------------------
                    }
                    else
                    {
                        //update silk
                        List<int> silk_info = Global.dbmgr.GetSilkDataByUsername(uname);

                        //------------------------------------------
                        Packet resp = new Packet(0x3153);

                        resp.WriteUInt32(silk_info[0]);
                        resp.WriteUInt32(silk_info[1]);
                        resp.WriteUInt32(silk_info[2]);
                        // m_ClientSecurity.Send(resp);
                        session.SendPacketToClient(resp);

                        //------------------------------------------
                        Packet inventory = new Packet(0xB034);
                        inventory.WriteUInt8(1);
                        inventory.WriteUInt8(24);
                        inventory.WriteUInt16(uint1);
                        inventory.WriteUInt8(b1);
                        inventory.WriteUInt8(b2);
                        inventory.WriteUInt8(b3);
                        inventory.WriteUInt8(1);
                        inventory.WriteUInt8(res);
                        inventory.WriteUInt16(nItems);
                        inventory.WriteUInt32(0);
                        //m_ClientSecurity.Send(inventory);
                        session.SendPacketToClient(inventory);
                        //------------------------------------------

                        Packet inventory2 = new Packet(0xB034);
                        inventory2.WriteUInt8(1);
                        inventory2.WriteUInt8(7);
                        inventory2.WriteUInt8((byte)(res));
                        //  m_ClientSecurity.Send(inventory2);
                        session.SendPacketToClient(inventory2);

                        //------------------------------------------

                        //  Global.g_LogManager.WriteLog(LogLevel.Notify, "GetBuyMallResult OK slot {0}", res);
                    }
                    return PacketProcessResult.ContinueLoop;
                }
            }

            return PacketProcessResult.DoNothing;
        }