Skip to content

Commit

Permalink
Make hacks detection API not suck so much (Thanks Venk)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Apr 16, 2021
1 parent 1ee8bcc commit 2fd79dd
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 75 deletions.
2 changes: 1 addition & 1 deletion MCGalaxy/Games/CTF/CtfGame.Round.cs
Expand Up @@ -59,7 +59,7 @@ public sealed partial class CTFGame : RoundsGame {

Player[] opponents = opposing.Members.Items;
foreach (Player other in opponents) {
if (!MovementCheck.InRange(p, other, dist)) continue;
if (!InRange(p, other, dist)) continue;
CtfData otherData = Get(other);

otherData.TagCooldown = true;
Expand Down
7 changes: 3 additions & 4 deletions MCGalaxy/Games/GameProps.cs
Expand Up @@ -36,9 +36,8 @@ public class GameProps {

//Zombie
public bool Referee = false;

internal List<DateTime> NoclipLog = new List<DateTime>(5);
internal List<DateTime> SpeedhackLog = new List<DateTime>(5);
internal DateTime LastNoclipWarn, LastSpeedhackWarn;

public NoclipDetector Noclip;
public SpeedhackDetector Speed;
}
}
76 changes: 76 additions & 0 deletions MCGalaxy/Games/HacksDetection.cs
@@ -0,0 +1,76 @@
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.osedu.org/licenses/ECL-2.0
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;

namespace MCGalaxy.Games {

public abstract class HacksDetector {
protected List<DateTime> log = new List<DateTime>(5);
protected DateTime lastWarn;
protected Player player;

public HacksDetector(Player p) { player = p; }

protected void Warn(string action) {
DateTime now = DateTime.UtcNow;
if (now < lastWarn) return;

player.Message("&4Do not {0} &W- ops have been warned.", action);
Chat.MessageFromOps(player, "λNICK &4appears to be " + action + "ing");
Logger.Log(LogType.SuspiciousActivity, "{0} appears to be {1}ing", player.name, action);
lastWarn = now.AddSeconds(5);
}

protected static TimeSpan interval = TimeSpan.FromSeconds(5);
}

public sealed class SpeedhackDetector : HacksDetector {

public SpeedhackDetector(Player p) : base(p) { }

public bool Detect(Position newPos, float moveDist) {
Player p = player;
if (p.Game.Referee || Hacks.CanUseSpeed(p)) return false;
int dx = Math.Abs(p.Pos.X - newPos.X), dz = Math.Abs(p.Pos.Z - newPos.Z);

int maxMove = (int)(moveDist * 32);
bool speeding = dx >= maxMove || dz >= maxMove;
if (!speeding || log.AddSpamEntry(5, interval)) return false;

Warn("speedhack");
p.SendPos(Entities.SelfID, p.Pos, p.Rot);
return true;
}
}


public sealed class NoclipDetector : HacksDetector {

public NoclipDetector(Player p) : base(p) { }

public bool Detect(Position newPos) {
Player p = player;
if (p.Game.Referee || Hacks.CanUseNoclip(p)) return false;
if (!p.IsLikelyInsideBlock() || log.AddSpamEntry(5, interval)) return false;

Warn("noclip");
return false;
}
}
}
8 changes: 8 additions & 0 deletions MCGalaxy/Games/IGame.cs
Expand Up @@ -98,5 +98,13 @@ public abstract class IGame {
protected void UpdateStatus3(Player p) {
p.SendCpeMessage(CpeMessageType.Status3, FormatStatus3(p));
}


public static bool InRange(Player a, Player b, int dist) {
int dx = Math.Abs(a.Pos.X - b.Pos.X);
int dy = Math.Abs(a.Pos.Y - b.Pos.Y);
int dz = Math.Abs(a.Pos.Z - b.Pos.Z);
return dx <= dist && dy <= dist && dz <= dist;
}
}
}
62 changes: 0 additions & 62 deletions MCGalaxy/Games/MovementCheck.cs

This file was deleted.

7 changes: 5 additions & 2 deletions MCGalaxy/Games/ZombieSurvival/ZSGame.Plugin.cs
Expand Up @@ -122,8 +122,11 @@ public sealed partial class ZSGame : RoundsGame {
void HandlePlayerMove(Player p, Position next, byte rotX, byte rotY) {
if (!RoundInProgress || p.level != Map) return;

bool reverted = MovementCheck.DetectNoclip(p, next)
|| MovementCheck.DetectSpeedhack(p, next, Config.MaxMoveDist);
// TODO: Maybe tidy this up?
if (p.Game.Noclip == null) p.Game.Noclip = new NoclipDetector(p);
if (p.Game.Speed == null) p.Game.Speed = new SpeedhackDetector(p);

bool reverted = p.Game.Noclip.Detect(next) || p.Game.Speed.Detect(next, Config.MaxMoveDist);
if (reverted) p.cancelmove = true;
}

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Games/ZombieSurvival/ZSGame.Round.cs
Expand Up @@ -109,7 +109,7 @@ public sealed partial class ZSGame : RoundsGame {

foreach (Player alive in aliveList) {
if (alive == killer) continue;
if (!MovementCheck.InRange(alive, killer, dist)) continue;
if (!InRange(alive, killer, dist)) continue;

if (killerData.Infected && !Get(alive).Infected
&& !alive.Game.Referee && !killer.Game.Referee
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/MCGalaxy_.csproj
Expand Up @@ -499,7 +499,7 @@
<Compile Include="Games\LavaSurvival\LSGame.cs" />
<Compile Include="Games\LavaSurvival\LSConfig.cs" />
<Compile Include="Games\LavaSurvival\LSGame.Round.cs" />
<Compile Include="Games\MovementCheck.cs" />
<Compile Include="Games\HacksDetection.cs" />
<Compile Include="Games\RoundsGame\LevelPicker.cs" />
<Compile Include="Games\RoundsGame\RoundsGameConfig.cs" />
<Compile Include="Games\RoundsGame\RoundsGame.cs" />
Expand Down
8 changes: 4 additions & 4 deletions MCGalaxy/Server/Server.Fields.cs
Expand Up @@ -81,8 +81,8 @@ public sealed partial class Server {
// Extra storage for custom commands
public static ExtrasCollection Extras = new ExtrasCollection();

public static int YesVotes = 0, NoVotes = 0;
public static bool voting = false;
public static int YesVotes, NoVotes;
public static bool voting;

public static Scheduler MainScheduler = new Scheduler("MCG_MainScheduler");
public static Scheduler Background = new Scheduler("MCG_BackgroundScheduler");
Expand All @@ -91,7 +91,7 @@ public sealed partial class Server {

public const byte version = 7;
public static string salt = "";
public static bool chatmod = false, flipHead = false;
public static bool shuttingDown = false;
public static bool chatmod, flipHead;
public static bool shuttingDown;
}
}

0 comments on commit 2fd79dd

Please sign in to comment.