Skip to content

Commit

Permalink
Merge pull request #8 from SoftU2F/net5
Browse files Browse the repository at this point in the history
Port to Net 5.0
  • Loading branch information
ibigbug authored Mar 27, 2021
2 parents bf98f50 + ccee2a2 commit bbd79e7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 97 deletions.
2 changes: 1 addition & 1 deletion JustTest/JustTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
162 changes: 75 additions & 87 deletions SoftU2FDaemon/Program.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using U2FLib.Storage;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using U2FLib;
using Application = System.Windows.Forms.Application;
using ContextMenu = System.Windows.Forms.ContextMenu;
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox;
using ContextMenu = System.Windows.Forms.ContextMenuStrip;
using MenuItem = System.Windows.Forms.ToolStripMenuItem;

namespace SoftU2FDaemon
{
class App : Form, INotifySender
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using U2FLib;
using U2FLib.Storage;

internal class App : Form, INotifySender
{
private NotifyIcon _trayIcon;
private ContextMenu _trayMenu;
private CancellationTokenSource _cancellation;

private IServiceProvider _serviceProvider;
private NotifyIcon _trayIcon;
private ContextMenu _trayMenu;

#region App settings

private static readonly string BinName = typeof(App).Assembly.GetName().Name;
private static readonly string BinFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BinName);
private static readonly string DBPath = Path.Combine(
BinFolder, "db.sqlite");

#endregion
public App()
{
SetupApplication();
InitializeTrayIcon();
InitializeBackgroundDaemon();
}

[STAThread]
public static void Main()
Expand All @@ -50,23 +39,29 @@ public static void Main()
Application.Run(new App());
}

public App()
{
SetupApplication();
InitializeTrayIcon();
InitializeBackgroundDaemon();
}
#region App settings

private static readonly string BinName = typeof(App).Assembly.GetName().Name;

private static readonly string BinFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), BinName);

private static readonly string DBPath = Path.Combine(
BinFolder, "db.sqlite");

#endregion

#region Application LifeCycle

IntPtr lastActiveWin = IntPtr.Zero;
private IntPtr lastActiveWin = IntPtr.Zero;

[DllImport("user32.dll", ExactSpelling = true)]
static extern IntPtr GetForegroundWindow();
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
private static extern bool SetForegroundWindow(IntPtr hWnd);

#endregion

#region Application Initialization
Expand Down Expand Up @@ -111,7 +106,8 @@ private void InitializeBackgroundDaemon()
Environment.Exit(1);
return;
}
(new Thread(() => { daemon.StartIoLoop(_cancellation.Token); })).Start();

new Thread(() => { daemon.StartIoLoop(_cancellation.Token); }).Start();
UserPresence.Sender = this;
}

Expand All @@ -131,21 +127,20 @@ private void InitializeTrayIcon()
{
_trayMenu = new ContextMenu();

var item = new MenuItem("Auto Start");
item.Checked = AutoStart();
var item = new MenuItem {Text = @"Auto Start", Checked = AutoStart()};
item.Click += OnAutoStartClick;
_trayMenu.MenuItems.Add(item);
_trayMenu.Items.Add(item);

_trayMenu.MenuItems.Add("Reset", OnResetClickedOnClick);
_trayMenu.MenuItems.Add("-");
_trayMenu.MenuItems.Add("Exit", (sender, args) => Application.Exit());
_trayMenu.Items.Add("Reset", null, OnResetClickedOnClick);
_trayMenu.Items.Add("-");
_trayMenu.Items.Add("Exit", null, (sender, args) => Application.Exit());

_trayIcon = new NotifyIcon
{
Text = "SoftU2F Daemon",
ContextMenu = _trayMenu,
Text = @"SoftU2F Daemon",
ContextMenuStrip = _trayMenu,
Icon = new Icon("tray.ico"),
Visible = true,
Visible = true
};

_trayIcon.BalloonTipClicked += (sender, args) =>
Expand All @@ -168,48 +163,42 @@ private void OnAutoStartClick(object sender, EventArgs e)
{
if (AutoStart())
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key?.DeleteValue(BinName, false);
}
using var key =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key?.DeleteValue(BinName, false);
}
else
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key?.SetValue(BinName, "\"" + Application.ExecutablePath + "\"");
}
using var key =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key?.SetValue(BinName, "\"" + Application.ExecutablePath + "\"");
}

