From: AeonLucid Date: Sat, 24 Oct 2020 00:36:11 +0000 (+0200) Subject: Disconnect client when caught hacking X-Git-Tag: v1.2.2~96^2~28 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=4dc12ceff5fe2ee9ed7d1fb6de951cbf40167421;p=rhonda%2Fimpostor.git Disconnect client when caught hacking --- diff --git a/src/Impostor.Api/Innersloth/Net/Objects/InnerPlayerControl.cs b/src/Impostor.Api/Innersloth/Net/Objects/InnerPlayerControl.cs index 57efe8c..58d5750 100644 --- a/src/Impostor.Api/Innersloth/Net/Objects/InnerPlayerControl.cs +++ b/src/Impostor.Api/Innersloth/Net/Objects/InnerPlayerControl.cs @@ -244,8 +244,7 @@ namespace Impostor.Api.Innersloth.Net.Objects if (!sender.Character.PlayerInfo.IsImpostor) { - // TODO: Uncomment - // throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.MurderPlayer)} as crewmate."); + throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.MurderPlayer)} as crewmate."); } var player = reader.ReadNetObject(_game); diff --git a/src/Impostor.Api/Innersloth/Net/Objects/InnerShipStatus.cs b/src/Impostor.Api/Innersloth/Net/Objects/InnerShipStatus.cs index abbbb25..95a76f5 100644 --- a/src/Impostor.Api/Innersloth/Net/Objects/InnerShipStatus.cs +++ b/src/Impostor.Api/Innersloth/Net/Objects/InnerShipStatus.cs @@ -55,7 +55,7 @@ namespace Impostor.Api.Innersloth.Net.Objects if (!sender.Character.PlayerInfo.IsImpostor) { - // throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.CloseDoorsOfType)} as crewmate."); + throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.CloseDoorsOfType)} as crewmate."); } var systemType = (SystemTypes)reader.ReadByte(); @@ -72,7 +72,7 @@ namespace Impostor.Api.Innersloth.Net.Objects if (!sender.Character.PlayerInfo.IsImpostor) { - // throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.RepairSystem)} as crewmate."); + throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.RepairSystem)} as crewmate."); } var systemType = (SystemTypes)reader.ReadByte(); diff --git a/src/Impostor.Api/Net/IHazelConnection.cs b/src/Impostor.Api/Net/IHazelConnection.cs index 282e439..4e6c4b3 100644 --- a/src/Impostor.Api/Net/IHazelConnection.cs +++ b/src/Impostor.Api/Net/IHazelConnection.cs @@ -36,6 +36,6 @@ namespace Impostor.Api.Net /// /// A reason. /// - ValueTask DisconnectAsync(string reason); + ValueTask DisconnectAsync(string? reason); } } \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/S2C/Message01JoinGameS2C.cs b/src/Impostor.Api/Net/Messages/S2C/Message01JoinGameS2C.cs index 159ed2f..e683815 100644 --- a/src/Impostor.Api/Net/Messages/S2C/Message01JoinGameS2C.cs +++ b/src/Impostor.Api/Net/Messages/S2C/Message01JoinGameS2C.cs @@ -19,7 +19,7 @@ namespace Impostor.Api.Net.Messages.S2C writer.EndMessage(); } - public static void SerializeError(IMessageWriter writer, bool clear, DisconnectReason reason, string message = null) + public static void SerializeError(IMessageWriter writer, bool clear, DisconnectReason reason, string? message = null) { if (clear) { @@ -27,7 +27,7 @@ namespace Impostor.Api.Net.Messages.S2C } writer.StartMessage(MessageFlags.JoinGame); - writer.Write((int) reason); + writer.Write((int)reason); if (reason == DisconnectReason.Custom) { diff --git a/src/Impostor.Server/Net/Client.cs b/src/Impostor.Server/Net/Client.cs index 860af7a..93bc4ee 100644 --- a/src/Impostor.Server/Net/Client.cs +++ b/src/Impostor.Server/Net/Client.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Impostor.Api; using Impostor.Api.Games; using Impostor.Api.Innersloth; using Impostor.Api.Innersloth.Data; @@ -65,7 +66,7 @@ namespace Impostor.Server.Net var game = _gameManager.Find(gameCode); if (game == null) { - await SendDisconnectReason(DisconnectReason.GameMissing); + await DisconnectAsync(DisconnectReason.GameMissing); return; } @@ -76,28 +77,28 @@ namespace Impostor.Server.Net case GameJoinError.None: break; case GameJoinError.InvalidClient: - await SendDisconnectReason(DisconnectReason.Custom, "Client is in an invalid state."); + await DisconnectAsync(DisconnectReason.Custom, "Client is in an invalid state."); break; case GameJoinError.Banned: - await SendDisconnectReason(DisconnectReason.Banned); + await DisconnectAsync(DisconnectReason.Banned); break; case GameJoinError.GameFull: - await SendDisconnectReason(DisconnectReason.GameFull); + await DisconnectAsync(DisconnectReason.GameFull); break; case GameJoinError.InvalidLimbo: - await SendDisconnectReason(DisconnectReason.Custom, "Invalid limbo state while joining."); + await DisconnectAsync(DisconnectReason.Custom, "Invalid limbo state while joining."); break; case GameJoinError.GameStarted: - await SendDisconnectReason(DisconnectReason.GameStarted); + await DisconnectAsync(DisconnectReason.GameStarted); break; case GameJoinError.GameDestroyed: - await SendDisconnectReason(DisconnectReason.Custom, DisconnectMessages.Destroyed); + await DisconnectAsync(DisconnectReason.Custom, DisconnectMessages.Destroyed); break; case GameJoinError.Custom: - await SendDisconnectReason(DisconnectReason.Custom, result.Message); + await DisconnectAsync(DisconnectReason.Custom, result.Message); break; default: - await SendDisconnectReason(DisconnectReason.Custom, "Unknown error."); + await DisconnectAsync(DisconnectReason.Custom, "Unknown error."); break; } @@ -149,25 +150,32 @@ namespace Impostor.Server.Net var readerCopy = reader.Slice(reader.Position); // TODO: Return value, either a bool (to cancel) or a writer (to cancel (null) or modify/overwrite). - var verified = await Player.Game.HandleGameDataAsync(readerCopy, Player, toPlayer); - if (verified) + try { - // Broadcast packet to all other players. - using (var writer = MessageWriter.Get(messageType)) + var verified = await Player.Game.HandleGameDataAsync(readerCopy, Player, toPlayer); + if (verified) { - if (toPlayer) + // Broadcast packet to all other players. + using (var writer = MessageWriter.Get(messageType)) { - var target = reader.ReadPackedInt32(); - reader.CopyTo(writer); - await Player.Game.SendToAsync(writer, target); - } - else - { - reader.CopyTo(writer); - await Player.Game.SendToAllExceptAsync(writer, Id); + if (toPlayer) + { + var target = reader.ReadPackedInt32(); + reader.CopyTo(writer); + await Player.Game.SendToAsync(writer, target); + } + else + { + reader.CopyTo(writer); + await Player.Game.SendToAllExceptAsync(writer, Id); + } } } } + catch (ImpostorCheatException e) + { + await DisconnectAsync(DisconnectReason.Hacking, e.Message); + } break; } @@ -223,7 +231,7 @@ namespace Impostor.Server.Net case MessageFlags.GetGameListV2: { Message16GetGameListC2S.Deserialize(reader, out var options); - await OnRequestGameList(options); + await OnRequestGameListAsync(options); break; } @@ -302,7 +310,7 @@ namespace Impostor.Server.Net /// All options given. /// At this moment, the client can only specify the map, impostor count and chat language. /// - private ValueTask OnRequestGameList(GameOptionsData options) + private ValueTask OnRequestGameListAsync(GameOptionsData options) { using var message = MessageWriter.Get(MessageType.Reliable); @@ -317,16 +325,18 @@ namespace Impostor.Server.Net return Connection.SendAsync(message); } - private ValueTask SendDisconnectReason(DisconnectReason reason, string message = null) + private async ValueTask DisconnectAsync(DisconnectReason reason, string message = null) { if (Connection == null) { - return default; + return; } using var packet = MessageWriter.Get(MessageType.Reliable); Message01JoinGameS2C.SerializeError(packet, false, reason, message); - return Connection.SendAsync(packet); + + await Connection.SendAsync(packet); + await Connection.DisconnectAsync(message); } } } \ No newline at end of file