61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using BlockIPAddr.ClassObj;
|
|
using static BlockIPAddr.ClassObj.ANSI;
|
|
|
|
namespace BlockIPAddr;
|
|
|
|
internal class Program
|
|
{
|
|
[STAThread]
|
|
static void Main(string[] args)
|
|
{
|
|
ConsoleColorization.Initialize(); // Allow ANSI in the console
|
|
|
|
if (args.Length > 0)
|
|
{
|
|
Console.WriteLine();
|
|
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)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|