Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. using CitizenFX.Core;
  2. using JetBrains.Annotations;
  3. using MercuryWorks.Whitelist.Server.Storage;
  4. using NFive.SDK.Core.Diagnostics;
  5. using NFive.SDK.Core.Rpc;
  6. using NFive.SDK.Server.Controllers;
  7. using NFive.SDK.Server.Events;
  8. using NFive.SDK.Server.Rpc;
  9. using NFive.SessionManager.Server;
  10. using NFive.SessionManager.Server.Models;
  11. using NFive.SessionManager.Shared;
  12. using System;
  13. using System.Collections.Concurrent;
  14. using System.Collections.Generic;
  15. using System.Dynamic;
  16. using System.Linq;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using NFive.SDK.Core.Models.Player;
  20.  
  21. namespace MercuryWorks.Whitelist.Server
  22. {
  23. [PublicAPI]
  24. public class WhitelistController : ConfigurableController<Configuration>
  25. {
  26.  
  27. private List<Models.Whitelist> whitelistedUsers;
  28.  
  29. private ConcurrentBag<Tuple<Task, CancellationTokenSource>> threads = new ConcurrentBag<Tuple<Task, CancellationTokenSource>>();
  30.  
  31. public WhitelistController(ILogger logger, IEventManager events, IRpcHandler rpc, Configuration configuration) : base(logger, events, rpc, configuration) => Startup();
  32.  
  33.  
  34. public async Task Startup()
  35. {
  36. this.Logger.Info($"Whitelist reload interval is every {this.Configuration.ReloadIntervalInMinutes} minutes");
  37.  
  38. //this.Rpc.Event("playerConnecting").OnRaw(new Action<Player, string, CallbackDelegate, ExpandoObject>(OnPlayerConnecting));
  39. this.Events.On<Client, Deferrals>(SessionEvents.ClientConnecting, OnClientConnecting);
  40.  
  41. StartThread(ReloadWhitelist, new CancellationTokenSource());
  42. }
  43.  
  44.  
  45. private void OnClientConnecting(Client client, Deferrals deferrals)
  46. {
  47. if (this.whitelistedUsers.Count == 0 || !this.whitelistedUsers.Any(s => s.SteamId.Equals(client.SteamId)))
  48. {
  49. this.Rpc.Event(SessionEvents.DisconnectPlayer).Trigger("User is not whitelisted");
  50.  
  51. //deferrals.Done("Not Whitelisted");
  52.  
  53. this.Logger.Debug("User is not whitelisted");
  54. }
  55. }
  56.  
  57.  
  58. private void OnPlayerConnecting([FromSource] Player player, string playerName, CallbackDelegate drop, ExpandoObject callbacks)
  59. {
  60. var client = new Client(int.Parse(player.Handle));
  61. var deferrals = new Deferrals(callbacks, drop);
  62.  
  63. if (this.whitelistedUsers.Count == 0 || !this.whitelistedUsers.Any(s => s.SteamId.Equals(client.SteamId)))
  64. {
  65. this.Rpc.Event(SessionEvents.DisconnectPlayer).Trigger("User is not whitelisted");
  66.  
  67. deferrals.Done("Not Whitelisted");
  68.  
  69. this.Logger.Debug("User is not whitelisted");
  70. }
  71. }
  72.  
  73.  
  74. private void StartThread(Func<CancellationTokenSource, Task> task, CancellationTokenSource cts)
  75. {
  76. this.threads.Add(new Tuple<Task, CancellationTokenSource>(Task.Factory.StartNew(async () => await task(cts)), cts));
  77. }
  78.  
  79.  
  80. public async Task LoadWhitelist()
  81. {
  82. // Flush whitelist
  83. this.whitelistedUsers = null;
  84.  
  85. // Pull whitelisted users from Configuration file
  86. var whitelistedUsersCfg = this.Configuration.WhitelistedUsers;
  87.  
  88. // Pull whitelisted users from Database
  89. var whitelistedUsersDb = new StorageContext().WhitelistedUsers;
  90.  
  91. // Concat lists
  92. this.whitelistedUsers = whitelistedUsersCfg.Concat(whitelistedUsersDb).ToList();
  93.  
  94. // Some logging for testing
  95. this.Logger.Debug(new Serializer().Serialize(this.whitelistedUsers.ToString()));
  96. }
  97.  
  98.  
  99. public async Task ReloadWhitelist(CancellationTokenSource cancellationTokenSource)
  100. {
  101. while (!cancellationTokenSource.Token.IsCancellationRequested)
  102. {
  103. await LoadWhitelist();
  104.  
  105. this.Logger.Info("Whitelist Reloaded...");
  106.  
  107. // Delay task every ReloadIntervalInMinutes minutes
  108. await BaseScript.Delay((this.Configuration.ReloadIntervalInMinutes * 60) * 1000);
  109. }
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement