PHPackages                             rjp2525/laravel-asn - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. rjp2525/laravel-asn

ActiveLibrary

rjp2525/laravel-asn
===================

IP ASN utility package for laravel

v1.1.0(2mo ago)168[1 PRs](https://github.com/rjp2525/laravel-asn/pulls)MITPHPPHP ^8.4CI passing

Since Feb 17Pushed 1mo agoCompare

[ Source](https://github.com/rjp2525/laravel-asn)[ Packagist](https://packagist.org/packages/rjp2525/laravel-asn)[ Docs](https://github.com/rjp2525/laravel-asn)[ GitHub Sponsors](https://github.com/rjp2525)[ RSS](/packages/rjp2525-laravel-asn/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (16)Versions (4)Used By (0)

Laravel ASN
===========

[](#laravel-asn)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8577ddcbc15c3a6764500ff9c0775e20006ddd9d0bf43bcefc270738a3ce0266/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726a70323532352f6c61726176656c2d61736e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rjp2525/laravel-asn)[![Tests](https://camo.githubusercontent.com/f102a57ad50475be39a963c2b582a03c7741cb8e235f52ab5181902f0eee16bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726a70323532352f6c61726176656c2d61736e2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/rjp2525/laravel-asn/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/f01b1780e636dc04d026a16b3a6206dad9ca10243fd18a258bf62f672ea9e53d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726a70323532352f6c61726176656c2d61736e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rjp2525/laravel-asn)

A full-featured Laravel package for working with Autonomous System Numbers (ASNs) and IP address ranges. Look up which network owns an IP, check if addresses belong to specific ASNs, resolve domains to their ASN, and query your database with driver-optimized IP range filters.

Installation
------------

[](#installation)

```
composer require rjp2525/laravel-asn
```

Publish the configuration file:

```
php artisan vendor:publish --tag="laravel-asn-config"
```

Usage
-----

[](#usage)

### ASN Lookups

[](#asn-lookups)

Look up which ASN owns an IP address:

```
use Reno\ASN\Facades\Asn;

$info = Asn::lookupIp('104.16.0.1');

$info->asn;         // 13335
$info->name;        // "CLOUDFLARENET"
$info->description; // "Cloudflare, Inc."
$info->country;     // "US"
```

Get all prefixes announced by an ASN:

```
$prefixes = Asn::getPrefixes(13335);

foreach ($prefixes as $prefix) {
    echo $prefix->prefix;  // "104.16.0.0/12"
    echo $prefix->name;    // "Cloudflare"
    echo $prefix->country; // "US"
}
```

Check if an IP belongs to an ASN:

```
Asn::ipBelongsToAsn('104.16.0.1', 13335); // true
Asn::ipBelongsToAsn('8.8.8.8', 13335);    // false
```

Check an IP against multiple ASNs:

```
$matched = Asn::ipMatchesAnyAsn('8.8.8.8', [13335, 15169]);
// Returns 15169 (Google's ASN) or null if no match
```

Check if two IPs are on the same network:

```
Asn::ipBelongsToSameAsn('104.16.0.1', '172.64.0.1'); // true (both Cloudflare)
```

### IP Matching

[](#ip-matching)

For checking many IPs against the same set of ranges, build a compiled `IpMatcher` for O(log n) binary search:

```
use Reno\ASN\Facades\Asn;

// Build from ASN prefixes
$matcher = Asn::buildMatcher([13335, 15169]); // Cloudflare + Google

$matcher->contains('104.16.0.1'); // true
$matcher->contains('8.8.8.8');    // true
$matcher->contains('1.2.3.4');    // false
```

Build a custom matcher with CIDR prefixes, single IPs, and explicit ranges:

```
use Reno\ASN\IpMatcher;

$matcher = (new IpMatcher)
    ->addPrefix('10.0.0.0/8')
    ->addPrefix('172.16.0.0/12')
    ->addIp('1.1.1.1')
    ->addExplicitRange('192.168.1.1', '192.168.1.100')
    ->compile();

$matcher->contains('10.0.0.50');    // true
$matcher->contains('1.1.1.1');      // true
$matcher->contains('192.168.1.50'); // true
```

Batch check with detailed results:

```
$results = Asn::batchCheck(
    ips: ['104.16.0.1', '8.8.8.8', '1.2.3.4'],
    asns: [13335],
);

foreach ($results as $result) {
    $result->ip;      // "104.16.0.1"
    $result->matched; // true
    $result->asn();   // 13335
    $result->label(); // "Cloudflare"
}
```

### Domain Resolution

[](#domain-resolution)

Resolve domains and check their ASN:

```
use Reno\ASN\Facades\AsnDns;

$ips = AsnDns::resolveIps('cloudflare.com');
// ["104.16.132.229", "104.16.133.229"]

$info = AsnDns::lookupAsn('cloudflare.com');
// AsnInfo { asn: 13335, name: "CLOUDFLARENET", ... }

AsnDns::domainBelongsToAsn('cloudflare.com', 13335); // true

$matched = AsnDns::domainMatchesAnyAsn('cloudflare.com', [13335, 15169]);
// 13335
```

Check a domain against a compiled matcher:

```
$matcher = (new IpMatcher)->addPrefix('104.16.0.0/12')->compile();

AsnDns::domainMatchesRanges('cloudflare.com', $matcher); // true
```

### Validation Rules

[](#validation-rules)

All rules support PHP Attributes for use on DTOs:

```
use Reno\ASN\Rules\IpInAsn;
use Reno\ASN\Rules\IpNotInAsn;
use Reno\ASN\Rules\IpInRange;
use Reno\ASN\Rules\DomainInAsn;

// In a FormRequest
public function rules(): array
{
    return [
        // IP must belong to Cloudflare
        'ip' => ['required', 'ip', new IpInAsn(13335)],

        // IP must not be from Comcast or AT&T
        'ip' => ['required', 'ip', new IpNotInAsn(7922, 7018)],

        // IP must be in a private range
        'ip' => ['required', 'ip', new IpInRange('10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16')],

        // Domain must resolve to a Cloudflare IP
        'domain' => ['required', 'string', new DomainInAsn(13335)],
    ];
}
```

As PHP Attributes on a DTO:

```
class ServerConfig
{
    #[IpInAsn(13335, 15169)]
    public string $ip;

    #[IpInRange('10.0.0.0/8')]
    public string $internalIp;
}
```

### Eloquent Query Macros

[](#eloquent-query-macros)

Query your database with driver-optimized IP range filters. Works with PostgreSQL (native `inet`), MySQL/MariaDB (`INET_ATON`), and SQLite (numeric extraction):

```
// Filter by CIDR range
User::whereIpInRange('ip_address', '10.0.0.0/8')->get();

// Filter by multiple ranges
User::whereIpInRanges('ip_address', ['10.0.0.0/8', '172.16.0.0/12'])->get();

// Filter by ASN (resolves prefixes automatically)
User::whereIpInAsn('ip_address', 13335)->get();
User::whereIpInAsn('ip_address', [13335, 15169])->get();

// Exclude by ASN
User::whereIpNotInAsn('ip_address', 7922)->get();

// Filter using a pre-compiled matcher
$matcher = Asn::buildMatcher([13335]);
User::whereIpInMatcher('ip_address', $matcher)->get();

// Normalized IP comparison
User::whereIpEquals('ip_address', '8.8.8.8')->get();
```

All macros work on both Eloquent Builder and base Query Builder:

```
DB::table('logs')->whereIpInRange('ip', '10.0.0.0/8')->get();
```

### Artisan Commands

[](#artisan-commands)

```
# Look up ASN information for an IP
php artisan asn:lookup 104.16.0.1

# Check if an IP belongs to an ASN
php artisan asn:check 104.16.0.1 13335

# List prefixes announced by an ASN
php artisan asn:prefixes 13335
php artisan asn:prefixes 13335 --ipv4-only
php artisan asn:prefixes 13335 --ipv6-only
php artisan asn:prefixes 13335 --count

# Look up ASN for a domain
php artisan asn:domain cloudflare.com
php artisan asn:domain cloudflare.com --check-asn=13335
```

Configuration
-------------

[](#configuration)

```
return [
    // ASN data provider: "ripestat" or "ipinfo"
    'provider' => env('ASN_PROVIDER', 'ripestat'),

    'cache' => [
        'enabled' => env('ASN_CACHE_ENABLED', true),
        'store'   => env('ASN_CACHE_STORE'),       // null = default store
        'ttl'     => env('ASN_CACHE_TTL', 86400),  // 24 hours
        'prefix'  => 'reno:asn:',
    ],

    'http' => [
        'timeout'     => env('ASN_HTTP_TIMEOUT', 15),
        'retry_times' => env('ASN_HTTP_RETRIES', 3),
        'retry_delay' => env('ASN_HTTP_RETRY_DELAY', 500),
    ],

    'dns' => [
        'record_type' => DNS_A,
        'cache_ttl'   => env('ASN_DNS_CACHE_TTL', 3600),
    ],

    // Required when using the "ipinfo" provider
    'ipinfo' => [
        'token' => env('ASN_IPINFO_TOKEN'),
    ],
];
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Reno Philibert](https://github.com/rjp2525)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance87

Actively maintained with recent releases

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

88d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/fce8bcb31baa82e9bbb79d1ea08d167125d3d0a2d9d269af1b293b7d0d8159ba?d=identicon)[rjp2525](/maintainers/rjp2525)

---

Top Contributors

[![rjp2525](https://avatars.githubusercontent.com/u/1334865?v=4)](https://github.com/rjp2525 "rjp2525 (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

rjp2525laravel-asn

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rjp2525-laravel-asn/health.svg)

```
[![Health](https://phpackages.com/badges/rjp2525-laravel-asn/health.svg)](https://phpackages.com/packages/rjp2525-laravel-asn)
```

###  Alternatives

[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[mikebronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k127.1k1](/packages/mikebronner-laravel-model-caching)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
