88using System . Threading ;
99using System . Threading . Tasks ;
1010using System . Windows ;
11+ using System . Windows . Controls ;
1112using System . Windows . Forms ;
1213using U2FLib . Storage ;
1314using Microsoft . EntityFrameworkCore ;
1415using Microsoft . Extensions . DependencyInjection ;
1516using Microsoft . Extensions . Logging ;
17+ using Microsoft . Win32 ;
1618using U2FLib ;
1719using Application = System . Windows . Forms . Application ;
20+ using ContextMenu = System . Windows . Forms . ContextMenu ;
21+ using MenuItem = System . Windows . Forms . MenuItem ;
1822using MessageBox = System . Windows . MessageBox ;
1923
2024namespace SoftU2FDaemon
@@ -30,8 +34,9 @@ class App : Form, INotifySender
3034
3135 #region App settings
3236
37+ private static readonly string BinName = typeof ( App ) . Assembly . GetName ( ) . Name ;
3338 private static readonly string BinFolder = Path . Combine (
34- Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , typeof ( App ) . Assembly . GetName ( ) . Name ) ;
39+ Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , BinName ) ;
3540 private static readonly string DBPath = Path . Combine (
3641 BinFolder , "db.sqlite" ) ;
3742
@@ -106,30 +111,15 @@ private void ConfigureServices(IServiceCollection service)
106111 private void InitializeTrayIcon ( )
107112 {
108113 _trayMenu = new ContextMenu ( ) ;
109- _trayMenu . MenuItems . Add ( "Exit" , ( ( sender , args ) =>
110- {
111- Application . Exit ( ) ;
112- } ) ) ;
113114
114- _trayMenu . MenuItems . Add ( "Reset" , ( sender , args ) =>
115- {
116- var confirm = MessageBox . Show ( "Do you want to reset SoftU2F? this will delete all your local data." ,
117- "Reset Database" , MessageBoxButton . YesNo ) ;
118- if ( confirm != MessageBoxResult . Yes )
119- {
120- MessageBox . Show ( "Reset cancelled" ) ;
121- return ;
122- }
123-
124- if ( File . Exists ( DBPath ) )
125- {
126- var bak = $ "{ DBPath } .bak";
127- if ( File . Exists ( bak ) ) File . Delete ( bak ) ;
128- File . Move ( DBPath , bak ) ;
129- Restart ( ) ;
130- }
131- } ) ;
115+ var item = new MenuItem ( "Auto Start" ) ;
116+ item . Checked = AutoStart ( ) ;
117+ item . Click += OnAutoStartClick ;
118+ _trayMenu . MenuItems . Add ( item ) ;
132119
120+ _trayMenu . MenuItems . Add ( "Reset" , OnResetClickedOnClick ) ;
121+ _trayMenu . MenuItems . Add ( "-" ) ;
122+ _trayMenu . MenuItems . Add ( "Exit" , ( sender , args ) => Application . Exit ( ) ) ;
133123
134124 components = new Container ( ) ;
135125
@@ -145,6 +135,55 @@ private void InitializeTrayIcon()
145135 _trayIcon . BalloonTipShown += ( sender , args ) => _notificationOpen = true ;
146136 _trayIcon . BalloonTipClosed += ( sender , args ) => _notificationOpen = false ;
147137 }
138+
139+ private void OnAutoStartClick ( object sender , EventArgs e )
140+ {
141+ if ( AutoStart ( ) )
142+ {
143+ using ( RegistryKey key = Registry . CurrentUser . OpenSubKey ( "SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , true ) )
144+ {
145+ key ? . DeleteValue ( BinName , false ) ;
146+ }
147+ }
148+ else
149+ {
150+ using ( RegistryKey key = Registry . CurrentUser . OpenSubKey ( "SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , true ) )
151+ {
152+ key ? . SetValue ( BinName , "\" " + Application . ExecutablePath + "\" " ) ;
153+ }
154+ }
155+
156+ var item = ( MenuItem ) sender ;
157+ item . Checked = ! item . Checked ;
158+ }
159+
160+ private bool AutoStart ( )
161+ {
162+ using ( var key = Registry . CurrentUser . OpenSubKey ( "SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , true ) )
163+ {
164+ return key != null && key . GetValueNames ( ) . Any ( v => v == BinName ) ;
165+ }
166+ }
167+
168+ private void OnResetClickedOnClick ( object sender , EventArgs args )
169+ {
170+ var confirm = MessageBox . Show ( "Do you want to reset SoftU2F? this will delete all your local data." ,
171+ "Reset Database" , MessageBoxButton . YesNo ) ;
172+ if ( confirm != MessageBoxResult . Yes )
173+ {
174+ MessageBox . Show ( "Reset cancelled" ) ;
175+ return ;
176+ }
177+
178+ if ( File . Exists ( DBPath ) )
179+ {
180+ var bak = $ "{ DBPath } .bak";
181+ if ( File . Exists ( bak ) ) File . Delete ( bak ) ;
182+ File . Move ( DBPath , bak ) ;
183+ Restart ( ) ;
184+ }
185+ }
186+
148187 protected override void OnLoad ( EventArgs e )
149188 {
150189 Visible = false ;
@@ -171,6 +210,7 @@ protected override void Dispose(bool disposing)
171210 private Action < bool > _userPresenceCallback ;
172211 private readonly object _userPresenceCallbackLock = new object ( ) ;
173212 private bool _notificationOpen ;
213+
174214 private Action < bool > UserPresenceCallback
175215 {
176216 set
0 commit comments