-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
223 lines (190 loc) · 7.4 KB
/
Program.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using RestSharp;
using TSLib;
using TSLib.Query;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace TF47_Teamspeak_AntiVpn
{
class Program
{
private static TsQueryClient _client;
private static IConfiguration _configuration;
private static readonly Queue<(string, DateTime)> Messages = new Queue<(string, DateTime)>();
private static List<string> _whitelistedUser = new List<string>();
private static List<string> _whitelistedProvider = new List<string>();
private static readonly string LogDestination = Path.Combine(Environment.CurrentDirectory, "log.txt");
private static FileSystemWatcher _fileSystemWatcher;
static void Main(string[] args)
{
Main().Wait();
}
static async Task Main()
{
WriteLog("Starting up!");
Task.Run(async () =>
{
LogBackgroundWorker();
});
var builder = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
_configuration = builder.Build();
var ipAddress = string.Empty;
try
{
ipAddress = (await Dns.GetHostAddressesAsync(_configuration["ServerAddress"]))[0].ToString();
}
catch
{
WriteLog("Failed to parse hostname! Exiting...");
Environment.Exit(-1);
}
_whitelistedProvider = _configuration.GetSection("ExcludedProviderList").Get<string[]>().ToList();
_client = new TsQueryClient();
_client.Connect(new ConnectionData
{
Address = ipAddress
});
WriteLog("Login in...");
_client.Login(_configuration["User"], _configuration["Password"]);
WriteLog("Logged in!");
_client.UseServer(int.Parse(_configuration["ServerId"]));
_client.RegisterNotificationServer();
_client.OnClientEnterView += Client_OnClientEnterView;
LoadWhitelist(true);
Console.WriteLine("Listening for new connections, press any key to stop");
while (true)
{
_client.WhoAmI();
Thread.Sleep(5000);
}
Console.ReadKey();
//MainAsync().Wait();
}
private static void Client_OnClientEnterView(object sender, IEnumerable<TSLib.Messages.ClientEnterView> e)
{
foreach (var clientEnterView in e)
{
var details= _client.ClientInfo(clientEnterView.ClientId).Value;
WriteLog($"Client {details.Name} joined! IP: {details.Ip}, Connected at: {details.LastConnected.ToLongTimeString()}");
if (_whitelistedUser.Contains(details.Uid.Value) || _whitelistedUser.Contains(details.MyTeamSpeakId))
{
WriteLog("> User is whitelisted!");
return;
}
if (CheckIsBlacklist(details.Ip).GetAwaiter().GetResult())
{
WriteLog($"> WARNING USER CONNECTED WITH VPN! Kicking...");
_client.KickClientFromServer(clientEnterView.ClientId, "VPN service is not allowed on this server!");
}
else
{
WriteLog("> Client clear");
}
}
}
private static async Task<bool> CheckIsBlacklist(string ip)
{
var client = new RestClient("https://ip.teoh.io/api/vpn/");
var request = new RestRequest(ip);
try
{
var response = await client.ExecuteGetAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var responseData = JsonSerializer.Deserialize<VpnTestResponse>(response.Content);
if (responseData == null)
{
WriteLog("Error VPN Api did not respond");
return false;
}
if (_whitelistedProvider.All(x => x != responseData.organization) && (responseData.vpn_or_proxy == "yes" || responseData.risk == "high"))
{
WriteLog($"IP: {ip} using proxy! Type: {responseData.type}, organization: {responseData.organization}");
return true;
}
}
else
{
WriteLog($"Query for ip {ip} failed! Response {response.StatusCode} {response.Content}");
}
}
catch (Exception ex)
{
WriteLog($"Query for ip {ip} failed! {ex.Message}");
}
return false;
}
private static void LogBackgroundWorker()
{
while (true)
{
if (Messages.Count > 0)
{
var messageToWrite = Messages.Dequeue();
File.AppendAllLines(LogDestination, new List<string>
{
$"[{messageToWrite.Item2}] {messageToWrite.Item1}"
}, Encoding.UTF8);
}
else
{
Thread.Sleep(1000 * 5);
}
}
}
private static void WriteLog(string message, bool isError = false)
{
Messages.Enqueue((message, DateTime.Now));
if (isError)
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[{DateTime.Now}] {message}");
Console.ForegroundColor = ConsoleColor.White;
}
private static void LoadWhitelist(bool refreshOnChange)
{
var file = Path.Combine(Environment.CurrentDirectory, "whitelist.txt");
UpdateWhitelist(file);
if (refreshOnChange)
{
_fileSystemWatcher = new FileSystemWatcher(file);
_fileSystemWatcher.Changed += (sender, args) => { UpdateWhitelist(args.FullPath); };
}
}
private static void UpdateWhitelist(string file)
{
if (File.Exists(file))
{
_whitelistedUser = File.ReadAllLines(file).ToList();
WriteLog("Whitelist loaded!");
}
else
{
WriteLog("No whitelist found, created empty file!");
File.Create(file);
}
}
private class VpnTestResponse
{
public string ip { get; set; }
public string organization { get; set; }
public string asn { get; set; }
public string type { get; set; }
public string risk { get; set; }
public string is_hosting { get; set; }
public string vpn_or_proxy { get; set; }
}
}
}