Пример #1
0
        private void AuthRequestPacketHandler(SchumixPacket 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(ServerConfigs.Password))
            {
                if (_HostList.ContainsKey(hst + SchumixBase.Colon + bck))
                {
                    _HostList.Remove(hst + SchumixBase.Colon + bck);
                }

                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 SchumixPacket();
                packet.Write <int>((int)Opcode.SMSG_AUTH_DENIED);
                packet.Write <int>((int)0);
                SendPacketBack(packet, stream, hst, bck);
            }
            else
            {
                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 SchumixPacket();
                packet.Write <int>((int)Opcode.SMSG_AUTH_APPROVED);
                packet.Write <int>((int)1);
                SendPacketBack(packet, stream, hst, bck);
            }
        }
Пример #2
0
 public void SendPacketBackAllHost(SchumixPacket packet)
 {
     foreach (var list in _HostList)
     {
         SendPacketBack(packet, list.Value, list.Key.Split(SchumixBase.Colon)[0], list.Key.Split(SchumixBase.Colon)[1].ToInt32());
     }
 }
Пример #3
0
 /// <summary>
 /// The auth denied packet handler. (SMSG_AUTH_DENIED)
 /// </summary>
 /// <param name='pck'>
 /// Packet.
 /// </param>
 /// <param name='hst'>
 /// Host.
 /// </param>
 public void AuthDeniedHandler(SchumixPacket pck, string hst)
 {
     Log.Error("SchumixServer", sLConsole.GetString("Authentication denied to SCS server!"));
     Log.Warning("CloseHandler", sLConsole.GetString("Connection closed!"));
     Log.Warning("CloseHandler", sLConsole.GetString("Program shutting down!"));
     Thread.Sleep(1000);
     Environment.Exit(1);
 }
Пример #4
0
        public void SendPacketBack(SchumixPacket 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();
            }
        }
Пример #5
0
        public void HandlePacket(SchumixPacket 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 SchumixPacket();
                packet2.Write <int>((int)Opcode.SCMSG_PACKET_NULL);
                packet2.Write <string>(sLConsole.GetString("Wrong packetid, aborting connection!"));
                SendPacketBack(packet2, stream, hst, bck);
                Log.Warning("HandlePacket", sLConsole.GetString("Wrong packetid, aborting connection!"));
                return;
            }

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

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

            if (!_HostList.ContainsKey(hst + SchumixBase.Colon + bck))
            {
                _HostList.Add(hst + SchumixBase.Colon + bck, stream);
            }

            if (PacketMethodMap.ContainsKey((Opcode)packetid))
            {
                PacketMethodMap[(Opcode)packetid].Method.Invoke(packet, stream, hst, bck);
                return;
            }
            else
            {
                Log.Notice("HandlePacket", sLConsole.GetString("Received unhandled packetid: {0}"), packetid);
            }
        }
Пример #6
0
        private void ScsRandomHandler(SchumixPacket pck, NetworkStream stream, string hst, int bck)
        {
            var rand   = new Random();
            int random = rand.Next();

            Log.Notice("Random", sLConsole.GetString("Sending random value: {0}"), random);

            var packet = new SchumixPacket();

            packet.Write <int>((int)Opcode.SMSG_SEND_SCS_RANDOM);
            packet.Write <int>(random);
            SendPacketBack(packet, stream, hst, bck);
        }
Пример #7
0
        public void HandlePacket(SchumixPacket 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 SchumixPacket();
                packet2.Write<int>((int)Opcode.SCMSG_PACKET_NULL);
                packet2.Write<string>(sLConsole.GetString("Wrong packetid, aborting connection!"));
                SendPacketBack(packet2, stream, hst, bck);
                Log.Warning("HandlePacket", sLConsole.GetString("Wrong packetid, aborting connection!"));
                return;
            }

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

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

            if(!_HostList.ContainsKey(hst + SchumixBase.Colon + bck))
                _HostList.Add(hst + SchumixBase.Colon + bck, stream);

            if(PacketMethodMap.ContainsKey((Opcode)packetid))
            {
                PacketMethodMap[(Opcode)packetid].Method.Invoke(packet, stream, hst, bck);
                return;
            }
            else
                Log.Notice("HandlePacket", sLConsole.GetString("Received unhandled packetid: {0}"), packetid);
        }
