In addition for sroprot, I made a small class to block the IP in windows firewall...
source: https://mega.co.nz/#!EMNFzDpA!_vJ3oO...VtnRmxIK2vsr7Y
compile it and add as reference to sroprot...
here's an example, btw this example below has block timer:
Code:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Net;
using System.Collections.Generic;
using System.Threading;
using FirewallControl;
namespace sroprot.Core
{
public sealed class FirewallManager
{
static FirewallManager m_Instance = null;
private FirewallManager() { }
public static FirewallManager getInstance()
{
if (m_Instance == null)
{
m_Instance = new FirewallManager();
}
return m_Instance;
}
public static string FWRuleName = "SR_Blocklist";
private List<FwBlockList> blocklist = new List<FwBlockList>();
private Thread m_FirewallWorker = null;
private bool m_IsRunning = false;
private bool m_IsCreated = false;
private Firewall FWCtrl = new Firewall();
private struct FwBlockList
{
public String ipaddr;
public DateTime time;
}
public void AddToFwBlockList(string ip)
{
try
{
Global.BlockedIpAddresses.Add(ip);
DateTime time = new DateTime();
time = DateTime.Now;
FwBlockList item = new FwBlockList()
{
ipaddr = ip,
time = time
};
blocklist.Add(item);
if (Global.EnableFirewallBlocking)
{
FWCtrl.AddBlockAddresses(ip);
}
}
catch
{
Console.WriteLine("Status: Failed to add entry");
return;
}
}
public void RemToFwBlockList(string ip)
{
try
{
Global.BlockedIpAddresses.Remove(ip);
if (Global.EnableFirewallBlocking)
{
FWCtrl.RemoveBlockAddresses(ip);
}
}
catch
{
Console.WriteLine("Status: Failed to add entry");
return;
}
}
void FirewallThread()
{
while (this.m_IsRunning)
{
try
{
if (blocklist != null)
{
for (int i = 0; i < blocklist.Count; i++)
{
var addr = blocklist[i].ipaddr;
var time = blocklist[i].time;
TimeSpan span = new TimeSpan();
span = DateTime.Now - time;
int timespan = span.Seconds;
if (timespan > Global.IPBlockDuration)
{
blocklist.RemoveAll
(
structure => structure.ipaddr == addr
);
RemToFwBlockList(addr);
}
}
}
}
catch
{
Console.WriteLine("Status: Error failed to start FirewallThread thread");
}
Thread.Sleep(1);
}
}
public void StartFirewall(string p1, string p2)
{
if (this.m_IsRunning)
return;
try
{
if (Global.EnableFirewallBlocking)
{
this.FWCtrl.CreateRule(FWRuleName, p1 + "," + p2);
if (this.FWCtrl.RuleIsCreated)
{
Console.WriteLine("Status: {0} firewall rule was created!", FWRuleName);
}
}
this.m_FirewallWorker = new Thread(this.FirewallThread);
this.m_IsRunning = true;
this.m_FirewallWorker.Start();
}
catch
{
Console.WriteLine("Status: Error at starting m_FirewallWorker thread");
return;
}
}
void StopFwThread()
{
if (!this.m_IsRunning)
return;
try
{
this.m_FirewallWorker.Abort();
this.m_FirewallWorker = null;
}
catch
{
Console.WriteLine("Status: Error at aborting m_FirewallWorker thread");
return;
}
this.m_IsRunning = false;
}
}
}
To fix disconnections or no responds from server issue in sroprot..
In UserContexts.cs go to DoRecvFromClientCallback and DoRecvFromModuleCallback functions and make it to process only a complete buffer, cuz SilkroadSecurityApi was made in blocking method...
correct me if I'm wrong with this.. I'm not a pro programmer...