miken32/network-rules
Composer 安装命令:
composer require miken32/network-rules
包简介
Laravel validation rules for checking IP addresses and networks
README 文档
README
A Collection of Rules
for the Reliable Validation
of
Internet Protocol Addreſſes,
Networks, and Subnets
which the author hopes will be found uſeful to
Laravel Programmers &c.
Installation
composer require miken32/network-rules
Available Validation Rules
Here is a list of the available rules and their usage.
In Network
Outside Network
IP Or Net
Network
Netv4
Netv6
Private IP
Private IPv4
Private IPv6
Private Net
Routable IP or Net
Routable IP
Routable IPv4
Routable IPv6
Routable Net
Routable Netv4
Routable Netv6
in_network:cidr,...
The field under validation must be an IP address within one of the given networks. The networks must be given in CIDR notation, and may be either IPv4 or IPv6 networks.
'ip4_address' => 'in_network:192.168.0.1/24',
'some_address' => 'in_network:192.168.0.0/24,192.168.1.0/24,192.168.2.0/24',
'ip6_address' => 'in_network:fd03:224f:a5c3:99ae::0/64'
outside_network:cidr,...
The field under validation must be an IP address outside all of the given networks. The networks must be given in CIDR notation, and may be either IPv4 or IPv6 networks.
'ip4_address' => 'outside_network:192.168.0.1/24',
'some_address' => 'outside_network:192.168.0.0/24,192.168.1.0/24,192.168.2.0/24',
'ip6_address' => 'outside_network:fd03:224f:a5c3:99ae::0/64'
ip_or_net
The field under validation must be an IP address or network in CIDR notation. The address or network may be either IPv4 or IPv6.
network
The field under validation must be an IP network in CIDR notation. The network may be either IPv4 or IPv6; use individual rules to limit mask bits.
netv4:low,high
The field under validation must be an IPv4 network in CIDR notation.
If provided, the number of bits in the mask must be between low and high.
'bounded_network' => 'netv4:20,24',
'unbounded_network' => 'netv4'
netv6:low,high
The field under validation must be an IPv6 network in CIDR notation.
If provided, the number of bits in the mask must be between low and high.
'bounded_network' => 'netv6:56,64'
'unbounded_network' => 'netv6'
private_ip
The field under validation must be a private IPv4 or IPv6 address. Private addresses are defined as being within one of the following networks:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
- fc00::/7
The following networks are considered reserved, not private. They are not considered valid by this rule:
- 0.0.0.0/8
- 127.0.0.0/8
- 169.254.0.0/16
- 240.0.0.0/4
- ::/128
- ::1/128
- 2001:10::/28
- 2001:db8::/32
- 3ffe::/16
- 5f00::/8
- fe80::/10
private_ipv4
The field under validation must be a private IPv4 addresses.
The networks considered private are described in the private_ip rule.
private_ipv6
The field under validation must be a private IPv6 addresses.
The networks considered private are described in the private_ip rule.
private_net
The field under validation must be a private IP network in CIDR notation.
The networks considered private are described in the private_ip rule.
routable_ip_or_net
The field under validation must be a globally routable IP address or network in CIDR notation.
This excludes all private and reserved ranges, as detailed the the private_ip rule.
routable_ip
The field under validation must be a globally routable IPv4 or IPv6 address.
This excludes all private and reserved ranges, as detailed the the private_ip rule.
routable_ipv4
The field under validation must be a globally routable IPv4 address.
This excludes all private and reserved ranges, as detailed the the private_ip rule.
routable_ipv6
The field under validation must be a globally routable IPv6 address.
This excludes all private and reserved ranges, as detailed the the private_ip rule.
routable_net:low,high
The field under validation must be a globally routable IP network in CIDR notation.
The network must not overlap any private or reserved ranges, as detailed the the private_ip rule.
If provided, the number of bits in the mask must be between low and high.
routable_netv4:low,high
The field under validation must be a globally routable IPv4 network in CIDR notation.
The network must not overlap any private or reserved ranges, as detailed the the private_ip rule.
If provided, the number of bits in the mask must be between low and high.
routable_netv6:low,high
The field under validation must be a globally routable IPv6 network in CIDR notation.
The network must not overlap any private or reserved ranges, as detailed the the private_ip rule.
If provided, the number of bits in the mask must be between low and high.
Usage
The included validation rules can be used either as traditional string-based validation rules or as instantiated classes. The following code blocks perform identical validations.
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class MyFormRequest extends FormRequest { public function rules(): array { return [ 'address' => ['in_network:192.168.10.0/24'], // must be an IPv4 address in the specified network 'subnet' => ['netv4'], // must be an IPv4 CIDR network 'ipv6_subnet' => ['netv6:48,56'], // must be an IPv6 CIDR network between 48 and 56 bits ]; } }
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Miken32\Validation\Network\Rules; class AnotherFormRequest extends FormRequest { public function rules(): array { return [ 'address' => [new Rules\InNetwork('192.168.10.0/24')], // must be an IPv4 address in the specified network 'subnet' => [new Rules\Netv4()], // must be an IPv4 CIDR network 'ipv6_subnet' => [new Rules\Netv6(48, 56)], // must be an IPv6 CIDR network between 48 and 56 bits ]; } }
Note, for all methods that accept a minimum or maximum bit mask, unspecified arguments
default to the minimum or maximum count, depending on the type of network. For example,
new Rules\Netv6(48) or netv6:48 are equivalent to new Rules\Netv6(48,128) or
netv6:48,128. To specify an exact bit mask, use the same value for both arguments:
new Rules\Netv6(48, 48) or netv6:48,48.
Standalone Usage
These checks can be used outside the Laravel framework as well; the Util class provides
many static methods for validating input using the framework (or not!) of your choice.
<?php use Miken32\Validation\Network\Util; $allowed_network = '172.16.40.0/22'; if (!Util::addressWithinNetwork($_POST["ip_address"], $allowed_network)) { // failure! } if (Util::validPrivateIPAddress($_POST["private_ip"])) { // success! } if (Util::validIPv6Network($_POST["ipv6_network"]), 56) { // success! }
miken32/network-rules 适用场景与选型建议
miken32/network-rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.9k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2023 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「networking」 「ip-address」 「subnets」 「ip-networking」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 miken32/network-rules 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 miken32/network-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 miken32/network-rules 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDRAM.
Retry tasks that fail due to transient faults
Lightweight TCP, UDP, TLS and SSL socket server/client toolkit for PHP 8.1+.
Adds request-parameter validation to the SLIM 3.x PHP framework
Quickly retrieve any type of DNS record you wish using PHP
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 74.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: AGPL-3.0
- 更新时间: 2023-10-17