This commit is contained in:
RDeck 2025-06-30 04:42:25 -04:00
parent 76d5541e7e
commit efb7f1d0d7
47 changed files with 225 additions and 21 deletions

Binary file not shown.

View File

@ -5,6 +5,10 @@
{ {
"AbsoluteMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|c:\\repos\\blockipaddr\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "AbsoluteMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|c:\\repos\\blockipaddr\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|solutionrelative:program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" "RelativeMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|solutionrelative:program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|c:\\repos\\blockipaddr\\classobj\\ansi.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|solutionrelative:classobj\\ansi.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
} }
], ],
"DocumentGroupContainers": [ "DocumentGroupContainers": [
@ -68,10 +72,23 @@
"RelativeDocumentMoniker": "Program.cs", "RelativeDocumentMoniker": "Program.cs",
"ToolTip": "C:\\Repos\\BlockIPAddr\\Program.cs", "ToolTip": "C:\\Repos\\BlockIPAddr\\Program.cs",
"RelativeToolTip": "Program.cs", "RelativeToolTip": "Program.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAABoAAAAAAAAAAAAAAA==", "ViewState": "AgIAAAIAAAAAAAAAAAAqwDYAAAAKAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-06-27T16:11:33.579Z", "WhenOpened": "2025-06-27T16:11:33.579Z",
"EditorCaption": "" "EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "ANSI.cs",
"DocumentMoniker": "C:\\Repos\\BlockIPAddr\\ClassObj\\ANSI.cs",
"RelativeDocumentMoniker": "ClassObj\\ANSI.cs",
"ToolTip": "C:\\Repos\\BlockIPAddr\\ClassObj\\ANSI.cs",
"RelativeToolTip": "ClassObj\\ANSI.cs",
"ViewState": "AgIAADgAAAAAAAAAAAAAAEoAAAARAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-06-30T08:09:46.703Z",
"EditorCaption": ""
} }
] ]
} }

View File

@ -5,6 +5,10 @@
{ {
"AbsoluteMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|c:\\repos\\blockipaddr\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}", "AbsoluteMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|c:\\repos\\blockipaddr\\program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|solutionrelative:program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}" "RelativeMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|solutionrelative:program.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
},
{
"AbsoluteMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|c:\\repos\\blockipaddr\\classobj\\ansi.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{388323B4-A1A6-465C-BE7F-5C3C7270C64E}|BlockIPAddr.csproj|solutionrelative:classobj\\ansi.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
} }
], ],
"DocumentGroupContainers": [ "DocumentGroupContainers": [
@ -68,10 +72,23 @@
"RelativeDocumentMoniker": "Program.cs", "RelativeDocumentMoniker": "Program.cs",
"ToolTip": "C:\\Repos\\BlockIPAddr\\Program.cs", "ToolTip": "C:\\Repos\\BlockIPAddr\\Program.cs",
"RelativeToolTip": "Program.cs", "RelativeToolTip": "Program.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAgAAABLAAAAAAAAAA==", "ViewState": "AgIAAAIAAAAAAAAAAAAqwBgAAAAmAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-06-27T16:11:33.579Z", "WhenOpened": "2025-06-27T16:11:33.579Z",
"EditorCaption": "" "EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "ANSI.cs",
"DocumentMoniker": "C:\\Repos\\BlockIPAddr\\ClassObj\\ANSI.cs",
"RelativeDocumentMoniker": "ClassObj\\ANSI.cs",
"ToolTip": "C:\\Repos\\BlockIPAddr\\ClassObj\\ANSI.cs",
"RelativeToolTip": "ClassObj\\ANSI.cs",
"ViewState": "AgIAADgAAAAAAAAAAAAAAEoAAAARAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-06-30T08:09:46.703Z",
"EditorCaption": ""
} }
] ]
} }

136
ClassObj/ANSI.cs Normal file
View File

