private static void DoAuthentication(string MessageString, IClientInfo Client) //NewConnection -U Username -P Password. { string UsernameC = AuxiliaryServerWorker.GetElement(MessageString, "-U ", " -P"); string Password = AuxiliaryServerWorker.GetElement(MessageString, "-P ", "."); if (AuxiliaryServerWorker.Usernames.Contains(UsernameC)) { if (AuxiliaryServerWorker.Passwords[AuxiliaryServerWorker.Usernames.IndexOf(UsernameC)] == Password)//Client authenticated successfully { Session NewSession = new Session() { IpPort = Client.RemoteIPv4, Username = UsernameC, Friends = AuxiliaryServerWorker.GetFriendsList(UsernameC), LoginTime = DateTime.Now, ProfilePicture = AuxiliaryServerWorker.GetProfilePicture(UsernameC), Status = AuxiliaryServerWorker.Statuses[AuxiliaryServerWorker.Usernames.IndexOf(UsernameC)] }; AuxiliaryServerWorker.Sessions.Add(NewSession); AntVaultServer.SendBytes(AuxiliaryServerWorker.GetClientID(Client), AuxiliaryServerWorker.MessageByte("/AcceptConnection"));//AcceptConnection AuxiliaryServerWorker.WriteToConsole("[INFO] Client " + UsernameC + " successfully authenticated! Creating new session file..."); foreach (Session Sess in AuxiliaryServerWorker.Sessions) { AntVaultServer.SendBytes(AuxiliaryServerWorker.GetClientIDFromIPPort(Sess.IpPort), AuxiliaryServerWorker.MessageByte("/UserConnected -U " + UsernameC + ".")); } if (File.Exists(UserDirectories + UsernameC + "\\" + UsernameC + "_lastsession.AntSession")) { try { AuxiliaryServerWorker.WriteToConsole("[INFO] Previous session file located. Updating it now..."); File.Delete(UserDirectories + UsernameC + "\\" + UsernameC + "_lastsession.AntSession"); } catch (Exception exc) { AuxiliaryServerWorker.WriteToConsole("[ERROR] Could not update session file for " + UsernameC + " due to " + exc); } } try { File.WriteAllBytes(UserDirectories + UsernameC + "\\" + UsernameC + "_lastsession.AntSession", AuxiliaryServerWorker.GetSessionBytes(NewSession)); AuxiliaryServerWorker.WriteToConsole("[INFO] Successfully appended session file for " + UsernameC); } catch (Exception exc) { AuxiliaryServerWorker.WriteToConsole("[ERROR] Could not append session file for " + UsernameC + " due to " + exc); } } else { AuxiliaryServerWorker.WriteToConsole("[WARN] Client " + UsernameC + " tried to authenticate with a wrong password!"); } } else { AuxiliaryServerWorker.WriteToConsole("[WARN] Address " + Client.RemoteIPv4 + " tried to authenticate with a non-existant username credential!"); } }
private static void Events_ClientDisconnected(IClientInfo Client, SimpleSockets.DisconnectReason e) { string Sender = null; foreach (Session Sess in AuxiliaryServerWorker.Sessions) { if (Sess.IpPort == Client.RemoteIPv4) { Sender = Sess.Username; } } AuxiliaryServerWorker.WriteToConsole("[INFO] Client " + Client.RemoteIPv4 + " disconnected due to " + e); foreach (Session Sess in AuxiliaryServerWorker.Sessions) { if (Sess.IpPort != Client.RemoteIPv4) { AntVaultServer.SendBytes(AuxiliaryServerWorker.GetClientID(Client), AuxiliaryServerWorker.MessageByte("/UserDisonnected -U " + Sender + ".")); } } foreach (Session Sess in AuxiliaryServerWorker.Sessions) { if (Sess.IpPort == Client.RemoteIPv4) { Sess.LogoutTime = DateTime.Now; try { File.Delete(UserDirectories + Sess.Username + "\\" + Sess.Username + "_lastsession.AntSession"); AuxiliaryServerWorker.WriteToConsole("[INFO] Updating session entry for " + Sess.Username); File.WriteAllBytes(UserDirectories + Sess.Username + "\\" + Sess.Username + "_lastsession.AntSession", AuxiliaryServerWorker.GetSessionBytes(Sess)); AuxiliaryServerWorker.WriteToConsole("[INFO] Updated session for " + Sess.Username + " successfully"); AuxiliaryServerWorker.Sessions.Remove(Sess); } catch (Exception exc) { AuxiliaryServerWorker.WriteToConsole("[ERROR] Could not update session entry for " + Sess.Username + " due to" + exc); } break; //No wasted resources } } }