Skip to content

Commit

Permalink
created scan factory and view
Browse files Browse the repository at this point in the history
closes #33
closes #45
closes #44
  • Loading branch information
ewilliams0305 committed Apr 13, 2024
1 parent 55a00b5 commit 2c2d42a
Show file tree
Hide file tree
Showing 17 changed files with 534 additions and 129 deletions.
3 changes: 2 additions & 1 deletion source/Kangaroo.UI/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</Application.Styles>

<Application.Resources>
<controls:ScanModeTextConverter x:Key="ScanModeTextConverter"></controls:ScanModeTextConverter>
<controls:ScanModeTextConverter x:Key="ScanModeTextConverter"/>
<controls:NetworkAdapterTextConverter x:Key="NetworkAdapterTextConverter"/>
</Application.Resources>
</Application>
22 changes: 18 additions & 4 deletions source/Kangaroo.UI/Controls/ScanConfiguratorView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,35 @@
<TextBox Width="300" Margin="4" Text="{Binding EndAddress}" Watermark="END IP ADDRESS (192.168.1.254)"/>
</StackPanel>

<StackPanel IsVisible="{Binding ShowSingleFields}">
<Label FontStyle="Italic" FontSize="10" Background="#2d2d2d">ENTER IP ADDRESS:</Label >
<TextBox Width="300" Margin="4" Text="{Binding IpAddress}" Watermark="END IP ADDRESS (192.168.1.254)"/>
</StackPanel>


<StackPanel IsVisible="{Binding ShowSubnetFields}">
<Label FontStyle="Italic" FontSize="10">ENTER IP ADDRESS:</Label >
<TextBox Width="300" Margin="4" Text="{Binding IpAddress}" Watermark="IP ADDRESS (192.168.1.1)"/>
</StackPanel>

<StackPanel IsVisible="{Binding ShowSubnetFields}">
<Label FontStyle="Italic" FontSize="10" Background="#2d2d2d">ENTER SUBNET MASK:</Label >
<TextBox Width="300" Margin="4" Text="{Binding NetmaskAddress}" Watermark="SUBNET MASK (255.255.255.0)"/>
</StackPanel>

<StackPanel IsVisible="{Binding ShowAdapterFields}">
<Label FontStyle="Italic" FontSize="10" Background="#2d2d2d">SELECT NETWORK ADAPTER:</Label >
<ComboBox ItemsSource="{Binding Adapters}"
SelectedItem="{Binding Adapter}"
Width="200"
HorizontalAlignment="Stretch"
Margin="4">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Converter={StaticResource NetworkAdapterTextConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>

<TextBlock IsVisible="{Binding ShowAdapterFields}" Text="{Binding SelectedMode, Converter={StaticResource ScanModeTextConverter}}"/>
</StackPanel>

</UserControl>
1 change: 1 addition & 0 deletions source/Kangaroo.UI/Controls/ScanConfiguratorView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Avalonia.Controls;
using Microsoft.Extensions.DependencyInjection;

Expand Down
170 changes: 156 additions & 14 deletions source/Kangaroo.UI/Controls/ScanConfiguratorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
using Kangaroo.UI.Models;
using Kangaroo.UI.ViewModels;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace Kangaroo.UI.Controls;
public partial class ScanConfiguratorViewModel : ViewModelBase
{
private readonly IScannerFactory _factory;

public ScanConfiguratorViewModel(IScannerFactory factory)
{
_factory = factory;
}

[ObservableProperty]
private ScanMode _selectedMode = ScanMode.AddressRange;

Expand All @@ -16,48 +26,180 @@ public partial class ScanConfiguratorViewModel : ViewModelBase
ScanMode.AddressRange,
ScanMode.SingleAddress,
ScanMode.NetworkSubnet,
ScanMode.SpecifiedAddresses,
ScanMode.NetworkAdapter,
};

[ObservableProperty]
private ObservableCollection<NetworkAdapter> _adapters = new()
private ObservableCollection<NetworkAdapter> _adapters = new(AddressFactory.GetInterfaces().Select(i => new NetworkAdapter()
{
new NetworkAdapter { IpAddress = "127.0.0.1", MacAddress = "00000000", Name = "Ethernet" },
new NetworkAdapter { IpAddress = "192.168.5.5", MacAddress = "00000000", Name = "Wifi" },
new NetworkAdapter { IpAddress = "127.0.0.1", MacAddress = "00000000", Name = "Ethernet" },
};
IpAddress = i.GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily != AddressFamily.InterNetwork).FirstOrDefault().Address.ToString(),
Name = i.Name,
MacAddress = i.GetPhysicalAddress().ToString()

}));

[ObservableProperty]
private NetworkAdapter _adapter;
private NetworkAdapter? _adapter;

