From be67c0949b8f37b2961f2d90fde3e5a34f83850c Mon Sep 17 00:00:00 2001 From: Mariki Date: Mon, 28 Oct 2024 23:07:18 +0300 Subject: [PATCH 1/3] Recoded Player.OnPreAuthenticating wia patch --- .../Player/PreAuthenticatingEventArgs.cs | 2 +- EXILED/Exiled.Events/Handlers/Player.cs | 27 +----- .../Events/Player/PreAuthenticating.cs | 93 +++++++++++++++++++ 3 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs diff --git a/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs index 0b10296f2..9ccc35e91 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs @@ -103,7 +103,7 @@ public PreAuthenticatingEventArgs( /// /// Gets a value indicating whether the player can be authenticated or not. /// - public bool IsAllowed { get; private set; } = true; + public bool IsAllowed { get; set; } = true; /// /// Gets or sets the cached that is returned back to the NwPluginAPI. diff --git a/EXILED/Exiled.Events/Handlers/Player.cs b/EXILED/Exiled.Events/Handlers/Player.cs index 2e6209741..aa9af41a1 100644 --- a/EXILED/Exiled.Events/Handlers/Player.cs +++ b/EXILED/Exiled.Events/Handlers/Player.cs @@ -1167,30 +1167,7 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item /// /// Called before pre-authenticating a . /// - /// - /// - /// - /// - /// - /// - /// - /// - /// Returns the instance. - [PluginEvent(ServerEventType.PlayerPreauth)] - public PreauthCancellationData OnPreAuthenticating( - string userId, - string ipAddress, - long expiration, - CentralAuthPreauthFlags flags, - string country, - byte[] signature, - LiteNetLib.ConnectionRequest request, - int readerStartPosition) - { - PreAuthenticatingEventArgs ev = new(userId, ipAddress, expiration, flags, country, signature, request, readerStartPosition); - PreAuthenticating.InvokeSafely(ev); - - return ev.CachedPreauthData; - } + /// instance. + public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev); } } diff --git a/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs new file mode 100644 index 000000000..3e0b666eb --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -0,0 +1,93 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Events.Player +{ + using System; + using System.Collections.Generic; + using System.Reflection.Emit; + + using API.Features.Pools; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + + using HarmonyLib; + + using Hazards; + using LiteNetLib; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds the event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.PreAuthenticating))] + [HarmonyPatch(typeof(CustomLiteNetLib4MirrorTransport), nameof(CustomLiteNetLib4MirrorTransport.ProcessConnectionRequest))] + internal static class PreAuthenticating + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + Label ret = generator.DefineLabel(); + newInstructions[newInstructions.Count - 1].labels.Add(ret); + + LocalBuilder ev = generator.DeclareLocal(typeof(PreAuthenticatingEventArgs)); + int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldstr && instruction.operand == (object)"{0};{1};{2};{3}"); + + newInstructions.InsertRange( + index, + new CodeInstruction[] + { + // userid + new CodeInstruction(OpCodes.Ldloc_S, 10), + + // ipaddress + new (OpCodes.Ldloc_S, 15), + + // expiration + new (OpCodes.Ldloc_S, 11), + + // flags + new (OpCodes.Ldloc_S, 12), + + // country + new (OpCodes.Ldloc_S, 13), + + // signature + new (OpCodes.Ldloc_S, 14), + + // request + new (OpCodes.Ldarg_1), + + // position + new (OpCodes.Ldloc_S, 9), + + // PreAuthenticatingEventArgs ev = new (userid, ipaddress, expiration, flags, country, signature, request, position) + new (OpCodes.Newobj, GetDeclaredConstructors(typeof(PreAuthenticatingEventArgs))[0]), + new (OpCodes.Dup), + new (OpCodes.Stloc_S, ev.LocalIndex), + + // OnPreAuthenticating(ev) + new (OpCodes.Call, AccessTools.Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPreAuthenticating))), + new (OpCodes.Ldloc_S, ev.LocalIndex), + + // ev.isallowed + new (OpCodes.Callvirt, PropertyGetter(typeof(PreAuthenticatingEventArgs), nameof(PreAuthenticatingEventArgs.IsAllowed))), + + // ret + new (OpCodes.Brfalse_S, ret), + }); + + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; + + ListPool.Pool.Return(newInstructions); + } + } +} \ No newline at end of file From 264d4bdc91ae9b26db0a2f4a5d34d2380801177b Mon Sep 17 00:00:00 2001 From: Mariki Date: Fri, 1 Nov 2024 10:57:18 +0300 Subject: [PATCH 2/3] Removed IsAllowed, so it would not conflict with Reject functions. --- .../Player/PreAuthenticatingEventArgs.cs | 82 ++----------------- .../Events/Player/PreAuthenticating.cs | 11 +-- 2 files changed, 10 insertions(+), 83 deletions(-) diff --git a/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs index 9ccc35e91..311a01891 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs @@ -12,7 +12,7 @@ namespace Exiled.Events.EventArgs.Player using Interfaces; using LiteNetLib; - + using LiteNetLib.Utils; using PluginAPI.Events; #pragma warning disable SA1600 //TODO: @@ -27,8 +27,6 @@ namespace Exiled.Events.EventArgs.Player /// public class PreAuthenticatingEventArgs : IExiledEvent { - private PreauthCancellationData cachedPreauthData = PreauthCancellationData.Accept(); - /// /// Initializes a new instance of the class. /// @@ -103,72 +101,8 @@ public PreAuthenticatingEventArgs( /// /// Gets a value indicating whether the player can be authenticated or not. /// - public bool IsAllowed { get; set; } = true; - - /// - /// Gets or sets the cached that is returned back to the NwPluginAPI. - /// - internal PreauthCancellationData CachedPreauthData - { - get => cachedPreauthData; - set - { - cachedPreauthData = value; - IsAllowed = false; - } - } - - /// - /// Delays a pre-authentincating player. - /// - /// The seconds of delay. - /// Indicates whether the delay is forced or not. - public void Delay(byte seconds, bool isForced) => - CachedPreauthData = PreauthCancellationData.RejectDelay(seconds, isForced); - - /// - /// Redirects a pre-authentincating player. - /// - /// The redirection port. - /// Indicates whether the redirection is forced or not. - public void Redirect(ushort port, bool isForced) => - CachedPreauthData = PreauthCancellationData.RejectRedirect(port, isForced); - - /// - /// Rejects a pre-authentincating banned player. - /// - /// The ban reason.> - /// The ban expiration. - /// Indicates whether the rejection is forced or not. - public void RejectBanned(string banReason, DateTime expiration, bool isForced) => - CachedPreauthData = PreauthCancellationData.RejectBanned(banReason, expiration, isForced); + public bool IsAllowed { get; private set; } = true; - /// - /// Rejects a pre-authentincating banned player. - /// - /// The ban reason. - /// The ban expiration. - /// Indicates whether the rejection is forced or not. - public void RejectBanned(string banReason, long expiration, bool isForced) => - CachedPreauthData = PreauthCancellationData.RejectBanned(banReason, expiration, isForced); - - /// - /// Rejects a pre-authentincating player. - /// - /// The rejection custom reason. - /// Indicates whether the rejection is forced or not. - public void Reject(string customReason, bool isForced) => - CachedPreauthData = PreauthCancellationData.Reject(customReason, isForced); - - /// - /// Rejects a pre-authentincating player. - /// - /// The . - /// Indicates whether the rejection is forced or not. - public void Reject(RejectionReason reason, bool isForced) => - CachedPreauthData = PreauthCancellationData.Reject(reason, isForced); - - /* /// /// Delays the connection. /// @@ -230,6 +164,11 @@ public void Reject(NetDataWriter writer, bool isForced) /// Indicates whether the player has to be rejected forcefully or not. public void Reject(string rejectionReason, bool isForced) => Reject(RejectionReason.Custom, isForced, rejectionReason); + /// + /// Rejects a player who's trying to authenticate. + /// + public void ForceReject() => Reject(RejectionReason.Custom, true, "Rejected By A Plugin"); + /// /// Rejects a player who's trying to authenticate. /// @@ -278,12 +217,5 @@ public void Reject(RejectionReason rejectionReason, bool isForced, string custom else Request.Reject(rejectData); } - - /// - /// Disallows the connection without sending any reason. Should only be used when the connection has already been - /// terminated by the plugin itself. - /// - public void Disallow() => IsAllowed = false; - */ } } \ No newline at end of file diff --git a/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs index 3e0b666eb..cf3279d06 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -36,10 +36,12 @@ private static IEnumerable Transpiler(IEnumerable instruction.opcode == OpCodes.Ldstr && instruction.operand == (object)"{0};{1};{2};{3}"); + Label cont = generator.DefineLabel(); + newInstructions[index].labels.Add(cont); + newInstructions.InsertRange( index, new CodeInstruction[] @@ -75,13 +77,6 @@ private static IEnumerable Transpiler(IEnumerable Date: Sat, 23 Nov 2024 10:03:20 +0100 Subject: [PATCH 3/3] Update PreAuthenticating.cs --- .../Patches/Events/Player/PreAuthenticating.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs b/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs index cf3279d06..9c81f8f27 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs @@ -1,6 +1,6 @@ // ----------------------------------------------------------------------- -// -// Copyright (c) Exiled Team. All rights reserved. +// +// Copyright (c) ExMod Team. All rights reserved. // Licensed under the CC BY-SA 3.0 license. // // ----------------------------------------------------------------------- @@ -85,4 +85,4 @@ private static IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); } } -} \ No newline at end of file +}