netglue / realip-helpers
用于检测真实客户端IP的PSR-15中间件,以及其他辅助工具。
Requires
- php: >=7.3
- psr/container: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- doctrine/coding-standard: ^8
- laminas/laminas-diactoros: ^2.2
- laminas/laminas-modulemanager: ^2.8
- laminas/laminas-mvc: ^3.1
- phpstan/phpstan: ^0
- phpunit/phpunit: ^9
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3
Suggests
- laminas/laminas-diactoros: Install a PSR-15 middleware implementation
- laminas/laminas-mvc: Install Laminas MVC in order to use this with a ZF3 app
This package is auto-updated.
Last update: 2024-08-29 04:50:18 UTC
README
为什么?
我整理了这个包来满足自己的需求 - 我通常使用Mezzio或Laminas MVC(以前称为Expressive和Zend Framework)编写服务器端应用程序,并且已经有一个现有的中间件实现akrabat/ip-address-middleware,它可以解决从PSR-7/PSR-15中间件中获取远程地址的问题。
我对该包的问题在于,它需要一个可信代理的数组来搜索这些头,所以如果你的应用程序位于负载均衡器后面,它的IP地址可能会变化,这意味着更新你的配置是一个痛苦的过程,尤其是如果你在同一环境中使用多个项目中的组件。此外,这个包引入了可信头部的概念,即如果你有一个可以保证发送包含远程地址的头的上游代理,并且你确信你的代理无法被绕过,那么你可以忽略可信代理的概念,只需使用该头。此外,我还希望中间件和辅助工具有默认配置和DI工厂的便利性。你也可以看看这个:middlewares/client-ip。
安装
使用Composer安装 "netglue/realip-helpers"
配置
在没有任何额外配置的情况下,辅助工具将简单地返回 $_SERVER['REMOTE_ADDR']
报告的内容(但会进行清理和验证,以确保它是一个实际的IP地址)。你之所以查看这个,可能是因为 REMOTE_ADDR
并不足以满足需求……选项在 ./src/ConfigProvider.php
中有详细说明,以下也进行了重复。这些值是默认值。
return [ 'proxy_headers' => [ // When figuring out the client IP, should common proxy headers be checked? 'checkProxyHeaders' => false, // If your app is firewalled, and you're sure you can trust that, say, // Cloud Flare is sending you the client IP in the header 'CF-Connecting-IP', you can add that here // and it will always be used 'trustedHeader' => null, // If your app is on a private network and REMOTE_ADDR is always the load balancer ip, you could say // that REMOTE_ADDR is always a trusted proxy 'remoteAddressIsTrustedProxy' => false, // You can provide an array of IP addresses (v4 or v6) of proxies that you trust. These will be eliminated as // potential client IP addresses 'trustedProxies' => [], // If you provide a non-empty array of proxy headers to inspect, only these headers will be checked, // overriding the defaults. If you know that your proxy/loadbalancer only sends X-Forwarded-For, you could // put just that one in here. By default, a number of headers are inspected: 'proxyHeadersToInspect' => [], ], ];
MVC使用
如果你使用Laminas组件安装器,配置将自动注入,因此你只需根据需要更改环境中的配置值,然后从控制器方法中发出 $ip = $this->clientIp()
以获取远程IP地址。
Mezzio使用
将 NetglueRealIP\Middleware\IpAddress::class
添加到你的管道中,你的请求将包含属性 'ip_address'
,即 $request->getAttribute('ip_address')
。如果你不使用DI容器,或者在DI容器中使用,你可以在构建时更改属性名称,或者通过将类别名到不同的工厂来更改。
测试
转到模块安装的位置,执行 composer install
然后执行 composer test
。
贡献
欢迎PR。请为新功能编写测试。
支持
你可以提交问题,但请理解,找到时间来回答支持请求非常有限,所以可能需要很长时间才能得到回复。
关于
Netglue在英国德文郡制作网站和应用程序。我们希望这对你有所帮助,我们非常欢迎任何反馈 :)