Skip to content

Commit

Permalink
Add API examples project and fix README
Browse files Browse the repository at this point in the history
  • Loading branch information
Rans4ckeR committed Jan 1, 2025
1 parent 0fdd6ae commit f8d3661
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ A Windows .NET WPF application for x64 and ARM64.
A NuGet package to manage FritzBox devices using pure WCF calls.

* [NuGet](https://www.nuget.org/packages/RS.Fritz.Manager.API)
* [GitHub](https://github.com/Rans4ckeR/RS.Fritz.Manager/packages/1302526)

### Usage Examples

Expand All @@ -45,16 +44,19 @@ using IServiceScope serviceScope = host.Services.CreateScope();

// Search for routers and take the first one
IDeviceSearchService deviceSearchService = serviceScope.ServiceProvider.GetRequiredService<IDeviceSearchService>();
InternetGatewayDevice device = (await deviceSearchService.GetDevicesAsync()).First();
GroupedInternetGatewayDevice groupedInternetGatewayDevice = (await deviceSearchService.GetInternetGatewayDevicesAsync()).First();

// Select the router's internal AVM (FritzBox) device, as opposed to a generic UPnP device
InternetGatewayDevice device = groupedInternetGatewayDevice.Devices.First(q => q.IsAvm);

// Show the device model from UPnP data
Console.WriteLine($"Device model: {device.UPnPDescription.Device.ModelDescription}");
Console.WriteLine($"Device model: {device.UPnPDescription?.Device?.ModelDescription}");

// Initialize the device for TR-064, retrieves the security port and the users
await device.InitializeAsync();

// Provide the password for the last logged on user
string lastUsedUserName = device.Users.Single(q => q.LastUser).Name;
string lastUsedUserName = device.Users!.OrderBy(q => q.LastUser).First().Name;
Console.WriteLine($"Enter password for {lastUsedUserName}:");
device.NetworkCredential = new NetworkCredential(lastUsedUserName, Console.ReadLine());

Expand Down Expand Up @@ -93,14 +95,14 @@ Console.WriteLine($"Session: {webUiSessionInfo.Sid}");
ICaptureControlService captureControlService = serviceScope.ServiceProvider.GetRequiredService<ICaptureControlService>();
IEnumerable<CaptureInterfaceGroup>? interfaceGroups = await captureControlService.GetInterfacesAsync(device);
CaptureInterface captureInterface = interfaceGroups.First().CaptureInterfaces.First();
var fileInfo = new FileInfo(FormattableString.Invariant($"c:\\temp\\{captureInterface.Name}_{DateTime.Now.ToString("s").Replace(":", string.Empty)}.eth"));
var fileInfo = new FileInfo(FormattableString.Invariant($@"c:\temp\{captureInterface.Name}_{DateTime.Now.ToString("s").Replace(":", string.Empty)}.eth"));

Task.Run(() => StopCaptureAsync(device, captureInterface, TimeSpan.FromSeconds(10), captureControlService));

await captureControlService.StartCaptureAsync(device, fileInfo, captureInterface);
Console.WriteLine($"Network trace written to file: {fileInfo}");

await host.RunAsync();
return;

static async Task StopCaptureAsync(InternetGatewayDevice device, CaptureInterface captureInterface, TimeSpan timeSpan, ICaptureControlService captureControlService)
{
Expand Down
82 changes: 82 additions & 0 deletions RS.Fritz.Manager.API.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma warning disable CA1506 // Avoid excessive class coupling
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RS.Fritz.Manager.API;

// Register the Fritz services in the dependency container using AddFritzApi()
using IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) => services.AddFritzApi())
.Build();

using IServiceScope serviceScope = host.Services.CreateScope();

// Search for routers and take the first one
IDeviceSearchService deviceSearchService = serviceScope.ServiceProvider.GetRequiredService<IDeviceSearchService>();
GroupedInternetGatewayDevice groupedInternetGatewayDevice = (await deviceSearchService.GetInternetGatewayDevicesAsync()).First();

// Select the router's internal AVM (FritzBox) device, as opposed to a generic UPnP device
InternetGatewayDevice device = groupedInternetGatewayDevice.Devices.First(q => q.IsAvm);

// Show the device model from UPnP data
Console.WriteLine($"Device model: {device.UPnPDescription?.Device?.ModelDescription}");

// Initialize the device for TR-064, retrieves the security port and the users
await device.InitializeAsync();

// Provide the password for the last logged on user
string lastUsedUserName = device.Users!.OrderBy(q => q.LastUser).First().Name;
Console.WriteLine($"Enter password for {lastUsedUserName}:");
device.NetworkCredential = new NetworkCredential(lastUsedUserName, Console.ReadLine());

// TR-064 example; show the device uptime from the TR-064 DeviceInfo service
DeviceInfoGetInfoResponse deviceInfo = await device.DeviceInfoGetInfoAsync();
Console.WriteLine($"Device uptime: {TimeSpan.FromSeconds(deviceInfo.Uptime)}");

// Special services

// Retrieving the device users manually
IUsersService usersService = serviceScope.ServiceProvider.GetRequiredService<IUsersService>();
IEnumerable<User> users = await usersService.GetUsersAsync(device);
users.ToList().ForEach(q => Console.WriteLine($"User: {q.Name}"));

// Retrieving a list of device hosts in the network
IDeviceHostsService deviceHostsService = serviceScope.ServiceProvider.GetRequiredService<IDeviceHostsService>();
DeviceHostInfo deviceHostInfo = await deviceHostsService.GetDeviceHostsAsync(device);
deviceHostInfo.DeviceHosts.ToList().ForEach(q => Console.WriteLine($"Device host: {q.HostName}"));

// Retrieving a list of mesh hosts in the network
IDeviceMeshService deviceMeshService = serviceScope.ServiceProvider.GetRequiredService<IDeviceMeshService>();
DeviceMeshInfo deviceMeshInfo = await deviceMeshService.GetDeviceMeshAsync(device);
deviceMeshInfo.DeviceMesh.Nodes.ToList().ForEach(q => Console.WriteLine($"Mesh host: {q.DeviceName}"));

// Retrieving a list of WLAN devices in the network
IWlanDeviceService wlanDeviceService = serviceScope.ServiceProvider.GetRequiredService<IWlanDeviceService>();
WlanDeviceInfo wlanDeviceInfo = await wlanDeviceService.GetWlanDevicesAsync(device);
wlanDeviceInfo.WlanDeviceList.Items.ToList().ForEach(q => Console.WriteLine($"WLAN device: {q.AssociatedDeviceIpAddress}"));

// Retrieve a new session for use in the WebUI
IWebUiService webUiService = serviceScope.ServiceProvider.GetRequiredService<IWebUiService>();
WebUiSessionInfo webUiSessionInfo = await webUiService.LogonAsync(device);
Console.WriteLine($"Session: {webUiSessionInfo.Sid}");

// Capture live network traffic from router to file
ICaptureControlService captureControlService = serviceScope.ServiceProvider.GetRequiredService<ICaptureControlService>();
IEnumerable<CaptureInterfaceGroup>? interfaceGroups = await captureControlService.GetInterfacesAsync(device);
CaptureInterface captureInterface = interfaceGroups.First().CaptureInterfaces.First();
var fileInfo = new FileInfo(FormattableString.Invariant($@"c:\temp\{captureInterface.Name}_{DateTime.Now.ToString("s").Replace(":", string.Empty)}.eth"));

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(() => StopCaptureAsync(device, captureInterface, TimeSpan.FromSeconds(10), captureControlService));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

await captureControlService.StartCaptureAsync(device, fileInfo, captureInterface);
Console.WriteLine($"Network trace written to file: {fileInfo}");

return;

static async Task StopCaptureAsync(InternetGatewayDevice device, CaptureInterface captureInterface, TimeSpan timeSpan, ICaptureControlService captureControlService)
{
await Task.Delay(timeSpan);
await captureControlService.StopCaptureAsync(device, captureInterface);
}
14 changes: 14 additions & 0 deletions RS.Fritz.Manager.API.Examples/RS.Fritz.Manager.API.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RS.Fritz.Manager.API\RS.Fritz.Manager.API.csproj" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions RS.Fritz.Manager.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<File Path=".github/workflows/dotnet.yml" />
<File Path="README.md" />
</Folder>
<Project Path="RS.Fritz.Manager.API.Examples/RS.Fritz.Manager.API.Examples.csproj" />
<Project Path="RS.Fritz.Manager.API/RS.Fritz.Manager.API.csproj" />
<Project Path="RS.Fritz.Manager.UI/RS.Fritz.Manager.UI.csproj">
<Platform Solution="*|Any CPU" Project="x64" />
Expand Down

0 comments on commit f8d3661

Please sign in to comment.