BlockIPAddr/Program.cs

61 lines
1.9 KiB
C#
Raw Normal View History

2025-06-30 03:56:30 -04:00
using Microsoft.Data.SqlClient;
2025-06-30 04:42:25 -04:00
using BlockIPAddr.ClassObj;
using static BlockIPAddr.ClassObj.ANSI;
2025-06-30 03:56:30 -04:00
namespace BlockIPAddr;
internal class Program
{
2025-06-30 04:42:25 -04:00
[STAThread]
2025-06-30 03:56:30 -04:00
static void Main(string[] args)
{
2025-06-30 04:42:25 -04:00
ConsoleColorization.Initialize(); // Allow ANSI in the console
if (args.Length > 0)
2025-06-30 03:56:30 -04:00
{
Console.WriteLine();
2025-06-30 04:42:25 -04:00
using (var cn = new SqlConnection(kmCommonLibsCore.Constants.cnPortal))
using (var cm = new SqlCommand("dbo.[BlockIPAddrInFirewall]", cn) { CommandType = System.Data.CommandType.StoredProcedure })
{
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();
rv = cm.ExecuteScalar().ToString() ?? string.Empty;
color = rv.StartsWith("!!") ? ANSI.fgBrightRed : ANSI.fgBrightYellow;
Console.WriteLine("Result: {1}{0}{2}", rv, color, ANSI.fgReset);
}
}
}
else
{
while (true)
2025-06-30 03:56:30 -04:00
{
2025-06-30 04:42:25 -04:00
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;
2025-06-30 03:56:30 -04:00
2025-06-30 04:42:25 -04:00
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;
2025-06-30 03:56:30 -04:00
2025-06-30 04:42:25 -04:00
color = rv.StartsWith("!!") ? ANSI.fgBrightRed : ANSI.fgBrightYellow;
Console.WriteLine("\nResult: {1}{0}{2}", rv, color, ANSI.fgReset);
}
2025-06-30 03:56:30 -04:00
}
}
}
}