-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentConnectionForm.cs
More file actions
425 lines (369 loc) · 18.1 KB
/
AgentConnectionForm.cs
File metadata and controls
425 lines (369 loc) · 18.1 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.IO;
using System.Text.Json;
using System.Net.Http;
using System.Text;
namespace BitcoinFinder
{
public partial class AgentConnectionForm : Form
{
private HttpClient httpClient;
private bool isConnected = false;
// UI элементы
private TextBox txtServerUrl;
private TextBox txtAgentName;
private NumericUpDown numThreads;
private Button btnConnect;
private Button btnTestConnection;
private Label lblConnectionStatus;
private TextBox txtLog;
private Button btnClearLog;
private const string ConfigFile = "agent_web_config.json";
public AgentConnectionForm()
{
InitializeComponent();
LoadConfig();
httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(30);
// Автосохранение конфигурации
txtServerUrl.TextChanged += (s, e) => SaveConfig();
txtAgentName.TextChanged += (s, e) => SaveConfig();
numThreads.ValueChanged += (s, e) => SaveConfig();
}
private void InitializeComponent()
{
this.Size = new Size(800, 600);
this.Text = "Подключение агентов через Web API";
this.StartPosition = FormStartPosition.CenterScreen;
this.MinimumSize = new Size(700, 500);
var mainLayout = new TableLayoutPanel();
mainLayout.Dock = DockStyle.Fill;
mainLayout.ColumnCount = 1;
mainLayout.RowCount = 4;
mainLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Подключение
mainLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Статус
mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 70F)); // Лог
mainLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Кнопки
mainLayout.Padding = new Padding(20);
// === СЕКЦИЯ ПОДКЛЮЧЕНИЯ ===
var connectionGroup = new GroupBox();
connectionGroup.Text = "Настройки подключения к Web API";
connectionGroup.Font = new Font("Segoe UI", 11F, FontStyle.Bold);
connectionGroup.Dock = DockStyle.Fill;
var connectionLayout = new TableLayoutPanel();
connectionLayout.Dock = DockStyle.Fill;
connectionLayout.ColumnCount = 4;
connectionLayout.RowCount = 4;
connectionLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
connectionLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));
connectionLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
connectionLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));
connectionLayout.Padding = new Padding(10);
// URL сервера
connectionLayout.Controls.Add(new Label { Text = "URL сервера:", Font = new Font("Segoe UI", 10F), TextAlign = ContentAlignment.MiddleRight }, 0, 0);
txtServerUrl = new TextBox { Text = "http://localhost:5000", Font = new Font("Segoe UI", 10F), Dock = DockStyle.Fill };
connectionLayout.Controls.Add(txtServerUrl, 1, 0);
// Кнопка теста соединения
btnTestConnection = new Button { Text = "Тест соединения", Font = new Font("Segoe UI", 9F), BackColor = Color.LightBlue, Dock = DockStyle.Fill };
btnTestConnection.Click += BtnTestConnection_Click;
connectionLayout.Controls.Add(btnTestConnection, 2, 0);
// Статус подключения
lblConnectionStatus = new Label { Text = "Статус: Отключено", Font = new Font("Segoe UI", 10F), ForeColor = Color.Red, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleLeft };
connectionLayout.Controls.Add(lblConnectionStatus, 3, 0);
// Имя агента
connectionLayout.Controls.Add(new Label { Text = "Имя агента:", Font = new Font("Segoe UI", 10F), TextAlign = ContentAlignment.MiddleRight }, 0, 1);
txtAgentName = new TextBox { Text = Environment.MachineName, Font = new Font("Segoe UI", 10F), Dock = DockStyle.Fill };
connectionLayout.Controls.Add(txtAgentName, 1, 1);
// Количество потоков
connectionLayout.Controls.Add(new Label { Text = "Потоков:", Font = new Font("Segoe UI", 10F), TextAlign = ContentAlignment.MiddleRight }, 2, 1);
numThreads = new NumericUpDown { Minimum = 1, Maximum = 128, Value = 4, Font = new Font("Segoe UI", 10F), Dock = DockStyle.Fill };
connectionLayout.Controls.Add(numThreads, 3, 1);
// Кнопка подключения
btnConnect = new Button { Text = "Подключиться к серверу", Font = new Font("Segoe UI", 10F, FontStyle.Bold), BackColor = Color.LightGreen, Dock = DockStyle.Fill };
btnConnect.Click += BtnConnect_Click;
connectionLayout.SetColumnSpan(btnConnect, 4);
connectionLayout.Controls.Add(btnConnect, 0, 2);
// Информация
var lblInfo = new Label {
Text = "Подключение к Web API серверу для получения и выполнения задач поиска seed-фраз",
Font = new Font("Segoe UI", 9F),
ForeColor = Color.Gray,
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter
};
connectionLayout.SetColumnSpan(lblInfo, 4);
connectionLayout.Controls.Add(lblInfo, 0, 3);
connectionGroup.Controls.Add(connectionLayout);
mainLayout.Controls.Add(connectionGroup, 0, 0);
// === СЕКЦИЯ ЛОГА ===
var logGroup = new GroupBox();
logGroup.Text = "Лог подключения";
logGroup.Font = new Font("Segoe UI", 11F, FontStyle.Bold);
logGroup.Dock = DockStyle.Fill;
var logLayout = new TableLayoutPanel();
logLayout.Dock = DockStyle.Fill;
logLayout.ColumnCount = 1;
logLayout.RowCount = 2;
logLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
logLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
txtLog = new TextBox();
txtLog.Dock = DockStyle.Fill;
txtLog.Multiline = true;
txtLog.ScrollBars = ScrollBars.Vertical;
txtLog.Font = new Font("Consolas", 9F);
txtLog.ReadOnly = true;
txtLog.BackColor = Color.Black;
txtLog.ForeColor = Color.Lime;
logLayout.Controls.Add(txtLog, 0, 0);
// Кнопка очистки лога
btnClearLog = new Button { Text = "Очистить лог", Font = new Font("Segoe UI", 9F), Dock = DockStyle.Fill };
btnClearLog.Click += BtnClearLog_Click;
logLayout.Controls.Add(btnClearLog, 0, 1);
logGroup.Controls.Add(logLayout);
mainLayout.Controls.Add(logGroup, 0, 2);
this.Controls.Add(mainLayout);
}
private async void BtnTestConnection_Click(object? sender, EventArgs e)
{
try
{
btnTestConnection.Enabled = false;
btnTestConnection.Text = "Тестирование...";
AddLog("Начинаем тест соединения...");
var url = txtServerUrl.Text.Trim();
if (string.IsNullOrEmpty(url))
{
AddLog("Ошибка: URL сервера не указан");
return;
}
// Убираем trailing slash если есть
if (url.EndsWith("/"))
url = url.TrimEnd('/');
var testUrl = $"{url}/api/health";
AddLog($"Тестируем соединение с: {testUrl}");
var response = await httpClient.GetAsync(testUrl);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
AddLog($"✅ Соединение успешно! Ответ сервера: {content}");
MessageBox.Show("Соединение с сервером установлено успешно!", "Тест соединения", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
AddLog($"❌ Ошибка соединения. Код: {response.StatusCode}");
MessageBox.Show($"Ошибка соединения. Код: {response.StatusCode}", "Тест соединения", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
AddLog($"❌ Ошибка теста соединения: {ex.Message}");
MessageBox.Show($"Ошибка теста соединения: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btnTestConnection.Enabled = true;
btnTestConnection.Text = "Тест соединения";
}
}
private async void BtnConnect_Click(object? sender, EventArgs e)
{
if (isConnected)
{
await DisconnectFromServer();
}
else
{
await ConnectToServer();
}
}
private async Task ConnectToServer()
{
try
{
btnConnect.Enabled = false;
btnConnect.Text = "Подключение...";
AddLog("Начинаем подключение к серверу...");
var url = txtServerUrl.Text.Trim();
if (string.IsNullOrEmpty(url))
{
AddLog("Ошибка: URL сервера не указан");
return;
}
// Убираем trailing slash если есть
if (url.EndsWith("/"))
url = url.TrimEnd('/');
// Регистрируем агента
var agentData = new
{
Name = txtAgentName.Text,
Threads = (int)numThreads.Value,
Status = "Connected"
};
var json = JsonSerializer.Serialize(agentData);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync($"{url}/api/agents/register", content);
if (response.IsSuccessStatusCode)
{
isConnected = true;
lblConnectionStatus.Text = "Статус: Подключено";
lblConnectionStatus.ForeColor = Color.Green;
btnConnect.Text = "Отключиться";
btnConnect.BackColor = Color.LightCoral;
AddLog("✅ Успешно подключились к серверу!");
// Запускаем получение задач
_ = Task.Run(async () => await StartTaskPolling(url));
}
else
{
var errorContent = await response.Content.ReadAsStringAsync();
AddLog($"❌ Ошибка подключения. Код: {response.StatusCode}, Ошибка: {errorContent}");
MessageBox.Show($"Ошибка подключения: {errorContent}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
AddLog($"❌ Ошибка подключения: {ex.Message}");
MessageBox.Show($"Ошибка подключения: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btnConnect.Enabled = true;
if (!isConnected)
{
btnConnect.Text = "Подключиться к серверу";
}
}
}
private async Task DisconnectFromServer()
{
try
{
btnConnect.Enabled = false;
btnConnect.Text = "Отключение...";
AddLog("Отключаемся от сервера...");
var url = txtServerUrl.Text.Trim();
if (url.EndsWith("/"))
url = url.TrimEnd('/');
var response = await httpClient.DeleteAsync($"{url}/api/agents/{txtAgentName.Text}");
isConnected = false;
lblConnectionStatus.Text = "Статус: Отключено";
lblConnectionStatus.ForeColor = Color.Red;
btnConnect.Text = "Подключиться к серверу";
btnConnect.BackColor = Color.LightGreen;
AddLog("✅ Отключились от сервера");
}
catch (Exception ex)
{
AddLog($"❌ Ошибка отключения: {ex.Message}");
}
finally
{
btnConnect.Enabled = true;
}
}
private async Task StartTaskPolling(string serverUrl)
{
while (isConnected)
{
try
{
// Получаем задачу
var response = await httpClient.GetAsync($"{serverUrl}/api/agents/{txtAgentName.Text}/task");
if (response.IsSuccessStatusCode)
{
var taskJson = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(taskJson) && taskJson != "null")
{
AddLog($"Получена задача: {taskJson}");
// Здесь можно добавить логику выполнения задачи
// Пока просто отправляем результат
var result = new { Status = "Completed", Result = "Task processed" };
var resultJson = JsonSerializer.Serialize(result);
var resultContent = new StringContent(resultJson, Encoding.UTF8, "application/json");
await httpClient.PostAsync($"{serverUrl}/api/agents/{txtAgentName.Text}/result", resultContent);
AddLog("Задача выполнена и результат отправлен");
}
}
await Task.Delay(5000); // Проверяем каждые 5 секунд
}
catch (Exception ex)
{
AddLog($"Ошибка при получении задач: {ex.Message}");
await Task.Delay(10000); // При ошибке ждем дольше
}
}
}
private void AddLog(string message)
{
if (txtLog.InvokeRequired)
{
txtLog.Invoke(new Action(() => AddLog(message)));
return;
}
var timestamp = DateTime.Now.ToString("HH:mm:ss");
txtLog.AppendText($"[{timestamp}] {message}{Environment.NewLine}");
txtLog.ScrollToCaret();
}
private void BtnClearLog_Click(object? sender, EventArgs e)
{
txtLog.Clear();
}
private void SaveConfig()
{
try
{
var config = new AgentWebConfig
{
ServerUrl = txtServerUrl.Text,
AgentName = txtAgentName.Text,
Threads = (int)numThreads.Value
};
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(ConfigFile, json);
}
catch (Exception ex)
{
AddLog($"Ошибка сохранения конфигурации: {ex.Message}");
}
}
private void LoadConfig()
{
try
{
if (File.Exists(ConfigFile))
{
var json = File.ReadAllText(ConfigFile);
var config = JsonSerializer.Deserialize<AgentWebConfig>(json);
if (config != null)
{
txtServerUrl.Text = config.ServerUrl ?? "http://localhost:5000";
txtAgentName.Text = config.AgentName ?? Environment.MachineName;
numThreads.Value = config.Threads > 0 ? config.Threads : 4;
}
}
}
catch (Exception ex)
{
AddLog($"Ошибка загрузки конфигурации: {ex.Message}");
}
}
private class AgentWebConfig
{
public string ServerUrl { get; set; } = "http://localhost:5000";
public string AgentName { get; set; } = "";
public int Threads { get; set; } = 4;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (isConnected)
{
_ = DisconnectFromServer();
}
httpClient?.Dispose();
base.OnFormClosing(e);
}
}
}