Skip to content

Commit

Permalink
#2 Highlight rules no longer valid & #3 Performance at loading
Browse files Browse the repository at this point in the history
Signed-off-by: pdulvp <[email protected]>
  • Loading branch information
pdulvp committed Nov 14, 2022
1 parent 495f374 commit b8f1185
Show file tree
Hide file tree
Showing 13 changed files with 1,767 additions and 2,558 deletions.
3 changes: 2 additions & 1 deletion EasyFirewall.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<ProductName>Firewall Block</ProductName>
<PublisherName>pdulvp</PublisherName>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<ApplicationVersion>1.1.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
Expand Down Expand Up @@ -171,6 +171,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Content Include="filter_change%402x.png" />
<Content Include="out.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
50 changes: 15 additions & 35 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 20 additions & 84 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ public Form1()
// Put it in "Details" mode, create a column so that "Details" mode will work,
// and set its theme so it will look like the one used by Explorer.
listView1.View = View.Details;
listView1.Columns.Add("Name", 500);
listView1.Columns.Add("Name", 400);
WinIcons.SetWindowTheme(listView1.Handle, "Explorer", null);

listView1.Columns.Add("Folder", 100);
listView1.Columns.Add("Folder", 200);
listView1.Columns.Add("Product Name", 100);
listView1.Columns.Add("Company", 100);

loadRules();
GroupBy(CurrentGroup);

}

private List<INetFwRule> getRules(INetFwPolicy2 policy)
Expand All @@ -90,6 +91,7 @@ private void loadRules()

// Obtain a handle to the system image list.
listView1.Items.Clear();
List<ListViewItem> items = new List<ListViewItem>();

try
{
Expand All @@ -104,7 +106,7 @@ private void loadRules()
if (!inversedRules.Exists(obj => obj == rule))
{
INetFwRule inverse = getInverse(rule, myRules);
addItem(rule, inverse);
items.Add(createItem(rule, inverse));
if (inverse != null)
{
inversedRules.Add(inverse);
Expand All @@ -119,6 +121,7 @@ private void loadRules()
{
Console.WriteLine("Error deleting a Firewall rule");
}
listView1.Items.AddRange(items.ToArray());
System.Threading.Tasks.Task.Delay(2 * 1000).ContinueWith(ResetStatusLine);

}
Expand All @@ -131,26 +134,19 @@ private void ResetStatusLine(Task obj)
}));

}
private void addItem(INetFwRule rule, INetFwRule inverse)
private ListViewItem createItem(INetFwRule rule, INetFwRule inverse)
{
WinIcons.SHFILEINFO shfi = new WinIcons.SHFILEINFO();
IntPtr hSysImgList = WinIcons.SHGetFileInfo("",
0,
ref shfi,
(uint)Marshal.SizeOf(shfi),
WinIcons.SHGFI_SYSICONINDEX
| WinIcons.SHGFI_SMALLICON);


IntPtr himl = WinIcons.SHGetFileInfo(rule.ApplicationName,
0,
ref shfi,
(uint)Marshal.SizeOf(shfi),
WinIcons.SHGFI_DISPLAYNAME
| WinIcons.SHGFI_SYSICONINDEX
| WinIcons.SHGFI_SMALLICON);

ListViewItem item = new ListViewItem(getRuleReadableName(rule), shfi.iIcon);

item.SubItems.Add(new ListViewSubItem(item, getRuleReadableProduct(rule)));
try
{
Expand All @@ -163,7 +159,14 @@ private void addItem(INetFwRule rule, INetFwRule inverse)

}
item.Tag = rule;
listView1.Items.Add(item);

if (!File.Exists(rule.ApplicationName))
{
item.ForeColor = Color.Orange;
item.Font = new Font(listView1.Font, listView1.Font.Style | FontStyle.Strikeout);
}

return item;
}

private void GroupBy(int i)
Expand Down Expand Up @@ -256,16 +259,11 @@ private void listView1_DragDrop(object sender, DragEventArgs e)
Policy.Rules.Add(firewallRule);
INetFwRule firewallRule2 = createInverseRule(firewallRule);
Policy.Rules.Add(firewallRule2);
addItem(firewallRule, firewallRule2);
listView1.Items.Add(createItem(firewallRule, firewallRule2));
}
GroupBy(CurrentGroup);
}

private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{

}

private INetFwRule getInverse(INetFwRule rule, List<INetFwRule> rules)
{
return rules.Find(r => r.Direction != rule.Direction && r.ApplicationName == rule.ApplicationName);
Expand Down Expand Up @@ -324,16 +322,6 @@ private INetFwRule createInverseRule(INetFwRule rule)
return firewallRule;
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
Expand All @@ -351,7 +339,7 @@ public void InsertFiles(object sender, CancelEventArgs e)
Policy.Rules.Add(firewallRule);
INetFwRule firewallRule2 = createInverseRule(firewallRule);
Policy.Rules.Add(firewallRule2);
addItem(firewallRule, firewallRule2);
listView1.Items.Add(createItem(firewallRule, firewallRule2));
}
openFileDialog1.FileOk -= InsertFiles;
GroupBy(CurrentGroup);
Expand Down Expand Up @@ -387,44 +375,6 @@ private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
GroupBy(CurrentGroup);
}

private void repairToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
List<INetFwRule> CreatedRules = new List<INetFwRule>();
List<INetFwRule> Rules = getRules(Policy);
toolStripProgressBar1.Maximum = Rules.Count;
toolStripProgressBar1.Value = 0;
foreach (INetFwRule rule in Rules)
{
string newName = getRuleReadableName(rule);
if (rule.Name != newName)
{
rule.Name = newName;
}
if (rule.Grouping != GROUP)
{
rule.Grouping = GROUP;
}

INetFwRule inverse = getInverse(rule, Rules);
if (inverse == null)
{
INetFwRule firewallRule = createInverseRule(rule);
Policy.Rules.Add(firewallRule);
}
toolStripProgressBar1.Value++;
}
}

catch (Exception r)
{
Console.WriteLine(r);
Console.WriteLine("Error deleting a Firewall rule");
}
System.Threading.Tasks.Task.Delay(2 * 1000).ContinueWith(ResetStatusLine);
}

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
loadRules();
Expand All @@ -440,25 +390,11 @@ private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
}
}

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{

}

private void listView1_Resize(object sender, EventArgs e)
{

}

private void cccToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/pdulvp/easy-firewall");
}

private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/pdulvp/easy-firewall");
}

}

}
Loading

0 comments on commit b8f1185

Please sign in to comment.