]> git.deb.at Git - rhonda/impostor.git/commitdiff
Disconnect client when caught hacking
authorAeonLucid <aeonlucid@outlook.com>
Sat, 24 Oct 2020 00:36:11 +0000 (02:36 +0200)
committerAeonLucid <aeonlucid@outlook.com>
Sat, 24 Oct 2020 00:36:11 +0000 (02:36 +0200)
src/Impostor.Api/Innersloth/Net/Objects/InnerPlayerControl.cs
src/Impostor.Api/Innersloth/Net/Objects/InnerShipStatus.cs
src/Impostor.Api/Net/IHazelConnection.cs
src/Impostor.Api/Net/Messages/S2C/Message01JoinGameS2C.cs
src/Impostor.Server/Net/Client.cs

index 57efe8c4bf804406d5ac13d22764bf0d6a84efc9..58d575068985578b50dd8edd16c422627430280f 100644 (file)
@@ -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<InnerPlayerControl>(_game);
index abbbb251e8da60ed837469db4f931811ce05cc34..95a76f56d81e41761467ab6f9af1a77b5a9c3edf 100644 (file)
@@ -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();
index 282e439a48ad052ed9d57776926629ed5c65a5c8..4e6c4b3fd03479a18e2a03a816f9f7c3b31d6c17 100644 (file)
@@ -36,6 +36,6 @@ namespace Impostor.Api.Net
         /// </summary>
         /// <param name="reason">A reason.</param>
         /// <returns></returns>
-        ValueTask DisconnectAsync(string reason);
+        ValueTask DisconnectAsync(string? reason);
     }
 }
\ No newline at end of file
index 159ed2f6fab539ac7222cb3d2d7bdd11a0158dc1..e683815fbfd87997da9e0be5f12a9d27f26c58dc 100644 (file)
@@ -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)
             {
index 860af7ae3e4332a9898717d22e16cdafab247e2c..93bc4eedc178514f3112ff088f1bd305b01eb4f4 100644 (file)
@@ -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.
         /// </param>
-        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