Пример #8
0
        public static void ServerDisconnect(bool Reconnect = true)
        {
            if (!ServerConfig.Enabled)
            {
                return;
            }

            var packet = new SchumixPacket();

            packet.Write <int>((int)Opcode.CMSG_CLOSE_CONNECTION);
            packet.Write <string>(_guid.ToString());
            packet.Write <string>(SchumixConfig.ConfigFile);
            packet.Write <string>(SchumixConfig.ConfigDirectory);
            packet.Write <string>(Encoding.UTF8.BodyName);
            packet.Write <string>(LocalizationConfig.Locale);
            packet.Write <string>(Reconnect.ToString());
            packet.Write <string>(ServerIdentify);
            ClientSocket.SendPacketToSCS(packet);
        }
Пример #9
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("ClientHandler", sLConsole.GetString("Handling client..."));

            while (true)
            {
                bytes_read = 0;

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

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

                    if (Exit)
                    {
                        return;
                    }

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

                Thread.Sleep(100);
            }

            Log.Warning("ClientHandler", sLConsole.GetString("Program shutting down!"));
            Environment.Exit(1);
        }
Пример #10
0
        private void CloseHandler(SchumixPacket 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>();
            string file      = pck.Read <string>();
            string dir       = pck.Read <string>();
            string ce        = pck.Read <string>();
            string locale    = pck.Read <string>();
            string reconnect = pck.Read <string>();
            string identify  = pck.Read <string>();

            Log.Warning("CloseHandler", sLConsole.GetString("Connection closed! Guid of client: {0}"), guid);

            if (hst != "127.0.0.1")
            {
                return;
            }

            if (!reconnect.ToBoolean())
            {
                return;
            }

            Log.Notice("CloseHandler", sLConsole.GetString("Restart in progress..."));

            if (sSchumix._processlist.ContainsKey(identify))
            {
                sSchumix.Start(file, dir, ce, locale, sUtilities.GetRandomString());
            }

            sSchumix._processlist.Remove(identify);
        }
Пример #11
0
        public static void Shutdown(Exception eventArgs = null)
        {
            sUtilities.RemovePidFile();
            sListener.Exit        = true;
            Console.CursorVisible = true;

            if (!eventArgs.IsNull())
            {
                Log.Error("Main", sLConsole.GetString("An unhandled exception has been thrown. ({0})"), eventArgs.Message);
                sCrashDumper.CreateCrashDump(eventArgs);
            }

            var packet = new SchumixPacket();

            packet.Write <int>((int)Opcode.SMSG_CLOSE_CONNECTION);
            packet.Write <int>((int)0);
            sServerPacketHandler.SendPacketBackAllHost(packet);

            Thread.Sleep(2000);
            sRuntime.Exit();
        }
Пример #12
0
        /// <summary>
        /// Sends the packet to SCS.
        /// </summary>
        /// <param name='packet'>
        /// Packet.
        /// </param>
        public static void SendPacketToSCS(SchumixPacket packet)
        {
            if(!ServerConfig.Enabled)
                return;

            try
            {
                if(client.Connected)
                {
                    Log.Debug("SchumixServer", sLConsole.GetString("Connected. Sending packet."));
                    var encoder = new UTF8Encoding();
                    byte[] buffer = encoder.GetBytes(packet.GetNetMessage());
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();
                    Log.Debug("SchumixServer", sLConsole.GetString("Packet sent."));
                }
            }
            catch
            {
                Log.Error("SchumixServer", sLConsole.GetString("Couldn't send SCS packet!"));
            }
        }
Пример #13
0
        private void AuthRequestPacketHandler(SchumixPacket 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(ServerConfigs.Password))
            {
                if(_HostList.ContainsKey(hst + SchumixBase.Colon + bck))
                    _HostList.Remove(hst + SchumixBase.Colon + bck);

                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 SchumixPacket();
                packet.Write<int>((int)Opcode.SMSG_AUTH_DENIED);
                packet.Write<int>((int)0);
                SendPacketBack(packet, stream, hst, bck);
            }
            else
            {
                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 SchumixPacket();
                packet.Write<int>((int)Opcode.SMSG_AUTH_APPROVED);
                packet.Write<int>((int)1);
                SendPacketBack(packet, stream, hst, bck);
            }
        }
