forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkConnectionProperties.cs
More file actions
323 lines (286 loc) · 14.2 KB
/
NetworkConnectionProperties.cs
File metadata and controls
323 lines (286 loc) · 14.2 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
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
namespace Microsoft.PowerToys.Run.Plugin.System.Components
{
/// <summary>
/// This class represents the information for a network connection/interface
/// </summary>
internal sealed class NetworkConnectionProperties
{
/// <summary>
/// Gets the name of the adapter
/// </summary>
internal string Adapter { get; private set; }
/// <summary>
/// Gets the physical address (MAC) of the adapter
/// </summary>
internal string PhysicalAddress { get; private set; }
/// <summary>
/// Gets a value indicating the interface type
/// </summary>
internal NetworkInterfaceType Type { get; private set; }
/// <summary>
/// Gets the speed of the adapter as unformatted value (Static information form the adapter device)
/// </summary>
internal long Speed { get; private set; }
/// <summary>
/// Gets a value indicating the operational state of the adapter
/// </summary>
internal OperationalStatus State { get; private set; }
/// <summary>
/// Gets the name of the network connection
/// </summary>
internal string ConnectionName { get; private set; }
/// <summary>
/// Gets a string with the suffix of the connection
/// </summary>
internal string Suffix { get; private set; }
/// <summary>
/// Gets the IPv4 address
/// </summary>
internal string IPv4 { get; private set; }
/// <summary>
/// Gets the IPv4 subnet mask
/// </summary>
internal string IPv4Mask { get; private set; }
/// <summary>
/// Gets the primarily used IPv6 address
/// </summary>
internal string IPv6Primary { get; private set; }
/// <summary>
/// Gets the global IPv6 address
/// </summary>
internal string IPv6Global { get; private set; }
/// <summary>
/// Gets the temporary IPv6 address
/// </summary>
internal string IPv6Temporary { get; private set; }
/// <summary>
/// Gets the link local IPv6 address
/// </summary>
internal string IPv6LinkLocal { get; private set; }
/// <summary>
/// Gets the site local IPv6 address
/// </summary>
internal string IPv6SiteLocal { get; private set; }
/// <summary>
/// Gets the unique local IPv6 address
/// </summary>
internal string IPv6UniqueLocal { get; private set; }
/// <summary>
/// Gets the list of gateway IPs as string
/// </summary>
internal List<IPAddress> Gateways { get; private set; } = new List<IPAddress>();
/// <summary>
/// Gets the list of DHCP server IPs as string
/// </summary>
internal IPAddressCollection DhcpServers { get; private set; }
/// <summary>
/// Gets the list of DNS server IPs as string
/// </summary>
internal IPAddressCollection DnsServers { get; private set; }
/// <summary>
/// Gets the list of WINS server IPs as string
/// </summary>
internal IPAddressCollection WinsServers { get; private set; }
private static readonly CompositeFormat MicrosoftPluginSysGbps = CompositeFormat.Parse(Properties.Resources.Microsoft_plugin_sys_Gbps);
private static readonly CompositeFormat MicrosoftPluginSysMbps = CompositeFormat.Parse(Properties.Resources.Microsoft_plugin_sys_Mbps);
/// <summary>
/// Initializes a new instance of the <see cref="NetworkConnectionProperties"/> class.
/// This private constructor is used when we crete the list of adapter (properties) by calling <see cref="NetworkConnectionProperties.GetList()"/>.
/// </summary>
/// <param name="networkInterface">Network interface of the connection</param>
private NetworkConnectionProperties(NetworkInterface networkInterface)
{
// Setting adapter properties
Adapter = networkInterface.Description;
PhysicalAddress = networkInterface.GetPhysicalAddress().ToString();
Type = networkInterface.NetworkInterfaceType;
Speed = networkInterface.Speed;
State = networkInterface.OperationalStatus;
// Connection properties
ConnectionName = networkInterface.Name;
if (State == OperationalStatus.Up)
{
Suffix = networkInterface.GetIPProperties().DnsSuffix;
SetIpProperties(networkInterface.GetIPProperties());
}
}
/// <summary>
/// Creates a list with all network adapters and their properties
/// </summary>
/// <returns>List containing all network adapters</returns>
internal static List<NetworkConnectionProperties> GetList()
{
var interfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback && x.GetPhysicalAddress() != null)
.Select(i => new NetworkConnectionProperties(i))
.OrderByDescending(i => i.IPv4) // list IPv4 first
.ThenBy(i => i.IPv6Primary) // then IPv6
.ToList();
return interfaces;
}
/// <summary>
/// Gets a formatted string with the adapter details
/// </summary>
/// <returns>String with the details</returns>
internal string GetAdapterDetails()
{
return $"{Resources.Microsoft_plugin_sys_AdapterName}: {Adapter}" +
$"\n{Resources.Microsoft_plugin_sys_PhysicalAddress}: {PhysicalAddress}" +
$"\n{Resources.Microsoft_plugin_sys_Speed}: {GetFormattedSpeedValue(Speed)}" +
$"\n{Resources.Microsoft_plugin_sys_Type}: {GetAdapterTypeAsString(Type)}" +
$"\n{Resources.Microsoft_plugin_sys_State}: " + (State == OperationalStatus.Up ? Resources.Microsoft_plugin_sys_Connected : Resources.Microsoft_plugin_sys_Disconnected) +
$"\n{Resources.Microsoft_plugin_sys_ConnectionName}: {ConnectionName}";
}
/// <summary>
/// Returns a formatted string with the connection details
/// </summary>
/// <returns>String with the details</returns>
internal string GetConnectionDetails()
{
return $"{Resources.Microsoft_plugin_sys_ConnectionName}: {ConnectionName}" +
$"\n{Resources.Microsoft_plugin_sys_State}: " + (State == OperationalStatus.Up ? Resources.Microsoft_plugin_sys_Connected : Resources.Microsoft_plugin_sys_Disconnected) +
$"\n{Resources.Microsoft_plugin_sys_Type}: {GetAdapterTypeAsString(Type)}" +
$"\n{Resources.Microsoft_plugin_sys_Suffix}: {Suffix}" +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip4Address}: ", IPv4) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip4SubnetMask}: ", IPv4Mask) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Address}:\n\t", IPv6Global) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Temp}:\n\t", IPv6Temporary) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Link}:\n\t", IPv6LinkLocal) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Site}:\n\t", IPv6SiteLocal) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Ip6Unique}:\n\t", IPv6UniqueLocal) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Gateways}:\n\t", Gateways) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Dhcp}:\n\t", DhcpServers) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Dns}:\n\t", DnsServers) +
CreateIpInfoForDetailsText($"{Resources.Microsoft_plugin_sys_Wins}:\n\t", WinsServers) +
$"\n\n{Resources.Microsoft_plugin_sys_AdapterName}: {Adapter}" +
$"\n{Resources.Microsoft_plugin_sys_PhysicalAddress}: {PhysicalAddress}" +
$"\n{Resources.Microsoft_plugin_sys_Speed}: {GetFormattedSpeedValue(Speed)}";
}
/// <summary>
/// Set the ip address properties of the <see cref="NetworkConnectionProperties"/> instance.
/// </summary>
/// <param name="properties">Element of the type <see cref="IPInterfaceProperties"/>.</param>
private void SetIpProperties(IPInterfaceProperties properties)
{
DateTime t = DateTime.Now;
UnicastIPAddressInformationCollection ipList = properties.UnicastAddresses;
GatewayIPAddressInformationCollection gwList = properties.GatewayAddresses;
DhcpServers = properties.DhcpServerAddresses;
DnsServers = properties.DnsAddresses;
WinsServers = properties.WinsServersAddresses;
for (int i = 0; i < ipList.Count; i++)
{
IPAddress ip = ipList[i].Address;
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
IPv4 = ip.ToString();
IPv4Mask = ipList[i].IPv4Mask.ToString();
}
else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
{
if (string.IsNullOrEmpty(IPv6Primary))
{
IPv6Primary = ip.ToString();
}
if (ip.IsIPv6LinkLocal)
{
IPv6LinkLocal = ip.ToString();
}
else if (ip.IsIPv6SiteLocal)
{
IPv6SiteLocal = ip.ToString();
}
else if (ip.IsIPv6UniqueLocal)
{
IPv6UniqueLocal = ip.ToString();
}
else if (ipList[i].SuffixOrigin == SuffixOrigin.Random)
{
IPv6Temporary = ip.ToString();
}
else
{
IPv6Global = ip.ToString();
}
}
}
for (int i = 0; i < gwList.Count; i++)
{
Gateways.Add(gwList[i].Address);
}
Debug.Print($"time for getting ips: {DateTime.Now - t}");
}
/// <summary>
/// Gets the interface type as string
/// </summary>
/// <param name="type">The type to convert</param>
/// <returns>A string indicating the interface type</returns>
private string GetAdapterTypeAsString(NetworkInterfaceType type)
{
switch (type)
{
case NetworkInterfaceType.Wman:
case NetworkInterfaceType.Wwanpp:
case NetworkInterfaceType.Wwanpp2:
return Resources.Microsoft_plugin_sys_MobileBroadband;
case NetworkInterfaceType.Wireless80211:
return Resources.Microsoft_plugin_sys_WirelessLan;
case NetworkInterfaceType.Loopback:
return Resources.Microsoft_plugin_sys_Loopback;
case NetworkInterfaceType.Tunnel:
return Resources.Microsoft_plugin_sys_TunnelConnection;
case NetworkInterfaceType.Unknown:
return Resources.Microsoft_plugin_sys_Unknown;
default:
return Resources.Microsoft_plugin_sys_Cable;
}
}
/// <summary>
/// Gets the speed as formatted text value
/// </summary>
/// <param name="speed">The adapter speed as <see langword="long"/>.</param>
/// <returns>A formatted string like `100 MB/s`</returns>
private static string GetFormattedSpeedValue(long speed)
{
return (speed >= 1000000000) ? string.Format(CultureInfo.InvariantCulture, MicrosoftPluginSysGbps, speed / 1000000000) : string.Format(CultureInfo.InvariantCulture, MicrosoftPluginSysMbps, speed / 1000000);
}
/// <summary>
/// Returns IP info or an empty string
/// </summary>
/// <param name="title">Descriptive header for the information.</param>
/// <param name="property">IP value as <see cref="string"/> or <see cref="List{String}"/>.</param>
/// <returns>Formatted string or an empty string.</returns>
/// <exception cref="ArgumentException">If the parameter <paramref name="property"/> is not of the type <see cref="string"/> or <see cref="List{String}"/>.</exception>
private static string CreateIpInfoForDetailsText(string title, dynamic property)
{
switch (property)
{
case string:
return $"\n{title}{property}";
case List<string> listString:
return listString.Count == 0 ? string.Empty : $"\n{title}{string.Join("\n\t", property)}";
case List<IPAddress> listIP:
return listIP.Count == 0 ? string.Empty : $"\n{title}{string.Join("\n\t", property)}";
case IPAddressCollection collectionIP:
return collectionIP.Count == 0 ? string.Empty : $"\n{title}{string.Join("\n\t", property)}";
case null:
return string.Empty;
default:
throw new ArgumentException($"'{property}' is not of type 'string', 'List<string>', 'List<IPAddress>' or 'IPAddressCollection'.", nameof(property));
}
}
}
}