@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace BlockIPAddr.ClassObj;
internal class ANSI
{
/*
ANSI Console Color Codes
8 Colors Foreground
The most basic terminals have a set of 8 different colors:
Black: \u001b[30m
Red: \u001b[31m
Green: \u001b[32m
Yellow: \u001b[33m
Blue: \u001b[34m
Magenta: \u001b[35m
Cyan: \u001b[36m
White: \u001b[37m
Reset: \u001b[0m
16 Colors Foreground
Most terminals, apart from the basic set of 8 colors, also support the "bright" or "bold" colors. These have their own set of codes, mirroring the normal
colors, but with an additional ;1 in their codes:
Bright Black: \u001b[30;1m
Bright Red: \u001b[31;1m
Bright Green: \u001b[32;1m
Bright Yellow: \u001b[33;1m
Bright Blue: \u001b[34;1m
Bright Magenta: \u001b[35;1m
Bright Cyan: \u001b[36;1m
Bright White: \u001b[37;1m
Reset: \u001b[0m
Background Colors
The Ansi escape codes let you set the color of the text-background the same way it lets you set the color of the foregrond. For example, the 8 background
colors correspond to the codes:
Background Black: \u001b[40m
Background Red: \u001b[41m
Background Green: \u001b[42m
Background Yellow: \u001b[43m
Background Blue: \u001b[44m
Background Magenta: \u001b[45m
Background Cyan: \u001b[46m
Background White: \u001b[47m
With the bright versions being:
Background Bright Black: \u001b[40;1m
Background Bright Red: \u001b[41;1m
Background Bright Green: \u001b[42;1m
Background Bright Yellow: \u001b[43;1m
Background Bright Blue: \u001b[44;1m
Background Bright Magenta: \u001b[45;1m
Background Bright Cyan: \u001b[46;1m
Background Bright White: \u001b[47;1m
Decorations
Apart from colors, and background-colors, Ansi escape codes also allow decorations on the text:
Bold: \u001b[1m
Underline: \u001b[4m
Reversed: \u001b[7m
Source: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
*/
internal const string fgReset = "\u001b[0m";
internal const string fgBrightBlack = "\u001b[30;1m";
internal const string fgBrightRed = "\u001b[31;1m";
internal const string fgBrightGreen = "\u001b[32;1m";
internal const string fgBrightYellow = "\u001b[33;1m";
internal const string fgBrightBlue = "\u001b[34;1m";
internal const string fgBrightMagenta = "\u001b[35;1m";
internal const string fgBrightCyan = "\u001b[36;1m";
internal const string fgBrightWhite = "\u001b[37;1m";
internal const string fgNormalBlack = "\u001b[30m";
internal const string fgNormalRed = "\u001b[31m";
internal const string fgNormalGreen = "\u001b[32m";
internal const string fgNormalYellow = "\u001b[33m";
internal const string fgNormalBlue = "\u001b[34m";
internal const string fgNormalMagenta = "\u001b[35m";
internal const string fgNormalCyan = "\u001b[36m";
internal const string fgNormalWhite = "\u001b[37m";
public static class ConsoleColorization
{
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
public static void Initialize()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
EnableAnsiEscapeSequencesOnWindows();
Console.OutputEncoding = System.Text.Encoding.UTF8;
}
} //Initialize
private static void EnableAnsiEscapeSequencesOnWindows()
{
IntPtr handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle == IntPtr.Zero)
{
throw new Exception("Cannot get standard output handle");
}
if (!GetConsoleMode(handle, out uint mode))
{
throw new Exception("Cannot get console mode");
}
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(handle, mode))
{
throw new Exception("Cannot set console mode");
}
} //EnableAnsiEscapeSequencesOnWindows
} //ConsoleColor
}

View File

