Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using CitizenFX.Core;
- using JetBrains.Annotations;
- using MercuryWorks.Whitelist.Server.Storage;
- using NFive.SDK.Core.Diagnostics;
- using NFive.SDK.Core.Rpc;
- using NFive.SDK.Server.Controllers;
- using NFive.SDK.Server.Events;
- using NFive.SDK.Server.Rpc;
- using NFive.SessionManager.Server;
- using NFive.SessionManager.Server.Models;
- using NFive.SessionManager.Shared;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using NFive.SDK.Core.Models.Player;
- namespace MercuryWorks.Whitelist.Server
- {
- [PublicAPI]
- public class WhitelistController : ConfigurableController<Configuration>
- {
- private List<Models.Whitelist> whitelistedUsers;
- private ConcurrentBag<Tuple<Task, CancellationTokenSource>> threads = new ConcurrentBag<Tuple<Task, CancellationTokenSource>>();
- public WhitelistController(ILogger logger, IEventManager events, IRpcHandler rpc, Configuration configuration) : base(logger, events, rpc, configuration) => Startup();
- public async Task Startup()
- {
- this.Logger.Info($"Whitelist reload interval is every {this.Configuration.ReloadIntervalInMinutes} minutes");
- //this.Rpc.Event("playerConnecting").OnRaw(new Action<Player, string, CallbackDelegate, ExpandoObject>(OnPlayerConnecting));
- this.Events.On<Client, Deferrals>(SessionEvents.ClientConnecting, OnClientConnecting);
- StartThread(ReloadWhitelist, new CancellationTokenSource());
- }
- private void OnClientConnecting(Client client, Deferrals deferrals)
- {
- if (this.whitelistedUsers.Count == 0 || !this.whitelistedUsers.Any(s => s.SteamId.Equals(client.SteamId)))
- {
- this.Rpc.Event(SessionEvents.DisconnectPlayer).Trigger("User is not whitelisted");
- //deferrals.Done("Not Whitelisted");
- this.Logger.Debug("User is not whitelisted");
- }
- }
- private void OnPlayerConnecting([FromSource] Player player, string playerName, CallbackDelegate drop, ExpandoObject callbacks)
- {
- var client = new Client(int.Parse(player.Handle));
- var deferrals = new Deferrals(callbacks, drop);
- if (this.whitelistedUsers.Count == 0 || !this.whitelistedUsers.Any(s => s.SteamId.Equals(client.SteamId)))
- {
- this.Rpc.Event(SessionEvents.DisconnectPlayer).Trigger("User is not whitelisted");
- deferrals.Done("Not Whitelisted");
- this.Logger.Debug("User is not whitelisted");
- }
- }
- private void StartThread(Func<CancellationTokenSource, Task> task, CancellationTokenSource cts)
- {
- this.threads.Add(new Tuple<Task, CancellationTokenSource>(Task.Factory.StartNew(async () => await task(cts)), cts));
- }
- public async Task LoadWhitelist()
- {
- // Flush whitelist
- this.whitelistedUsers = null;
- // Pull whitelisted users from Configuration file
- var whitelistedUsersCfg = this.Configuration.WhitelistedUsers;
- // Pull whitelisted users from Database
- var whitelistedUsersDb = new StorageContext().WhitelistedUsers;
- // Concat lists
- this.whitelistedUsers = whitelistedUsersCfg.Concat(whitelistedUsersDb).ToList();
- // Some logging for testing
- this.Logger.Debug(new Serializer().Serialize(this.whitelistedUsers.ToString()));
- }
- public async Task ReloadWhitelist(CancellationTokenSource cancellationTokenSource)
- {
- while (!cancellationTokenSource.Token.IsCancellationRequested)
- {
- await LoadWhitelist();
- this.Logger.Info("Whitelist Reloaded...");
- // Delay task every ReloadIntervalInMinutes minutes
- await BaseScript.Delay((this.Configuration.ReloadIntervalInMinutes * 60) * 1000);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement