rjp2525/laravel-asn 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rjp2525/laravel-asn

Composer 安装命令:

composer require rjp2525/laravel-asn

包简介

IP ASN utility package for laravel

README 文档

README

Latest Version on Packagist Tests Total Downloads

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

composer require rjp2525/laravel-asn

Publish the configuration file:

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

Usage

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

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

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

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

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

# 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

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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

rjp2525/laravel-asn 适用场景与选型建议

rjp2525/laravel-asn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 150 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rjp2525」 「laravel-asn」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rjp2525/laravel-asn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rjp2525/laravel-asn 我们能提供哪些服务?
定制开发 / 二次开发

基于 rjp2525/laravel-asn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

与 rjp2525/laravel-asn 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 150
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-17