예제 #1
0
 public HandleClient(GarbageWindow _gw)
 {
     gw = _gw;
 }
예제 #2
0
        private void DoChat()
        {
            var joinTime     = DateTime.Now;
            var requestCount = 0;
            var error        = 0;
            var username     = $"User_{new Random().Next(0, 1000)}";

            GarbageWindow.Log($"Client #{_clNo} connected, IP: {_clientSocket.Client.RemoteEndPoint}");
            while (error < 1)
            {
                Thread.Sleep(100);
                try
                {
                    requestCount = requestCount + 1;
                    var networkStream = _clientSocket.GetStream();
                    var bytesFrom     = new byte[_clientSocket.ReceiveBufferSize];
                    networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                    var utf8string = Encoding.UTF8.GetString(bytesFrom).TrimEnd('\0');

                    // GarbageWindow.Log(utf8string);
                    if (utf8string.Contains("GET") && utf8string.Contains("HTTP/1.1"))
                    {
                        var sendBytes = Encoding.UTF8.GetBytes("ARCANECHAT_SERVER.HTTP_BOT_FOUND_EXCEPTION: Nice attempt to connect to this server using a web browser, real clever...\nDid you really thing I am **THAT** stupid?");
                        try
                        {
                            var sendBytes2 = Encoding.UTF8.GetBytes("\n\n\nException occurred:\n");
                            networkStream.Write(sendBytes2, 0, sendBytes2.Length);
                            sendBytes = Encoding.UTF8.GetBytes(System.Convert.ToBase64String(sendBytes));
                            networkStream.Write(sendBytes, 0, sendBytes.Length);
                        }
                        catch
                        {
                        }
                        networkStream.Close();
                        GarbageWindow.Clients.Remove(_clientSocket);
                        GarbageWindow.BroadcastMessage("Kicked user attempting HTTP tapping");
                        break;
                    }
                    string dataFromClient = Encoding.Unicode.GetString(bytesFrom).TrimEnd('\0');
                    GarbageWindow.Log($"#{_clNo}: \"{dataFromClient}\"");
                    if (dataFromClient.StartsWith("\0CLIMSG\0"))
                    {
                        switch (dataFromClient.Replace("\0CLIMSG\0", ""))
                        {
                        case "exit":
                            GarbageWindow.Clients.Remove(_clientSocket);
                            _clientSocket.Close();
                            GarbageWindow.BroadcastMessage($"Client { _clNo } logged off!");
                            break;
                        }
                    }
                    else if (dataFromClient.StartsWith("\0cmd\0"))
                    {
                        switch (dataFromClient.Replace("\0cmd\0", "").Split(' ')[0].ToLower().Trim())
                        {
                        case "nick":
                            username = dataFromClient.Replace("\0cmd\0nick", "");
                            break;

                        case "logon":
                            username = dataFromClient.Replace("\0cmd\0logon", "");
                            GarbageWindow.BroadcastMessage($"Welcome {username} (#{_clNo} since server launch, {GarbageWindow.Clients.Count} currently logged in)");
                            break;
                        }
                    }
                    else if (dataFromClient.StartsWith("/"))
                    {
                        switch ((dataFromClient.Remove(0, 1) + " ").Split(' ')[0].ToLower().Trim())
                        {
                        case "cli":
                            GarbageWindow.BroadcastMessage("*working*");
                            GarbageWindow.BroadcastMessage($"SERVER BROADCAST: --CLIENTS CONNECTED: {GarbageWindow.Clients.Count}--");
                            break;

                        case "nick":
                            username = dataFromClient.Split(" ".ToCharArray(), 2)[1];
                            GarbageWindow.WhisperMessage(_clientSocket, "Your nickname has been changed to " + username);
                            break;

                        case "serverinfo":
                            GarbageWindow.BroadcastMessage($"Server memory usage: {GC.GetTotalMemory(false)} bytes\nConnection count: {GarbageWindow.Clients.Count}\nUptime: {GarbageWindow.StopWatch.Elapsed.ToString()}\nHost machine name: {Environment.MachineName}");
                            break;

                        case "userinfo":
                            GarbageWindow.BroadcastMessage($"Client number: {_clNo}\nJoin time: {joinTime.ToLongTimeString()}\nConnection time: {DateTime.Now.Subtract(joinTime)}");
                            break;

                        case "clilist":
                            var clis = GarbageWindow.Clients;
                            foreach (var cli in clis)
                            {
                                GarbageWindow.BroadcastMessage($"Client #{_clNo}: {username} ");
                            }
                            break;
                        }
                    }
                    else
                    {
                        GarbageWindow.BroadcastMessage($"{_clNo} - {username}: {dataFromClient}");
                    }
                }
                catch (Exception ex)
                {
                    GarbageWindow.Log("Exception occurred with a client: " + ex.StackTrace);
                    _clientSocket.Close();

                    error++;
                }
            }
        }