@@ -127,7 +127,7 @@ private async Task StartServer()
127
127
var url = string . Format ( "http://{0}:{1}" , selectedIP , numPort . Value . ToString ( ) ) ;
128
128
txtURL . Text = url ;
129
129
serv . Prefixes . Clear ( ) ;
130
- serv . Prefixes . Add ( "http://localhost:" + numPort . Value . ToString ( ) + "/" ) ;
130
+ // serv.Prefixes.Add("http://localhost:" + numPort.Value.ToString() + "/");
131
131
//serv.Prefixes.Add("http://*:" + numPort.Value.ToString() + "/"); // Uncomment this to Allow Public IP Over Internet. [Commented for Security Reasons.]
Has a conversation. Original line has a conversation. 132
132
serv . Prefixes . Add ( url + "/" ) ;
133
133
serv . Start ( ) ;
@@ -335,45 +335,73 @@ private async Task AddFirewallRule(int port)
335
335
{
336
336
await Task . Run ( ( ) =>
337
337
{
338
-
339
- string cmd = RunCMD ( "netsh advfirewall firewall show rule \" Screen Task\" " ) ;
340
- if ( ! cmd . Contains ( "Screen Task" ) )
338
+ var rulename = $ "Screen Task On Port { _currentSettings . Port } ";
339
+ var remoteip = _currentSettings . AllowPublicAccess ? "any" : "localsubnet" ;
340
+ string cmd = RunCMD ( $ "netsh advfirewall firewall show rule \" { rulename } \" ") ;
341
+ var splittedResponse = cmd . Split ( new string [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
342
+ if ( cmd . Contains ( rulename ) && cmd . Contains ( _currentSettings . Port . ToString ( ) ) && splittedResponse . Length >= 8 && splittedResponse [ 8 ] . ToLower ( ) . Contains ( remoteip ) )
343
+ {
344
+ // Do Nothing, to prevent ask for admin access everytime without a change in the configurations
345
+ }
346
+ else if ( ! cmd . Contains ( rulename ) && ! cmd . Contains ( _currentSettings . Port . ToString ( ) ) && splittedResponse . Length >= 8 && ! splittedResponse [ 8 ] . ToLower ( ) . Contains ( remoteip ) )
341
347
{
342
- cmd = RunCMD ( "netsh advfirewall firewall add rule name=\" Screen Task\" dir=in action=allow remoteip=localsubnet protocol=tcp localport=" + port ) ;
343
- cmd = RunCMD ( "netsh advfirewall firewall show rule \" Screen Task\" " ) ;
344
- if ( cmd . Contains ( "Screen Task" ) )
348
+ cmd = RunCMD ( $ "netsh advfirewall firewall add rule name=\" { rulename } \" dir=in action=allow remoteip={ remoteip } protocol=tcp localport={ port } "
349
+ + " & " +
350
+ $ "netsh http add urlacl url=http://{ _currentSettings . IP } :{ _currentSettings . Port } / user=Everyone listen=yes"
351
+ , true ) ;
352
+
353
+ cmd = RunCMD ( $ "netsh advfirewall firewall show rule \" { rulename } \" ") ;
354
+ if ( cmd . Contains ( rulename ) )
345
355
{
346
356
Log ( "Screen Task Rule added to your firewall" ) ;
347
357
}
348
358
}
349
359
else
350
360
{
351
- cmd = RunCMD ( "netsh advfirewall firewall delete rule name=\" Screen Task\" " ) ;
352
- cmd = RunCMD ( "netsh advfirewall firewall add rule name=\" Screen Task\" dir=in action=allow remoteip=localsubnet protocol=tcp localport=" + port ) ;
353
- cmd = RunCMD ( "netsh advfirewall firewall show rule \" Screen Task\" " ) ;
354
- if ( cmd . Contains ( "Screen Task" ) )
361
+ cmd = RunCMD ( $ "netsh advfirewall firewall delete rule name=\" { rulename } \" "
362
+ + " & " +
363
+ $ "netsh http delete urlacl url=http://{ _currentSettings . IP } :{ _currentSettings . Port } /"
364
+ + " & " +
365
+ $ "netsh advfirewall firewall add rule name=\" { rulename } \" dir=in action=allow remoteip={ remoteip } protocol=tcp localport={ port } "
366
+ + " & " +
367
+ $ "netsh http add urlacl url=http://{ _currentSettings . IP } :{ _currentSettings . Port } / user=Everyone listen=yes"
368
+ , true ) ;
369
+
370
+ cmd = RunCMD ( $ "netsh advfirewall firewall show rule \" { rulename } \" ") ;
371
+ if ( cmd . Contains ( rulename ) )
355
372
{
356
373
Log ( "Screen Task Rule updated to your firewall" ) ;
357
374
}
358
375
}
359
376
} ) ;
360
377
361
378
}
362
- private string RunCMD ( string cmd )
379
+ private string RunCMD ( string cmd , bool requireAdmin = false )
363
380
{
364
381
Process proc = new Process ( ) ;
365
382
proc . StartInfo . FileName = "cmd.exe" ;
366
- proc . StartInfo . Arguments = "/C " + cmd ;
383
+ proc . StartInfo . Arguments = "/C " + cmd ;
367
384
proc . StartInfo . CreateNoWindow = true ;
368
- proc . StartInfo . UseShellExecute = false ;
369
- proc . StartInfo . RedirectStandardOutput = true ;
370
- proc . StartInfo . RedirectStandardError = true ;
371
- proc . Start ( ) ;
372
- string res = proc . StandardOutput . ReadToEnd ( ) ;
373
- proc . StandardOutput . Close ( ) ;
374
-
375
- proc . Close ( ) ;
376
- return res ;
385
+ if ( requireAdmin )
386
+ {
387
+ proc . StartInfo . UseShellExecute = true ;
388
+ proc . StartInfo . Verb = "runas" ;
389
+ proc . Start ( ) ;
390
+ return null ;
391
+ }
392
+ else
393
+ {
394
+ proc . StartInfo . UseShellExecute = false ;
395
+ proc . StartInfo . RedirectStandardOutput = true ;
396
+ proc . StartInfo . RedirectStandardError = true ;
397
+ proc . Start ( ) ;
398
+
399
+ string res = proc . StandardOutput . ReadToEnd ( ) ;
400
+ proc . StandardOutput . Close ( ) ;
401
+ proc . Close ( ) ;
402
+ return res ;
403
+ }
404
+
377
405
}
378
406
private void Log ( string text )
379
407
{
@@ -448,6 +476,7 @@ private void frmMain_Load(object sender, EventArgs e)
448
476
this . numPort . Value = _currentSettings . Port ;
449
477
this . numShotEvery . Value = _currentSettings . ScreenshotsSpeed ;
450
478
this . qualitySlider . Value = _currentSettings . ImageQuality != default ? _currentSettings . ImageQuality : 75 ;
479
+ this . cbAllowPublicAccess . Checked = _currentSettings . AllowPublicAccess ;
451
480
this . comboIPs . SelectedIndex = _ips . IndexOf ( _ips . FirstOrDefault ( ip => ip . Item2 . Contains ( _currentSettings . IP ) ) ) ;
452
481
if ( _currentSettings . SelectedScreenIndex > - 1 && comboScreens . Items . Count > 0 && _currentSettings . SelectedScreenIndex <= comboScreens . Items . Count - 1 )
453
482
this . comboScreens . SelectedIndex = _currentSettings . SelectedScreenIndex ;
@@ -519,6 +548,7 @@ private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
519
548
_currentSettings . IP = _ips . ElementAt ( comboIPs . SelectedIndex ) . Item2 ;
520
549
_currentSettings . SelectedScreenIndex = comboScreens . SelectedIndex ;
521
550
_currentSettings . ImageQuality = qualitySlider . Value ;
551
+ _currentSettings . AllowPublicAccess = cbAllowPublicAccess . Checked ;
522
552
523
553
using ( var appSettingsFile = new FileStream ( "appsettings.xml" , FileMode . Create , FileAccess . Write ) )
524
554
{
@@ -569,9 +599,9 @@ private void qualitySlider_Scroll(object sender, EventArgs e)
569
599
570
600
}
571
601
572
- private void groupBox1_Enter ( object sender , EventArgs e )
602
+ private void cbAllowPublicAccess_CheckedChanged ( object sender , EventArgs e )
573
603
{
574
-
604
+ _currentSettings . AllowPublicAccess = cbAllowPublicAccess . Checked ;
575
605
}
576
606
}
577
607
}