Skip to content

Commit d67cabe

Browse files
committed
PortLookup improved, contains #22 #20
1 parent 809e872 commit d67cabe

6 files changed

Lines changed: 198 additions & 114 deletions

File tree

Source/NETworkManager/Models/Network/PortLookup.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using NETworkManager.Models.Settings;
66
using System;
7+
using System.Globalization;
78

89
namespace NETworkManager.Models.Network
910
{
@@ -12,13 +13,14 @@ public static class PortLookup
1213
#region Variables
1314
private static string PortsFilePath = Path.Combine(ConfigurationManager.Current.ExecutionPath, "Resources", "ports.txt");
1415

15-
public static Lookup<int, PortLookupInfo> Ports;
16+
private static List<PortLookupInfo> PortList;
17+
private static Lookup<int, PortLookupInfo> Ports;
1618
#endregion
1719

1820
#region Methods
1921
static PortLookup()
2022
{
21-
List<PortLookupInfo> portList = new List<PortLookupInfo>();
23+
PortList = new List<PortLookupInfo>();
2224

2325
// Load list from resource folder (.txt-file)
2426
foreach (string line in File.ReadAllLines(PortsFilePath))
@@ -33,10 +35,10 @@ static PortLookup()
3335

3436
// string key = GetKey(port, protocol);
3537

36-
portList.Add(new PortLookupInfo(port, protocol, portData[2], portData[3]));
38+
PortList.Add(new PortLookupInfo(port, protocol, portData[2], portData[3]));
3739
}
3840

39-
Ports = (Lookup<int, PortLookupInfo>)portList.ToLookup(x => x.Number);
41+
Ports = (Lookup<int, PortLookupInfo>)PortList.ToLookup(x => x.Number);
4042
}
4143

4244
public static Task<List<PortLookupInfo>> LookupAsync(int port)
@@ -56,12 +58,12 @@ public static List<PortLookupInfo> Lookup(int port)
5658
return list;
5759
}
5860

59-
public static Task<List<PortLookupInfo>> LookupAsync(int[] ports)
61+
public static Task<List<PortLookupInfo>> LookupAsync(List<int> ports)
6062
{
6163
return Task.Run(() => Lookup(ports));
6264
}
6365

64-
public static List<PortLookupInfo> Lookup(int[] ports)
66+
public static List<PortLookupInfo> Lookup(List<int> ports)
6567
{
6668
List<PortLookupInfo> list = new List<PortLookupInfo>();
6769

@@ -75,6 +77,28 @@ public static List<PortLookupInfo> Lookup(int[] ports)
7577

7678
return list;
7779
}
80+
81+
public static Task<List<PortLookupInfo>> LookupByServiceAsync(List<string> portsByService)
82+
{
83+
return Task.Run(() => LookupByService(portsByService));
84+
}
85+
86+
public static List<PortLookupInfo> LookupByService(List<string> portsByService)
87+
{
88+
List<PortLookupInfo> list = new List<PortLookupInfo>();
89+
90+
foreach (PortLookupInfo info in PortList)
91+
{
92+
foreach (string portByService in portsByService)
93+
{
94+
if (info.Service.IndexOf(portByService, StringComparison.OrdinalIgnoreCase) > 0 || info.Description.IndexOf(portByService, StringComparison.OrdinalIgnoreCase) > 0)
95+
list.Add(info);
96+
}
97+
}
98+
99+
return list;
100+
101+
}
78102
#endregion
79103

80104
public enum Protocol

Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@
201201
<system:String x:Key="String_StartTime">Startzeit</system:String>
202202
<system:String x:Key="String_Duration">Dauer</system:String>
203203
<system:String x:Key="String_EndTime">Endzeit</system:String>
204-
204+
<system:String x:Key="String_NoPortsFoundCheckYourInput">Keine Ports gefunden. Überprüfen Sie Ihre Eingabe!</system:String>
205+
205206
<!-- Buttons -->
206207
<system:String x:Key="String_Button_Change">Wechseln</system:String>
207208
<system:String x:Key="String_Button_Default">Standard</system:String>

Source/NETworkManager/Resources/Localization/Resources.en-US.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<system:String x:Key="String_StartTime">Start time</system:String>
204204
<system:String x:Key="String_Duration">Duration</system:String>
205205
<system:String x:Key="String_EndTime">End time</system:String>
206+
<system:String x:Key="String_NoPortsFoundCheckYourInput">No ports found. Check your input!</system:String>
206207

207208
<!-- Buttons -->
208209
<system:String x:Key="String_Button_Change">Change</system:String>

Source/NETworkManager/ViewModels/Applications/WikiViewModel.cs

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,29 @@ public ObservableCollection<OUIInfo> OUILookupResult
9090
}
9191
}
9292

93-
private string _ports;
94-
public string Ports
93+
private string _portsOrService;
94+
public string PortsOrService
9595
{
96-
get { return _ports; }
96+
get { return _portsOrService; }
9797
set
9898
{
99-
if (value == _ports)
99+
if (value == _portsOrService)
100100
return;
101101

102-
_ports = value;
102+
_portsOrService = value;
103103
OnPropertyChanged();
104104
}
105105
}
106106

