Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

hanswolff/ipranges

Repository files navigation

Overview

This package provides a lightweight offline classification of IP addresses. It determines if an IP address is within a specific range. Some common IP ranges are already included:

  • Amazon
  • Azure
  • CloudFlare
  • DigitalOcean
  • LeaseWeb
  • OVH

THERE IS NO GUARANTEE THAT THE PROVIDED IP RANGES ARE ACCURATE! PLEASE READ THE "LICENSE" FILE!

Get it on NuGet!

Install-Package IpRanges

Usage Example

public void run_example()
{
	// get regions from resource
	var regions = IPRangesParser.ParseFromResources().SelectMany(x => x.Regions);

	// create regions dictionary
	var dictionary = new IPRangeDictionary<IPRangesRegion>();
	foreach (var region in regions)
		foreach (var range in region.Ranges)
			dictionary.Add(range, region);

	var ipAddress = IPAddress.Parse("23.20.123.123");

	// test if IP address is within any region
	var foundRegion = dictionary[ipAddress]; // throws KeyNotFoundException if not found
	Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);

	// or better (to avoid KeyNotFoundException)
	IPRangesRegion value;
	if (dictionary.TryGetValue(ipAddress, out value))
		Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);
}

About

Lightweight offline classification of IP addresses.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published