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.OnPreAuthenticating wia patch #159

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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 @@ -12,7 +12,7 @@ namespace Exiled.Events.EventArgs.Player
using Interfaces;

using LiteNetLib;

using LiteNetLib.Utils;
using PluginAPI.Events;

#pragma warning disable SA1600 //TODO:
Expand All @@ -27,8 +27,6 @@ namespace Exiled.Events.EventArgs.Player
/// </summary>
public class PreAuthenticatingEventArgs : IExiledEvent
{
private PreauthCancellationData cachedPreauthData = PreauthCancellationData.Accept();

/// <summary>
/// Initializes a new instance of the <see cref="PreAuthenticatingEventArgs"/> class.
/// </summary>
Expand Down Expand Up @@ -105,70 +103,6 @@ public PreAuthenticatingEventArgs(
/// </summary>
public bool IsAllowed { get; private set; } = true;

/// <summary>
/// Gets or sets the cached <see cref="CachedPreauthData"/> that is returned back to the NwPluginAPI.
/// </summary>
internal PreauthCancellationData CachedPreauthData
{
get => cachedPreauthData;
set
{
cachedPreauthData = value;
IsAllowed = false;
}
}

/// <summary>
/// Delays a pre-authentincating player.
/// </summary>
/// <param name="seconds">The seconds of delay.</param>
/// <param name="isForced">Indicates whether the delay is forced or not.</param>
public void Delay(byte seconds, bool isForced) =>
CachedPreauthData = PreauthCancellationData.RejectDelay(seconds, isForced);

/// <summary>
/// Redirects a pre-authentincating player.
/// </summary>
/// <param name="port">The redirection port.</param>
/// <param name="isForced">Indicates whether the redirection is forced or not.</param>
public void Redirect(ushort port, bool isForced) =>
CachedPreauthData = PreauthCancellationData.RejectRedirect(port, isForced);

/// <summary>
/// Rejects a pre-authentincating banned player.
/// </summary>
/// <param name="banReason">The ban reason.</param>>
/// <param name="expiration">The ban <see cref="DateTime"/> expiration.</param>
/// <param name="isForced">Indicates whether the rejection is forced or not.</param>
public void RejectBanned(string banReason, DateTime expiration, bool isForced) =>
CachedPreauthData = PreauthCancellationData.RejectBanned(banReason, expiration, isForced);

/// <summary>
/// Rejects a pre-authentincating banned player.
/// </summary>
/// <param name="banReason">The ban reason.</param>
/// <param name="expiration">The ban expiration.</param>
/// <param name="isForced">Indicates whether the rejection is forced or not.</param>
public void RejectBanned(string banReason, long expiration, bool isForced) =>
CachedPreauthData = PreauthCancellationData.RejectBanned(banReason, expiration, isForced);

/// <summary>
/// Rejects a pre-authentincating player.
/// </summary>
/// <param name="customReason">The rejection custom reason.</param>
/// <param name="isForced">Indicates whether the rejection is forced or not.</param>
public void Reject(string customReason, bool isForced) =>
CachedPreauthData = PreauthCancellationData.Reject(customReason, isForced);

/// <summary>
/// Rejects a pre-authentincating player.
/// </summary>
/// <param name="reason">The <see cref="RejectionReason"/>.</param>
/// <param name="isForced">Indicates whether the rejection is forced or not.</param>
public void Reject(RejectionReason reason, bool isForced) =>
CachedPreauthData = PreauthCancellationData.Reject(reason, isForced);

/*
/// <summary>
/// Delays the connection.
/// </summary>
Expand Down Expand Up @@ -230,6 +164,11 @@ public void Reject(NetDataWriter writer, bool isForced)
/// <param name="isForced">Indicates whether the player has to be rejected forcefully or not.</param>
public void Reject(string rejectionReason, bool isForced) => Reject(RejectionReason.Custom, isForced, rejectionReason);

/// <summary>
/// Rejects a player who's trying to authenticate.
/// </summary>
public void ForceReject() => Reject(RejectionReason.Custom, true, "Rejected By A Plugin");

/// <summary>
/// Rejects a player who's trying to authenticate.
/// </summary>
Expand Down Expand Up @@ -278,12 +217,5 @@ public void Reject(RejectionReason rejectionReason, bool isForced, string custom
else
Request.Reject(rejectData);
}

/// <summary>
/// Disallows the connection without sending any reason. Should only be used when the connection has already been
/// terminated by the plugin itself.
/// </summary>
public void Disallow() => IsAllowed = false;
*/
}
}
27 changes: 2 additions & 25 deletions EXILED/Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,30 +1178,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);
}
}
88 changes: 88 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Player/PreAuthenticating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// -----------------------------------------------------------------------
// <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;
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;

/// <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 && instruction.operand == (object)"{0};{1};{2};{3}");

Label cont = generator.DefineLabel();
newInstructions[index].labels.Add(cont);

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))),
});

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

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