Skip to content

Instantly share code, notes, and snippets.

@emnopal
Last active November 6, 2024 10:53
Show Gist options
  • Select an option

  • Save emnopal/ed5e55e00b7e561c15e961eaa5e9e219 to your computer and use it in GitHub Desktop.

Select an option

Save emnopal/ed5e55e00b7e561c15e961eaa5e9e219 to your computer and use it in GitHub Desktop.

Revisions

  1. emnopal revised this gist Nov 6, 2024. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions ipv46validation.ts
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,7 @@
    *
    */
    const isInternalIP = (ip?: string): boolean => {
    if (!ip || ip.trim() === "") {
    return false;
    }
    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;
    }
    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);
  2. emnopal revised this gist Nov 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ipv46validation.ts
    Original file line number Diff line number Diff line change
    @@ -31,5 +31,5 @@ const isIPValid = (ip?: string): boolean => {

    export {
    isInternalIP,
    validateIP
    isIPValid
    }
  3. emnopal revised this gist Nov 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ipv46validation.ts
    Original 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 validateIP = (ip?: string): boolean => {
    const isIPValid = (ip?: string): boolean => {
    if (!ip || ip.trim() === "") {
    return false;
    }
  4. emnopal revised this gist Nov 6, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ipv46validation.ts
    Original 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]))?$/;
    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);
    }

  5. emnopal created this gist Nov 6, 2024.
    35 changes: 35 additions & 0 deletions ipv46validation.ts
    Original 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
    }