diff --git a/Galactic Colors Control Server/App.config b/Galactic Colors Control Server/App.config index 88fa402..bae5d6d 100644 --- a/Galactic Colors Control Server/App.config +++ b/Galactic Colors Control Server/App.config @@ -1,6 +1,6 @@ - + - + - \ No newline at end of file + diff --git a/Galactic Colors Control Server/Commands/ClearCommand.cs b/Galactic Colors Control Server/Commands/ClearCommand.cs new file mode 100644 index 0000000..7ae4885 --- /dev/null +++ b/Galactic Colors Control Server/Commands/ClearCommand.cs @@ -0,0 +1,23 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class ClearCommand : ICommand + { + public string Name { get { return "clear"; } } + public string DescText { get { return "Clears the console screen."; } } + public string HelpText { get { return "Use /clear to execute Console.Clear()."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return true; } } + public bool IsNoConnect { get { return true; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + Console.Clear(); + Console.Write(">"); + } + } +} diff --git a/Galactic Colors Control Server/Commands/CloseCommand.cs b/Galactic Colors Control Server/Commands/CloseCommand.cs new file mode 100644 index 0000000..1724d31 --- /dev/null +++ b/Galactic Colors Control Server/Commands/CloseCommand.cs @@ -0,0 +1,30 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class CloseCommand : ICommand + { + public string Name { get { return "close"; } } + public string DescText { get { return "Closes server from connections."; } } + public string HelpText { get { return "Use /close to stop connection process"; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if (Program._open) + { + Program._open = false; + Logger.Write("Server closed", Logger.logType.warm); + } + else + { + Utilities.ConsoleWrite("Server already close"); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/ConnectCommand.cs b/Galactic Colors Control Server/Commands/ConnectCommand.cs new file mode 100644 index 0000000..be467a4 --- /dev/null +++ b/Galactic Colors Control Server/Commands/ConnectCommand.cs @@ -0,0 +1,37 @@ +using System; +using System.Net; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class ConnectCommand : ICommand + { + public string Name { get { return "connect"; } } + public string DescText { get { return "Gets an username."; } } + public string HelpText { get { return "Use /connect [username] to start identification"; } } + public bool IsServer { get { return false; } } + public bool IsClient { get { return true; } } + public bool IsNoConnect { get { return true; } } + public int minArgs { get { return 1; } } + public int maxArgs { get { return 1; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if (!Utilities.IsConnect(soc)) + { + Logger.Write("Identifiaction request from " + Utilities.GetName(soc), Logger.logType.debug); + Program.clients[soc].status = 0; + //args[1] = args[1][0].ToString().ToUpper()[0] + args[1].Substring(1); + Program.clients[soc].pseudo = args[1]; + Utilities.Send(soc, "Identified as " + args[1], Utilities.dataType.message); + Utilities.Broadcast(args[1] + "joined the server", Utilities.dataType.message); + Logger.Write("Identified as " + Utilities.GetName(soc) + " form " + ((IPEndPoint)soc.LocalEndPoint).Address.ToString(), Logger.logType.info); + + } + else + { + Utilities.Send(soc, "You are allready " + Utilities.GetName(soc), Utilities.dataType.message); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/CountCommand.cs b/Galactic Colors Control Server/Commands/CountCommand.cs new file mode 100644 index 0000000..3be6f3b --- /dev/null +++ b/Galactic Colors Control Server/Commands/CountCommand.cs @@ -0,0 +1,22 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class CountCommand : ICommand + { + public string Name { get { return "count"; } } + public string DescText { get { return "Counts connected clients."; } } + public string HelpText { get { return "Use /count to show connected clients count and size"; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + Utilities.ConsoleWrite(Program.clients.Count + "/" + Program.config.size); + } + } +} diff --git a/Galactic Colors Control Server/Commands/ExitCommand.cs b/Galactic Colors Control Server/Commands/ExitCommand.cs new file mode 100644 index 0000000..13243c8 --- /dev/null +++ b/Galactic Colors Control Server/Commands/ExitCommand.cs @@ -0,0 +1,34 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class ExitCommand : ICommand + { + public string Name { get { return "exit"; } } + public string DescText { get { return "Leave the program."; } } + public string HelpText { get { return "Use /exit to stop actual program."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return true; } } + public bool IsNoConnect { get { return true; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if (server) + { + Program._run = false; + Utilities.ConsoleWrite("Exit server"); + } + else + { + soc.Shutdown(SocketShutdown.Both); + Logger.Write("Client disconnected from " + Utilities.GetName(soc), Logger.logType.info); + soc.Close(); + Program.clients.Remove(soc); + Logger.Write("Size: " + Program.clients.Count + "/" + Program.config.size, Logger.logType.debug); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/HelpCommand.cs b/Galactic Colors Control Server/Commands/HelpCommand.cs new file mode 100644 index 0000000..780cafb --- /dev/null +++ b/Galactic Colors Control Server/Commands/HelpCommand.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class HelpCommand : ICommand + { + public string Name { get { return "help"; } } + public string DescText { get { return "Shows the help."; } } + public string HelpText { get { return "Use /help [command] to display command help."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return true; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 1; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if(args.Length == 1) + { + List list = new List(); + int maxLen = 0; + foreach (string com in Manager.commands.Keys) + { + if(Manager.CanAccess(Manager.commands[com], soc, server)) + { + list.Add(com); + if(com.Length > maxLen) { maxLen = com.Length; } + } + } + list.Sort(); + string text = "Use /help [command] for more informations." + Environment.NewLine + "Available commands:" + Environment.NewLine; + foreach (var key in list) + { + text += (" " + key + new string(' ', maxLen - key.Length) + " : " + Manager.commands[key].DescText + Environment.NewLine); + } + Utilities.Return(text, soc, server); + } + else + { + if (Manager.commands.ContainsKey(args[1])) + { + if (Manager.CanAccess(Manager.commands[args[1]], soc, server)) + { + Utilities.Return(Manager.commands[args[1]].HelpText, soc, server); + } + else + { + Utilities.Return("Any help for " + args[1], soc, server); + } + } + else + { + Utilities.Return("Any help for " + args[1], soc, server); + } + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/ICommand.cs b/Galactic Colors Control Server/Commands/ICommand.cs new file mode 100644 index 0000000..d1f2cb9 --- /dev/null +++ b/Galactic Colors Control Server/Commands/ICommand.cs @@ -0,0 +1,18 @@ +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public interface ICommand + { + string Name { get; } + string DescText { get; } + string HelpText { get; } + bool IsServer { get; } + bool IsClient { get; } + bool IsNoConnect { get; } + int minArgs { get; } + int maxArgs { get; } + + void Execute(string[] args, Socket soc = null, bool server = false); + } +} diff --git a/Galactic Colors Control Server/Commands/KickCommand.cs b/Galactic Colors Control Server/Commands/KickCommand.cs new file mode 100644 index 0000000..ae52094 --- /dev/null +++ b/Galactic Colors Control Server/Commands/KickCommand.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class KickCommand : ICommand + { + public string Name { get { return "kick"; } } + public string DescText { get { return "Kicks selected client."; } } + public string HelpText { get { return "Use /kick [username] to kick client from server."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return true; } } + public int minArgs { get { return 1; } } + public int maxArgs { get { return 2; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + Socket target = null; + foreach(Socket client in Program.clients.Keys) + { + if(Utilities.GetName(client) == args[1]) { target = client; } + } + if (target != null) + { + Logger.Write(args[1] + " was kick by server.", Logger.logType.info); + if (args.Length > 2) + { + Utilities.Send(target, "/kick " + args[2], Utilities.dataType.message); + Logger.Write("because" + args[1], Logger.logType.debug); + } + else { + Utilities.Send(target, "/kick", Utilities.dataType.message); + } + } + else + { + Utilities.Return("Can't find " + args[1], soc, server); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/ListCommand.cs b/Galactic Colors Control Server/Commands/ListCommand.cs new file mode 100644 index 0000000..76b2c63 --- /dev/null +++ b/Galactic Colors Control Server/Commands/ListCommand.cs @@ -0,0 +1,28 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class ListCommand : ICommand + { + public string Name { get { return "list"; } } + public string DescText { get { return "Lists connected clients."; } } + public string HelpText { get { return "Use /list to display all connected client username or IP."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + string text = " "; + foreach (Socket socket in Program.clients.Keys) + { + text += (Utilities.GetName(socket) + ", "); + } + text = text.Remove(text.Length - 2, 2); + Utilities.ConsoleWrite(text); + } + } +} diff --git a/Galactic Colors Control Server/Commands/LogLevelCommand.cs b/Galactic Colors Control Server/Commands/LogLevelCommand.cs new file mode 100644 index 0000000..4c78019 --- /dev/null +++ b/Galactic Colors Control Server/Commands/LogLevelCommand.cs @@ -0,0 +1,29 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class LogLevelCommand : ICommand + { + public string Name { get { return "loglevel"; } } + public string DescText { get { return "Change console loglevel."; } } + public string HelpText { get { return "Use /loglevel [loglevel] to change Loglevel."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 1; } } + public int maxArgs { get { return 1; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if (Enum.TryParse(args[1], true, out Program.config.logLevel)) + { + Utilities.ConsoleWrite("LogLevel: " + Program.config.logLevel.ToString()); + } + else + { + Utilities.ConsoleWrite("Incorrect argument (debug, info, important, error, fatal)"); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/Manager.cs b/Galactic Colors Control Server/Commands/Manager.cs new file mode 100644 index 0000000..4a7e891 --- /dev/null +++ b/Galactic Colors Control Server/Commands/Manager.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Reflection; + +namespace Galactic_Colors_Control_Server.Commands +{ + class Manager + { + public static Dictionary commands { get; private set; } = new Dictionary(); + + public static void Load() + { + IEnumerable coms = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetInterfaces().Contains(typeof(ICommand)) && x.GetConstructor(Type.EmptyTypes) != null).Select(x => Activator.CreateInstance(x) as ICommand); + foreach (ICommand com in coms) + { + commands.Add(com.Name, com); + Logger.Write("Added command " + com.GetType().Name, Logger.logType.debug); + } + } + + public static void Execute(string[] args, Socket soc = null, bool server = false) + { + if (commands.ContainsKey(args[0])) + { + ICommand command = commands[args[0]]; + if (CanAccess(command,soc,server)) + { + if(args.Length > command.minArgs) + { + if(args.Length - 1 <= command.maxArgs) + { + command.Execute(args, soc, server); + } + else + { + Utilities.Return("Command " + command.Name + " require at most " + command.minArgs + " argument(s).", soc, server); + } + } + else + { + Utilities.Return("Command " + command.Name + " require at least " + command.minArgs + " argument(s).", soc, server); + } + } + else + { + Utilities.Return("Unknown command", soc, server); + } + } + else + { + Utilities.Return("Unknown command", soc, server); + } + } + + public static bool CanAccess(ICommand command, Socket soc = null, bool server = false) + { + if (server) + { + return command.IsServer; + } + else + { + if (command.IsClient) + { + if(Utilities.IsConnect(soc)) + { + return command.IsNoConnect; + } + else + { + return true; + } + } + else + { + return false; + } + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/OpenCommand.cs b/Galactic Colors Control Server/Commands/OpenCommand.cs new file mode 100644 index 0000000..714a55f --- /dev/null +++ b/Galactic Colors Control Server/Commands/OpenCommand.cs @@ -0,0 +1,30 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class OpenCommand : ICommand + { + public string Name { get { return "open"; } } + public string DescText { get { return "Opens server for connections."; } } + public string HelpText { get { return "Use /open to restart connection process"; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if (!Program._open) + { + Program._open = true; + Logger.Write("Server opened", Logger.logType.warm); + } + else + { + Utilities.ConsoleWrite("Server already open"); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/StatusCommand.cs b/Galactic Colors Control Server/Commands/StatusCommand.cs new file mode 100644 index 0000000..6446ea1 --- /dev/null +++ b/Galactic Colors Control Server/Commands/StatusCommand.cs @@ -0,0 +1,28 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class StatusCommand : ICommand + { + public string Name { get { return "status"; } } + public string DescText { get { return "Shows server status."; } } + public string HelpText { get { return "Use /status to display server actual status."; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return false; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + if (Program._open) + { + Utilities.ConsoleWrite("Server open"); + } + else { + Utilities.ConsoleWrite("Server close"); + } + } + } +} diff --git a/Galactic Colors Control Server/Commands/TimeCommand.cs b/Galactic Colors Control Server/Commands/TimeCommand.cs new file mode 100644 index 0000000..6e4bacb --- /dev/null +++ b/Galactic Colors Control Server/Commands/TimeCommand.cs @@ -0,0 +1,22 @@ +using System; +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server.Commands +{ + public class TimeCommand : ICommand + { + public string Name { get { return "time"; } } + public string DescText { get { return "Gives server time."; } } + public string HelpText { get { return "Use /time to display server time. (format is server dependent)"; } } + public bool IsServer { get { return true; } } + public bool IsClient { get { return true; } } + public bool IsNoConnect { get { return false; } } + public int minArgs { get { return 0; } } + public int maxArgs { get { return 0; } } + + public void Execute(string[] args, Socket soc, bool server = false) + { + Utilities.Return(DateTime.Now.ToLongTimeString(), soc, server); + } + } +} diff --git a/Galactic Colors Control Server/Config.cs b/Galactic Colors Control Server/Config.cs new file mode 100644 index 0000000..45c993f --- /dev/null +++ b/Galactic Colors Control Server/Config.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Serialization; + +namespace Galactic_Colors_Control_Server +{ + [XmlRoot("config")] + public class Config + { + public string logPath = AppDomain.CurrentDomain.BaseDirectory + "Logs"; + public Logger.logType logLevel = Logger.logType.info; + public int port = 25001; + public int size = 20; + public ConsoleColor[] logForeColor = new ConsoleColor[5] {ConsoleColor.Gray , ConsoleColor.White, ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.White}; + public ConsoleColor[] logBackColor = new ConsoleColor[5] { ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Black, ConsoleColor.Red }; + + public Config Load() + { + Logger.Write("Loading config", Logger.logType.info); + Config config = new Config(); + if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Config.xml")) + { + if (CorrectConfig()) + { + XmlSerializer xs = new XmlSerializer(typeof(Config)); + using (StreamReader re = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Config.xml")) + { + config = xs.Deserialize(re) as Config; + }; + } + else + { + Logger.Write("Old config in Config.xml.old", Logger.logType.warm); + File.Move(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", AppDomain.CurrentDomain.BaseDirectory + "Config.xml.old"); + config.Save(); + } + } + else + { + Logger.Write("Any config file", Logger.logType.error); + config.Save(); + } + if (Program._debug) { config.logLevel = Logger.logType.debug; } + return config; + } + + public void Save() + { + XmlSerializer xs = new XmlSerializer(typeof(Config)); + if (Program._debug) { logLevel = Logger.logType.info; } + using (StreamWriter st = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Config.xml")) + { + xs.Serialize(st,this); + }; + if (Program._debug) { logLevel = Logger.logType.debug; } + } + + public bool CorrectConfig() + { + bool isCorrect = false; + + using (Stream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "Config.xml", FileMode.Open)) + { + XmlReader re = new XmlTextReader(fs); + XmlSerializer xs = new XmlSerializer(typeof(Config)); + try + { + isCorrect = xs.CanDeserialize(re); + } + catch (XmlException e) + { + isCorrect = false; + Logger.Write("Error: " + e.Message, Logger.logType.error); + } + } + + if (isCorrect) + { + try + { + XmlDocument d = new XmlDocument(); + d.Load(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"); + d.Schemas.Add("", XmlReader.Create("ConfigSchema.xsd")); + + d.Validate((o, e) => + { + Logger.Write("Error: " + e.Message, Logger.logType.error); + isCorrect = false; + }); + } + catch (XmlException e) + { + isCorrect = false; + Logger.Write("Error: " + e.Message, Logger.logType.error); + } + } + + return isCorrect; + } + } +} diff --git a/Galactic Colors Control Server/ConfigSchema.xsd b/Galactic Colors Control Server/ConfigSchema.xsd new file mode 100644 index 0000000..7e1457f --- /dev/null +++ b/Galactic Colors Control Server/ConfigSchema.xsd @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Galactic Colors Control Server/Data.cs b/Galactic Colors Control Server/Data.cs new file mode 100644 index 0000000..587c7a5 --- /dev/null +++ b/Galactic Colors Control Server/Data.cs @@ -0,0 +1,11 @@ +using System.Net.Sockets; + +namespace Galactic_Colors_Control_Server +{ + public class Data + { + public int id; + public int status = -1; + public string pseudo = ""; + } +} diff --git a/Galactic Colors Control Server/Galactic Colors Control Server.csproj b/Galactic Colors Control Server/Galactic Colors Control Server.csproj index 502a45a..7da9eb8 100644 --- a/Galactic Colors Control Server/Galactic Colors Control Server.csproj +++ b/Galactic Colors Control Server/Galactic Colors Control Server.csproj @@ -12,6 +12,21 @@ v4.5.2 512 true + publier\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 1 + 1.0.0.%2a + false + false + true AnyCPU @@ -34,20 +49,47 @@ - - - - - - - + + + + + + + + + + + + + + + + + + + + + Designer + + + + + False + Microsoft .NET Framework 4.5.2 %28x86 et x64%29 + true + + + False + .NET Framework 3.5 SP1 + false +