コード例 #1
0
ファイル: Functions.cs プロジェクト: orf53975/HenkChat
        public static void Error(Exception Error, HenkChatServer Server, bool Critical = false)
        {
            string ErrorMessage = $"[{Server.S_Name} {DateTime.Now.ToString("yyyy-MM h:mm:ss")}] {Error.Message}";

            Log("[Error]" + Error.Message, Server);
            File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrorLog.txt"), Environment.NewLine + ErrorMessage);

            if (Critical)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(ErrorMessage);
                Console.ForegroundColor = ConsoleColor.White;

                Server.Server.OnError -= Server.OnError;
                try { Program.Servers.Remove(Server.S_Name); } catch { }
                try { Server.Server.Stop(); } catch { }
                try { Server.Server = null; } catch { }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ErrorMessage);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }
コード例 #2
0
ファイル: Functions.cs プロジェクト: orf53975/HenkChat
        public static void Print(string Text, HenkChatServer Server, ConsoleColor Color = ConsoleColor.Green)
        {
            Console.ForegroundColor = Color;
            Console.WriteLine($"[{Server.S_Name} {DateTime.Now.ToString("h:mm:ss")}] {Text}");
            Console.ForegroundColor = ConsoleColor.White;

            Log(Text, Server);
        }
コード例 #3
0
 public ConfigLoader(string ServerFolder, HenkChatServer Server, out int Port, out int MaxConnections)
 {
     if (File.Exists(Path.Combine(ServerFolder, "Config.conf")))
     {
         _LoadConfig(Path.Combine(ServerFolder, "Config.conf"), Server, out Port, out MaxConnections);
     }
     else
     {
         _CreateNewConfig(Path.Combine(ServerFolder, "Config.conf")); _LoadConfig(Path.Combine(ServerFolder, "Config.conf"), Server, out Port, out MaxConnections);
     }
 }
コード例 #4
0
ファイル: Functions.cs プロジェクト: orf53975/HenkChat
        public static void Ban(TcpClient Client, HenkChatServer Server)
        {
            string IP = ((IPEndPoint)Client.Client.RemoteEndPoint).Address.ToString();

            if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BannedIps.txt")))
            {
                File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BannedIps.txt"), IP);
            }
            else
            {
                File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BannedIps.txt"), Environment.NewLine + IP);
            }

            Server.Server.BannedIps.Add(IP);
            Kick(Client);
            Print($"Banned {IP}", Server, ConsoleColor.Blue);
        }
コード例 #5
0
 private static string _Handle(Message e, string Command, HenkChatServer Server, Database Database)
 {
     if (Command.Equals("!help"))
     {
         if (Server.UserList[e.TcpClient.GetHashCode()].Admin)
         {
             return("!users                                         displays the online users\n!admin {admin password}          become admin\n!kick {username}                         kick a user\n!ban {username}                         give a user an ipban");
         }
         else
         {
             return("!users                                         displays the online users\n!admin {admin password}          become admin");
         }
     }
     if (Command.Equals("!admin"))
     {
         return(Server.UserList[e.TcpClient.GetHashCode()].Admin ? "You are an admin" : "You are not an admin");
     }
     else if (Command.StartsWith("!admin "))
     {
         if (Server.S_AdminPassword.Equals(Command.Remove(0, 7)))
         {
             User x = Server.UserList[e.TcpClient.GetHashCode()];
             x.Admin = true;
             Server.UserList[e.TcpClient.GetHashCode()] = x;
             return("You are now admin");
         }
         else
         {
             return("Invalid password");
         }
     }
     else if (Command.Equals("!cleardata"))
     {
         if (!Server.UserList[e.TcpClient.GetHashCode()].Admin)
         {
             return("You need to be admin to do this");
         }
         Database.Clear();
         return("All messages are deleted from database");
     }
     else
     {
         return("Error, invalid command.");
     }
 }
コード例 #6
0
 public ClientHandler(HenkChatServer Server) => this.Server = Server;
コード例 #7
0
 private static bool _IsLoggedIn(int ID, HenkChatServer Server) => Server.UserList[ID].Login;
