ColdFusion isIPInRange()UDF支持IPv4,IPv6,CIDR和Regex

在我们的Web应用程序框架意外将CDN / WAF提供程序列入黑名单之后,今天早上我收到了有关502服务器错误的报告。 我想通过将其IP列入白名单来确保不会再次发生这种情况,但是它们具有许多IP,并且其数据仅以IPv4和IPv6 CIDR表示法可用。

除了确定当前主机的IP之外,ColdFusion没有任何内置的IP相关工具...因此,我开始寻找潜在的现有解决方案。

I checked CFDocs and discovered that there was an is一世PInRange function (without any examples), but it was a Lucee-only function. The Lucee function accepts either a comma-separated list or an array of IP definitions, but doesn't support CIDR or Regex.

I checked CFLib and discovered that there was a is一世PInRange UDF with the same name from 2005 that used a list of regex values, but it didn't support CIDR or dash-delimited range (well, unless you wanted to write the regex for that).

I had been using a method that Anjo Gakhar blogged about in 2008 that demonstrates the use of the undocumented coldfusion.util.一世PAddressUtils class to identify whether a string is a valid IPv4 or IPv6 IP address or not.

I then found a 2018 blog post by Ben Nadel documenting his implementation of using Commons 一世P Math to check if an IPv4 or IPv6 IP exists in a CIDR range, but it is dependent on separate java library (MIT) called Commons 一世P Math. The code example also only supported a single CIDR range.

由于现有解决方案都无法完全满足我的需求,因此我决定编写自己的ColdFusion UDF,并结合所有方面的最佳功能。

  • Performs IPv4 & IPv6 validation
  • Supports IPv4 & IPv6 CIDR range notation
  • Supports list or array of ranges
  • Supports regex range rules
  • Supports dash-delimited IP address range
  • Supports single IP (versus a range)
  • Requires Commons IP Math JAR File

由于我所有的Web应用程序都位于CDN / WAF的后面,并且如果将CDN列入黑名单,可能会受到负面影响,所以我认为最好将JAR添加到全局路径而不是使用Javaloader。 我也没有像Ben那样将其写为独立的CFC。 我的大多数项目都使用全局UDF库,这是最简单的集成方法,因为我不必手动更新每个项目。

Source Code

Here's a link to the gist. Due to the JAR requirement, this unfortunately can't be demoed using TryCF.


[ https://gist.github.com/JamoCA/27cf5307d7b8854c62539fdeebbea51f ]

from: https://dev.to//gamesover/coldfusion-isipinrange-udf-to-support-ipv4-ipv6-cidr-regex-212h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值