-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClient.cs
More file actions
98 lines (86 loc) · 3.19 KB
/
MainClient.cs
File metadata and controls
98 lines (86 loc) · 3.19 KB
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
using NetLimiter.Service;
using System;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
namespace NetLimiterBridge
{
public class MainClient
{
private static string _hostname = "38.54.101.108";
private static ushort _port = 9298;
private static string _username = "rssaka";
private static string _password = "Dk75Rn43s!";
private static string _processName = "dfbhd.exe";
public async Task RunAsync()
{
SecureString securePassword = CreateSecurePassword(_password);
using (NLClient client = new NLClient())
{
string host = _hostname;
if (host == "localhost" || host == "127.0.0.1")
{
try
{
client.Connect();
}
catch (Exception ex)
{
Console.WriteLine($"Failed to connect to localhost: {ex.Message}");
return;
}
}
else
{
if (!await TryConnectAsync(client, host, _port, _username, securePassword))
{
Console.WriteLine("Failed to connect after multiple attempts.");
return;
}
}
while (true)
{
NodeLoader nodeLoader = client.CreateNodeLoader();
nodeLoader.SelectAllNodes();
nodeLoader.Load();
var cnnLogEvents = nodeLoader.Connections.Nodes
.Where(node => node.Parent?.Parent?.AppId?.Path != null &&
node.Parent.Parent.AppId.Path.EndsWith(_processName, StringComparison.OrdinalIgnoreCase))
.ToList();
foreach (ConnectionNode logEvent in cnnLogEvents)
{
string ipString = logEvent.RemoteAddress.ToIPAddress4().ToString();
Console.WriteLine(ipString);
}
}
}
}
private static SecureString CreateSecurePassword(string password)
{
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
securePassword.MakeReadOnly();
return securePassword;
}
private static async Task<bool> TryConnectAsync(NLClient client, string host, ushort port, string username, SecureString password)
{
for (int attempt = 1; attempt <= 3; attempt++)
{
try
{
await Task.Run(() => client.Connect(host, port, username, password));
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Connection attempt {attempt} failed: {ex.Message}");
if (attempt == 3) return false;
}
}
return false;
}
}
}