Giter VIP home page Giter VIP logo

libloc-sharp's Introduction

libloc-sharp

DragonFruit Discord GitHub Latest Release)

A version of IPFire's libloc library rewritten for .NET

Overview

This is a rewritten version of libloc (an IP address database used by IPFire firewall software to screen network traffic) along with an additional library, libloc.Access, that enables the database to be loaded as a hosted service, enabling automatic database updates and access control.

The core libloc project has been written to require a single 3rd party library (IPNetwork) and has most of the expected read functionality.

Usage (libloc)

Latest Nuget

The libloc DatabaseLoader.LoadFromFile(string path) returns an ILocationDatabase if the database was successfully loaded in. Accessing different features of the database can then be done through this instance:

  • ILocationDatabase.AS provides a database of Autonomous Systems, where the entire collection can be accessed, or a specific entry can be loaded by ASN
  • ILocationDatabase.Networks provides all networks that own a block of IP addresses which can be accessed by index or enumerated over
  • ILocationDatabase.Countries provides a list of all countries and their continents. These can be accessed by country code, index or enumerated over
  • ILocationDatabase.ResolveAddress(IPAddress address) performs a network tree traversal to locate the network a specific address belongs to, along with the ASN of the owner, the start and end addresses and the prefix length. These values are stored and processed as IPv6, so an IPv4 address will need mapping back from v6 if desired.
  • ILocationDatabase.GetEnumerator(AddressFamily family) provides an enumerator that can be used to get all networks, and their corresponding address blocks.
var database = DatabaseLoader.LoadFromFile("C:\location.db");

// locate 1.1.1.1
var networkInfo = database.ResolveAddress(IPAddress.Parse("1.1.1.1"));

// load AS info
var asInfo = database.AS[networkInfo.ASN];

Usage (libloc.Access)

Latest Nuget (libloc.Access)

There are a few things to note when using libloc.Access:

  • A DragonFruit.Data.ApiClient must be registered in the dependency container - see here for more information
  • IConfiguration is used to load service preferences in (under the LocationDb section)
  • ILogger is injected into the database accessor for logging and diagnosing the database update process

To use the database accessor, use builder.Services.AddLocationDb() in the startup code (after the client has been registered) and access the database using the ILocationDbAccessor interface:

builder.Serivces.AddSingleton<ApiClient<ApiSystemTextJsonSerializer>>();

// add databases
builder.Services.AddLocationDb();
builder.Services.AddDbContext<DummyDatabase>(c => c.UseNpgsql("Host=localhost"));

// after building the host, you can access the database via dependency injection
var locationDb = services.GetRequiredService<ILocationDbAccessor>();
var network = await locationDb.PerformAsync(db => db.ResolveNetwork(IPAddress.Parse("1.1.1.1")));

License

The project is licensed under LGPL-2.1, the same as the original library.

libloc-sharp's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.