コード例 #8
0
 private static byte[] _Decrypt(byte[] Data, TcpClient Client, HenkChatServer Server) => HenkTcp.Encryption.Decrypt(Aes.Create(), Data, Server.UserList[Client.GetHashCode()].Key);
コード例 #9
0
        public static void Handle(Message e, HenkChatServer Server, Database Database)
        {
            if (e.Data[2].Equals(1))//Users
            {
                if (e.Data.Length > 3)
                {
                    byte[] UserBytes = new byte[e.Data.Length - 3];
                    Buffer.BlockCopy(e.Data, 3, UserBytes, 0, UserBytes.Length);

                    int User = BitConverter.ToInt32(UserBytes);
                    if (User > Server.UserList.Count)
                    {
                        return;
                    }

                    e.Reply(Server.UserList.ElementAt(User).Value.Name);
                }
                else
                {
                    e.Reply(_Encrypt(BitConverter.GetBytes(Server.UserList.Where(x => x.Value.Name != null).Count()), e.TcpClient, Server));
                }
            }
            else if (e.Data[2].Equals(2))//kick
            {
                if (!Server.UserList[e.TcpClient.GetHashCode()].Admin)
                {
                    e.Reply(_Encrypt(Encoding.UTF8.GetBytes("You need to be admin to do this"), e.TcpClient, Server)); return;
                }
                byte[] UserName = new byte[e.Data.Length - 3];
                Buffer.BlockCopy(e.Data, 3, UserName, 0, UserName.Length);

                byte[] AdminName = Server.UserList.Values.First(x => x.TcpClient == e.TcpClient).Name;
                if (_BytesEquals(AdminName, UserName))
                {
                    e.Reply(_Encrypt(Encoding.UTF8.GetBytes("You could not kick yourself"), e.TcpClient, Server)); return;
                }

                if (Server.UserList.Values.Any(x => _BytesEquals(x.Name, UserName)))
                {
                    Functions.Kick(Server.UserList.Values.First(x => _BytesEquals(x.Name, UserName)).TcpClient); e.Reply(_Encrypt(Encoding.UTF8.GetBytes("Kicked user"), e.TcpClient, Server));
                }
                else
                {
                    e.Reply(_Encrypt(Encoding.UTF8.GetBytes("Invalid user"), e.TcpClient, Server));
                }
            }
            else if (e.Data[2].Equals(3))//ban
            {
                if (!Server.UserList[e.TcpClient.GetHashCode()].Admin)
                {
                    e.Reply(_Encrypt(Encoding.UTF8.GetBytes("You need to be admin to do this"), e.TcpClient, Server)); return;
                }
                byte[] UserName = new byte[e.Data.Length - 3];
                Buffer.BlockCopy(e.Data, 3, UserName, 0, UserName.Length);

                byte[] AdminName = Server.UserList.Values.First(x => x.TcpClient == e.TcpClient).Name;
                if (_BytesEquals(AdminName, UserName))
                {
                    e.Reply(_Encrypt(Encoding.UTF8.GetBytes("You could not ban yourself"), e.TcpClient, Server)); return;
                }

                if (Server.UserList.Values.Any(x => _BytesEquals(x.Name, UserName)))
                {
                    Functions.Kick(Server.UserList.Values.First(x => _BytesEquals(x.Name, UserName)).TcpClient); Server.Server.BannedIps.Add(((IPEndPoint)e.TcpClient.Client.RemoteEndPoint).Address.ToString()); e.Reply(_Encrypt(Encoding.UTF8.GetBytes("Banned user"), e.TcpClient, Server));
                }
                else
                {
                    e.Reply(_Encrypt(Encoding.UTF8.GetBytes("Invalid user"), e.TcpClient, Server));
                }
            }
            else
            {
                byte[] Command = new byte[e.Data.Length - 2];
                Buffer.BlockCopy(e.Data, 2, Command, 0, Command.Length);

                e.Reply(_Encrypt(Encoding.UTF8.GetBytes(_Handle(e, Encoding.UTF8.GetString(_Decrypt(Command, e.TcpClient, Server)), Server, Database)), e.TcpClient, Server));
            }
        }
