Ejemplo n.º 1
0
        private void CacheDBHandler(ListenerPacket pck, NetworkStream stream, string hst, int bck)
        {
            var db = SchumixBase.DManager.Query("SELECT Id, ServerId, ServerName, Name, Password, Vhost, Flag FROM admins");

            if (!db.IsNull())
            {
                int i      = 0;
                var packet = new ListenerPacket();
                packet.Write <int>((int)ListenerOpcode.SMSG_CACHE_DB);

                foreach (DataRow row in db.Rows)
                {
                    i++;
                    int    Id         = row["Id"].ToInt32();
                    int    ServerId   = row["ServerId"].ToInt32();
                    string ServerName = row["ServerName"].ToString();
                    string Name       = row["Name"].ToString();
                    string Password   = row["Password"].ToString();
                    string Vhost      = row["Vhost"].ToString();
                    int    Flag       = row["Flag"].ToInt32();
                    packet.Write <string>(string.Format("INSERT INTO `admins` VALUES (\"{0}\", \"{1}\", \"{2}\", \"{3}\", \"{4}\", \"{5}\", \"{6}\"", Id, ServerId, ServerName, Name, Password, Vhost, Flag));
                }

                if (i > 0)
                {
                    SendPacketBack(packet, stream, hst, bck);
                }
            }
        }
Ejemplo n.º 2
0
        private void AuthRequestPacketHandler(ListenerPacket pck, NetworkStream stream, string hst, int bck)
        {
            // opcode is already read, DO _NOT_ READ AGAIN
            string guid = pck.Read <string>();
            string hash = pck.Read <string>();

            if (hash != sUtilities.Md5(ListenerConfig.Password))
            {
                Log.Warning("AuthHandler", sLConsole.GetString("Auth unsuccessful! Guid of client: {0}"), guid);
                Log.Debug("Security", sLConsole.GetString("Hash was: {0}"), hash);
                Log.Notice("AuthHandler", sLConsole.GetString("Back port is: {0}"), bck);
                var packet = new ListenerPacket();
                packet.Write <int>((int)ListenerOpcode.SMSG_AUTH_DENIED);
                packet.Write <int>((int)0);
                SendPacketBack(packet, stream, hst, bck);
            }
            else
            {
                if (!_HostList.ContainsKey(hst + SchumixBase.Colon + bck))
                {
                    _HostList.Add(hst + SchumixBase.Colon + bck, new Host(stream, guid, hst, bck));
                }

                _HostList[hst + SchumixBase.Colon + bck].LastPing = DateTime.Now;
                Log.Success("AuthHandler", sLConsole.GetString("Auth successful. Guid of client: {0}"), guid);
                Log.Debug("Security", sLConsole.GetString("Hash was: {0}"), hash);
                Log.Notice("AuthHandler", sLConsole.GetString("Back port is: {0}"), bck);
                var packet = new ListenerPacket();
                packet.Write <int>((int)ListenerOpcode.SMSG_AUTH_APPROVED);
                packet.Write <int>((int)1);
                SendPacketBack(packet, stream, hst, bck);
            }
        }
Ejemplo n.º 3
0
        private void SchumixVersionHandler(ListenerPacket pck, NetworkStream stream, string hst, int bck)
        {
            var packet = new ListenerPacket();

            packet.Write <int>((int)ListenerOpcode.SMSG_SCHUMIX_VERSION);
            packet.Write <string>(sUtilities.GetVersion());
            SendPacketBack(packet, stream, hst, bck);
        }
Ejemplo n.º 4
0
        private void PongHandler(ListenerPacket pck, NetworkStream stream, string hst, int bck)
        {
            _HostList[hst + SchumixBase.Colon + bck].LastPing = DateTime.Now;

            var packet = new ListenerPacket();

            packet.Write <int>((int)ListenerOpcode.SMSG_PONG);
            SendPacketBack(packet, stream, hst, bck);
        }
Ejemplo n.º 5
0
        public void SendPacketBack(ListenerPacket packet, NetworkStream stream, string hst, int backport)
        {
            Log.Debug("PacketHandler", "SendPacketBack(): host is: " + hst + ", port is: " + backport);

            if (stream.CanWrite)
            {
                var buff = new UTF8Encoding().GetBytes(packet.GetNetMessage());
                stream.Write(buff, 0, buff.Length);
                stream.Flush();
            }
        }
Ejemplo n.º 6
0
        public void SendPacketBackAllHost(ListenerPacket packet)
        {
            if (_HostList.Count == 0)
            {
                return;
            }

            foreach (var list in _HostList)
            {
                SendPacketBack(packet, list.Value.Stream, list.Key.Split(SchumixBase.Colon)[0], list.Key.Split(SchumixBase.Colon)[1].ToInt32());
            }
        }
Ejemplo n.º 7
0
        private void UpdateDBHandler(ListenerPacket pck, NetworkStream stream, string hst, int bck)
        {
            foreach (var list in pck.GetBuffer())
            {
                // ide még jöhetnek részek amik majd például a csatornák frissítését idézi elő
                if (list == ListenerOpcode.CMSG_UPDATE_DB.ToString())
                {
                    continue;
                }

                SchumixBase.DManager.ExecuteNonQuery(list);
            }
        }
