Skip to content

Commit

Permalink
Added Recover WiFi passwords quick action (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Dec 27, 2023
1 parent 5f70763 commit 2be1242
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 1 deletion.
93 changes: 92 additions & 1 deletion InternetTest/InternetTest/Pages/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
FontFamily="../Fonts/#FluentSystemIcons-Filled"
FontSize="16"
Foreground="{Binding Source={StaticResource AccentColor}}"
Text="" />
Text="" />
<TextBlock
Margin="10 0 0 0"
VerticalAlignment="Center"
Expand All @@ -307,6 +307,33 @@
Text="{x:Static lang:Resources.ConnectWiFi}" />
</StackPanel>
</Border>
<Border
x:Name="RecoverWiFi"
Margin="5 0"
Background="{Binding Source={StaticResource LightAccentColor}}"
CornerRadius="15"
Cursor="Hand"
MouseLeftButtonUp="RecoverWiFi_MouseLeftButtonUp">
<StackPanel
Margin="10 5"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock
VerticalAlignment="Center"
FontFamily="../Fonts/#FluentSystemIcons-Filled"
FontSize="16"
Foreground="{Binding Source={StaticResource AccentColor}}"
Text="&#xF5A8;" />
<TextBlock
Margin="10 0 0 0"
VerticalAlignment="Center"
FontFamily="../Fonts/#Hauora"
FontSize="14"
FontWeight="ExtraBold"
Foreground="{Binding Source={StaticResource AccentColor}}"
Text="{x:Static lang:Resources.WifiPasswords}" />
</StackPanel>
</Border>
</WrapPanel>

<StackPanel
Expand Down Expand Up @@ -456,5 +483,69 @@
</Grid>
</Border>
</Popup>
<Popup
x:Name="PasswordPopup"
Width="400"
Height="400"
AllowsTransparency="True"
Placement="Center"
PlacementTarget="{Binding ElementName=Home}"
PopupAnimation="Fade"
StaysOpen="True">
<Border
Margin="10"
Padding="5"
Background="{Binding Source={StaticResource Background1}}"
CornerRadius="5">
<Border.Effect>
<DropShadowEffect
BlurRadius="10"
Opacity="0.4"
RenderingBias="Performance"
ShadowDepth="0"
Color="Black" />
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="../Fonts/#FluentSystemIcons-Filled"
FontSize="24"
Foreground="{Binding Source={StaticResource AccentColor}}"
Text="&#xF5A8;" />
<TextBlock
HorizontalAlignment="Center"
FontSize="14"
FontWeight="Bold"
Text="{x:Static lang:Resources.WifiPasswords}" />
</StackPanel>

<ScrollViewer
Grid.Row="1"
HorizontalScrollBarVisibility="Disabled"
Style="{DynamicResource ScrollViewerStyle}">
<StackPanel x:Name="WiFiItemDisplayer" />
</ScrollViewer>
<Button
x:Name="ClosePasswordBtn"
Grid.Row="2"
Margin="5"
Padding="3"
HorizontalAlignment="Center"
Background="{Binding Source={StaticResource LightAccentColor}}"
Click="ClosePasswordBtn_Click"
Content="{x:Static lang:Resources.Close}"
FontWeight="Bold"
Foreground="{Binding Source={StaticResource AccentColor}}"
Style="{DynamicResource DefaultButton}" />
</Grid>
</Border>
</Popup>
</Grid>
</Page>
70 changes: 70 additions & 0 deletions InternetTest/InternetTest/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ MIT License
using InternetTest.UserControls;
using ManagedNativeWifi;
using PeyrSharp.Core;
using PeyrSharp.Env;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml.Serialization;

namespace InternetTest.Pages;
/// <summary>
Expand Down Expand Up @@ -135,6 +139,7 @@ private async void SpeedTest_MouseLeftButtonUp(object sender, System.Windows.Inp
{
SpeedTestPopup.IsOpen = true;
ConnectPopup.IsOpen = false;
PasswordPopup.IsOpen = false;
try
{
CloseSpeedTestBtn.IsEnabled = false;
Expand Down Expand Up @@ -186,6 +191,7 @@ private async void ConnectWiFi_MouseLeftButtonUp(object sender, System.Windows.I
{
ConnectPopup.IsOpen = true;
SpeedTestPopup.IsOpen = false;
PasswordPopup.IsOpen = false;
WiFiDisplayer.Children.Clear();
await NativeWifi.ScanNetworksAsync(TimeSpan.FromSeconds(10));
var wifis = Global.GetWiFis();
Expand All @@ -199,4 +205,68 @@ private void CloseConnectBtn_Click(object sender, System.Windows.RoutedEventArgs
{
ConnectPopup.IsOpen = false;
}

private async void RecoverWiFi_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
PasswordPopup.IsOpen = true;
SpeedTestPopup.IsOpen = false;
ConnectPopup.IsOpen = false;
await GetWiFiNetworksInfo();
}

private void ClosePasswordBtn_Click(object sender, System.Windows.RoutedEventArgs e)
{
PasswordPopup.IsOpen = false;
}

async Task GetWiFiNetworksInfo()
{

try
{
WiFiItemDisplayer.Children.Clear(); // Clear the panel

// Check if the temp directory exists
string path = FileSys.AppDataPath + @"\Léo Corporation\InternetTest Pro\Temp";

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
};

// Run "netsh wlan export profile key=clear" command
Process process = new();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/c netsh wlan export profile key=clear folder=\"{path}\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
await process.WaitForExitAsync();

// Read the files
string[] files = Directory.GetFiles(path);
for (int i = 0; i < files.Length; i++)
{
XmlSerializer serializer = new(typeof(WLANProfile));
StreamReader streamReader = new(files[i]); // Where the file is going to be read

var test = (WLANProfile?)serializer.Deserialize(streamReader);

if (test != null)
{
WiFiItemDisplayer.Children.Add(new WiFiInfoItem(test));
}
streamReader.Close();

File.Delete(files[i]); // Remove the temp file

}
Directory.Delete(path); // Delete the temp directory
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

0 comments on commit 2be1242

Please sign in to comment.