var item = (MenuItem) sender;
item.Checked = !item.Checked;
}

private bool AutoStart()
private static bool AutoStart()
{
using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
return key != null && key.GetValueNames().Any(v => v == BinName);
}
using var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
return key != null && key.GetValueNames().Any(v => v == BinName);
}

private void OnResetClickedOnClick(object sender, EventArgs args)
{
var confirm = MessageBox.Show("Do you want to reset SoftU2F? this will delete all your local data.",
"Reset Database", MessageBoxButton.YesNo);
if (confirm != MessageBoxResult.Yes)
var confirm = MessageBox.Show(@"Do you want to reset SoftU2F? this will delete all your local data.",
@"Reset Database", MessageBoxButtons.YesNo);
if (confirm != DialogResult.Yes)
{
MessageBox.Show("Reset cancelled");
return;
}

if (File.Exists(DBPath))
{
var bak = $"{DBPath}.bak";
if (File.Exists(bak)) File.Delete(bak);
File.Move(DBPath, bak);
Restart();
}
if (!File.Exists(DBPath)) return;
var bak = $"{DBPath}.bak";
if (File.Exists(bak)) File.Delete(bak);
File.Move(DBPath, bak);
Restart();
}

protected override void OnLoad(EventArgs e)
Expand All @@ -223,21 +212,18 @@ protected override void OnLoad(EventArgs e)

#region IDisposable

public void Dispose()
public new void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private bool disposed = false;
private readonly bool disposed = false;

protected override void Dispose(bool disposing)
{
if (disposed) return;
if (disposing)
{

_cancellation.Cancel();
}
if (disposing) _cancellation.Cancel();
base.Dispose(disposing);
}

Expand All @@ -251,7 +237,7 @@ protected override void Dispose(bool disposing)
#region UserPresence

private Action<bool> _userPresenceCallback;
private readonly object _userPresenceCallbackLock = new object();
private readonly object _userPresenceCallbackLock = new();
private bool _notificationOpen;

private Action<bool> UserPresenceCallback
Expand All @@ -265,13 +251,15 @@ private Action<bool> UserPresenceCallback
}
}
}

public void Send(string title, string message, Action<bool> userClicked)
{
if (_notificationOpen) return;
_trayIcon.ShowBalloonTip((int)TimeSpan.FromSeconds(10).TotalMilliseconds, title, message, ToolTipIcon.Info);
_trayIcon.ShowBalloonTip((int) TimeSpan.FromSeconds(10).TotalMilliseconds, title, message,
ToolTipIcon.Info);
UserPresenceCallback = userClicked;
}

#endregion
}
}
}
12 changes: 8 additions & 4 deletions SoftU2FDaemon/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishDir>bin\Debug\netcoreapp3.0\publish\</PublishDir>
<SelfContained>false</SelfContained>
<Platform>x64</Platform>
<TargetFramework>net5.0-windows</TargetFramework>
<PublishDir>bin\Release\net5.0\publish\</PublishDir>
<SelfContained>true</SelfContained>
<_IsPortable>true</_IsPortable>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>True</PublishSingleFile>
<PublishReadyToRun>True</PublishReadyToRun>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
</Project>
9 changes: 4 additions & 5 deletions SoftU2FDaemon/SoftU2FDaemon.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>Resources\tray.ico</ApplicationIcon>
<StartupObject />
<StartupObject>SoftU2FDaemon.App</StartupObject>
<Authors>ibigbug</Authors>
<Company>Watfaq</Company>
<Product>SoftU2F</Product>
<Description>Software U2F authenticator for Windows 10
</Description>
<Description>Software U2F authenticator for Windows 10</Description>
<PackageProjectUrl>https://github.com/ibigbug/softu2f-win</PackageProjectUrl>
<RepositoryUrl>https://github.com/ibigbug/softu2f-win</RepositoryUrl>
<PackageTags>u2f,security</PackageTags>
Expand Down

0 comments on commit bbd79e7

Please sign in to comment.