Skip to content

Commit

Permalink
PreAuth patch port
Browse files Browse the repository at this point in the history
  • Loading branch information
xNexusACS committed Oct 28, 2024
1 parent 3dbe837 commit 450bd74
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Exiled.Events/EventArgs/Player/PreAuthenticatingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public PreAuthenticatingEventArgs(
public ConnectionRequest Request { get; }

/// <summary>
/// Gets a value indicating whether the player can be authenticated or not.
/// Gets or sets 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
32 changes: 2 additions & 30 deletions Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Exiled.Events.Handlers
{
using Exiled.API.Features.Pickups;
#pragma warning disable IDE0079
#pragma warning disable IDE0060
#pragma warning disable SA1623 // Property summary documentation should match accessors
Expand All @@ -16,10 +15,6 @@ namespace Exiled.Events.Handlers

using Exiled.Events.Features;

using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;

/// <summary>
/// Player related events.
/// </summary>
Expand Down Expand Up @@ -1218,30 +1213,7 @@ 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);

return ev.CachedPreauthData;
}
/// <param name="ev"><The cref="PreAuthenticatingEventArgs"/> instance.</param>
public static void OnPreAuthenticating(PreAuthenticatingEventArgs ev) => PreAuthenticating.InvokeSafely(ev);
}
}
91 changes: 91 additions & 0 deletions Exiled.Events/Patches/Events/Player/PreAuthenticating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// -----------------------------------------------------------------------
// <copyright file="PreAuthenticating.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Player
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Core.Generic.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;

using HarmonyLib;

using LiteNetLib;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="CustomLiteNetLib4MirrorTransport.ProcessConnectionRequest(ConnectionRequest)" />.
/// Adds the <see cref="Handlers.Player.PreAuthenticating" /> event.
/// </summary>
[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 && (string)instruction.operand == "{0};{1};{2};{3}");

newInstructions.InsertRange(
index,
new[]
{
// 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, 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),
});

foreach (CodeInstruction instruction in newInstructions)
yield return instruction;

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

0 comments on commit 450bd74

Please sign in to comment.