public void ProcessAdminEnableRequest(string admin, string userToPromote) { if (!adminUsers.Contains(admin)) { return; } if (adminUsers.Contains(userToPromote)) { return; } Logger.Log( $"{admin} has promoted {userToPromote} to admin. Time: {DateTime.Now}"); File.AppendAllLines(adminsPath, new string[] { "\r\n" + userToPromote }); adminUsers.Add(userToPromote); var user = GetByUserID(userToPromote); if (user == null) { return; } var newToken = System.Guid.NewGuid().ToString(); if (!loggedInAdmins.ContainsKey(userToPromote)) { loggedInAdmins.Add(userToPromote, newToken); AdminEnableMessage.Send(user.Connection, newToken); } }
public void CheckAdminState(ConnectedPlayer playerConn, string userid) { //full admin privs for local offline testing for host player if (adminUsers.Contains(userid) || (GameData.Instance.OfflineMode && playerConn.GameObject == PlayerManager.LocalViewerScript.gameObject)) { //This is an admin, send admin notify to the users client Logger.Log($"{playerConn.Username} logged in as Admin. " + $"IP: {playerConn.Connection.address}"); var newToken = System.Guid.NewGuid().ToString(); if (!loggedInAdmins.ContainsKey(userid)) { loggedInAdmins.Add(userid, newToken); AdminEnableMessage.Send(playerConn.Connection, newToken); } } }
//Check if tokens match and if the player is an admin or is banned private async Task <bool> CheckUserState( string unverifiedUserid, string unverifiedToken, ConnectedPlayer unverifiedConnPlayer, string unverifiedClientId) { if (GameData.Instance.OfflineMode) { Logger.Log($"{unverifiedConnPlayer.Username} logged in successfully in offline mode. " + $"userid: {unverifiedUserid}", Category.Admin); return(true); } //allow empty token for local offline testing if (string.IsNullOrEmpty(unverifiedToken) || string.IsNullOrEmpty(unverifiedUserid)) { StartCoroutine(KickPlayer(unverifiedConnPlayer, $"Server Error: Account has invalid cookie.")); Logger.Log($"A user tried to connect with null userid or token value" + $"Details: Username: {unverifiedConnPlayer.Username}, ClientID: {unverifiedClientId}, IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); return(false); } //check if they are already logged in, skip this check if offline mode is enable or if not a release build. if (BuildPreferences.isForRelease) { var otherUser = GetByUserID(unverifiedUserid); if (otherUser != null) { if (otherUser.Connection != null && otherUser.GameObject != null) { if (unverifiedConnPlayer.UserId == unverifiedUserid && unverifiedConnPlayer.Connection != otherUser.Connection) { StartCoroutine( KickPlayer(unverifiedConnPlayer, $"Server Error: You are already logged into this server!")); Logger.Log($"A user tried to connect with another client while already logged in \r\n" + $"Details: Username: {unverifiedConnPlayer.Username}, ClientID: {unverifiedClientId}, IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); return(false); } } } otherUser = GetByConnection(unverifiedConnPlayer.Connection); if (otherUser != null) { StartCoroutine( KickPlayer(unverifiedConnPlayer, $"Server Error: You already have an existing connection with the server!")); Logger.LogWarning($"Warning 2 simultaneous connections from same IP detected\r\n" + $"Details: Unverified Username: {unverifiedConnPlayer.Username}, Unverified ClientID: {unverifiedClientId}, IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); } } var refresh = new RefreshToken { userID = unverifiedUserid, refreshToken = unverifiedToken }; //Assuming this validates it for now var response = await ServerData.ValidateToken(refresh, true); //fail, unless doing local offline testing if (!GameData.Instance.OfflineMode) { if (response == null) { StartCoroutine(KickPlayer(unverifiedConnPlayer, $"Server Error: Server request error")); Logger.Log($"Server request error for " + $"Details: Username: {unverifiedConnPlayer.Username}, ClientID: {unverifiedClientId}, IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); return(false); } } else { if (response == null) { return(false); } } //allow error response for local offline testing if (response.errorCode == 1) { StartCoroutine(KickPlayer(unverifiedConnPlayer, $"Server Error: Account has invalid cookie.")); Logger.Log($"A spoof attempt was recorded. " + $"Details: Username: {unverifiedConnPlayer.Username}, ClientID: {unverifiedClientId}, IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); return(false); } var Userid = unverifiedUserid; var Token = unverifiedToken; //whitelist checking: var lines = File.ReadAllLines(whiteListPath); //Adds server to admin list if not already in it. if (Userid == ServerData.UserID && !adminUsers.Contains(Userid)) { File.AppendAllLines(adminsPath, new string[] { "\r\n" + Userid }); adminUsers.Add(Userid); var user = GetByUserID(Userid); if (user == null) { return(false); } var newToken = System.Guid.NewGuid().ToString(); if (!loggedInAdmins.ContainsKey(Userid)) { loggedInAdmins.Add(Userid, newToken); AdminEnableMessage.Send(user.Connection, newToken); } } //Checks whether the userid is in either the Admins or whitelist AND that the whitelist file has something in it. //Whitelist only activates if whitelist is populated. if (lines.Length > 0 && !adminUsers.Contains(Userid) && !whiteListUsers.Contains(Userid)) { StartCoroutine(KickPlayer(unverifiedConnPlayer, $"Server Error: This account is not whitelisted.")); Logger.Log($"{unverifiedConnPlayer.Username} tried to log in but the account is not whitelisted. " + $"IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); return(false); } //banlist checking: var banEntry = banList?.CheckForEntry(Userid, unverifiedConnPlayer.Connection.address, unverifiedClientId); if (banEntry != null) { var entryTime = DateTime.ParseExact(banEntry.dateTimeOfBan, "O", CultureInfo.InvariantCulture); var totalMins = Mathf.Abs((float)(entryTime - DateTime.Now).TotalMinutes); if (totalMins > (float)banEntry.minutes) { //Old ban, remove it banList.banEntries.Remove(banEntry); SaveBanList(); Logger.Log($"{unverifiedConnPlayer.Username} ban has expired and the user has logged back in.", Category.Admin); } else { //User is still banned: StartCoroutine(KickPlayer(unverifiedConnPlayer, $"Server Error: This account is banned. " + $"You were banned for {banEntry.reason}. This ban has {banEntry.minutes - totalMins} minutes remaining.")); Logger.Log($"{unverifiedConnPlayer.Username} tried to log back in but the account is banned. " + $"IP: {unverifiedConnPlayer.Connection.address}", Category.Admin); return(false); } } Logger.Log($"{unverifiedConnPlayer.Username} logged in successfully. " + $"userid: {Userid}", Category.Admin); return(true); }
//Check if tokens match and if the player is an admin or is banned private async Task <bool> CheckUserState(string userid, string token, ConnectedPlayer playerConn, string clientID) { //Only do the account check on release builds as its not important when developing if (BuildPreferences.isForRelease) { if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(userid)) { StartCoroutine(KickPlayer(playerConn, $"Server Error: Account has invalid cookie.")); Logger.Log($"A user tried to connect with null userid or token value" + $"Details: Username: {playerConn.Username}, ClientID: {clientID}, IP: {playerConn.Connection.address}", Category.Admin); return(false); } var refresh = new RefreshToken { userID = userid, refreshToken = token }; var response = await ServerData.ValidateToken(refresh, true); if (response.errorCode == 1) { StartCoroutine(KickPlayer(playerConn, $"Server Error: Account has invalid cookie.")); Logger.Log($"A spoof attempt was recorded. " + $"Details: Username: {playerConn.Username}, ClientID: {clientID}, IP: {playerConn.Connection.address}", Category.Admin); return(false); } } var banEntry = banList.CheckForEntry(userid); if (banEntry != null) { DateTime entryTime; DateTime.TryParse(banEntry.dateTimeOfBan, out entryTime); if (entryTime.AddMinutes(banEntry.minutes) < DateTime.Now) { //Old ban, remove it banList.banEntries.Remove(banEntry); SaveBanList(); Logger.Log($"{playerConn.Username} ban has expired and the user has logged back in.", Category.Admin); } else { //User is still banned: StartCoroutine(KickPlayer(playerConn, $"Server Error: This account is banned. " + $"Check your initial ban message for expiry time")); Logger.Log($"{playerConn.Username} tried to log back in but the account is banned. " + $"IP: {playerConn.Connection.address}", Category.Admin); return(false); } } if (adminUsers.Contains(userid)) { //This is an admin, send admin notify to the users client Logger.Log($"{playerConn.Username} logged in as Admin. " + $"IP: {playerConn.Connection.address}", Category.Admin); var newToken = System.Guid.NewGuid().ToString(); if (!loggedInAdmins.ContainsKey(userid)) { loggedInAdmins.Add(userid, newToken); AdminEnableMessage.Send(playerConn.GameObject, newToken); } } Logger.Log($"{playerConn.Username} logged in successfully. " + $"userid: {userid}", Category.Admin); return(true); }