Last active
November 6, 2024 10:53
-
-
Save emnopal/ed5e55e00b7e561c15e961eaa5e9e219 to your computer and use it in GitHub Desktop.
Revisions
-
emnopal revised this gist
Nov 6, 2024 . 1 changed file with 2 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 @@ -5,9 +5,7 @@ * */ const isInternalIP = (ip?: string): boolean => { if (!ip || ip.trim() === "") return false; const ipv4InternalRegex = /^(?:0\.0\.0\.0|10(?:\.\d{1,3}){3}|127(?:\.\d{1,3}){3}|169\.254\.(?:[1-9]|1\d{1,2}|2[0-4]\d|25[0-4])\.\d{1,3}|172\.(1[6-9]|2\d|3[01])(?:\.\d{1,3}){2}|192\.168(?:\.\d{1,3}){2})$/; const ipv6InternalRegex = /^(?:fc[0-9a-f]{2}(?::[\da-f]{1,4}){0,7}|fe[89ab][\da-f]{2}(?::[\da-f]{1,4}){0,7})$/i; return ipv4InternalRegex.test(ip) || ipv6InternalRegex.test(ip); @@ -21,9 +19,7 @@ const isInternalIP = (ip?: string): boolean => { * */ const isIPValid = (ip?: string): boolean => { if (!ip || ip.trim() === "") return false; const ipv4Regex = /^(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\/([0-9]|[1-2]\d|3[0-2]))?$/; const ipv6Regex = /^((?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}|((?:[a-fA-F0-9]{1,4}:){1,7}:|:(:[a-fA-F0-9]{1,4}){1,7}|((?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4})|((?:[a-fA-F0-9]{1,4}:){1,5}(:[a-fA-F0-9]{1,4}){1,2})|((?:[a-fA-F0-9]{1,4}:){1,4}(:[a-fA-F0-9]{1,4}){1,3})|((?:[a-fA-F0-9]{1,4}:){1,3}(:[a-fA-F0-9]{1,4}){1,4})|((?:[a-fA-F0-9]{1,4}:){1,2}(:[a-fA-F0-9]{1,4}){1,5})|([a-fA-F0-9]{1,4}:((:[a-fA-F0-9]{1,4}){1,6}))))(\/([0-9]|[1-9]\d|1[0-1]\d|12[0-8]))?$/i; return ipv4Regex.test(ip) || ipv6Regex.test(ip); -
emnopal revised this gist
Nov 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 @@ -31,5 +31,5 @@ const isIPValid = (ip?: string): boolean => { export { isInternalIP, isIPValid } -
emnopal revised this gist
Nov 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 @@ -20,7 +20,7 @@ const isInternalIP = (ip?: string): boolean => { * like 10.10.10.1/24 or ::ffff:a0a:a01/24 or without CIDR like 10.10.10.1 or ::ffff:a0a:a01 * */ const isIPValid = (ip?: string): boolean => { if (!ip || ip.trim() === "") { return false; } -
emnopal revised this gist
Nov 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 @@ -25,7 +25,7 @@ const validateIP = (ip?: string): boolean => { return false; } const ipv4Regex = /^(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\/([0-9]|[1-2]\d|3[0-2]))?$/; const ipv6Regex = /^((?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}|((?:[a-fA-F0-9]{1,4}:){1,7}:|:(:[a-fA-F0-9]{1,4}){1,7}|((?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4})|((?:[a-fA-F0-9]{1,4}:){1,5}(:[a-fA-F0-9]{1,4}){1,2})|((?:[a-fA-F0-9]{1,4}:){1,4}(:[a-fA-F0-9]{1,4}){1,3})|((?:[a-fA-F0-9]{1,4}:){1,3}(:[a-fA-F0-9]{1,4}){1,4})|((?:[a-fA-F0-9]{1,4}:){1,2}(:[a-fA-F0-9]{1,4}){1,5})|([a-fA-F0-9]{1,4}:((:[a-fA-F0-9]{1,4}){1,6}))))(\/([0-9]|[1-9]\d|1[0-1]\d|12[0-8]))?$/i; return ipv4Regex.test(ip) || ipv6Regex.test(ip); } -
emnopal created this gist
Nov 6, 2024 .There are no files selected for viewing
This file contains hidden or 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,35 @@ /* * * Validate Internal / Private IP both IPv4 and IPv6 * */ const isInternalIP = (ip?: string): boolean => { if (!ip || ip.trim() === "") { return false; } const ipv4InternalRegex = /^(?:0\.0\.0\.0|10(?:\.\d{1,3}){3}|127(?:\.\d{1,3}){3}|169\.254\.(?:[1-9]|1\d{1,2}|2[0-4]\d|25[0-4])\.\d{1,3}|172\.(1[6-9]|2\d|3[01])(?:\.\d{1,3}){2}|192\.168(?:\.\d{1,3}){2})$/; const ipv6InternalRegex = /^(?:fc[0-9a-f]{2}(?::[\da-f]{1,4}){0,7}|fe[89ab][\da-f]{2}(?::[\da-f]{1,4}){0,7})$/i; return ipv4InternalRegex.test(ip) || ipv6InternalRegex.test(ip); } /* * * Validate IP both IPv4 and IPv6 either with CIDR notation or without it * like 10.10.10.1/24 or ::ffff:a0a:a01/24 or without CIDR like 10.10.10.1 or ::ffff:a0a:a01 * */ const validateIP = (ip?: string): boolean => { if (!ip || ip.trim() === "") { return false; } const ipv4Regex = /^(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.(25[0-5]|(2[0-4]|1\d|[1-9]|)\d)(\/([0-9]|[1-2]\d|3[0-2]))?$/; const ipv6Regex = /^((?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}|((?:[a-fA-F0-9]{1,4}:){1,7}:|:(:[a-fA-F0-9]{1,4}){1,7}|((?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4})|((?:[a-fA-F0-9]{1,4}:){1,5}(:[a-fA-F0-9]{1,4}){1,2})|((?:[a-fA-F0-9]{1,4}:){1,4}(:[a-fA-F0-9]{1,4}){1,3})|((?:[a-fA-F0-9]{1,4}:){1,3}(:[a-fA-F0-9]{1,4}){1,4})|((?:[a-fA-F0-9]{1,4}:){1,2}(:[a-fA-F0-9]{1,4}){1,5})|([a-fA-F0-9]{1,4}:((:[a-fA-F0-9]{1,4}){1,6}))))(\/([0-9]|[1-9]\d|1[0-1]\d|12[0-8]))?$/; return ipv4Regex.test(ip) || ipv6Regex.test(ip); } export { isInternalIP, validateIP }