akrabat/ip-address-middleware 问题修复 & 功能扩展

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

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

akrabat/ip-address-middleware

Composer 安装命令:

composer require akrabat/ip-address-middleware

包简介

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

README 文档

README

PSR-15 Middleware that determines the client IP address and stores it as an ServerRequest attribute called ip_address. It optionally checks various common proxy headers and then falls back to $_SERVER['REMOTE_ADDR'].

Installation

Install via Composer:

composer require akrabat/ip-address-middleware

Configuration

The constructor takes 5 parameters which can be used to configure this middleware.

Check proxy headers

The proxy headers are only checked if the first parameter to the constructor is set to true. If it is set to false, then only $_SERVER['REMOTE_ADDR'] is used.

Trusted Proxies

If you enable checking of the proxy headers (first parameter is true), you have to provide an array as the second parameter. This is the list of IP addresses (supporting wildcards) of your proxy servers. If the array is empty, the proxy headers will always be used and the selection is based on the hop count (parameter 5).

If the array is not empty, it must contain strings with IP addresses (wildcard * is allowed in any given part) or networks in CIDR-notation. One of them must match the $_SERVER['REMOTE_ADDR'] variable in order to allow evaluating the proxy headers - otherwise the REMOTE_ADDR itself is returned. This list is not ordered and there is no requirement that any given proxy header includes all the listed proxies.

Attribute name

By default, the name of the attribute is 'ip_address'. This can be changed by the third constructor parameter.

Headers to inspect

By default, this middleware checks the 'Forwarded', 'X-Forwarded-For', 'X-Forwarded', 'X-Cluster-Client-Ip' and 'Client-Ip' headers. You can replace this list with your own using the fourth constructor parameter.

If you use the nginx, set_real_ip_from directive, then you should probably set this to:

$headersToInspect = [
    'X-Real-IP',
    'Forwarded',
    'X-Forwarded-For',
    'X-Forwarded',
    'X-Cluster-Client-Ip',
    'Client-Ip',
];

If you use CloudFlare, then according to the documentation you should probably set this to:

$headersToInspect = [
    'CF-Connecting-IP',
    'True-Client-IP',
    'Forwarded',
    'X-Forwarded-For',
    'X-Forwarded',
    'X-Cluster-Client-Ip',
    'Client-Ip',
];

hop count

Set this to the number of known proxies between ingress and the application. This is used to determine the number of proxies to check in the X-Forwarded-For header, and is generally used when the IP addresses of the proxies cannot be reliably determined. The default is 0.

Security considerations

A malicious client may send any header to your proxy, including any proxy headers, containing any IP address. If your proxy simply adds another IP address to the header, an attacker can send a fake IP. Make sure to setup your proxy in a way that removes any sent (and possibly faked) headers from the original request and replaces them with correct values (i.e. the currently used REMOTE_ADDR on the proxy server).

This library cannot by design ensure you get correct and trustworthy results if your network environment isn't setup properly.

Installation

composer require akrabat/ip-address-middleware

In Mezzio, copy Mezzio/config/ip_address.global.php.dist into your Mezzio Application config/autoload directory as ip_address.global.php

Usage

In Slim:

$checkProxyHeaders = true; // Note: Never trust the IP address for security processes!
$trustedProxies = ['10.0.0.1', '10.0.0.2']; // Note: Never trust the IP address for security processes!
$app->add(new RKA\Middleware\IpAddress($checkProxyHeaders, $trustedProxies));

$app->get('/', function ($request, $response, $args) {
    $ipAddress = $request->getAttribute('ip_address');

    return $response;
});

In Laminas or Mezzio, add to your pipeline.php config at the correct stage, usually just before the DispatchMiddleware:

# config/pipeline.php
# using default config
$app->add(RKA\Middleware\IpAddress::class);

If required, update your .env file with the environmental variables found in /config/autoload/ip_address.global.php.

Testing

  • Code style: $ vendor/bin/phpcs
  • Fix style: $ vendor/bin/phpcbf
  • Unit tests: $ vendor/bin/phpunit
  • Code coverage: $ vendor/bin/phpunit --coverage-html ./build

You can also use Composer scripts:

  • Check both: $ composer check
  • Code style: $ composer cs
  • Fix style: $ composer cs-fix
  • Unit tests: $ composer test

akrabat/ip-address-middleware 适用场景与选型建议

akrabat/ip-address-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.61M 次下载、GitHub Stars 达 169, 最近一次更新时间为 2018 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 akrabat/ip-address-middleware 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.61M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 172
  • 点击次数: 24
  • 依赖项目数: 21
  • 推荐数: 0

GitHub 信息

  • Stars: 169
  • Watchers: 10
  • Forks: 38
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-02-18