SetNet.GeoBlock
1.2.0
dotnet add package SetNet.GeoBlock --version 1.2.0
NuGet\Install-Package SetNet.GeoBlock -Version 1.2.0
<PackageReference Include="SetNet.GeoBlock" Version="1.2.0" />
<PackageVersion Include="SetNet.GeoBlock" Version="1.2.0" />
<PackageReference Include="SetNet.GeoBlock" />
paket add SetNet.GeoBlock --version 1.2.0
#r "nuget: SetNet.GeoBlock, 1.2.0"
#:package SetNet.GeoBlock@1.2.0
#addin nuget:?package=SetNet.GeoBlock&version=1.2.0
#tool nuget:?package=SetNet.GeoBlock&version=1.2.0
<p align="center"> <img src="https://raw.githubusercontent.com/Povstalez/SetNet/master/assets/icon.png" alt="SetNet" width="96"> </p>
SetNet.GeoBlock
Reject connections by country of origin for SetNet.
Filter incoming connections by the peer's geographic location — a blocklist ("everyone except these countries") or an allowlist ("only these countries"). Blocked peers are kicked the moment they connect, before any application frame is processed. The GeoIP lookup is pluggable via IGeoResolver, so you back it with whatever you already use — MaxMind GeoLite2, IP2Location, an HTTP GeoIP API, or a static map for tests. This package ships no database. Added by composition — no base class.
Install
dotnet add package SetNet
dotnet add package SetNet.GeoBlock
Usage
Block specific countries
var geo = server.UseGeoBlock(myResolver, new GeoBlockOptions
{
Policy = GeoPolicy.Blocklist,
Countries = new[] { "KP", "RU" }, // ISO 3166-1 alpha-2, case-insensitive
});
geo.Blocked += (peer, country) =>
Console.WriteLine($"blocked {peer.RemoteEndPoint} ({country ?? "unknown"})");
Allow only specific countries
server.UseGeoBlock(myResolver, new GeoBlockOptions
{
Policy = GeoPolicy.Allowlist,
Countries = new[] { "UA", "PL" },
BlockUnknown = true, // also reject peers whose country can't be resolved
});
Implementing IGeoResolver
public sealed class MaxMindResolver : IGeoResolver
{
private readonly DatabaseReader _db = new("GeoLite2-Country.mmdb");
public string? CountryOf(IPAddress address)
=> _db.TryCountry(address, out var r) ? r?.Country?.IsoCode : null;
}
API
server.UseGeoBlock(IGeoResolver resolver, GeoBlockOptions options) → GeoBlock
GeoBlockOptions |
Meaning |
|---|---|
GeoPolicy Policy |
Blocklist (reject listed) or Allowlist (reject unlisted). Default Blocklist. |
IReadOnlyCollection<string> Countries |
ISO alpha-2 codes the policy applies to (case-insensitive). |
bool BlockUnknown |
Reject peers whose country can't be resolved. Default false (allow). |
GeoBlock
| Member | Purpose |
|---|---|
event Action<BasePeer, string?> Blocked |
fired when a peer is kicked (its country, or null if unknown) |
IGeoResolver — string? CountryOf(IPAddress address) (return null when unknown).
Notes
- Needs a real remote endpoint. GeoBlock reads
peer.RemoteEndPoint?.Address; transports that don't expose one (e.g. the in-memory test transport) resolve tonull→ treated perBlockUnknown. TCP/UDP/WebSocket all expose the IP. - Ships no GeoIP data. You supply the resolver and its database/service; keep the DB updated for accuracy.
- Kick-on-connect, not a gate chain. Blocked peers are disconnected immediately via
PeerConnected; this is complementary to (and composes with)SetNet.BanListandSetNet.Auth. - Geo is coarse. VPNs/proxies defeat country filtering; use it for compliance/region-locking, not as a security boundary.
Documentation & source
- 🐙 https://github.com/Povstalez/SetNet — full module catalog in docs/MODULES.md
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- SetNet (>= 1.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.