-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS2-AntiVPN.cs
198 lines (163 loc) · 7.2 KB
/
CS2-AntiVPN.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Linq;
using System.Threading;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Entities;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using CounterStrikeSharp.API.Modules.Utils;
using System.Text.RegularExpressions;
using System.Text;
using System.Numerics;
namespace CS2_AntiVPN;
public class CS2_AntiVPN : BasePlugin, IPluginConfig<CS2_AntiVPN_Config>
{
public required CS2_AntiVPN_Config Config { get; set; }
public override string ModuleName => "CS2-AntiVPN";
public override string ModuleVersion => "1.4";
public override string ModuleAuthor => "mintyx";
public override string ModuleDescription => "Kicks Players with a VPN enabled.";
private string? _serverName;
private string? ipAddress;
public override void Load(bool hotReload)
{
Logger.LogInformation("CS2-AntiVPN Loaded! -> mintyx");
}
public void OnConfigParsed(CS2_AntiVPN_Config config)
{
Config = config;
}
private string ReplaceMessageColors(string input)
{
string[] ColorAlphabet = { "{GREEN}", "{BLUE}", "{RED}", "{SILVER}", "{MAGENTA}", "{GOLD}", "{DEFAULT}", "{LIGHTBLUE}", "{LIGHTPURPLE}", "{LIGHTRED}", "{LIGHTYELLOW}", "{YELLOW}", "{GREY}", "{LIME}", "{OLIVE}", "{ORANGE}", "{DARKRED}", "{DARKBLUE}", "{BLUEGREY}", "{PURPLE}" };
string[] ColorChar = { $"{ChatColors.Green}", $"{ChatColors.Blue}", $"{ChatColors.Red}", $"{ChatColors.Silver}", $"{ChatColors.Magenta}", $"{ChatColors.Gold}", $"{ChatColors.Default}", $"{ChatColors.LightBlue}", $"{ChatColors.LightPurple}", $"{ChatColors.LightRed}", $"{ChatColors.LightYellow}", $"{ChatColors.Yellow}", $"{ChatColors.Grey}", $"{ChatColors.Lime}", $"{ChatColors.Olive}", $"{ChatColors.Orange}", $"{ChatColors.DarkRed}", $"{ChatColors.DarkBlue}", $"{ChatColors.BlueGrey}", $"{ChatColors.Purple}" };
for (int z = 0; z < ColorAlphabet.Length; z++)
{
input = Regex.Replace(input, Regex.Escape(ColorAlphabet[z]), ColorChar[z], RegexOptions.IgnoreCase);
}
return input;
}
private static int PlayersConnected()
{
return Utilities.GetPlayers()
.Where(player => player.IsValid && !player.IsHLTV && !player.IsBot)
.Count();
}
[GameEventHandler]
public HookResult OnPlayerConnect(EventPlayerConnectFull @event, GameEventInfo info)
{
if (@event.Userid == null)
{
return HookResult.Continue;
}
var player = @event.Userid;
string ipAddress = player.IpAddress?.Split(":")[0] ?? string.Empty;
string playerName = player.PlayerName ?? "Unknown Player";
string webhookUrl = Config.WebhookUrl;
var playerID = player.SteamID;
Logger.LogInformation($"{playerName} Joined from IP: {ipAddress}");
Task.Run(async () =>
{
var isVpn = await CheckVpn(ipAddress, player);
if (isVpn)
{
await VpnAction(player);
await SendDiscordWebhook(webhookUrl, playerName, playerID, ipAddress);
}
});
return HookResult.Continue;
}
private async Task<bool> CheckVpn(string ipAddress, CCSPlayerController player)
{
using var client = new HttpClient();
var url = $"http://ip-api.com/json/{ipAddress}?fields=status,mobile,proxy,hosting,query";
try
{
var response = await client.GetAsync(url);
var responseBody = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var jsonResult = JsonDocument.Parse(responseBody).RootElement;
var isUsingVpn = jsonResult.GetProperty("proxy").GetBoolean();
var isUsingHost = jsonResult.GetProperty("hosting").GetBoolean();
Logger.LogInformation($"Proxy: {isUsingVpn} | Hosting: {isUsingHost}");
if (isUsingVpn || isUsingHost)
{
Logger.LogInformation($"VPN or Hosting Service Detected! Attempting to kick the player...");
return true;
}
}
}
catch (Exception ex)
{
Logger.LogError(ex, $"Unable to fetch IP `{ipAddress}` info!");
}
return false;
}
private Task VpnAction(CCSPlayerController player)
{
Server.NextFrame(() =>
{
player.ChangeTeam(CounterStrikeSharp.API.Modules.Utils.CsTeam.Spectator);
});
Thread.Sleep(Config.Kick_Message_Delay * 1000);
string Kick_Message = ReplaceMessageColors(Config.Kick_Message);
Server.NextFrame(() =>
{
player.PrintToChat($"{Kick_Message}");
});
Server.NextFrame(() =>
{
player.PrintToCenterAlert($"{Kick_Message}");
});
Thread.Sleep((Config.KickDelay - Config.Kick_Message_Delay) * 1000);
Server.NextFrame(() =>
{
Server.ExecuteCommand($"kickid {player.UserId} VPN usage is not allowed on this server");
Logger.LogInformation($"Kicked {player.PlayerName} for using VPN!");
});
return Task.CompletedTask;
}
private static readonly HttpClient client = new HttpClient();
public async Task SendDiscordWebhook(string webhookUrl, string playerName, ulong playerID, string ipAddress)
{
using HttpClient? client = new();
var payload = new
{
content = "",
embeds = new[]
{
new
{
author = new
{
name = "VPN Detected!"
},
description = $"User: [{playerName}](https://steamcommunity.com/profiles/{playerID}) (`{playerID}`)\nIP: [{ipAddress}](http://ip-api.com/json/{ipAddress})",
color = 0xFF0000,
footer = new
{
text = "CS2 - AntiVPN"
},
timestamp = DateTime.UtcNow.ToString("o")
}
}
};
string jsonPayload = JsonSerializer.Serialize(payload);
using StringContent content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
using HttpResponseMessage response = await client.PostAsync(webhookUrl, content);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"[{DateTime.Now}] Webhook sent successfully.");
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{DateTime.Now}] Failed to send message to Discord: {response.StatusCode}");
string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"[{DateTime.Now}] Response content: {responseContent}");
Console.ResetColor();
}
}
}