Пример #14
0
 /// <summary>
 /// The SCS Random number handler. (SMSG_SEND_SCS_RANDOM)
 /// </summary>
 /// <param name='pck'>
 /// Packet.
 /// </param>
 /// <param name='hst'>
 /// Host.
 /// </param>
 public void ScsRandHandler(SchumixPacket pck, string hst)
 {
     // read random value.
     var rand = pck.Read<int>();
     Log.Notice("Random", sLConsole.GetString("SCS sent random: {0}"), rand);
 }
Пример #15
0
 private void NickNameHandler(SchumixPacket pck, NetworkStream stream, string hst, int bck)
 {
 }
Пример #16
0
        private void CloseHandler(SchumixPacket 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>();
            string file = pck.Read<string>();
            string dir = pck.Read<string>();
            string ce = pck.Read<string>();
            string locale = pck.Read<string>();
            string reconnect = pck.Read<string>();
            string identify = pck.Read<string>();
            Log.Warning("CloseHandler", sLConsole.GetString("Connection closed! Guid of client: {0}"), guid);

            if(hst != "127.0.0.1")
                return;

            if(!reconnect.ToBoolean())
                return;

            Log.Notice("CloseHandler", sLConsole.GetString("Restart in progress..."));

            if(sSchumix._processlist.ContainsKey(identify))
                sSchumix.Start(file, dir, ce, locale, sUtilities.GetRandomString());

            sSchumix._processlist.Remove(identify);
        }
Пример #17
0
        private void ScsRandomHandler(SchumixPacket pck, NetworkStream stream, string hst, int bck)
        {
            var rand = new Random();
            int random = rand.Next();
            Log.Notice("Random", sLConsole.GetString("Sending random value: {0}"), random);

            var packet = new SchumixPacket();
            packet.Write<int>((int)Opcode.SMSG_SEND_SCS_RANDOM);
            packet.Write<int>(random);
            SendPacketBack(packet, stream, hst, bck);
        }
Пример #18
0
 private void NickNameHandler(SchumixPacket pck, NetworkStream stream, string hst, int bck)
 {
 }
Пример #19
0
        public static void ServerDisconnect(bool Reconnect = true)
        {
            if(!ServerConfig.Enabled)
                return;

            var packet = new SchumixPacket();
            packet.Write<int>((int)Opcode.CMSG_CLOSE_CONNECTION);
            packet.Write<string>(_guid.ToString());
            packet.Write<string>(SchumixConfig.ConfigFile);
            packet.Write<string>(SchumixConfig.ConfigDirectory);
            packet.Write<string>(Encoding.UTF8.BodyName);
            packet.Write<string>(LocalizationConfig.Locale);
            packet.Write<string>(Reconnect.ToString());
            packet.Write<string>(ServerIdentify);
            ClientSocket.SendPacketToSCS(packet);
        }
Пример #20
0
        /// <summary>
        /// Handles the packet.
        /// </summary>
        /// <param name='packet'>
        /// Packet.
        /// </param>
        /// <param name='client'>
        /// Client.
        /// </param>
        public void HandlePacket(SchumixPacket packet, TcpClient client)
        {
            var hst = client.Client.RemoteEndPoint.ToString().Split(SchumixBase.Colon)[0];
            int packetid = 0;

            try
            {
                packetid = packet.Read<int>();
            }
            catch(Exception)
            {
                var packet2 = new SchumixPacket();
                packet2.Write<int>((int)Opcode.SCMSG_PACKET_NULL);
                packet2.Write<string>(sLConsole.GetString("Wrong packetid, aborting connection!"));
                ClientSocket.SendPacketToSCS(packet);
                Log.Warning("ClientPacketHandler", sLConsole.GetString("Wrong packetid, aborting connection!"));
                return;
            }

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

            if(PacketMethodMap.ContainsKey((Opcode)packetid))
            {
                PacketMethodMap[(Opcode)packetid].Method.Invoke(packet, hst);
                return;
            }
            else
                Log.Notice("ClientPacketHandler", sLConsole.GetString("Received unhandled packetid: {0}"), packetid);
        }
