-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
350 lines (318 loc) · 13.4 KB
/
MainForm.cs
File metadata and controls
350 lines (318 loc) · 13.4 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
using log4net;
using Microsoft.Win32;
using NetworkAdapterRouteControl.WinApi;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Caching;
using System.Threading;
using System.Windows.Forms;
namespace NetworkAdapterRouteControl
{
public partial class MainForm : Form
{
private static readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private readonly System.Threading.Timer _timer;
private static readonly MemoryCache _cache = new MemoryCache("SyncCache");
private static DateTimeOffset _vpnAdapterEmptyThrowTime = DateTimeOffset.MaxValue;
private string _adapterDescription;
private int _interfaceMetric;
private int _routeMetric;
private IPAddress[] _routeDestinationList;
private int _syncPeriod;
private Exception _lastException;
private static DateTimeOffset _lastExceptionTime = DateTimeOffset.MaxValue;
private static string ReadSetting(string key)
{
try
{
var appSettings = ConfigurationManager.AppSettings;
return appSettings[key];
}
catch (ConfigurationErrorsException)
{
return string.Empty;
}
}
private bool ReadSettings()
{
try
{
// AdapterDescription
_adapterDescription = ReadSetting("AdapterDescription");
if (string.IsNullOrEmpty(_adapterDescription))
{
throw new Exception("Сетевой адаптер не задан");
}
// InterfaceMetric
var value = ReadSetting("InterfaceMetric");
if (string.IsNullOrEmpty(value))
{
_interfaceMetric = 1000;
}
else if (!int.TryParse(ReadSetting("InterfaceMetric"), NumberStyles.Integer, CultureInfo.InvariantCulture, out _interfaceMetric))
{
throw new Exception("Метрика адаптера не задана или не является целым числом");
}
if (_interfaceMetric <= 0)
{
throw new Exception("Метрика адаптера должна быть больше нуля");
}
// RouteMetric
value = ReadSetting("RouteMetric");
if (string.IsNullOrEmpty(value))
{
_routeMetric = 1000;
}
else if (!int.TryParse(ReadSetting("RouteMetric"), NumberStyles.Integer, CultureInfo.InvariantCulture, out _routeMetric))
{
throw new Exception("Метрика маршрута не задана или не является целым числом");
}
if (_routeMetric <= 0)
{
throw new Exception("Метрика маршрута должна быть больше нуля");
}
// RouteDestinationList
var routeDestinationValueList = ReadSetting("RouteDestinationList").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (routeDestinationValueList.Length > 0)
{
_routeDestinationList = new IPAddress[routeDestinationValueList.Length];
for (int i = 0; i < routeDestinationValueList.Length; i++)
{
if (!IPAddress.TryParse(routeDestinationValueList[i], out _routeDestinationList[i]))
{
throw new Exception(String.Format(
"Список адресов назначения содержит адрес в неверном формате {0}",
routeDestinationValueList[i]));
}
}
}
// SyncPeriod
value = ReadSetting("SyncPeriod");
if (string.IsNullOrEmpty(value))
{
_syncPeriod = 100;
}
else if (!int.TryParse(ReadSetting("SyncPeriod"), NumberStyles.Integer, CultureInfo.InvariantCulture, out _syncPeriod))
{
throw new Exception("Период синхронизации не задан или не является целым числом");
}
if (_syncPeriod <= 0)
{
throw new Exception("Период синхронизации должен быть больше нуля");
}
return true;
}
catch (Exception e)
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(500, "Ошибка в параметрах", e.Message, ToolTipIcon.Error);
return false;
}
}
private static void Sync(Object o)
{
var sender = (MainForm) o;
try
{
var vpnAdapter = (AdapterInfo) _cache.Get("vpnAdapter");
if (vpnAdapter == null)
{
vpnAdapter = IpHelper.GetAdaptersInfo().Find(i => i.Description.Equals(sender._adapterDescription));
if (vpnAdapter == null)
{
if (_vpnAdapterEmptyThrowTime == DateTimeOffset.MaxValue)
{
_vpnAdapterEmptyThrowTime = DateTimeOffset.Now.AddSeconds(10);
}
else if (DateTimeOffset.Now > _vpnAdapterEmptyThrowTime)
{
throw new Exception(String.Format("Не найден сетевой адаптер {0}", sender._adapterDescription));
}
return;
}
else
{
_cache.Add("vpnAdapter", vpnAdapter, DateTimeOffset.Now.AddMilliseconds(2000));
_vpnAdapterEmptyThrowTime = DateTimeOffset.MaxValue;
}
}
var routeTable = IpHelper.GetRouteTable(false);
if (vpnAdapter.PrimaryGateway == null)
{
vpnAdapter.PrimaryGateway = routeTable.Where(i => i.InterfaceIndex == vpnAdapter.Index)?.FirstOrDefault()?.GatewayIP;
if (vpnAdapter.PrimaryGateway == null && vpnAdapter.DhcpServer != null)
{
vpnAdapter.PrimaryGateway = vpnAdapter.DhcpServer;
}
if (vpnAdapter.PrimaryGateway == null) return;
}
var destinationHashSet = new HashSet<IPAddress>()
{
IPAddress.Parse("0.0.0.0")
};
if (sender._routeDestinationList != null)
{
foreach (var routeDestination in sender._routeDestinationList)
{
destinationHashSet.Add(routeDestination);
}
}
IpHelper.SetMetric(vpnAdapter.Index, Convert.ToUInt32(sender._interfaceMetric));
var currentDestinationHashSet = new HashSet<IPAddress>();
foreach (var route in routeTable)
{
if (route.InterfaceIndex != vpnAdapter.Index || route.DestinationIP.AddressFamily != AddressFamily.InterNetwork) continue;
if (destinationHashSet.Contains(route.DestinationIP))
{
if (route.Metric != sender._routeMetric + sender._interfaceMetric)
{
route.Metric = Convert.ToUInt32(sender._routeMetric + sender._interfaceMetric);
IpHelper.SetRoute(route);
}
currentDestinationHashSet.Add(route.DestinationIP);
}
else
{
IpHelper.DeleteRoute(route);
}
}
foreach (var destinationIp in destinationHashSet)
{
if (!currentDestinationHashSet.Contains(destinationIp))
{
IpHelper.CreateRoute(new RouteEntry()
{
DestinationIP = destinationIp,
SubnetMask = IPAddress.Parse("255.255.255.255"),
GatewayIP = vpnAdapter.PrimaryGateway,
InterfaceIndex = vpnAdapter.Index,
Metric = Convert.ToUInt32(sender._routeMetric)
});
}
}
}
catch (Exception e)
{
_log.Error(e.ToString());
sender.PauseSync(1000, e);
}
}
public MainForm()
{
InitializeComponent();
_timer = new System.Threading.Timer(Sync, this, Timeout.Infinite, Timeout.Infinite);
StartSync();
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
Visible = false;
}
private void ShowNotify(string tipTitle, string tipText, int tipTimeout, ToolTipIcon tipIcon)
{
if (InvokeRequired)
{
BeginInvoke((MethodInvoker)(() => notifyIcon.Visible = true));
BeginInvoke((MethodInvoker)(() => notifyIcon.ShowBalloonTip(tipTimeout, tipTitle, tipText, tipIcon)));
}
else
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(500, tipTitle, tipText, ToolTipIcon.Info);
}
}
private void StopSync(string message)
{
SystemEvents.PowerModeChanged -= OnPowerChange;
_timer.Change(Timeout.Infinite, Timeout.Infinite);
ShowNotify("Остановлен", message, 500, ToolTipIcon.Info);
if (InvokeRequired)
BeginInvoke((MethodInvoker)(() => controlToolStripMenuItem.Checked = false));
else
controlToolStripMenuItem.Checked = false;
}
private void StartSync()
{
if (!ReadSettings())
{
controlToolStripMenuItem.Checked = false;
return;
}
_timer.Change(0, _syncPeriod);
SystemEvents.PowerModeChanged += OnPowerChange;
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(500, "Запущен", " ", ToolTipIcon.Info);
controlToolStripMenuItem.Checked = true;
}
private void OnPowerChange(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
_log.Debug("Resume");
_timer.Change(0, _syncPeriod);
break;
case PowerModes.Suspend:
_log.Debug("Suspend");
_timer.Change(Timeout.Infinite, Timeout.Infinite);
break;
}
}
private void PauseSync(int dueTime, Exception ex)
{
_timer.Change(dueTime, _syncPeriod);
if (_lastException == null ||
String.Compare(ex.Message, _lastException.Message) != 0 ||
String.Compare(ex.StackTrace, _lastException.StackTrace) != 0 ||
DateTime.Now > _lastExceptionTime.AddSeconds(60))
{
_lastException = ex;
_lastExceptionTime = DateTime.Now;
ShowNotify("Ошибка", ex.Message, 500, ToolTipIcon.Error);
}
}
public void SyncSettings()
{
if (controlToolStripMenuItem.Checked) _timer.Change(Timeout.Infinite, Timeout.Infinite);
if (!ReadSettings()) return;
if (controlToolStripMenuItem.Checked) _timer.Change(0, _syncPeriod);
}
private void ControlToolStripMenuItem_Click(object sender, EventArgs e)
{
if (controlToolStripMenuItem.Checked)
{
StopSync(" ");
}
else
{
StartSync();
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false;
Application.Exit();
}
private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
var form = new SettingForm();
form.ShowDialog(this);
}
private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
mainContextMenuStrip.Show(MousePosition);
} else if (e.Button == MouseButtons.Left)
{
var form = new SettingForm();
form.ShowDialog(this);
}
}
}
}