Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recoded Player.PreAuthenticating wia Patch #2786

Merged
merged 7 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public PreAuthenticatingEventArgs(
/// <summary>
/// Gets a value indicating whether the player can be authenticated or not.
/// </summary>
public bool IsAllowed { get; private set; } = true;
public bool IsAllowed { get; set; } = true;

/// <summary>
/// Gets or sets the cached <see cref="CachedPreauthData"/> that is returned back to the NwPluginAPI.
Expand Down
26 changes: 2 additions & 24 deletions Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,30 +1120,8 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item
/// <summary>
/// Called before pre-authenticating a <see cref="API.Features.Player"/>.
/// </summary>
/// <param name="userId"><inheritdoc cref="PreAuthenticatingEventArgs.UserId"/></param>
/// <param name="ipAddress"><inheritdoc cref="PreAuthenticatingEventArgs.IpAddress"/></param>
/// <param name="expiration"><inheritdoc cref="PreAuthenticatingEventArgs.Expiration"/></param>
/// <param name="flags"><inheritdoc cref="PreAuthenticatingEventArgs.Flags"/></param>
/// <param name="country"><inheritdoc cref="PreAuthenticatingEventArgs.Country"/></param>
/// <param name="signature"><inheritdoc cref="PreAuthenticatingEventArgs.Signature"/></param>
/// <param name="request"><inheritdoc cref="PreAuthenticatingEventArgs.Request"/></param>
/// <param name="readerStartPosition"><inheritdoc cref="PreAuthenticatingEventArgs.ReaderStartPosition"/></param>
/// <returns>Returns the <see cref="PreauthCancellationData"/> instance.</returns>
[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);
/// <param name="ev"><The cref="PreAuthenticatingEventArgs"/> instance.</param>
public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev);

return ev.CachedPreauthData;
}
}
}
82 changes: 82 additions & 0 deletions Exiled.Events/Patches/Events/Player/PreAuthenticating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@


namespace Exiled.Events.Patches.Events.Player
xNexusACS marked this conversation as resolved.
Show resolved Hide resolved
{
using System;
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Pools;
using Exiled.API.Features;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;
using HarmonyLib;
using Hazards;
using LiteNetLib;
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="CustomLiteNetLib4MirrorTransport.ProcessConnectionRequest(ConnectionRequest)" />.
/// Adds the <see cref="Handlers.Player.PreAuthenticating" /> event.
/// </summary>
///
xNexusACS marked this conversation as resolved.
Show resolved Hide resolved
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.PreAuthenticating))]
[HarmonyPatch(typeof(CustomLiteNetLib4MirrorTransport), nameof(CustomLiteNetLib4MirrorTransport.ProcessConnectionRequest))]
internal static class PreAuthenticating
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.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 == "{0};{1};{2};{3}");

newInstructions.InsertRange(
index,
new CodeInstruction[]
{

new CodeInstruction(OpCodes.Ldloc_S, 10),
//ipaddress
new CodeInstruction(OpCodes.Ldloc_S, 15),
//expiration
new CodeInstruction(OpCodes.Ldloc_S, 11),
//flags
new CodeInstruction(OpCodes.Ldloc_S, 12),
//country
new CodeInstruction(OpCodes.Ldloc_S, 13),
//signature
new CodeInstruction(OpCodes.Ldloc_S, 14),
//request
new CodeInstruction(OpCodes.Ldarg_1),
//position
new CodeInstruction(OpCodes.Ldloc_S, 9),

//PreAuthenticatingEventArgs ev = new (userid, ipaddress, expiration, flags, country, signature, request, position)
new CodeInstruction(OpCodes.Newobj, GetDeclaredConstructors(typeof(PreAuthenticatingEventArgs))[0]),
new CodeInstruction(OpCodes.Dup),
new CodeInstruction(OpCodes.Stloc_S, ev.LocalIndex),

new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPreAuthenticating))),
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex),
// if ev.IsAllowed==false


new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(PreAuthenticatingEventArgs), nameof(PreAuthenticatingEventArgs.IsAllowed))),
new CodeInstruction(OpCodes.Brfalse_S, ret),



});
xNexusACS marked this conversation as resolved.
Show resolved Hide resolved


for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
Loading