-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeknoAntiVPN.cs
145 lines (126 loc) · 4.86 KB
/
TeknoAntiVPN.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
using System;
using InfinityScript;
using Tiny.RestClient;
using System.Net.Http;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace TeknoAntiVPN
{
public class TeknoAntiVPN : BaseScript
{
string apiKeyTxt = "scripts\\TeknoAntiVPN\\apikey.txt";
string ignoredPlayersTxt = "scripts\\TeknoAntiVPN\\ignoredPlayers.txt";
string playersInGameTxt = "scripts\\TeknoAntiVPN\\playersInGame.txt";
string API_KEY;
string[] ignoredPlayers;
string[] playersInGame;
public TeknoAntiVPN()
{
OnServerStart();
}
void OnServerStart()
{
PlayerConnected += OnPlayerConnected;
PlayerDisconnected += OnPlayerDisconnected;
if (!System.IO.Directory.Exists("scripts\\TeknoAntiVPN")) System.IO.Directory.CreateDirectory("scripts\\TeknoAntiVPN");
if (!System.IO.File.Exists(apiKeyTxt))
{
WriteLog.Warning("Api key file doesn't exist. Creating... Please fill this out and restart the server");
System.IO.File.Create(apiKeyTxt);
}
if (!System.IO.File.Exists(ignoredPlayersTxt))
{
WriteLog.Warning("Ignored Players file doesn't exist. Creating... ");
System.IO.File.Create(ignoredPlayersTxt);
}
if (!System.IO.File.Exists(playersInGameTxt))
{
WriteLog.Warning("PlayersInGame file doesn't exist. Creating... ");
System.IO.File.Create(playersInGameTxt);
}
WriteLog.Info("AntiVPN by Mahjestic successfully started.");
API_KEY = System.IO.File.ReadAllText(apiKeyTxt);
}
public void OnPlayerConnected(Entity player)
{
playersInGame = System.IO.File.ReadAllLines(playersInGameTxt);
foreach (string playerInGame in playersInGame)
{
if (player.Name == playerInGame)
{
WriteLog.Info($"{player.Name} has already been scanned...ignoring them.");
return;
}
}
WriteLog.Info($"Detecting if player {player.Name} has a VPN...");
if (isVPN(player.IP.Address.ToString(), player))
{
AfterDelay(2000, () => Utilities.ExecuteCommand($"kickclient {player.GetEntityNumber()} \"^1Proxies and VPNs are not allowed in this server.\""));
WriteLog.Warning($"Player {player.Name} has a VPN. Kicking them out.");
}
else
{
WriteLog.Info($"Player {player.Name} does not have a VPN. Allowing them in.");
}
}
void addToPlayersInGame(Entity player)
{
WriteLog.Info($"Adding {player.Name} to playersInGame.txt...");
System.IO.File.AppendAllText(playersInGameTxt, "\n" + player.Name);
}
void removeFromPlayersInGame(Entity player)
{
WriteLog.Info($"Removing {player.Name} from playersInGame.txt...");
string playersInGame_ = System.IO.File.ReadAllText(playersInGameTxt);
System.IO.File.WriteAllText(playersInGameTxt, playersInGame_.Replace($"\n{player.Name}", ""));
}
bool isVPN(string ip, Entity player)
{
ignoredPlayers = System.IO.File.ReadAllLines(ignoredPlayersTxt);
foreach (string ignoredPlayer in ignoredPlayers) {
if (player.Name == ignoredPlayer)
{
WriteLog.Info($"{player.Name} has a VPN but they have been ignored.");
return false;
}
}
var client = new TinyRestClient(new HttpClient(), "http://v2.api.iphub.info");
var response = client.GetRequest($"ip/{ip}")
.AddHeader("X-Key", API_KEY)
.ExecuteAsStringAsync();
Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(response.Result);
if(result["block"].ToString() == "1")
{
return true;
}
else
{
addToPlayersInGame(player);
return false;
}
}
public void OnPlayerDisconnected(Entity player)
{
removeFromPlayersInGame(player);
}
}
public static class WriteLog
{
public static void None(string message)
{
Log.Write(LogLevel.None, message);
}
public static void Info(string message)
{
Log.Write(LogLevel.Info, message);
}
public static void Error(string message)
{
Log.Write(LogLevel.Error, message);
}
public static void Warning(string message)
{
Log.Write(LogLevel.Warning, message);
}
}
}