Advertisement
Guest User

Untitled

a guest
Feb 10th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.43 KB | None | 0 0
  1. // Reference: Oxide.Ext.Rust
  2.  
  3. using System.Collections.Generic;
  4. using System;
  5. using System.Data;
  6. using UnityEngine;
  7. using Oxide.Core;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11.     [Info("Anti Wallhack", "Reneb & ownprox", 1.7)]
  12.     class AntiWallhack : RustPlugin
  13.     {
  14.         private static DateTime epoch;
  15.         private static double lastCheck;
  16.         private static Dictionary<BaseEntity, Dictionary<TriggerBase, Vector3>> posSave;
  17.         private static Dictionary<BaseEntity, double> lastDetection;
  18.         private static Dictionary<BaseEntity, int> detectionsAmount;
  19.         private static List<BasePlayer> players;
  20.         private static Dictionary<BuildingBlock, double> buildingblockToAdd;
  21.         private LayerMask intLayers;
  22.         private static float distance;
  23.         private bool punishbyBan;
  24.         private bool punishbyKick;
  25.         private float ignoreFPS;
  26.         private double resetTime;
  27.         private bool Changed;
  28.         private int detectionsNeeded;
  29.         private float healthlimit;
  30.         private bool hasStarted;
  31.         private bool ignoreAdmins;
  32.         private double nextAddCheck;
  33.         void Loaded()
  34.         {
  35.             buildingblockToAdd = new Dictionary<BuildingBlock, double>();
  36.             posSave = new Dictionary<BaseEntity, Dictionary<TriggerBase, Vector3>>();
  37.             lastDetection = new Dictionary<BaseEntity, double>();
  38.             detectionsAmount = new Dictionary<BaseEntity, int>();
  39.             distance = 0.8f;
  40.             epoch = new System.DateTime(1970, 1, 1);
  41.             lastCheck = CurrentTime();
  42.             nextAddCheck = CurrentSecTime();
  43.             hasStarted = false;
  44.         }
  45.         float CurrentFPS()
  46.         {
  47.             return (1 / UnityEngine.Time.smoothDeltaTime);
  48.         }
  49.         double CurrentTime()
  50.         {
  51.             return System.DateTime.UtcNow.Subtract(epoch).TotalMilliseconds;
  52.         }
  53.         double CurrentSecTime()
  54.         {
  55.             return System.DateTime.UtcNow.Subtract(epoch).TotalSeconds;
  56.         }
  57.         void OnServerInitialized()
  58.         {
  59.             var triggers = UnityEngine.Object.FindObjectsOfType<TriggerRadiation>();
  60.             foreach (TriggerRadiation trigger in triggers)
  61.             {
  62.                 intLayers = trigger.GetComponent<TriggerBase>().interestLayers;
  63.                 break;
  64.             }
  65.             LoadVariables();
  66.             hasStarted = true;
  67.             RefreshAllWalls();
  68.         }
  69.         object GetConfig(string menu, string datavalue, object defaultValue)
  70.         {
  71.             var data = Config[menu] as Dictionary<string, object>;
  72.             if (data == null)
  73.             {
  74.                 data = new Dictionary<string, object>();
  75.                 Config[menu] = data;
  76.                 Changed = true;
  77.             }
  78.             object value;
  79.             if (!data.TryGetValue(datavalue, out value))
  80.             {
  81.                 value = defaultValue;
  82.                 data[datavalue] = value;
  83.                 Changed = true;
  84.             }
  85.             return value;
  86.         }
  87.         void LoadVariables()
  88.         {
  89.             GetConfig("GeneralConfig", "Version", Version.ToString()); // TODO update check if new config required
  90.             punishbyBan = Convert.ToBoolean(GetConfig("Punish", "byBan", true));
  91.             punishbyKick = Convert.ToBoolean(GetConfig("Punish", "byKick", true));
  92.             ignoreAdmins = Convert.ToBoolean(GetConfig("AntiWallhack", "ignoreAdmins", true));
  93.             ignoreFPS = Convert.ToSingle(GetConfig("Punish", "IgnoreUnderFPS", 20));
  94.             resetTime = Convert.ToDouble(GetConfig("Punish", "resetDetections", 2)) * 1000;
  95.             detectionsNeeded = Convert.ToInt32(GetConfig("Punish", "detectionsNeededToPunish", 3));
  96.             healthlimit = Convert.ToSingle(GetConfig("AntiWallhack", "ignoreWallHealthInPercentage", 0.1));
  97.  
  98.             if (Changed)
  99.             {
  100.                 ((Dictionary<string, object>)Config["GeneralConfig"])["Version"] = Version.ToString(); //updated to new version
  101.                 SaveConfig();
  102.                 Changed = false;
  103.             }
  104.         }
  105.         void LoadDefaultConfig()
  106.         {
  107.             Puts("AntiWallhack: Creating a new config file");
  108.             Config.Clear(); // force clean new config
  109.             LoadVariables();
  110.         }
  111.         void OnEntitySpawn(UnityEngine.Object gameobject)
  112.         {
  113.             if (hasStarted)
  114.             {
  115.                 if (gameobject as BuildingBlock)
  116.                 {
  117.                     var buildingblock = gameobject as BuildingBlock;
  118.                     if (buildingblock.blockDefinition.name == "wall" || buildingblock.blockDefinition.name == "door.hinged")
  119.                     {
  120.                         if (!buildingblockToAdd.ContainsKey(buildingblock)) //check if exists
  121.                             buildingblockToAdd.Add(buildingblock, CurrentSecTime() + 600);// need to add a delay here or you cannot build more then 4 doors
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.         void OnTick()
  127.         {
  128.             if (CurrentSecTime() > nextAddCheck)
  129.             {
  130.                 nextAddCheck = CurrentSecTime() + 60; //adjust this abit
  131.                 var currenttime = CurrentSecTime();
  132.                 if (buildingblockToAdd.Count > 0)
  133.                 {
  134.                     List<BuildingBlock> Temp = new List<BuildingBlock>();
  135.                     foreach (KeyValuePair<BuildingBlock, double> pair in buildingblockToAdd)
  136.                     {
  137.                         if (pair.Value >= currenttime)
  138.                         {
  139.                             BuildingBlock buildingblock = pair.Key;
  140.                             if (buildingblock != null && buildingblock.blockDefinition != null)
  141.                             {
  142.                                 if (buildingblock.GetComponentInChildren<TriggerBase>() == null)
  143.                                 {
  144.                                     buildingblock.GetComponentInChildren<MeshCollider>().gameObject.AddComponent<TriggerBase>();
  145.                                     buildingblock.GetComponentInChildren<TriggerBase>().gameObject.layer = UnityEngine.LayerMask.NameToLayer("Trigger");
  146.                                     buildingblock.GetComponentInChildren<TriggerBase>().interestLayers = intLayers;
  147.                                 }
  148.                             }
  149.                             Temp.Add(buildingblock);
  150.                         }
  151.                     }
  152.                     foreach (BuildingBlock b in Temp)
  153.                         buildingblockToAdd.Remove(b);
  154.  
  155.                 }
  156.                 //bulding update might take abit might aswell grab new time
  157.                 currenttime = CurrentSecTime();
  158.                 //cleanup
  159.                 List<BaseEntity> TempDelete = new List<BaseEntity>();
  160.                 foreach (KeyValuePair<BaseEntity, double> p in lastDetection)
  161.                     if ((currenttime - p.Value) >= 120000) TempDelete.Add(p.Key);
  162.                 foreach (BaseEntity b in TempDelete)
  163.                 {
  164.                     detectionsAmount.Remove(b);
  165.                     lastDetection.Remove(b);
  166.                 }
  167.                 TempDelete.Clear();
  168.             }
  169.         }
  170.  
  171.         void OnEntityEnter(TriggerBase triggerbase, BaseEntity entity)
  172.         {
  173.             if (entity != null && triggerbase.gameObject.name == "servercollision"&& (string)entity.ToString() == "player/player (BasePlayer)")
  174.             {
  175.                 if (entity.GetComponentInParent<BasePlayer>())
  176.                 {
  177.                     var player = entity.GetComponentInParent<BasePlayer>();
  178.                     if (player != null && player.net != null) //check for more nulls
  179.                     {
  180.                         if (player.net.connection.authLevel < 1)
  181.                         {
  182.                             if (posSave.ContainsKey(entity) == false) { posSave.Add(entity, new Dictionary<TriggerBase, Vector3>()); }
  183.                             posSave[entity][triggerbase] = entity.transform.position;
  184.                         }
  185.                     }
  186.                 }
  187.             }
  188.         }
  189.  
  190.         bool RayCast(BuildingBlock buildingblock, Vector3 origin, Vector3 destination)
  191.         {
  192.             var vect = destination - origin;
  193.             if (origin.y - buildingblock.transform.position.y > 2)
  194.                 return false;
  195.             var hits = UnityEngine.Physics.RaycastAll(origin, vect.normalized, distance);
  196.             foreach (var hit in hits)
  197.             {
  198.                 if (hit.collider.GetComponentInParent<BuildingBlock>() == buildingblock)
  199.                     return true;
  200.             }
  201.             return false;
  202.  
  203.         }
  204.  
  205.         void ForcePlayerPosition(BasePlayer player, Vector3 destination)
  206.         {
  207.             player.transform.position = destination;
  208.             player.ClientRPC(null, player, "ForcePositionTo", new object[] { destination });
  209.             player.TransformChanged();
  210.         }
  211.  
  212.         void SendMsgAdmin(string msg)
  213.         {
  214.             foreach (var player in BasePlayer.activePlayerList)
  215.             {
  216.                 if (player != null && player.net != null)
  217.                 {
  218.                     if (player.net.connection.authLevel > 0)
  219.                     {
  220.                         player.SendConsoleCommand("chat.add", new object[] { 0, msg.QuoteSafe() });
  221.                     }
  222.                 }
  223.             }
  224.         }
  225.  
  226.         void OnEntityLeave(TriggerBase triggerbase, BaseEntity entity)
  227.         {
  228.             if (entity != null && triggerbase != null && triggerbase.gameObject.name == "servercollision" && (string)entity.ToString() == "player/player (BasePlayer)")
  229.             {
  230.                 if (entity.GetComponentInParent<BasePlayer>())
  231.                 {
  232.                     if ((posSave.ContainsKey(entity) == true))
  233.                     {
  234.                         if (posSave[entity].ContainsKey(triggerbase))
  235.                         {
  236.                             if (RayCast(triggerbase.GetComponentInParent<BuildingBlock>(), posSave[entity][triggerbase], entity.transform.position))
  237.                             {
  238.                                 if (lastDetection.ContainsKey(entity) == false) { lastDetection.Add(entity, 0); }
  239.                                 if (detectionsAmount.ContainsKey(entity) == false) { detectionsAmount.Add(entity, 0); }
  240.                                 var currenttime = CurrentTime();
  241.                                 BasePlayer player = entity.GetComponentInParent<BasePlayer>();
  242.                                 if (player == null) return;
  243.                                 if (triggerbase.GetComponentInParent<BuildingBlock>().blockDefinition.name == "wall")
  244.                                 {
  245.                                     if (CurrentFPS() > ignoreFPS)
  246.                                     {
  247.                                         if ((currenttime - lastDetection[entity]) < resetTime)
  248.                                         {
  249.                                             detectionsAmount[entity] = detectionsAmount[entity] + 1;
  250.                                             SendMsgAdmin(player.displayName + " was detected wallhacking");
  251.                                             Puts("{0} was detected wallhacking @ to: {1} from: {2}", player.displayName, entity.transform.position.ToString(), posSave[entity][triggerbase].ToString());
  252.                                             if (detectionsAmount[entity] >= detectionsNeeded)
  253.                                             {
  254.                                                 if (punishbyBan)
  255.                                                 {
  256.                                                     Interface.GetMod().CallHook("Ban", new object[] { null, player, "r-Wallhack2", false });
  257.                                                     Puts(player.displayName + " was detected wallhacking and was banned for it");
  258.                                                     SendMsgAdmin(player.displayName + " was detected wallhacking and was banned");
  259.                                                 }
  260.                                                 if (punishbyBan || punishbyKick)
  261.                                                 {
  262.                                                     if (player.net != null)
  263.                                                     {
  264.                                                         if (!punishbyBan)
  265.                                                         {
  266.                                                             Puts(player.displayName + " was detected wallhacking and was kicked for it");
  267.                                                             SendMsgAdmin(player.displayName + " was detected wallhacking and was kicked");
  268.                                                         }
  269.                                                         Network.Net.sv.Kick(player.net.connection, "Kicked from the server");
  270.                                                     }
  271.                                                 }
  272.                                                 return;
  273.                                             }
  274.                                         }
  275.                                         else
  276.                                         {
  277.                                             detectionsAmount[entity] = 0;
  278.                                         }
  279.  
  280.                                     }
  281.                                     else
  282.                                     {
  283.                                         Puts(player.displayName + " was detected wallhacking but was ignored because of the low server FPS");
  284.                                         SendMsgAdmin(player.displayName + " was detected wallhacking but was ignored because of the low server FPS");
  285.                                     }
  286.                                 }
  287.                                 else
  288.                                 {
  289.                                     Puts("{0} was detected walking threw a door @ to: {1} from: {2}", player.displayName, entity.transform.position.ToString(), posSave[entity][triggerbase].ToString());
  290.                                     SendMsgAdmin(player.displayName + " was detected walking threw a door");
  291.                                 }
  292.                                 ForcePlayerBack(player, posSave[entity][triggerbase], player.transform.position);
  293.  
  294.                                 lastDetection[entity] = currenttime;
  295.                             }
  296.                             posSave[entity].Remove(triggerbase);
  297.                         }
  298.                     }
  299.                 }
  300.             }
  301.         }
  302.         void ForcePlayerBack(BasePlayer player, Vector3 entryposition, Vector3 exitposition)
  303.         {
  304.             var distance = Vector3.Distance(exitposition, entryposition) + 0.5f;
  305.             var direction = (entryposition - exitposition).normalized;
  306.             ForcePlayerPosition(player, exitposition + (direction * distance));
  307.         }
  308.         object RefreshAllWalls()
  309.         {
  310.             var allbuildings = UnityEngine.Resources.FindObjectsOfTypeAll<BuildingBlock>();
  311.             var protectedwall = 0;
  312.             foreach (BuildingBlock block in allbuildings)
  313.             {
  314.                 if (block.blockDefinition != null && (block.blockDefinition.name == "wall" || block.blockDefinition.name == "door.hinged"))
  315.                 {
  316.                     if (block.GetComponentInChildren<TriggerBase>() == null)
  317.                     {
  318.                         block.GetComponentInChildren<MeshCollider>().gameObject.AddComponent<TriggerBase>();
  319.                         block.GetComponentInChildren<TriggerBase>().gameObject.layer = UnityEngine.LayerMask.NameToLayer("Trigger");
  320.                         block.GetComponentInChildren<TriggerBase>().interestLayers = intLayers;
  321.                         protectedwall++;
  322.                     }
  323.                 }
  324.             }
  325.             return protectedwall;
  326.         }
  327.         [ConsoleCommand("wallhack.init")]
  328.         void cmdWallhackInitialize(ConsoleSystem.Arg arg)
  329.         {
  330.             if (arg.connection != null)
  331.             {
  332.                 if (arg.connection.authLevel < 1)
  333.                 {
  334.                     SendReply(arg, "You are not allowed to use this command");
  335.                     return;
  336.                 }
  337.             }
  338.             var protectedwall = RefreshAllWalls();
  339.             SendReply(arg, string.Format("{0} walls were added to the anti wallhack protection", protectedwall.ToString()));
  340.         }
  341.     }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement