diff --git a/ErgometerServer/ClientThread.cs b/ErgometerServer/ClientThread.cs index f1dbc42..88d79bf 100644 --- a/ErgometerServer/ClientThread.cs +++ b/ErgometerServer/ClientThread.cs @@ -55,28 +55,50 @@ namespace ErgometerServer break; case NetCommand.CommandType.LOGIN: - if(input.IsDoctor) + if(! server.CheckPassword(input.DisplayName, input.Password)) { - server.ChangeClientToDoctor(client, this); - Console.WriteLine("Doctor connected"); - running = false; + NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session)); + loggedin = false; } else { - name = input.DisplayName; + NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session)); loggedin = true; - Console.WriteLine(name + " (client) connected with password: " + input.Password); - FileHandler.CreateSession(session, name); + + if (input.IsDoctor) + { + server.ChangeClientToDoctor(client, this); + Console.WriteLine("Doctor connected"); + running = false; + } + else + { + name = input.DisplayName; + loggedin = true; + Console.WriteLine(name + " (client) connected with password: " + input.Password); + FileHandler.CreateSession(session, name); + } } break; case NetCommand.CommandType.DATA: - metingen.Add(input.Meting); - server.SendToDoctor(input); + if (loggedin) + { + metingen.Add(input.Meting); + server.SendToDoctor(input); + } + else + NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session)); + break; case NetCommand.CommandType.CHAT: - chat.Add(new ChatMessage(name, input.ChatMessage)); - server.SendToDoctor(input); - Console.WriteLine(name + ": " + input.ChatMessage); + if (loggedin) + { + chat.Add(new ChatMessage(name, input.ChatMessage)); + server.SendToDoctor(input); + Console.WriteLine(name + ": " + input.ChatMessage); + } + else + NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session)); break; case NetCommand.CommandType.LOGOUT: loggedin = false; diff --git a/ErgometerServer/DoctorThread.cs b/ErgometerServer/DoctorThread.cs index 21477a2..0ebbea2 100644 --- a/ErgometerServer/DoctorThread.cs +++ b/ErgometerServer/DoctorThread.cs @@ -12,33 +12,29 @@ namespace ErgometerServer TcpClient client; Server server; - string name; - bool running; - bool loggedin; public DoctorThread(TcpClient client, Server server) { this.client = client; this.server = server; - this.name = "Unknown"; + this.running = false; - this.loggedin = false; } public void run() { running = true; - loggedin = true; - while (loggedin && running) + while (running) { NetCommand input = NetHelper.ReadNetCommand(client); switch (input.Type) { case NetCommand.CommandType.LOGOUT: - loggedin = false; + running = false; + client.Close(); Console.WriteLine("Doctor logged out"); break; case NetCommand.CommandType.CHAT: @@ -49,7 +45,6 @@ namespace ErgometerServer throw new FormatException("Unknown Command"); } } - client.Close(); } public void sendToDoctor(NetCommand command) diff --git a/ErgometerServer/Server.cs b/ErgometerServer/Server.cs index fabc081..29c675a 100644 --- a/ErgometerServer/Server.cs +++ b/ErgometerServer/Server.cs @@ -111,25 +111,39 @@ namespace ErgometerServer private void AddUser(string name, string password) { users.Add(name, password); + FileHandler.SaveUsers(users); } - private bool CheckPassword(string name, string password) + public bool CheckPassword(string name, string password) { string pass; bool isOk = users.TryGetValue(name, out pass); + + Console.WriteLine($"Checking {name}, {password}: found {isOk} with response {pass}"); + if (!isOk) return false; return pass == password; } - private void ChangePassword(string name, string newpassword) + private string GeneratePassword(int len = 8) { + string pass = ""; + char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray(); + + Random rand = new Random(); + + for (int i=0; i<=len; i++) + { + pass += chars[rand.Next(0, chars.Length - 1)]; + } + + return pass; } private static void OnProcessExit(object sender, EventArgs e) { - FileHandler.SaveUsers(users); Console.WriteLine("Closing server"); } }