@ -1,25 +1,59 @@
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using BlockIPAddr.ClassObj;
using static BlockIPAddr.ClassObj.ANSI;
namespace BlockIPAddr; namespace BlockIPAddr;
internal class Program internal class Program
{ {
[STAThread]
static void Main(string[] args) static void Main(string[] args)
{ {
using (var cn = new SqlConnection(kmCommonLibsCore.Constants.cnPortal)) ConsoleColorization.Initialize(); // Allow ANSI in the console
using (var cm = new SqlCommand("dbo.[BlockIPAddrInFirewall]", cn) { CommandType = System.Data.CommandType.StoredProcedure })
if (args.Length > 0)
{ {
Console.WriteLine(); Console.WriteLine();
cn.Open(); using (var cn = new SqlConnection(kmCommonLibsCore.Constants.cnPortal))
cm.Parameters.Add("@ip4", System.Data.SqlDbType.VarChar, 30); using (var cm = new SqlCommand("dbo.[BlockIPAddrInFirewall]", cn) { CommandType = System.Data.CommandType.StoredProcedure })
foreach (string arg in args)
{ {
string rv; cn.Open();
cm.Parameters.Add("@ip4", System.Data.SqlDbType.VarChar, 30);
foreach (string arg in args)
{
string rv;
string color = string.Empty;
cm.Parameters["@ip4"].Value = arg.Trim(); cm.Parameters["@ip4"].Value = arg.Trim();
rv = Convert.ToString(cm.ExecuteScalar() ?? string.Empty); rv = cm.ExecuteScalar().ToString() ?? string.Empty;
Console.WriteLine(rv); color = rv.StartsWith("!!") ? ANSI.fgBrightRed : ANSI.fgBrightYellow;
Console.WriteLine("Result: {1}{0}{2}", rv, color, ANSI.fgReset);
}
}
}
else
{
while (true)
{
string ip4 = string.Empty;
Console.Write("\nEnter an IP Address to block (can include slash-notation) or Enter to Exit: ");
ip4 = Console.ReadLine();
if (string.IsNullOrWhiteSpace(ip4))
break;
using (var cn = new SqlConnection(kmCommonLibsCore.Constants.cnPortal))
using (var cm = new SqlCommand("dbo.[BlockIPAddrInFirewall]", cn) { CommandType = System.Data.CommandType.StoredProcedure })
{
string color = string.Empty;
string rv = string.Empty;
cn.Open();
cm.Parameters.Add("@ip4", System.Data.SqlDbType.VarChar, 30).Value = ip4;
rv = cm.ExecuteScalar().ToString() ?? string.Empty;
color = rv.StartsWith("!!") ? ANSI.fgBrightRed : ANSI.fgBrightYellow;
Console.WriteLine("\nResult: {1}{0}{2}", rv, color, ANSI.fgReset);
}
} }
} }
} }

View File

@ -2,7 +2,7 @@
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. --> <!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<History>True|2025-06-27T16:28:32.0039427Z||;True|2025-06-27T12:27:16.4031564-04:00||;True|2025-06-27T12:23:51.7850485-04:00||;True|2025-06-27T12:13:48.5390689-04:00||;True|2025-06-27T12:12:59.9985493-04:00||;</History> <History>True|2025-06-30T08:39:28.6629030Z||;True|2025-06-30T04:04:51.2689872-04:00||;True|2025-06-30T04:02:09.8676131-04:00||;True|2025-06-27T12:28:32.0039427-04:00||;True|2025-06-27T12:27:16.4031564-04:00||;True|2025-06-27T12:23:51.7850485-04:00||;True|2025-06-27T12:13:48.5390689-04:00||;True|2025-06-27T12:12:59.9985493-04:00||;</History>
<LastFailureDetails /> <LastFailureDetails />
</PropertyGroup> </PropertyGroup>
</Project> </Project>

Binary file not shown.

Binary file not shown.

View File

@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyCompanyAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+76d5541e7e2e23eff1f7874ec3c40a46ef7e1f8d")]
[assembly: System.Reflection.AssemblyProductAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyProductAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyTitleAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyTitleAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
313cd8038b24a65586affabdf0353707afc31917b5e8da890d6f0beb7a0374f6 61c37c42ee97e4459c046c73f3178cb9717e64e39648654751cd12b3dad0d28b

View File

@ -1 +1 @@
8dfa9e7f3c869e2e14b066aa2d7c79984b599ff4fa64c91aac225f45322424a9 9590f15ff63e7ceda5d0b7c318d17b8b6b71cb6e26cc7210ed178dbbf4e839bf

Binary file not shown.

View File

@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyCompanyAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+76d5541e7e2e23eff1f7874ec3c40a46ef7e1f8d")]
[assembly: System.Reflection.AssemblyProductAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyProductAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyTitleAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyTitleAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
6f3359db292eb2114d5a51ea818e78f5826177dd9aa1bd2a50813edf124a3cf1 87dc558f82f5265012bd9142dc02bd8e54fbd91b8009a10a518788f869939f12

View File

@ -1 +1 @@
7a9a61db58009c4791125726acc7c6d3434c17b94cbd969d486f625fc7cb7815 025007fdead242d002df3e7a84a8860b71b465181722a4480c71a69ce525c5c0

View File

@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyCompanyAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+76d5541e7e2e23eff1f7874ec3c40a46ef7e1f8d")]
[assembly: System.Reflection.AssemblyProductAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyProductAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyTitleAttribute("BlockIPAddr")] [assembly: System.Reflection.AssemblyTitleAttribute("BlockIPAddr")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
6f3359db292eb2114d5a51ea818e78f5826177dd9aa1bd2a50813edf124a3cf1 87dc558f82f5265012bd9142dc02bd8e54fbd91b8009a10a518788f869939f12

View File

@ -1 +1 @@
a3a6613be5e96d60d680735a3eb8101ca88cc21301deaeb883e860dc1f24fcb1 a59c45d6fa9b1a8838db49c52e7e2fce04ea6b133c5d6a07e174f9a3cf96e3c3