Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
Release 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NockyCZ authored Dec 15, 2023
1 parent d62e556 commit 2738751
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
101 changes: 101 additions & 0 deletions AntiVPN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using Microsoft.Extensions.Logging;

namespace AntiVPN;

public class AntiVPN : BasePlugin
{
public override string ModuleName => "Anti VPN & Country Blocker";
public override string ModuleAuthor => "Nocky";
public override string ModuleVersion => "1.0";

public override void Load(bool hotReload)
{
Config.CreateOrLoadConfig(ModuleDirectory + "/antivpn_config.json");
}
private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info)
{
CCSPlayerController player = @event.Userid;

if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || player.AuthorizedSteamID == null)
return HookResult.Continue;

AddTimer(0.5f, () => CheckPlayerIP(player));

return HookResult.Continue;
}

[ConsoleCommand("css_antivpn_reload")]
[CommandHelper(whoCanExecute: CommandUsage.SERVER_ONLY)]
public void OnReloadCommand(CCSPlayerController caller, CommandInfo command)
{
Config.CreateOrLoadConfig(ModuleDirectory + "/antivpn_config.json");
command.ReplyToCommand("The configuration file \"antivpn_config.json\" reloaded.");
}
public void CheckPlayerIP(CCSPlayerController player)
{
string ipAddress = player!.IpAddress!.Split(":")[0];
string steamid = player!.AuthorizedSteamID!.SteamId64.ToString();

var isVPN = IsIpVPN(ipAddress).GetAwaiter().GetResult();
if (isVPN){
if (!Config.SteamidWhitelist!.Contains(steamid))
{
Server.ExecuteCommand($"kickid {player.UserId} VPN usage is not allowed on this server");
Logger.LogInformation($"Player {player.PlayerName} ({steamid}) ({ipAddress}) has been kicked. (VPN Usage)");
return;
}
return;
}
var countryCode = GetCountryCode(ipAddress).GetAwaiter().GetResult();
if (Config.BlockedCountries!.Contains(countryCode))
{
if (!Config.SteamidWhitelist!.Contains(steamid))
{
Server.ExecuteCommand($"kickid {player.UserId} Your country is not allowed on this server");
Logger.LogInformation($"Player {player.PlayerName} ({steamid}) ({ipAddress} | {countryCode}) has been kicked. (Disabled Country)");
return;
}
return;
}
}
static async Task <bool>IsIpVPN(string ipAddress)
{
using (var client = new HttpClient())
{
string requestURL = $"https://blackbox.ipinfo.app/lookup/{ipAddress}";

HttpResponseMessage response = await client.GetAsync(requestURL);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
if(jsonResponse == "Y"){
return true;
}
else{
return false;
}
}
return false;
}
}
static async Task <string>GetCountryCode(string ipAddress)
{
using (var client = new HttpClient())
{
string requestURL = $"https://ipinfo.io/{ipAddress}/json";

HttpResponseMessage response = await client.GetAsync(requestURL);
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonResponse)!;
return $"{data.country}";
}
return "";
}
}
}
14 changes: 14 additions & 0 deletions AntiVPN.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.126" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Newtonsoft.Json.Linq;

namespace AntiVPN;
public static class Config
{
public static JObject? JsonConfigData { get; private set; }
public static List<string>? SteamidWhitelist { get; private set; }
public static List<string>? BlockedCountries { get; private set; }
public static void CreateOrLoadConfig(string filepath)
{
if (!File.Exists(filepath))
{
JObject exampleData = new JObject
{
["AntiVPN"] = new JObject
{
["steamid_whitelist"] = new JArray { "76561198429950772", "76561198808392634" },
["blocked_countries"] = new JArray { "RU", "CN" }
}
};
File.WriteAllText(filepath, exampleData.ToString());
var jsonData = File.ReadAllText(filepath);
JsonConfigData = JObject.Parse(jsonData);
}
else
{
var jsonData = File.ReadAllText(filepath);
JsonConfigData = JObject.Parse(jsonData);
}

JArray steamidWhitelistArray = (JArray)JsonConfigData["AntiVPN"]!["steamid_whitelist"]!;
SteamidWhitelist = steamidWhitelistArray.ToObject<List<string>>();

JArray blockedCountriesArray = (JArray)JsonConfigData["AntiVPN"]!["blocked_countries"]!;
BlockedCountries = blockedCountriesArray.ToObject<List<string>>();
}
}

0 comments on commit 2738751

Please sign in to comment.