-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpnc.main.cs
236 lines (220 loc) · 9.81 KB
/
vpnc.main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
using System.Diagnostics;
using System.Net.NetworkInformation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Config {
public string? ProcessName { get; set; }
public string? ProcessPath { get; set; }
public string? InterfaceName { get; set; }
public string? ApiPort { get; set; }
public string? ApiKey { get; set; }
public string? PingHost { get; set; }
public bool? PingLog { get; set; }
public string? PingTimeout { get; set; }
public string? VpnTimeout { get; set; }
public string? VpnRestartTimeout { get; set; }
public bool? PingStartup { get; set; }
public bool? VpnStartup { get; set; }
public bool? ApiStartup { get; set; }
}
public static class MainProgram {
public static Config? config;
// Функция загрузки конфигурационного файла
public static void LoadConfig() {
try {
string json = File.ReadAllText("vpnc.json");
config = JsonConvert.DeserializeObject<Config>(json);
}
catch (Exception ex) {
Console.WriteLine($"Error loading config: {ex.Message}");
return;
}
}
// Функция остановки процесса
public static void StopProcess(string processName, bool wildcard) {
try {
Process[] processes;
if (wildcard) {
// Получаем все процессы и фильтруем по совпадению с processName
processes = Process.GetProcesses()
.Where(p => p.ProcessName.IndexOf(processName, StringComparison.OrdinalIgnoreCase) >= 0)
.ToArray();
}
else {
// Получаем процессы по точному имени
processes = Process.GetProcessesByName(processName);
}
if (processes.Length == 0) {
Console.WriteLine("Process not running");
return;
}
foreach (Process process in processes) {
Console.WriteLine($"Stopping process: {process.ProcessName} (PID: {process.Id})");
process.Kill();
// process.WaitForExit();
}
Console.WriteLine("Processes stopped successfully");
}
catch (Exception ex) {
Console.WriteLine($"Error stopping process: {ex.Message}");
}
}
// Асинхронное завершение процесса
public static async Task StopProcessAsync(string processName, bool wildcard) {
try {
Process[] processes;
if (wildcard) {
processes = Process.GetProcesses()
.Where(p => p.ProcessName.IndexOf(processName, StringComparison.OrdinalIgnoreCase) >= 0)
.ToArray();
} else {
processes = Process.GetProcessesByName(processName);
}
if (processes.Length == 0) {
Console.WriteLine("Process not running");
return;
}
foreach (Process process in processes) {
Console.WriteLine($"Stopping process: {process.ProcessName} (PID: {process.Id})");
await Task.Run(() => process.Kill());
}
Console.WriteLine("Processes stopped successfully");
}
catch (Exception ex) {
Console.WriteLine($"Error stopping process: {ex.Message}");
}
}
// Функция запуска процесса
public static void StartProcess(string processPath) {
try {
ProcessStartInfo startInfo = new ProcessStartInfo {
FileName = processPath,
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(startInfo);
}
catch (Exception ex) {
Console.WriteLine($"Error starting process: {ex.Message}");
}
}
// Функция проверки существования процесса
public static bool CheckProcess(string processName, bool wildcard = false) {
try {
var processes = Process.GetProcesses();
return wildcard
? processes.Any(p => p.ProcessName.IndexOf(processName, StringComparison.OrdinalIgnoreCase) >= 0)
: Process.GetProcessesByName(processName).Length > 0;
} catch { return false; }
}
// Функция проверка статуса процесса
public static string StatusProcess(string processName) {
var processes = Process.GetProcessesByName(processName);
return processes.Length > 0 ? "Running" : "Not running";
}
// Функция получения uptime процесса
public static string UptimeProcess(string processName) {
var processes = Process.GetProcessesByName(processName);
if (processes.Length == 0) {
return "N/A";
} else {
var startTime = processes[0].StartTime;
var uptime = DateTime.Now - startTime;
return $"{uptime.Days}.{uptime.Hours}:{uptime.Minutes}:{uptime.Seconds}";
}
}
// Функция получения uptime системы
public static string UptimeSystem() {
using var uptime = new PerformanceCounter("System", "System Up Time");
uptime.NextValue();
TimeSpan systemUptime = TimeSpan.FromSeconds(uptime.NextValue());
return $"{systemUptime.Days}.{systemUptime.Hours}:{systemUptime.Minutes}:{systemUptime.Seconds}";
}
// Функция получения статуса сетевого адаптера
public static string StatusInterface(string interfaceName) {
string interfaceStatus = "Not found";
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) {
if (ni.Name == interfaceName) {
interfaceStatus = ni.OperationalStatus == OperationalStatus.Up ? "Up" : "Down";
break;
}
}
return interfaceStatus;
}
// Функция ICMP-запроса для проверки интернет-соединения
public static (string internetStatus, string responseTimeOut) StatusPing(string PingHost) {
using Ping ping = new Ping();
string internetStatus = "Disconnected";
string responseTimeOut = "N/A";
PingReply reply = ping.Send(PingHost, 2000); // Timeout 2 seconds
if (reply.Status == IPStatus.Success) {
internetStatus = "Connected";
responseTimeOut = $"{reply.RoundtripTime} ms";
}
// Запись в лог
if (config != null && config.PingLog == true) {
LogToFile(PingHost, internetStatus, responseTimeOut);
}
return (internetStatus, responseTimeOut);
}
private static void LogToFile(string PingHost, string internetStatus, string responseTimeOut) {
string logFilePath = "vpnc.log";
string logMessage = $"{DateTime.Now:dd.MM.yyyy HH:mm:ss}: {PingHost} - {internetStatus} ({responseTimeOut})";
try {
using (StreamWriter writer = new StreamWriter(logFilePath, true)) {
writer.WriteLine(logMessage);
}
} catch (Exception ex) {
Console.WriteLine($"Error writing to log file: {ex.Message}");
}
}
// HTTP-запрос для получения информации о местоположении
public static async Task<dynamic> GetLocationInfo(string internetStatus) {
string urlCheckRegion = "https://ipinfo.io/json";
JObject locationInfo = new JObject();
if (internetStatus == "Connected") {
using HttpClient client = new HttpClient {
Timeout = TimeSpan.FromSeconds(5) // Timeout 5 seconds
};
try {
HttpResponseMessage response = await client.GetAsync(urlCheckRegion);
response.EnsureSuccessStatusCode();
string locationJson = await response.Content.ReadAsStringAsync();
locationInfo = JObject.Parse(locationJson);
}
catch (Exception ex) {
locationInfo["error"] = ex.Message;
}
} else {
locationInfo["status"] = "Not available";
}
return locationInfo;
}
// Функция сбора всех метрик в формате json
public static async Task<object> StatusConnection(string interfaceName, string processName, string PingHost) {
// Собираем всю информацию из функций и возвращаем ответ в формате JSON
string processStatus = StatusProcess(processName);
string processUptime = UptimeProcess(processName);
string systemUptime = UptimeSystem();
string interfaceStatus = StatusInterface(interfaceName);
var (internetStatus, responseTimeOut) = StatusPing(PingHost);
JObject locationInfo = await GetLocationInfo(internetStatus);
// Формируем ответ в формате JSON
return new {
ProcessName = processName,
ProcessStatus = processStatus,
ProcessUptime = processUptime,
SystemUptime = systemUptime,
InterfaceName = interfaceName,
InterfaceStatus = interfaceStatus,
PingAddress = PingHost,
PingStatus = internetStatus,
PingTimeout = responseTimeOut,
Country = (string?)locationInfo?["country"] ?? "N/A",
TimeZone = (string?)locationInfo?["timezone"] ?? "N/A",
Region = (string?)locationInfo?["region"] ?? "N/A",
City = (string?)locationInfo?["city"] ?? "N/A",
ExternalIp = (string?)locationInfo?["ip"] ?? "N/A"
};
}
}