11using Hardcodet . Wpf . TaskbarNotification ;
2+ using Microsoft . Win32 ;
23using System ;
34using System . Configuration ;
45using System . Diagnostics ;
@@ -27,21 +28,18 @@ public partial class App : Application
2728 private RelayServer server ;
2829 private TaskCompletionSource < bool > stop ;
2930 private Task < bool > listen ;
30- private string hostName , remoteHost , listenPrefix , urlBase ;
31- private bool remote , stayOpen ;
31+ private string urlBase ;
32+ private string hostName = ConfigurationManager . AppSettings [ "hostname" ] ?? Environment . MachineName ;
33+ private string remoteHost = ConfigurationManager . AppSettings [ "remoteHost" ] ;
34+ private string listenPrefix = ConfigurationManager . AppSettings [ "listenPrefix" ] ?? "http://*:80/" ;
35+ private bool remote , stayOpen = bool . Parse ( ConfigurationManager . AppSettings [ "stayOpen" ] ?? "true" ) ;
3236 private Icon appIcon = Icon . ExtractAssociatedIcon ( Assembly . GetExecutingAssembly ( ) . Location ) ;
3337
3438 private void Application_Startup ( object sender , StartupEventArgs e )
3539 {
40+ bool isAdmin = new WindowsPrincipal ( WindowsIdentity . GetCurrent ( ) ) . IsInRole ( WindowsBuiltInRole . Administrator ) ;
3641 try
3742 {
38- stayOpen = bool . Parse ( ConfigurationManager . AppSettings [ "stayOpen" ] ?? "true" ) ;
39- if ( e . Args . Length == 0 && ! stayOpen )
40- {
41- Shutdown ( ) ;
42- return ;
43- }
44-
4543 if ( OneInstance . First ( ) )
4644 OneInstance . OnMessage += AddRelay ;
4745
@@ -52,11 +50,21 @@ private void Application_Startup(object sender, StartupEventArgs e)
5250 return ;
5351 }
5452
55- hostName = ConfigurationManager . AppSettings [ "hostname" ] ?? Environment . MachineName ;
56- remoteHost = ConfigurationManager . AppSettings [ "remoteHost" ] ;
57- listenPrefix = ConfigurationManager . AppSettings [ "listenPrefix" ] ?? "http://*:80/" ;
53+ if ( e . Args . Length == 0 )
54+ {
55+ if ( isAdmin )
56+ InstallRegistry ( ) ;
57+
58+ if ( ! stayOpen )
59+ {
60+ Shutdown ( ) ;
61+ return ;
62+ }
63+ }
64+
5865 urlBase = listenPrefix . Replace ( "*" , hostName ) . Replace ( "+" , hostName ) . Replace ( ":80" , "" ) ;
5966 var enableWebclient = bool . Parse ( ConfigurationManager . AppSettings [ "enableWebClient" ] ?? "true" ) ;
67+ var acceptRelays = bool . Parse ( ConfigurationManager . AppSettings [ "acceptRelays" ] ?? "true" ) ;
6068 var maxConnections = int . Parse ( ConfigurationManager . AppSettings [ "maxConnections" ] ?? "8" ) ;
6169
6270 if ( ! string . IsNullOrEmpty ( remoteHost ) )
@@ -72,7 +80,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
7280 else
7381 {
7482 remote = false ;
75- server = new RelayServer ( ) { EnableBuiltinWebclient = enableWebclient } ;
83+ server = new RelayServer ( ) { EnableBuiltinWebclient = enableWebclient , AcceptSocketConnections = acceptRelays } ;
7684 stop = new TaskCompletionSource < bool > ( ) ;
7785 listen = server . Listen ( listenPrefix , maxConnections , stop ) ;
7886 if ( listen . IsFaulted ) throw listen . Exception . InnerException ;
@@ -83,7 +91,10 @@ private void Application_Startup(object sender, StartupEventArgs e)
8391 urlBase = Regex . Replace ( urlBase , "localhost" , hostName , RegexOptions . IgnoreCase ) ;
8492
8593 var idleStatus = $ "{ ( remote ? "Relaying to" : "Listening on" ) } { urlBase } ";
86- if ( ! remote ) idleStatus += $ "\r \n Webclient is { ( enableWebclient ? "enabled" : "disabled" ) } \r \n Max connections { maxConnections } ";
94+ if ( ! remote ) idleStatus +=
95+ $ "\r \n { ( acceptRelays ? "Accepting" : "Not accepting" ) } relays" +
96+ $ "\r \n Webclient is { ( enableWebclient ? "enabled" : "disabled" ) } " +
97+ $ "\r \n { maxConnections } max connections";
8798
8899 relayStatus = new RelayStatus ( idleStatus ) ;
89100 notifyIcon = ( TaskbarIcon ) FindResource ( "NotifyIcon" ) ;
@@ -97,11 +108,10 @@ private void Application_Startup(object sender, StartupEventArgs e)
97108 notifyIcon . Icon = appIcon ;
98109 ShowRelays ( ) ;
99110 }
100-
101111 }
102112 catch ( System . Net . HttpListenerException ex ) when ( ( uint ) ex . HResult == 0x80004005 ) // access denied
103113 {
104- if ( ! new WindowsPrincipal ( WindowsIdentity . GetCurrent ( ) ) . IsInRole ( WindowsBuiltInRole . Administrator ) )
114+ if ( ! isAdmin )
105115 {
106116 OneInstance . Dispose ( ) ;
107117 Process . Start ( new ProcessStartInfo ( Assembly . GetEntryAssembly ( ) . Location , e . Args . Length > 0 ? e . Args [ 0 ] : "" ) { Verb = "runas" } ) ;
@@ -121,25 +131,35 @@ private void Application_Startup(object sender, StartupEventArgs e)
121131
122132 private async void AddRelay ( string filename )
123133 {
124- IRelay relay ;
125- var file = new FileInfo ( filename ) ;
126- var stream = file . OpenRead ( ) ;
127- string code ;
128- if ( remote )
134+ try
129135 {
130- relay = new SocketRelayClient ( ) ;
131- code = await ( relay as SocketRelayClient ) . AddRelay ( new Uri ( remoteHost ) , stream , file . Name ) ;
136+ IRelay relay ;
137+ var file = new FileInfo ( filename ) ;
138+ var stream = file . OpenRead ( ) ;
139+ string code ;
140+ if ( remote )
141+ {
142+ relay = new SocketRelayClient ( ) ;
143+ code = await ( relay as SocketRelayClient ) . AddRelay ( new Uri ( remoteHost ) , stream , file . Name ) ;
144+ }
145+ else
146+ {
147+ relay = new LocalRelay ( stream , file . Name ) ;
148+ code = server . AddRelay ( relay ) ;
149+ }
150+
151+ relay . OnComplete += ( ) => stream . Close ( ) ;
152+ relay . OnCancel += ( ) => stream . Close ( ) ;
153+ AddRelayStatus ( file . FullName , file . Length , urlBase + code , relay ) ;
154+ Clipboard . SetDataObject ( urlBase + code , true ) ;
132155 }
133- else
156+ catch ( Exception e )
134157 {
135- relay = new LocalRelay ( stream , file . Name ) ;
136- code = server . AddRelay ( relay ) ;
137- }
158+ MessageBox . Show ( e . Message , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
138159
139- relay . OnComplete += ( ) => stream . Close ( ) ;
140- relay . OnCancel += ( ) => stream . Close ( ) ;
141- AddRelayStatus ( file . FullName , file . Length , urlBase + code , relay ) ;
142- Clipboard . SetDataObject ( urlBase + code , true ) ;
160+ if ( relayStatus . Relays . Count == 1 && ! stayOpen )
161+ Shutdown ( ) ;
162+ }
143163 }
144164
145165 private void AddRelayStatus ( string filename , long ? filesize , string url , IRelay relay )
@@ -247,5 +267,19 @@ private static Icon ProgressIcon(double percentage)
247267 }
248268 }
249269 }
270+
271+ private void InstallRegistry ( )
272+ {
273+ var appPath = $ "\" { Assembly . GetExecutingAssembly ( ) . Location } \" ";
274+ var root = $@ "HKEY_CLASSES_ROOT\*\shell\{ nameof ( WebRelay ) } ";
275+ var cmd = $@ "{ root } \command";
276+
277+ if ( ( string ) Registry . GetValue ( cmd , "" , null ) != appPath )
278+ {
279+ Registry . SetValue ( root , "" , "Copy download link" ) ;
280+ Registry . SetValue ( root , "Icon" , appPath ) ;
281+ Registry . SetValue ( $@ "{ root } \command", "" , $ "{ appPath } \" %1\" ") ;
282+ }
283+ }
250284 }
251285}
0 commit comments