Пример #21
0
        public static void Shutdown(Exception eventArgs = null)
        {
            sUtilities.RemovePidFile();
            sListener.Exit = true;
            Console.CursorVisible = true;

            if(!eventArgs.IsNull())
            {
                Log.Error("Main", sLConsole.GetString("An unhandled exception has been thrown. ({0})"), eventArgs.Message);
                sCrashDumper.CreateCrashDump(eventArgs);
            }

            var packet = new SchumixPacket();
            packet.Write<int>((int)Opcode.SMSG_CLOSE_CONNECTION);
            packet.Write<int>((int)0);
            sServerPacketHandler.SendPacketBackAllHost(packet);

            Thread.Sleep(2000);
            sRuntime.Exit();
        }
Пример #22
0
 public void SendPacketBackAllHost(SchumixPacket packet)
 {
     foreach(var list in _HostList)
         SendPacketBack(packet, list.Value, list.Key.Split(SchumixBase.Colon)[0], list.Key.Split(SchumixBase.Colon)[1].ToInt32());
 }
Пример #23
0
        /// <summary>
        /// Start listening.
        /// </summary>
        public void Socket()
        {
            Log.Notice("ClientSocket", sLConsole.GetString("Successfully started the ClientSocket."));
            Log.Notice("ClientSocket", sLConsole.GetString("Client connection from: {0}"), client.Client.RemoteEndPoint);
            var client_thread = new Thread(new ParameterizedThreadStart(ClientHandler));
            client_thread.Start(client);
            Thread.Sleep(50);

            var packet = new SchumixPacket();
            packet.Write<int>((int)Opcode.CMSG_REQUEST_AUTH);
            packet.Write<string>(SchumixBase.GetGuid().ToString());
            packet.Write<string>(sUtilities.Md5(_password));
            packet.Write<string>(SchumixBase.ServerIdentify);
            SendPacketToSCS(packet);
        }
Пример #24
0
        /// <summary>
        /// Client handler procedure.
        /// </summary>
        /// <param name='ob'>
        /// The object passed with ParameterizedThreadStart (a TcpClient)
        /// </param>
        public void ClientHandler(object ob)
        {
            client = (ob as TcpClient);
            stream = client.GetStream();
            byte[] message_buffer = new byte[262144];
            int bytes_read;
            Log.Notice("ClientHandler", sLConsole.GetString("Handling client..."));

            while(true)
            {
                bytes_read = 0;

                // read
                if(stream.DataAvailable && stream.CanRead)
                {
                    Log.Debug("ClientHandler", 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("ClientHandler", sLConsole.GetString("Lost connection!"));
                        break;
                    }

                    var encoding = new UTF8Encoding();
                    var msg = encoding.GetString(message_buffer, 0, bytes_read);
                    var packet = new SchumixPacket(msg);
                    sClientPacketHandler.HandlePacket(packet, client);
                }

                Thread.Sleep(100);
            }

            Log.Warning("ClientHandler", sLConsole.GetString("Program shutting down!"));
            Environment.Exit(1);
        }
Пример #25
0
        private void CloseHandler(SchumixPacket pck, string hst)
        {
            if(SchumixBase.ExitStatus)
                return;

            Log.Warning("CloseHandler", sLConsole.GetString("Connection closed!"));
            Log.Warning("CloseHandler", sLConsole.GetString("Program shutting down!"));
            SchumixBase.Quit();

            foreach(var nw in INetwork.WriterList)
            {
                if(!nw.Value.IsNull())
                    nw.Value.WriteLine("QUIT :Server killed.");
            }

            Thread.Sleep(1000);
            sRuntime.Exit();
        }
Пример #26
0
 /// <summary>
 /// The auth approved packet handler. (SMSG_AUTH_APPROVED)
 /// </summary>
 /// <param name='pck'>
 /// Packet.
 /// </param>
 /// <param name='hst'>
 /// Host.
 /// </param>
 public void AuthApprovedHandler(SchumixPacket pck, string hst)
 {
     Log.Success("SchumixServer", sLConsole.GetString("Successfully authed to SCS server."));
     SchumixBase.ThreadStop = false;
 }
Пример #27
0
        public void SendPacketBack(SchumixPacket 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();
            }
        }