Skip to content

Commit 8e8267f

Browse files
committedFeb 25, 2022
refactor adding firewall rules
1 parent 2d5fb37 commit 8e8267f

File tree

4 files changed

+133
-102
lines changed

4 files changed

+133
-102
lines changed
 

‎ScreenTask/AppSettings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public class AppSettings
2020
public bool IsAutoStartServerEnabled { get; set; }
2121
public bool IsStartMinimizedEnabled { get; set; }
2222
public int ImageQuality { get; set; }
23+
public bool AllowPublicAccess { get; set; }
2324
}
2425
}

‎ScreenTask/app.manifest

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
If you want to utilize File and Registry Virtualization for backward
1717
compatibility then delete the requestedExecutionLevel node.
1818
-->
19-
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
19+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
2020
</requestedPrivileges>
2121
</security>
2222
</trustInfo>

‎ScreenTask/frmMain.Designer.cs

+77-77
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ScreenTask/frmMain.cs

+54-24
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private async Task StartServer()
127127
var url = string.Format("http://{0}:{1}", selectedIP, numPort.Value.ToString());
128128
txtURL.Text = url;
129129
serv.Prefixes.Clear();
130-
serv.Prefixes.Add("http://localhost:" + numPort.Value.ToString() + "/");
130+
//serv.Prefixes.Add("http://localhost:" + numPort.Value.ToString() + "/");
131131
//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.
132132
serv.Prefixes.Add(url + "/");
133133
serv.Start();
@@ -335,45 +335,73 @@ private async Task AddFirewallRule(int port)
335335
{
336336
await Task.Run(() =>
337337
{
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))
341347
{
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))
345355
{
346356
Log("Screen Task Rule added to your firewall");
347357
}
348358
}
349359
else
350360
{
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))
355372
{
356373
Log("Screen Task Rule updated to your firewall");
357374
}
358375
}
359376
});
360377

361378
}
362-
private string RunCMD(string cmd)
379+
private string RunCMD(string cmd, bool requireAdmin = false)
363380
{
364381
Process proc = new Process();
365382
proc.StartInfo.FileName = "cmd.exe";
366-
proc.StartInfo.Arguments = "/C " + cmd;
383+
proc.StartInfo.Arguments = "/C " + cmd ;
367384
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+
377405
}
378406
private void Log(string text)
379407
{
@@ -448,6 +476,7 @@ private void frmMain_Load(object sender, EventArgs e)
448476
this.numPort.Value = _currentSettings.Port;
449477
this.numShotEvery.Value = _currentSettings.ScreenshotsSpeed;
450478
this.qualitySlider.Value = _currentSettings.ImageQuality != default ? _currentSettings.ImageQuality : 75;
479+
this.cbAllowPublicAccess.Checked = _currentSettings.AllowPublicAccess;
451480
this.comboIPs.SelectedIndex = _ips.IndexOf(_ips.FirstOrDefault(ip => ip.Item2.Contains(_currentSettings.IP)));
452481
if (_currentSettings.SelectedScreenIndex > -1 && comboScreens.Items.Count > 0 && _currentSettings.SelectedScreenIndex <= comboScreens.Items.Count - 1)
453482
this.comboScreens.SelectedIndex = _currentSettings.SelectedScreenIndex;
@@ -519,6 +548,7 @@ private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
519548
_currentSettings.IP = _ips.ElementAt(comboIPs.SelectedIndex).Item2;
520549
_currentSettings.SelectedScreenIndex = comboScreens.SelectedIndex;
521550
_currentSettings.ImageQuality = qualitySlider.Value;
551+
_currentSettings.AllowPublicAccess = cbAllowPublicAccess.Checked;
522552

523553
using (var appSettingsFile = new FileStream("appsettings.xml", FileMode.Create, FileAccess.Write))
524554
{
@@ -569,9 +599,9 @@ private void qualitySlider_Scroll(object sender, EventArgs e)
569599

570600
}
571601

572-
private void groupBox1_Enter(object sender, EventArgs e)
602+
private void cbAllowPublicAccess_CheckedChanged(object sender, EventArgs e)
573603
{
574-
604+
_currentSettings.AllowPublicAccess = cbAllowPublicAccess.Checked;
575605
}
576606
}
577607
}

0 commit comments

Comments
 (0)
Please sign in to comment.