Skip to content

Commit be56e92

Browse files
author
Manuel Ullmann
committed
Add Firewall application.
Signed-off-by: Manuel Ullmann <manuel.ullmann@rediecon.com>
1 parent 65969a5 commit be56e92

File tree

63 files changed

+6802
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+6802
-26
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Linq;
4+
using System.Windows.Data;
5+
using NETworkManager.Models.Network;
6+
using NETworkManager.Localization.Resources;
7+
8+
9+
namespace NETworkManager.Converters;
10+
11+
public class BoolArrayToFwRuleCategoriesConverter : IValueConverter
12+
{
13+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
14+
{
15+
if (targetType != typeof(string)) return null;
16+
const int expectedLength = 3;
17+
var fallback = GetTranslation(Enum.GetName(NetworkProfiles.NotConfigured), false);
18+
if (value is not bool[] { Length: expectedLength } boolArray)
19+
return fallback;
20+
var result = string.Empty;
21+
var numSelected = boolArray.CountAny(true);
22+
switch (numSelected)
23+
{
24+
case 0:
25+
return fallback;
26+
case < 2:
27+
return GetTranslation(Enum.GetName(typeof(NetworkProfiles),
28+
Array.FindIndex(boolArray, b => b)), false);
29+
}
30+
31+
if (boolArray.All(b => b))
32+
return Strings.All;
33+
34+
for (var i = 0; i < expectedLength; i++)
35+
{
36+
if (boolArray[i])
37+
result += $", {GetTranslation(Enum.GetName(typeof(NetworkProfiles), i), true)}";
38+
}
39+
40+
return result[2..];
41+
42+
}
43+
44+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
private static string GetTranslation(string key, bool trimmed)
50+
{
51+
return Strings.ResourceManager.GetString(trimmed ? $"{key}_Short3" : key, Strings.Culture);
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace NETworkManager.Converters;
6+
7+
public class EmptyToIntMaxValueConverter : IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
const int fallback = int.MaxValue;
12+
if (targetType == typeof(int))
13+
{
14+
if (value is not string strValue)
15+
return fallback;
16+
if (string.IsNullOrWhiteSpace(strValue))
17+
return fallback;
18+
if (!int.TryParse(strValue, out int parsedIntValue))
19+
return fallback;
20+
return parsedIntValue;
21+
}
22+
23+
if (targetType != typeof(string))
24+
return null;
25+
if (value is not int intValue)
26+
return null;
27+
return intValue is fallback ? string.Empty : intValue.ToString();
28+
}
29+
30+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
31+
{
32+
return Convert(value, targetType, parameter, culture);
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Globalization;
3+
using System.IO;
4+
using System.Windows.Data;
5+
using NETworkManager.Models.Firewall;
6+
7+
namespace NETworkManager.Converters;
8+
9+
/// <summary>
10+
/// Convert a program reference to a string and vice versa.
11+
/// </summary>
12+
public class FirewallRuleProgramConverter : IValueConverter
13+
{
14+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
15+
{
16+
if (targetType == typeof(FirewallRuleProgram))
17+
{
18+
if (value is not string program)
19+
return null;
20+
if (string.IsNullOrWhiteSpace(program))
21+
return null;
22+
try
23+
{
24+
var exe = new FirewallRuleProgram(program);
25+
return exe;
26+
}
27+
catch (FileNotFoundException)
28+
{
29+
return null;
30+
}
31+
}
32+
33+
if (targetType != typeof(string))
34+
return null;
35+
return value is not FirewallRuleProgram prog ? null : prog.ToString();
36+
}
37+
38+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
39+
{
40+
return Convert(value, targetType, parameter, culture);
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace NETworkManager.Converters;
6+
/// <summary>
7+
/// Convert a value of 0 to the boolean false.
8+
/// </summary>
9+
public class IntZeroToFalseConverter : IValueConverter
10+
{
11+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12+
{
13+
return value switch
14+
{
15+
null => false,
16+
int i => i is not 0,
17+
bool boolean => boolean ? 1 : 0,
18+
_ => false
19+
};
20+
}
21+
22+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
23+
{
24+
return Convert(value, targetType, parameter, culture);
25+
}
26+
}

Source/NETworkManager.Converters/NETworkManager.Converters.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<Compile Include="..\GlobalAssemblyInfo.cs" Link="Properties\GlobalAssemblyInfo.cs" />
2121
</ItemGroup>
2222
<ItemGroup>
23+
<ProjectReference Include="..\NETworkManager.Interfaces\NETworkManager.Interfaces.csproj" />
2324
<ProjectReference Include="..\NETworkManager.Localization\NETworkManager.Localization.csproj" />
2425
<ProjectReference Include="..\NETworkManager.Models\NETworkManager.Models.csproj" />
2526
<ProjectReference Include="..\NETworkManager.Profiles\NETworkManager.Profiles.csproj" />
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Windows.Data;
6+
using NETworkManager.Models.Firewall;
7+
using NETworkManager.Settings;
8+
9+
namespace NETworkManager.Converters;
10+
11+
/// <summary>
12+
/// Converts a port range or port to an instance of FirewallPortSpecification.
13+
/// </summary>
14+
public class PortRangeToPortSpecificationConverter : IValueConverter
15+
{
16+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
17+
{
18+
char portDelimiter = SettingsManager.Current.Firewall_UseWindowsPortSyntax ? ',' : ';';
19+
if (targetType == typeof(List<FirewallPortSpecification>))
20+
{
21+
if (value is not string input)
22+
return null;
23+
const char rangeDelimiter = '-';
24+
List<FirewallPortSpecification> resultList = [];
25+
var portList = input.Split(portDelimiter);
26+
foreach (var port in portList)
27+
{
28+
if (port.Contains(rangeDelimiter))
29+
{
30+
var portRange = port.Split(rangeDelimiter);
31+
if (!int.TryParse(portRange[0], out var startPort))
32+
return null;
33+
if (!int.TryParse(portRange[1], out var endPort))
34+
return null;
35+
resultList.Add(new FirewallPortSpecification(startPort, endPort));
36+
}
37+
else
38+
{
39+
if (!int.TryParse(port, out var portNumber))
40+
return null;
41+
resultList.Add(new FirewallPortSpecification(portNumber));
42+
}
43+
}
44+
return resultList;
45+
}
46+
47+
if (targetType != typeof(string))
48+
return null;
49+
if (value is not List<FirewallPortSpecification> portSpecs)
50+
return string.Empty;
51+
string result = portSpecs
52+
.Aggregate("", (current, portSpecification) => current + $"{portSpecification}{portDelimiter} ");
53+
return result.Length > 0 ? result[..^2] : string.Empty;
54+
}
55+
56+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
57+
{
58+
return Convert(value, targetType, parameter, culture);
59+
}
60+
}

Source/NETworkManager.Documentation/DocumentationIdentifier.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ public enum DocumentationIdentifier
134134
/// ARP Table documentation page.
135135
/// </summary>
136136
ApplicationArpTable,
137-
137+
138+
/// <summary>
139+
/// Firewall documentation page.
140+
/// </summary>
141+
ApplicationFirewall,
142+
138143
/// <summary>
139144
/// Settings\General documentation page.
140145
/// </summary>

Source/NETworkManager.Documentation/DocumentationManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Reflection.Metadata;
34
using System.Windows.Input;
45
using NETworkManager.Models;
56
using NETworkManager.Settings;
@@ -96,6 +97,9 @@ public static class DocumentationManager
9697

9798
new DocumentationInfo(DocumentationIdentifier.ApplicationArpTable,
9899
@"docs/application/arp-table"),
100+
101+
new DocumentationInfo(DocumentationIdentifier.ApplicationFirewall,
102+
@"docs/application/firewall"),
99103

100104
new DocumentationInfo(DocumentationIdentifier.SettingsGeneral,
101105
@"docs/settings/general"),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using NETworkManager.Models.Firewall;
2+
3+
namespace NETworkManager.Interfaces.ViewModels;
4+
5+
/// <summary>
6+
/// Interface to allow converters and validators access to the firewall rule view model,
7+
/// in this case validating with the other network profile checkboxes.
8+
/// </summary>
9+
public interface IFirewallRuleViewModel
10+
{
11+
public bool NetworkProfileDomain { get; }
12+
13+
public bool NetworkProfilePrivate { get; }
14+
15+
public bool NetworkProfilePublic { get; }
16+
17+
public List<FirewallPortSpecification>? LocalPorts { get; }
18+
19+
public List<FirewallPortSpecification>? RemotePorts { get; }
20+
21+
public FirewallRuleProgram? Program { get; }
22+
23+
public int MaxLengthName { get; }
24+
25+
public string? UserDefinedName { get; }
26+
27+
public bool NameHasError { get; set; }
28+
29+
public bool HasError { get; }
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.ObjectModel;
2+
using System.Windows.Input;
3+
4+
namespace NETworkManager.Interfaces.ViewModels;
5+
6+
public interface IFirewallViewModel
7+
{
8+
public ObservableCollection<IFirewallRuleViewModel> FirewallRulesInterface { get; }
9+
10+
public ICommand ExpandAllProfileGroupsCommand { get; }
11+
12+
public ICommand CollapseAllProfileGroupsCommand { get; }
13+
14+
public int MaxLengthHistory { get; }
15+
16+
public static IFirewallViewModel? Instance { get; set; }
17+
18+
public static void SetInstance(IFirewallViewModel? viewModel)
19+
{
20+
Instance = viewModel;
21+
}
22+
}

0 commit comments

Comments
 (0)