Ejemplo n.º 8
0
        public void HandlePacket(ListenerPacket packet, TcpClient client, NetworkStream stream)
        {
            var hst = client.Client.RemoteEndPoint.ToString().Split(SchumixBase.Colon)[0];
            int bck = client.Client.RemoteEndPoint.ToString().Split(SchumixBase.Colon)[1].ToInt32();

            int packetid = 0;

            try
            {
                packetid = packet.Read <int>();
            }
            catch (Exception)
            {
                var packet2 = new ListenerPacket();
                packet2.Write <int>((int)ListenerOpcode.SCMSG_PACKET_NULL);
                packet2.Write <string>(sLConsole.GetString("Wrong packetid, aborting connection!"));
                SendPacketBack(packet2, stream, hst, bck);
                Log.Warning("SchumixPacketHandler", sLConsole.GetString("Wrong packetid, aborting connection!"));
                return;
            }

            Log.Debug("SchumixPacketHandler", sLConsole.GetString("Got packet with ID: {0} from: {1}"), packetid, client.Client.RemoteEndPoint);

            if (!_AuthList.ContainsKey(hst + SchumixBase.Colon + bck))
            {
                if (packetid != (int)ListenerOpcode.CMSG_REQUEST_AUTH)
                {
                    var packet2 = new ListenerPacket();
                    packet2.Write <int>((int)ListenerOpcode.SMSG_AUTH_DENIED);
                    packet2.Write <int>((int)0);
                    SendPacketBack(packet2, stream, hst, bck);
                    return;
                }
                else
                {
                    _AuthList.Add(hst + SchumixBase.Colon + bck, true);
                }
            }

            if (PacketMethodMap.ContainsKey((ListenerOpcode)packetid))
            {
                PacketMethodMap[(ListenerOpcode)packetid].Method.Invoke(packet, stream, hst, bck);
                return;
            }
            else
            {
                Log.Notice("SchumixPacketHandler", sLConsole.GetString("Received unhandled packetid: {0}"), packetid);
            }
        }
Ejemplo n.º 9
0
        private void CloseHandler(ListenerPacket pck, NetworkStream stream, string hst, int bck)
        {
            if (_HostList.ContainsKey(hst + SchumixBase.Colon + bck))
            {
                _HostList.Remove(hst + SchumixBase.Colon + bck);
            }

            if (_AuthList.ContainsKey(hst + SchumixBase.Colon + bck))
            {
                _AuthList.Remove(hst + SchumixBase.Colon + bck);
            }

            string guid = pck.Read <string>();

            Log.Warning("CloseHandler", sLConsole.GetString("Connection closed! Guid of client: {0}"), guid);
        }
Ejemplo n.º 10
0
        private void HandlePingTimer(object sender, ElapsedEventArgs e)
        {
            var rlist = new  Dictionary <string, Host>();

            foreach (var list in _HostList)
            {
                if ((DateTime.Now - list.Value.LastPing).Minutes >= 5)                // 5 minute
                {
                    rlist.Add(list.Key, list.Value);
                    var packet = new ListenerPacket();
                    packet.Write <int>((int)ListenerOpcode.SMSG_CLOSE_CONNECTION);
                    packet.Write <string>(sLConsole.GetString("Ping Timeout"));
                    SendPacketBack(packet, list.Value.Stream, list.Key.Split(SchumixBase.Colon)[0], list.Key.Split(SchumixBase.Colon)[1].ToInt32());
                    Log.Warning("HandlePingTimer", sLConsole.GetString("Connection closed! Guid of client: {0}"), list.Value.Guid);
                }
            }

            foreach (var l in rlist)
            {
                if (_HostList.ContainsKey(l.Value.Hst + SchumixBase.Colon + l.Value.Bck))
                {
                    _HostList.Remove(l.Value.Hst + SchumixBase.Colon + l.Value.Bck);
                }

                if (_AuthList.ContainsKey(l.Value.Hst + SchumixBase.Colon + l.Value.Bck))
                {
                    _AuthList.Remove(l.Value.Hst + SchumixBase.Colon + l.Value.Bck);
                }
            }

            if (rlist.Count > 0)
            {
                rlist.Clear();
            }

            foreach (var list in _HostList)
            {
                var packet = new ListenerPacket();
                packet.Write <int>((int)ListenerOpcode.SMSG_PING);
                packet.Write <long>((long)sUtilities.UnixTime);
                SendPacketBack(packet, list.Value.Stream, list.Key.Split(SchumixBase.Colon)[0], list.Key.Split(SchumixBase.Colon)[1].ToInt32());
            }
        }
Ejemplo n.º 11
0
        public void ClientHandler(object ob)
        {
            var client = (ob as TcpClient);
            var stream = client.GetStream();

            byte[] message_buffer = new byte[262144];
            int    bytes_read;

            Log.Notice("SchumixListener", sLConsole.GetString("Handling client..."));

            while (true)
            {
                bytes_read = 0;

                // read
                if (stream.DataAvailable && stream.CanRead)
                {
                    Log.Debug("SchumixListener", sLConsole.GetString("Stream data available, reading."));
                    bytes_read = stream.Read(message_buffer, 0, message_buffer.Length);

                    if (SchumixBase.ExitStatus)
                    {
                        return;
                    }

                    if (bytes_read == 0)
                    {
                        Log.Warning("SchumixListener", sLConsole.GetString("Lost connection!"));
                        break;
                    }

                    var encoding = new UTF8Encoding();
                    var msg      = encoding.GetString(message_buffer, 0, bytes_read);
                    var packet   = new ListenerPacket(msg);
                    sSchumixPacketHandler.HandlePacket(packet, client, stream);
                }

                Thread.Sleep(100);
            }

            Log.Warning("SchumixListener", sLConsole.GetString("The data's processing is completed."));
        }