partial void OnAdapterChanged(NetworkAdapter? value)
{
if (SelectedMode != ScanMode.NetworkAdapter)
{
return;
}

if (Adapter != null)
{
_factory.CreateScanner(new ScannerOptions()
{
NetworkInterface = AddressFactory.GetInterfaces().First(i => i.Name == Adapter.Name),
ScanMode = ScanMode.NetworkAdapter,
Timeout = TimeSpan.FromSeconds(1),
Ttl = 10,
WithHttp = true
});
}
}

[ObservableProperty]
private bool _showRangeFields = true;

[ObservableProperty]
private bool _showSubnetFields = true;
private bool _showSingleFields = false;

[ObservableProperty]
private bool _showSubnetFields = false;

[ObservableProperty]
private bool _showAdapterFields = true;
private bool _showAdapterFields = false;

[ObservableProperty]
private string _startAddress;

[ObservableProperty]
private string _endAddress;

partial void OnSelectedModeChanged(ScanMode mode)
partial void OnStartAddressChanged(string value)
{
ShowRangeFields = mode == ScanMode.AddressRange;
ShowSubnetFields = mode == ScanMode.NetworkSubnet;
ShowAdapterFields = mode == ScanMode.NetworkAdapter;
if (SelectedMode != ScanMode.AddressRange)
{
return;
}

if (IPAddress.TryParse(StartAddress, out var start) &&
IPAddress.TryParse(EndAddress, out var end))
{
_factory.CreateScanner(new ScannerOptions()
{
ScanMode = ScanMode.AddressRange,
Timeout = TimeSpan.FromSeconds(1),
Ttl = 10,
WithHttp = true,
StartAddress = start,
EndAddress = end
});
}
}
partial void OnEndAddressChanged(string value)
{
if (SelectedMode != ScanMode.AddressRange)
{
return;
}

public ScanConfiguratorViewModel()
if (IPAddress.TryParse(StartAddress, out var start) &&
IPAddress.TryParse(EndAddress, out var end))
{

_factory.CreateScanner(new ScannerOptions()
{
ScanMode = ScanMode.AddressRange,
Timeout = TimeSpan.FromSeconds(1),
Ttl = 10,
WithHttp = true,
StartAddress = start,
EndAddress = end
});
}
}

[ObservableProperty]
private string _ipAddress;

[ObservableProperty]
private string _netmaskAddress;

partial void OnIpAddressChanged(string value)
{
if (SelectedMode == ScanMode.SingleAddress)
{
if (IPAddress.TryParse(IpAddress, out var singleAddress))
{
_factory.CreateScanner(new ScannerOptions()
{
ScanMode = SelectedMode,
Timeout = TimeSpan.FromSeconds(1),
Ttl = 10,
WithHttp = true,
SpecificAddress = singleAddress,
});
}
}

if (SelectedMode != ScanMode.NetworkSubnet)
{
return;
}

if (IPAddress.TryParse(IpAddress, out var ip) &&
IPAddress.TryParse(NetmaskAddress, out var mask))
{
_factory.CreateScanner(new ScannerOptions()
{
ScanMode = SelectedMode,
Timeout = TimeSpan.FromSeconds(1),
Ttl = 10,
WithHttp = true,
SpecificAddress = ip,
NetmaskAddress = mask
});
}
}
partial void OnNetmaskAddressChanged(string value)
{
if (SelectedMode != ScanMode.NetworkSubnet)
{
return;
}

if (IPAddress.TryParse(IpAddress, out var ip) &&
IPAddress.TryParse(NetmaskAddress, out var mask))
{

_factory.CreateScanner(new ScannerOptions()
{
ScanMode = SelectedMode,
Timeout = TimeSpan.FromSeconds(1),
Ttl = 10,
WithHttp = true,
SpecificAddress = ip,
NetmaskAddress = mask
});
}
}

partial void OnSelectedModeChanged(ScanMode mode)
{
ShowRangeFields = mode == ScanMode.AddressRange;
ShowSubnetFields = mode == ScanMode.NetworkSubnet;
ShowAdapterFields = mode == ScanMode.NetworkAdapter;
ShowSingleFields = mode == ScanMode.SingleAddress;

if (mode == ScanMode.NetworkAdapter)
{
Adapter = Adapters.FirstOrDefault();
}
}

}

Expand Down
21 changes: 21 additions & 0 deletions source/Kangaroo.UI/Controls/ScanModeTextConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ public class ScanModeTextConverter : IValueConverter
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}

public object ConvertBack(object? value, Type targetType,
object? parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}

public class NetworkAdapterTextConverter : IValueConverter
{
public static readonly ScanModeTextConverter Instance = new();

public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is NetworkAdapter apt && targetType.IsAssignableTo(typeof(string)))
{
return $"{apt.Name} | {apt.IpAddress} | {apt.MacAddress}";
}

return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}

public object ConvertBack(object? value, Type targetType,
object? parameter, CultureInfo culture)
{
Expand Down
Loading

0 comments on commit 2c2d42a

Please sign in to comment.