107-
private bool _portsHasError;
108-
public bool PortsHasError
107+
private bool _portsOrServiceHasError;
108+
public bool PortsOrServiceHasError
109109
{
110-
get { return _portsHasError; }
110+
get { return _portsOrServiceHasError; }
111111
set
112112
{
113-
if (value == _portsHasError)
113+
if (value == _portsOrServiceHasError)
114114
return;
115-
_portsHasError = value;
115+
_portsOrServiceHasError = value;
116116
OnPropertyChanged();
117117
}
118118
}
@@ -160,6 +160,20 @@ public ObservableCollection<PortLookupInfo> PortLookupResult
160160
_portLookupResult = value;
161161
}
162162
}
163+
164+
private bool _noPortsFound;
165+
public bool NoPortsFound
166+
{
167+
get { return _noPortsFound; }
168+
set
169+
{
170+
if (value == _noPortsFound)
171+
return;
172+
173+
_noPortsFound = value;
174+
OnPropertyChanged();
175+
}
176+
}
163177
#endregion
164178

165179
#region Constructor, Load settings
@@ -187,16 +201,12 @@ private void LoadSettings()
187201
}
188202
#endregion
189203

190-
#region Settings
191-
192-
#endregion
193-
194204
#region ICommands & Actions
195205
public ICommand OUILookupCommand
196206
{
197207
get { return new RelayCommand(p => OUILookupAction(), OUILookup_CanExecute); }
198208
}
199-
209+
200210
private bool OUILookup_CanExecute(object parameter)
201211
{
202212
return !MACAddressHasError;
@@ -223,12 +233,12 @@ private async void OUILookupAction()
223233

224234
public ICommand PortLookupCommand
225235
{
226-
get { return new RelayCommand(p => PortLookupAction(),PortLookup_CanExecute); }
236+
get { return new RelayCommand(p => PortLookupAction(), PortLookup_CanExecute); }
227237
}
228238

229239
private bool PortLookup_CanExecute(object parameter)
230240
{
231-
return !PortsHasError;
241+
return !PortsOrServiceHasError;
232242
}
233243

234244
private async void PortLookupAction()
@@ -237,16 +247,68 @@ private async void PortLookupAction()
237247

238248
PortLookupResult.Clear();
239249

240-
int[] ports = await PortRangeHelper.ConvertPortRangeToIntArrayAsync(Ports);
250+
List<int> ports = new List<int>();
251+
List<string> portsByService = new List<string>();
252+
253+
foreach (string portOrService in PortsOrService.Split(';'))
254+
{
255+
string portOrService1 = portOrService.Trim();
256+
257+
if (portOrService1.Contains("-"))
258+
{
259+
string[] portRange = portOrService1.Split('-');
260+
261+
if (int.TryParse(portRange[0], out int startPort) && int.TryParse(portRange[1], out int endPort))
262+
{
263+
if ((startPort > 0) && (startPort < 65536) && (endPort > 0) && (endPort < 65536) && (startPort < endPort))
264+
{
265+
for (int i = startPort; i < endPort +1; i++)
266+
{
267+
ports.Add(i);
268+
}
269+
}
270+
else
271+
{
272+
portsByService.Add(portOrService1);
273+
}
274+
275+
}
276+
else
277+
{
278+
portsByService.Add(portOrService1);
279+
}
280+
}
281+
else
282+
{
283+
if (int.TryParse(portOrService1, out int port))
284+
{
285+
if (port > 0 && port < 65536)
286+
ports.Add(port);
287+
else
288+
portsByService.Add(portOrService1);
289+
}
290+
else
291+
{
292+
portsByService.Add(portOrService1);
293+
}
294+
}
295+
}
241296

242297
foreach (PortLookupInfo info in await PortLookup.LookupAsync(ports))
243298
{
244299
PortLookupResult.Add(info);
245300
}
246301

247-
PortsHistory = new List<string>(HistoryListHelper.Modify(PortsHistory, Ports, SettingsManager.Current.Application_HistoryListEntries));
302+
foreach(PortLookupInfo info in await PortLookup.LookupByServiceAsync(portsByService))
303+
{
304+
PortLookupResult.Add(info);
305+
}
306+
307+
PortsHistory = new List<string>(HistoryListHelper.Modify(PortsHistory, PortsOrService, SettingsManager.Current.Application_HistoryListEntries));
248308

249309
IsPortLookupRunning = false;
310+
311+
NoPortsFound = PortLookupResult.Count == 0;
250312
}
251313
#endregion
252314
}

Source/NETworkManager/Views/Applications/PingView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
<TextBlock Grid.Column="0" Grid.Row="0" Text="{DynamicResource String_StartTime}" />
164164
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding StartTime, Converter={StaticResource NullableDateTimeToStringConverter}}" />
165165
<TextBlock Grid.Column="0" Grid.Row="2" Text="{DynamicResource String_Duration}" />
166-
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Duration, StringFormat={}{0:hh}:{0:mm}:{0:ss}:{0:ff}}" />
166+
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Duration, StringFormat={}{0:hh}h {0:mm}m {0:ss}s {0:ff}ms}" />
167167
<TextBlock Grid.Column="0" Grid.Row="4" Text="{DynamicResource String_EndTime}" />
168168
<TextBox Grid.Column="1" Grid.Row="4" Text="{Binding EndTime, Converter={StaticResource NullableDateTimeToStringConverter}}" />
169169
<TextBlock Grid.Column="3" Grid.Row="0" Text="{DynamicResource String_PacketsTransmitted}" />

0 commit comments

Comments
 (0)