Skip to content

Commit

Permalink
FIX: fixed a "no-impact" issue with CountryFlags.WPF when the Coordin…
Browse files Browse the repository at this point in the history
…ates.CountryISOCode property is null (or the object itself).

FIX: fixed a regression with GetOrSetValueAsync from Wokhan.Core (it wasn't working anymore with inherited private backing fields).
FIX: first step to fix #163 (rules matching doesn't work as expected). Still need to investigate but a code update was required (check on "service" parameters nullability).
  • Loading branch information
wokhan committed Apr 2, 2023
1 parent 63e8f3a commit 52de1c5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Common.Tests/Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Wokhan.Core" Version="0.9.6-beta" />
<PackageReference Include="Wokhan.Core" Version="0.9.8-beta" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services\" />
Expand Down
2 changes: 1 addition & 1 deletion Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="System.Diagnostics.Debug" Version="4.3.0" />
<PackageReference Include="System.Runtime.Extensions" Version="4.3.1" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
<PackageReference Include="Wokhan.Core" Version="0.9.6-beta" />
<PackageReference Include="Wokhan.Core" Version="0.9.8-beta" />
<PackageReference Include="Wokhan.UI" Version="0.9.8-beta" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Common/Net/WFP/Rules/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ internal bool Matches(string path, string service, int protocol, string localpor
return Enabled
&& ((Profiles & currentProfile) != 0 || (Profiles & (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL) != 0)
&& (string.IsNullOrEmpty(ApplicationName) || StringComparer.CurrentCultureIgnoreCase.Equals(ApplicationName, path))
&& (string.IsNullOrEmpty(ServiceName) || service.Any() && ServiceName == "*" || StringComparer.CurrentCultureIgnoreCase.Equals(ServiceName, service))
&& (string.IsNullOrEmpty(ServiceName) || !string.IsNullOrEmpty(service) && ServiceName == "*" || StringComparer.CurrentCultureIgnoreCase.Equals(ServiceName, service))
&& (Protocol == WFP.Protocol.ANY || Protocol == protocol)
&& CheckRuleAddresses(RemoteAddresses, target)
&& CheckRulePorts(RemotePorts, remoteport)
Expand All @@ -230,8 +230,8 @@ public bool MatchesEvent(int currentProfile, string appPkgId, string service, st
&& CheckRuleAddresses(RemoteAddresses, target)
&& CheckRulePorts(RemotePorts, remoteport)
&& (string.IsNullOrEmpty(AppPkgId) || AppPkgId == appPkgId)
&& (string.IsNullOrEmpty(ServiceName) || service.Any() && ServiceName == "*" || service.Equals(ServiceName, StringComparison.OrdinalIgnoreCase))
;
&& (string.IsNullOrEmpty(ServiceName) || !string.IsNullOrEmpty(service) && ServiceName == "*" || StringComparer.CurrentCultureIgnoreCase.Equals(ServiceName, service));

if (ret && LogHelper.IsDebugEnabled())
{
LogHelper.Debug("Found enabled " + ActionStr + " " + DirectionStr + " Rule '" + Name + "'");
Expand All @@ -240,7 +240,7 @@ public bool MatchesEvent(int currentProfile, string appPkgId, string service, st
LogHelper.Debug("\t" + RemoteAddresses + " <--> " + target + " : " + CheckRuleAddresses(RemoteAddresses, target).ToString());
LogHelper.Debug("\t" + RemotePorts + " <--> " + remoteport + " : " + CheckRulePorts(RemotePorts, remoteport).ToString());
LogHelper.Debug("\t" + AppPkgId + " <--> " + appPkgId + " : " + (string.IsNullOrEmpty(AppPkgId) || AppPkgId == appPkgId).ToString());
LogHelper.Debug("\t" + ServiceName + " <--> " + service + " : " + (string.IsNullOrEmpty(ServiceName) || service.Equals(ServiceName, StringComparison.OrdinalIgnoreCase)).ToString());
LogHelper.Debug("\t" + ServiceName + " <--> " + service + " : " + (string.IsNullOrEmpty(ServiceName) || !string.IsNullOrEmpty(service) && ServiceName == "*" || StringComparer.CurrentCultureIgnoreCase.Equals(ServiceName, service)));
}
return ret;
}
Expand Down
5 changes: 3 additions & 2 deletions Common/Processes/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,16 @@ private static string GetServiceDesc(string service)
}
}

static readonly Version minVersionForApps = new Version(6, 2);
public static string GetAppPkgId(uint pid)
{
if (Environment.OSVersion.Version <= new Version(6, 2))
if (Environment.OSVersion.Version <= minVersionForApps)
{
//Not Windows 8 or higher, there are no Apps
return String.Empty;
}

IntPtr hProcess = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.QueryLimitedInformation, false, (uint)pid);
IntPtr hProcess = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.QueryLimitedInformation, false, pid);
if (hProcess == IntPtr.Zero)
{
LogHelper.Warning("Unable to retrieve process package id: process cannot be found!");
Expand Down
5 changes: 2 additions & 3 deletions Common/Security/EventLogAsyncReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static class EventLogAsyncReader
{
public static readonly string EVENTLOG_SECURITY = "security";


public static bool IsFirewallEventSimple(EventLogEntry entry)
{
var instanceId = entry.InstanceId;
Expand Down Expand Up @@ -91,7 +90,7 @@ public event EntryWrittenEventHandler EntryWritten
remove { eventLog.EntryWritten -= value; }
}

private EventLog eventLog;
private EventLog? eventLog;

public EventLogAsyncReader(string eventLogName, Func<EventLogEntry, int, T?> projection, int pageSize = 20)
{
Expand Down Expand Up @@ -139,7 +138,7 @@ public async Task<PagedSourceItemsPacket<T>> GetItemsAtAsync(int pageoffset, int
private DateTime firstEventTimeWritten;
private readonly PaginationManager<T> paginationManager;

public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

public int NewMatchingEntriesCount { get; private set; }
public int NewEntriesCount { get; private set; }
Expand Down
4 changes: 3 additions & 1 deletion Console/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:BindingConverters="clr-namespace:Wokhan.UI.BindingConverters;assembly=Wokhan.UI"
xmlns:LocalBindingConverters="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.BindingConverters"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:ext="clr-namespace:Wokhan.UI.Xaml.Extensibility;assembly=Wokhan.UI"
StartupUri="UI/Windows/MainWindow.xaml" Startup="Application_Startup">
Expand All @@ -13,12 +14,13 @@
</ResourceDictionary.MergedDictionaries>

<BindingConverters:BooleanNegateConverter x:Key="booleanNegator" />
<BindingConverters:UnitFormatConverter x:Key="unitFormatter" />
<BindingConverters:ObjectToBoolConverter x:Key="objectToBool" />
<BindingConverters:ValueToVisibilityConverter x:Key="valueToVisibility" />
<BindingConverters:ValueToVisibilityNegateConverter x:Key="valueToVisibilityNegate" />
<BindingConverters:ValueChecker x:Key="valueChecker" />

<LocalBindingConverters:UnitFormatConverter x:Key="unitFormatter" />

<system:Double x:Key="ConsoleSizeHeight">600</system:Double>
<system:Double x:Key="ConsoleSizeWidth">900</system:Double>

Expand Down
2 changes: 1 addition & 1 deletion Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>-->
<PackageReference Include="Microsoft.Maps.MapControl.WPF" Version="1.0.0.3" />
<PackageReference Include="Wokhan.Core" Version="0.9.6-beta" />
<PackageReference Include="Wokhan.Core" Version="0.9.8-beta" />
<PackageReference Include="Wokhan.UI" Version="0.9.8-beta" />
</ItemGroup>
<ItemGroup>
Expand Down
30 changes: 30 additions & 0 deletions Console/UI/BindingConverters/UnitFormatConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

using Wokhan.Core;

namespace Wokhan.WindowsFirewallNotifier.Console.UI.BindingConverters;

// Should be moved to Wokhan.UI (it already exists there but with a wrong Wokhan.Core dependency)
public sealed class UnitFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
return UnitFormatter.FormatValue(System.Convert.ToDouble(value), parameter as string);
}
catch
{
return "#ERR";
}
}


public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
14 changes: 3 additions & 11 deletions Console/UI/Controls/BandwidthGraph.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Windows.Media;

using Wokhan.Collections;
using Wokhan.Core;
using Wokhan.WindowsFirewallNotifier.Console.ViewModels;

namespace Wokhan.WindowsFirewallNotifier.Console.UI.Controls;
Expand Down Expand Up @@ -64,15 +65,6 @@ private void InitMiniGraph()
};
}

private static string[] units = new[] { "", "K", "M", "G", "T" };
//TODO: move in Wokhan.Core library to replace the actual FormatBytes implementation (based on a loop). Do a perf test first and try with 1024 as a base (for KiB, MiB, ...).
private static string FormatBytes(double size, string? suffix = null)
{
var num = Math.Min(units.Length - 1, (int)Math.Log(size, 1000));
size = (int)(size / Math.Pow(1000, num) * 100) / 100;
return $"{size:#0.##}{units[num]}{suffix}";
}

private void InitAxes()
{
var skAxisPaint = new SolidColorPaint(((SolidColorBrush)Application.Current.Resources[SystemColors.WindowTextBrushKey]).Color.ToSKColor());
Expand All @@ -90,7 +82,7 @@ private void InitAxes()
yAxis.MinLimit = 0;
yAxis.TextSize = 10;
yAxis.MinStep = 1;
yAxis.Labeler = (value) => value == 0 ? "0Bps" : FormatBytes(Math.Pow(10, value), "Bps");
yAxis.Labeler = (value) => value == 0 ? "0Bps" : UnitFormatter.FormatValue(Math.Pow(10, value), "Bps");
yAxis.LabelsPaint = skAxisPaint;
yAxis.CrosshairPaint = crosshairPaint;

Expand All @@ -99,7 +91,7 @@ private void InitAxes()
miniYAxis.TextSize = 10;
miniYAxis.Padding = new LiveChartsCore.Drawing.Padding(0);
miniYAxis.ShowSeparatorLines = false;
miniYAxis.Labeler = (value) => value == 0 ? "0Bps" : FormatBytes(Math.Pow(10, value), "Bps");
miniYAxis.Labeler = (value) => value == 0 ? "0Bps" : UnitFormatter.FormatValue(Math.Pow(10, value), "Bps");

miniChart.XAxes.First().IsVisible = false;
}
Expand Down
25 changes: 22 additions & 3 deletions Console/UI/Pages/Connections.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,26 @@
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="viewmodels:Connection">
<StackPanel Orientation="Horizontal">
<flags:CountryFlag CountryCode="{Binding Coordinates.CountryISOCode,Mode=OneWay,TargetNullValue=''}" Height="12" MaxWidth="40" Margin="0" Padding="0" />
<!-- Next is an ugly way to only show a flag when the country's ISO code is actually set (to avoid a useless exception) -->
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Coordinates,Mode=OneWay,Converter={StaticResource objectToBool}}" Value="True" />
<Condition Binding="{Binding Coordinates.CountryISOCode,Mode=OneWay,Converter={StaticResource objectToBool}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Content">
<Setter.Value>
<flags:CountryFlag CountryCode="{Binding Coordinates.CountryISOCode,Mode=OneWay}" Height="12" MaxWidth="40" Margin="0" Padding="0" />
</Setter.Value>
</Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Grid.Column="1" Text="{Binding TargetIP, Mode=OneTime}" />
</StackPanel>
</DataTemplate>
Expand All @@ -190,7 +209,7 @@
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Text="{Binding OutboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='ps'}" />
<TextBlock Text="{Binding OutboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='bps'}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Expand All @@ -211,7 +230,7 @@
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Text="{Binding InboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='ps'}" />
<TextBlock Text="{Binding InboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='bps'}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Expand Down
1 change: 1 addition & 0 deletions Console/ViewModels/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Media;

Expand Down

0 comments on commit 52de1c5

Please sign in to comment.