Last active
January 26, 2023 19:12
Revisions
-
JamoCA revised this gist
Jan 26, 2023 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -70,14 +70,16 @@ boolean function isIPInRange(any ips, required ip) hint="Checks if an ip is with if (!structkeyexists(request, "isIPInRange_Ipv6")){ request.isIPInRange_Ipv6 = createobject("java","com.github.jgonian.ipmath.Ipv6"); } if (local.ipVersion eq 6){ local.parsedIP = request.isIPInRange_Ipv6.parse( javacast("string", trim(arguments.ip)) ); } else if (local.ipVersion eq 4){ local.parsedIP = request.isIPInRange_Ipv4.parse( javacast("string", trim(arguments.ip)) ); } else { return local.response; } if (!structkeyexists(request, "isIPInRange_Ipv4Range")){ request.isIPInRange_Ipv4Range = createobject("java","com.github.jgonian.ipmath.Ipv4Range"); request.isIPInRange_Ipv6Range = createobject("java","com.github.jgonian.ipmath.Ipv6Range"); } for (local.thisRange in local.ipRange) { if (trim(local.thisRange).replaceall("/32$","") eq trim(arguments.ip)) { /* single IP */ -
JamoCA revised this gist
Jan 26, 2023 . 1 changed file with 122 additions and 74 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,112 +1,160 @@ <!--- Blog Entry: https://dev.to/gamesover/coldfusion-isipinrange-udf-to-support-ipv4-ipv6-cidr-regex-212h ---> <!--- 20200109 getIPVersionACF() Inspired by https://www.anujgakhar.com/2008/02/21/validate-ip-address-natively-with-coldfusion/ Uses built-in undocumented coldfusion.util.IPAddressUtils, so this may not work on Lucee without this function being rewritten ---> <!--- 20230126 getIPVersion() Inspired by https://docs.lucee.org/reference/functions/isipinrange.html Requires JAR https://github.com/jgonian/commons-ip-math (Add to existing java path (recommended) or modify to use javaloader) ---> <!--- 20200109 isIPInRange() Inspired by https://www.bennadel.com/blog/3503-using-commons-ip-math-to-check-if-an-ip-address-exists-in-an-ipv4-or-ipv6-cidr-range-in-coldfusion.htm Inspired by https://cflib.org/udf/isIPInRange Inspired by https://docs.lucee.org/reference/functions/isipinrange.html Requires JAR https://github.com/jgonian/commons-ip-math (Add to existing java path (recommended) or modify to use javaloader) ---> <cfscript> numeric function getIPVersionACF(required string ip) hint="identifies whether string represents an IPv4 or IPv6 address; returns 0 if invalid (Adobe ColdFusion Only)" { local.response = 0; if (!structkeyexists(request, "getIPVersion_IPAddressUtils")){ request.getIPVersion_IPAddressUtils = createobject("java","coldfusion.util.IPAddressUtils"); } if (request.getIPVersion_IPAddressUtils.validateIPv4Address( javacast("string", trim(arguments.ip)) )){ local.response = 4; } else if (request.getIPVersion_IPAddressUtils.validateIPv6Address( javacast("string", trim(arguments.ip)) )){ local.response = 6; } return javacast("int", local.response); } numeric function getIPVersion(required string ip) hint="identifies whether string represents an IPv4 or IPv6 address; returns 0 if invalid" { local.response = 0; arguments.ip = javacast("string", arguments.ip); if (find(".", arguments.ip) && listlen(arguments.ip,".") eq 4){ if (!structkeyexists(request, "isIPInRange_Ipv4")){ request.isIPInRange_Ipv4 = createobject("java","com.github.jgonian.ipmath.Ipv4"); } try { local.test = request.isIPInRange_Ipv4.parse(arguments.ip); local.response = 4; } catch (any e) {} } else if (find(":", arguments.ip)){ if (!structkeyexists(request, "isIPInRange_Ipv6")){ request.isIPInRange_Ipv6 = createobject("java","com.github.jgonian.ipmath.Ipv6"); } try { local.test = request.isIPInRange_Ipv6.parse(arguments.ip); local.response = 6; } catch (any e) {} } return javacast("int", local.response); } boolean function isIPInRange(any ips, required ip) hint="Checks if an ip is within the range of a list or array of ips; supports regex & CIDR notation for ranges" { local.ipVersion = getIPVersion(arguments.ip); local.response = javacast("boolean", false); if (!val(local.ipVersion)){ return local.response; } else if (isarray(arguments.ips)){ local.ipRange = arguments.ips; } else if (issimplevalue(arguments.ips)){ local.ipRange = listtoarray(arguments.ips); } else { throw(type="Custom", message="ips must be comma-delimited list or array"); } if (!structkeyexists(request, "isIPInRange_Ipv4")){ request.isIPInRange_Ipv4 = createobject("java","com.github.jgonian.ipmath.Ipv4"); } if (!structkeyexists(request, "isIPInRange_Ipv6")){ request.isIPInRange_Ipv6 = createobject("java","com.github.jgonian.ipmath.Ipv6"); } if (!structkeyexists(request, "isIPInRange_Ipv4Range")){ request.isIPInRange_Ipv4Range = createobject("java","com.github.jgonian.ipmath.Ipv4Range"); request.isIPInRange_Ipv6Range = createobject("java","com.github.jgonian.ipmath.Ipv6Range"); } if (local.ipVersion eq 6){ local.parsedIP = request.isIPInRange_Ipv6.parse( javacast("string", trim(arguments.ip)) ); } else { local.parsedIP = request.isIPInRange_Ipv4.parse( javacast("string", trim(arguments.ip)) ); } for (local.thisRange in local.ipRange) { if (trim(local.thisRange).replaceall("/32$","") eq trim(arguments.ip)) { /* single IP */ local.response = true; break; /* regex */ } else if (refind("(?:\^|\[|\+|\*|\(|\$|\\\.)", local.thisRange)){ try { if (refindnocase(local.thisRange, arguments.ip)){ local.response = true; break; } } catch (any e){} /* compare w/IPv4 dash-delimited range */ } else if (local.ipVersion eq 4 && !find("/",local.thisRange) && listlen(local.thisRange,"-") eq 2 && getIPVersion(listfirst(local.thisRange,"-")) eq 4 && getIPVersion(listlast(local.thisRange,"-")) eq 4){ local.start = request.isIPInRange_Ipv4.of( javacast("string", trim(listfirst(local.thisRange,"-"))) ); local.end = request.isIPInRange_Ipv4.of( javacast("string", trim(listlast(local.thisRange,"-"))) ); local.range = request.isIPInRange_Ipv4Range.from(local.start).to(local.end); if (local.range.contains(local.parsedIP)){ local.response = true; break; } /* compare w/IPv6 dash-delimited range */ } else if (local.ipVersion eq 6 && !find("/",local.thisRange) && listlen(local.thisRange,"-") eq 2 && getIPVersion(listfirst(local.thisRange,"-")) eq 6 && getIPVersion(listlast(local.thisRange,"-")) eq 6){ local.start = request.isIPInRange_Ipv6.of( javacast("string", trim(listfirst(local.thisRange,"-"))) ); local.end = request.isIPInRange_Ipv6.of( javacast("string", trim(listlast(local.thisRange,"-"))) ); local.range = request.isIPInRange_Ipv6Range.from(local.start).to(local.end); if (local.range.contains(local.parsedIP)){ local.response = true; break; } /* compare w/CIDR range */ } else if (find("/", local.thisRange)){ structdelete(local, "CIDR"); if (local.ipVersion eq 4 && !find(":", local.thisRange)){ local.CIDR = request.isIPInRange_Ipv4Range.parse(local.thisRange); } else if (local.ipVersion eq 6 && find(":", local.thisRange)){ local.CIDR = request.isIPInRange_Ipv6Range.parse(local.thisRange); } if (structkeyexists(local, "CIDR")){ if (local.CIDR.contains(local.parsedIP)){ local.response = true; break; } } } } return javacast("boolean", local.response); } testRanges = [ "2a0a:e200:1600::/40" ,"209.197.31.0/25" ,"81.171.68.64/26" ,"95.138.175.4/32" ,"127\.0\.1\.*" ,"127.0.0.1-127.0.0.5" ]; testIPs = [ "209.197.31.25" ,"95.138.175.4" ,"81.171.68.124" ,"2a0a:e200:16ff:ffff:ffff:ffff:ffff:ffff" ,"127.0.1.4" ,"127.0.0.6" ,"127.0.0.15" ,"127.1.1.4" ,CGI.REMOTE_ADDR ,getLocalHostIP() ]; writedump(var=testRanges, label="Test Ranges"); writeoutput("<h2>isIPInRange() Results</h2>"); for (thisIP in testIPs){ writeoutput("<div><b>#thisIP# (v#getIPVersion(thisIP)#):</b> #yesnoformat(isIPInRange(TestRanges, thisIP))#</div>"); } </cfscript> -
JamoCA revised this gist
Jan 9, 2020 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,8 @@ <!--- Blog Entry: https://dev.to/gamesover/coldfusion-isipinrange-udf-to-support-ipv4-ipv6-cidr-regex-212h ---> <!--- 20200109 getIPVersion() Inspired by https://www.anujgakhar.com/2008/02/21/validate-ip-address-natively-with-coldfusion/ Uses built-in undocumented coldfusion.util.IPAddressUtils, so this may not work on Lucee without this function being rewritten ---> <!--- 20200109 isIPInRange() Inspired by https://www.bennadel.com/blog/3503-using-commons-ip-math-to-check-if-an-ip-address-exists-in-an-ipv4-or-ipv6-cidr-range-in-coldfusion.htm -
JamoCA created this gist
Jan 9, 2020 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ <!--- 20200109 getIPVersion() Inspired by https://www.anujgakhar.com/2008/02/21/validate-ip-address-natively-with-coldfusion/ ---> <!--- 20200109 isIPInRange() Inspired by https://www.bennadel.com/blog/3503-using-commons-ip-math-to-check-if-an-ip-address-exists-in-an-ipv4-or-ipv6-cidr-range-in-coldfusion.htm Inspired by https://cflib.org/udf/isIPInRange Inspired by https://docs.lucee.org/reference/functions/isipinrange.html Requires JAR https://github.com/jgonian/commons-ip-math (Add to existing java path (recommended) or modify to use javaloader) ---> <cfscript> function getIPVersion(required ip){ var response = 0; if (NOT StructKeyExists(Request, "getIPVersion_IPAddressUtils")){ Request.getIPVersion_IPAddressUtils = createObject("java","coldfusion.util.IPAddressUtils"); } if (Request.getIPVersion_IPAddressUtils.validateIPv4Address( javacast("string", trim(arguments.ip)) )){ response = 4; } else if (Request.getIPVersion_IPAddressUtils.validateIPv6Address( javacast("string", trim(arguments.ip)) )){ response = 6; } return response; } function isIPInRange(ips, required ip){ var temp = { ipRange = [], ipVersion = getIPVersion(arguments.ip), response = false }; if (NOT val(temp.ipVersion)){ return false; } else if (isArray(arguments.ips)){ temp.ipRange = arguments.ips; } else if (isSimpleValue(arguments.ips)){ temp.ipRange = listtoArray(arguments.ips); } else { throw(type="Custom", message="ips must be comma-delimited list or array"); } if (NOT StructKeyExists(Request, "isIPInRange_Ipv4")){ Request.isIPInRange_Ipv4 = createObject("java","com.github.jgonian.ipmath.Ipv4"); Request.isIPInRange_Ipv4Range = createObject("java","com.github.jgonian.ipmath.Ipv4Range"); Request.isIPInRange_Ipv6 = createObject("java","com.github.jgonian.ipmath.Ipv6"); Request.isIPInRange_Ipv6Range = createObject("java","com.github.jgonian.ipmath.Ipv6Range"); } if (temp.ipVersion IS 6){ temp.parsedIP = Request.isIPInRange_Ipv6.parse( javaCast("string", trim(arguments.ip)) ); } else { temp.parsedIP = Request.isIPInRange_Ipv4.parse( javaCast("string", trim(arguments.ip)) ); } for (temp.thisRange in temp.ipRange) { if (trim(temp.thisRange).replaceAll("/32$","") IS trim(arguments.ip)) { /* single IP */ temp.response = true; break; /* regex */ } else if (find("^", temp.thisRange) OR find("[", temp.thisRange) OR find("+", temp.thisRange) OR find("*", temp.thisRange) OR find("(", temp.thisRange) OR find("$", temp.thisRange) OR find("\.", temp.thisRange)){ try { if (REFindnocase(temp.thisRange, arguments.ip)){ temp.response = true; break; } } catch (any e){} /* compare w/IPv4 dash-delimited range */ } else if (temp.ipVersion IS 4 AND NOT find("/",temp.thisRange) AND find("-",temp.thisRange) AND ListLen(temp.thisRange,"-") IS 2 AND getIPVersion(ListFirst(temp.thisRange,"-")) IS 4 AND getIPVersion(ListLast(temp.thisRange,"-")) IS 4){ temp.start = Request.isIPInRange_Ipv4.of( javacast("string", trim(ListFirst(temp.thisRange,"-"))) ); temp.end = Request.isIPInRange_Ipv4.of( javacast("string", trim(ListLast(temp.thisRange,"-"))) ); temp.range = Request.isIPInRange_Ipv4Range.from(temp.start).to(temp.end); if (temp.range.contains(temp.parsedIP)){ temp.response = true; break; } /* compare w/IPv6 dash-delimited range */ } else if (temp.ipVersion IS 6 AND NOT find("/",temp.thisRange) AND find("-",temp.thisRange) AND ListLen(temp.thisRange,"-") IS 2 AND getIPVersion(ListFirst(temp.thisRange,"-")) IS 6 AND getIPVersion(ListLast(temp.thisRange,"-")) IS 6){ temp.start = Request.isIPInRange_Ipv6.of( javacast("string", trim(ListFirst(temp.thisRange,"-"))) ); temp.end = Request.isIPInRange_Ipv6.of( javacast("string", trim(ListLast(temp.thisRange,"-"))) ); temp.range = Request.isIPInRange_Ipv6Range.from(temp.start).to(temp.end); if (temp.range.contains(temp.parsedIP)){ temp.response = true; break; } /* compare w/CIDR range */ } else if (find("/",temp.thisRange)){ StructDelete(temp, "CIDR"); if (temp.ipVersion IS 4 AND NOT find(":",temp.thisRange)){ temp.CIDR = Request.isIPInRange_Ipv4Range.parse(temp.thisRange); } else if (temp.ipVersion IS 6 AND find(":",temp.thisRange)){ temp.CIDR = Request.isIPInRange_Ipv6Range.parse(temp.thisRange); } if (StructKeyExists(temp, "CIDR")){ if (temp.CIDR.contains(temp.parsedIP)){ temp.response = true; break; } } } } return temp.response; } </cfscript> <cfset TestRanges = ["2a0a:e200:1600::/40", "209.197.31.0/25", "81.171.68.64/26", "95.138.175.4/32", "127\.0\.1\.*", "127.0.0.1-127.0.0.5"]> <cfset TestIPs = ["209.197.31.25", "95.138.175.4", "81.171.68.124", "2a0a:e200:16ff:ffff:ffff:ffff:ffff:ffff", "127.0.1.4", "127.0.0.6", "127.0.0.15", "127.1.1.4", CGI.Remote_Addr, GetLocalHostIP()]> <cfoutput> <cfloop array="#TestIPs#" index="thisIP"> <div> #thisIP# = #YesNoFormat(isIPInRange(TestRanges, thisIP))# </div> </cfloop> </cfoutput>