-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class1.cs
118 lines (102 loc) · 3.42 KB
/
Class1.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
using System.Net;
using System.Net.Sockets;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
namespace AntiVPN;
[ApiVersion(2, 1)]
public class AntiVPN : TerrariaPlugin
{
public override string Author => "FrankV22";
public override string Description => "AntiVPN";
public override string Name => "AntiVPN";
public override Version Version => new Version(1, 0, 1);
public AntiVPN(Main game) : base(game) { }
public override void Initialize()
{
ServerApi.Hooks.ServerJoin.Register(this, this.OnJoin);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.ServerJoin.Deregister(this, this.OnJoin);
}
base.Dispose(disposing);
}
public class Message
{
public TSPlayer Player { get; set; }
public TaskCompletionSource<string> Result { get; set; }
public async void ThreadMain()
{
try
{
await Task.Delay(3000);
var ipAddress = this.Player.IP;
var apiUrl = $"https://blackbox.ipinfo.app/lookup/{ipAddress}";
using var client = new HttpClient();
var response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
this.Result.SetResult(responseContent);
}
catch (Exception ex)
{
this.Result.SetException(ex);
}
}
}
private async void OnJoin(JoinEventArgs args)
{
var player = TShock.Players[args.Who];
if (this.IsPrivateOrLoopbackAddress(player.IP))
{
Console.WriteLine($"{player.Name} is using a private or loopback address, skipping proxy detection.");
return;
}
var message = new Message
{
Player = player,
Result = new TaskCompletionSource<string>()
};
var thread = new Thread(message.ThreadMain);
thread.Start();
try
{
var result = await message.Result.Task;
if (result == "Y")
{
Console.WriteLine($"It has been detected that {player.Name} is using a proxy and has been kicked.");
player.Disconnect("Using VPN is not allowed on this server.");
}
else
{
Console.WriteLine($"{player.Name} has a valid IP.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error checking the IP of {player.Name}: {ex.Message}");
}
}
private bool IsPrivateOrLoopbackAddress(string ipAddress)
{
if (!IPAddress.TryParse(ipAddress, out var ip))
{
return false;
}
// Check if it's a private IP
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
var bytes = ip.GetAddressBytes();
if ((bytes[0] == 10) || // 10.0.0.0/8
(bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31) || // 172.16.0.0/12
(bytes[0] == 192 && bytes[1] == 168)) // 192.168.0.0/16
{
return true;
}
}
return IPAddress.IsLoopback(ip);
}
}