コード例 #10
0
        private void _LoadConfig(string Path, HenkChatServer Server, out int Port, out int MaxConnections)
        {
            Port = 0; MaxConnections = 0;

            foreach (var Line in File.ReadAllLines(Path))
            {
                if (Line[0].Equals('#'))
                {
                    continue;
                }
                else if (Line.StartsWith("Name="))
                {
                    string Name = Line.Remove(0, 5);
                    if (Name.Length < 1 || Name.Length > 50)
                    {
                        Functions.Error(new Exception("You entered a invalid name, the name must be between 1 and 50 characters."), Server, false); Name = DEFAULT_NAME;
                    }

                    Server.S_Name      = Name;
                    Server.S_NameBytes = Encoding.UTF8.GetBytes(Name);
                }
                else if (Line.StartsWith("Port="))
                {
                    try
                    {
                        Port = int.Parse(Line.Remove(0, 5));
                        if (Port > 65535 || Port <= 0)
                        {
                            Functions.Error(new Exception("You entered a invalid port."), Server, false); Port = DEFAULT_PORT;
                        }
                        if (Port.Equals(Program.NameServerPort))
                        {
                            Functions.Error(new Exception("Port already used by nameserver"), Server, false); Port = DEFAULT_PORT;
                        }
                    }
                    catch { Functions.Error(new Exception("You entered a invalid port."), Server, false); Port = DEFAULT_PORT; }
                }
                else if (Line.StartsWith("MaxConnections="))
                {
                    try
                    {
                        MaxConnections = int.Parse(Line.Remove(0, 15));
                        if (MaxConnections < 0)
                        {
                            MaxConnections = DEFAULT_MAXCONNECTIONS;
                        }
                    }
                    catch { Functions.Error(new Exception("You entered a invalid MaxConnections count."), Server, false); MaxConnections = DEFAULT_MAXCONNECTIONS; }
                }
                else if (Line.StartsWith("AdvancedLog="))
                {
                    if (Line.Remove(0, 12).Equals("true"))
                    {
                        Server.AdvancedLog = true;
                    }
                    else
                    {
                        Server.AdvancedLog = false;
                    }
                }
                else if (Line.StartsWith("Password="******"AdminPassword="******"Salt="))
                {
                    Server.S_Salt = Convert.FromBase64String(Line.Remove(0, 5));
                }
            }

            if (string.IsNullOrEmpty(Server.S_Name))
            {
                Server.S_Name = DEFAULT_NAME;
            }
            if (Port == 0)
            {
                Port = DEFAULT_PORT;
            }
            if (MaxConnections == 0)
            {
                MaxConnections = DEFAULT_MAXCONNECTIONS;
            }
            if (Server.S_Password == null)
            {
                Functions.Error(new Exception("Could not find the password in the config file"), Server, true);
            }
            if (string.IsNullOrEmpty(Server.S_AdminPassword))
            {
                Functions.Error(new Exception("Could not find the admin password in the config file"), Server, true);
            }
            if (Server.S_Salt == null)
            {
                Functions.Error(new Exception("Could not find the salt in the config file"), Server, true);
            }
        }
コード例 #11
0
 public DataHandler(HenkChatServer _Server, string ServerFolder)
 {
     this._Server = _Server; _Database.Open(ServerFolder);
 }
コード例 #12
0
ファイル: Functions.cs プロジェクト: orf53975/HenkChat
 public static void Log(string Text, HenkChatServer Server) => File.AppendAllTextAsync(Path.Combine(Server.ServerFolder, "Log.txt"), $"[{DateTime.Now.ToString("yyyy-MM h:mm:ss")}] {Text}{Environment.NewLine}");
コード例 #13
0
ファイル: Functions.cs プロジェクト: orf53975/HenkChat
 public static void Broadcast(Dictionary <int, User> UserList, byte[] Message, HenkChatServer Server)
 {
     foreach (var User in UserList.Values.Where(x => x.Login))
     {
         try { User.TcpClient.GetStream().Write(Message, 0, Message.Length); }
         catch { Error(new Exception("Error while sending message to tcpclient"), Server); }
     }
 }