using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using GameNetworkLayer.Shared; namespace CLre_server.API.MainServer { public class Moderator { private static Moderator _instance = null; public static Moderator Instance { get { if (_instance == null) Init(); return _instance; } } internal static void Init() { if (_instance == null) _instance = new Moderator(); } private ModerationEngine _moderationEngine; private Moderator() { _moderationEngine = new ModerationEngine(); Server.Instance.PlayerConnect += (sender, args) => { #if DEBUG Utility.Logging.MetaLog($"Player {args.PlayerId} is connecting, starting ban checker"); #endif CheckConnectingPlayerAsap(args.PlayerId).Run(); }; } public bool DisconnectPlayerById(string publicId) { int? playerId = _moderationEngine.FindConnectedPlayerById(publicId); if (playerId.HasValue) { UserVerification.Instance.DisconnectPlayer(playerId.Value, NetworkDispatcherCode.GameDataVerificationFail); return true; } return false; } public bool DisconnectPlayerByName(string name) { int? playerId = _moderationEngine.FindConnectedPlayerByName(name); if (playerId.HasValue) { UserVerification.Instance.DisconnectPlayer(playerId.Value, NetworkDispatcherCode.GameDataVerificationFail); return true; } return false; } public bool BanPlayerById(string publicId) { List bans = new List(CLre.Config.bans); if (!bans.Contains(publicId)) { bans.Add(publicId); CLre.Config.bans = bans.ToArray(); } return DisconnectPlayerById(publicId); } public bool BanPlayerByName(string name) { List bans = new List(CLre.Config.bans); if (!bans.Contains(name)) { bans.Add(name); CLre.Config.bans = bans.ToArray(); } return DisconnectPlayerByName(name); } public bool IsModerator(string name) { foreach (string modName in CLre.Config.moderators) { if (string.Compare(name, modName, StringComparison.InvariantCultureIgnoreCase) == 0) { return true; } } Guid? publicId = _moderationEngine.FindConnectedPlayerGuidByName(name); if (publicId.HasValue) { foreach (string modGuid in CLre.Config.moderators) { if (modGuid == publicId.ToString()) { return true; } } } return false; } public IEnumerator CheckConnectingPlayerAsap(int playerId) { while (Server.Instance.Players.Length <= playerId) { yield return null; yield return null; yield return null; yield return null; } var connector = Server.Instance.Players[playerId]; if (CLre.Config.bans.Contains(connector.accountId.displayName) || CLre.Config.bans.Contains(connector.accountId.publicId.ToString())) { #if DEBUG Utility.Logging.MetaLog($"Banned player {connector.accountId.displayName} ({connector.accountId.publicId}) tried to connect, kicking"); #endif UserVerification.Instance.DisconnectPlayer(playerId, NetworkDispatcherCode.GameDataVerificationFail); } #if DEBUG else { Utility.Logging.MetaLog($"Player {connector.accountId.displayName} ({connector.accountId.publicId}) is not banned, skipping auto-kick"); } #endif } } }