Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ac-ip

admiralcloud253MIT4.1.3

This is a little helper for IP and network operations.

readme

AC IP

This is a little helper for IP and network operations.

Node.js CI

BREAKING CHANGE Version 4

  • package ip is no longer maintained, therefore we now use ip-address instead
  • all functions are synchronous
  • all errors are thrown and no longer returned as string, make sure to handle them in your code (try/catch)

Usage

const acts = require('ac-ip')

determineIP

Determines the IP from the (ExpressJS) request object

const ip = acts.determineIP(req) 
// -> 1.2.3.4

Use X-AdmiralCloud-Test "true" to overwrite the IP with params.ip from request object.

Otherwise IP is determined from X-Forwarded-For (if present) or req.ip.

AWS Environment

If you are in an AWS environment, the client ip is added to the right of list by ALB. In this you might want to set environment variable X-Forwarded-For to "reverse".

ipsFromCIDR

Ingests a cidr and returns a list of valid IP addresses for the cidr.

const list = acts.ipsFromCIDR({ cidr: '192.168.10.0/29' })

const list = acts.ipsFromCIDR({ cidr: '2001:db8::/120' })

checkCIDR

Ingests a cidr array, optional ip and noMatchAllowed.

// If no ip is given, the function checks if all cidr in array are valid

acts.checkCIDR({ cidr: [{ cidr: '192.168.10.200/32' }] })
// return true, if all are valid 
// throws an error if one cidr is invalid
// If  ip is given, the function checks if the ip is in range of cidr

acts.checkCIDR({ 
  cidr: [{ cidr: '192.168.10.0/29' }], 
  ip: 192.168.10.1 
})
// return true, if ip is in range of cidr
// throws an error if ip is not in range

acts.checkCIDR({ 
  cidr: [{ cidr: '192.168.10.0/29' }], 
  ip: 192.168.10.1, 
  noMatchAllowed: true 
})
// return true, if ip is in range of cidr
// returns null, if ip is not in range

Breaking changes:

  • checkCIDR returns true instead of undefined if CIDRs are valid

ipsToPrivacy

Ingests a list of IPs an return them anonymized (see anonymizeIP) and GDPR ready. Invalid IPs are ignores and not returned.

const privacyIP = acts.ipsToPrivacy('1.2.3', '8.8.8.8', '2001:db8:85a3:7942:1a2f:3e4c:7890:5def'])
// -> ['8.8.x.x', ''2001:db8:85a3:7942:x:x:x:x'']

Breaking changes: This function worked with IPv4 only in version < 4.

anonymizeIP

Anonymize single IP addresses (IPv4 or IPv6 addresses). If you send an invalid IP address the function returns undefined.

const anonymizedIP = acts.anonymizeIP('1.2.3.4') -> 1.2.x.x
const anonymizedIP = acts.anonymizeIP('2001:4860:4860::8888') -> 2001:4860:4860:x:x

// optional replacement
const anonymizedIP = acts.anonymizeIP('1.2.3.4', { replacement: 0 }) -> 1.2.0.0

isPrivateIP

Checks is a function is a private IP. Please checkout isSpecialIP function - it is more generic.

const isPrivate = acts.isPrivate('1.2.3.4')
// -> false

const isPrivate = acts.isPrivate('127.0.0.1')
// -> true

Breaking change: Function is now isPrivateIP insteand of isPrivate.

isSpecialIP

Check if given IP is a special IP (e.g. private, loopback, link-local, etc)

const isSpecial = acts.isSpecial('127.0.0.1') // true
const isSpecial = acts.isSpecial('8.8.8.8') // false

Deprecated functions

Function ipInIPList no longer exists. Use checkCIDR instead.

Error codes

All errors have a message, but messages can change. Therefor all error messages now also have an error code:

Code Message
9000 acip_determineIP_noIPDetected
9001 acip_checkCIDR_listIsEmpty
9002 acip_checkCIDR_ipNotInCIDRrange
9003 acip_checkCIDR_cidrIsNotValid
9004 acip_checkCIDR_thisIsNoCIDR
9005 acip_checkCIDR_maskInvalid
9006 acip_checkCIDR_invalid
9007 acip_checkCIDR_maskInvalid

Nginx

If you have a node application behind an NGINX proxy (which is recommended) and this NGINX proxy behind another proxy (e.g. AWS load balancer) use a config like this:

set_real_ip_from 172.0.0.0/16; // range from AWS Load balancer 
real_ip_header X-Forwarded-For;
real_ip_recursive on;

server {
    listen 80 default_server;
    listen [::]:80 default_server;

  location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto 'https'; #$scheme;
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $http_host;
        proxy_read_timeout 300;
    }
}

License

MIT License Copyright © 2009-present, AdmiralCloud AG, Mark Poepping

changelog

4.1.3 (2025-03-17 19:40:38)

Bug Fix

4.1.2 (2024-07-24 09:12:53)

Bug Fix

4.1.1 (2024-07-06 19:10:34)

Bug Fix

4.1.0 (2024-07-06 18:43:59)

Feature

4.0.0 (2024-07-06 12:06:03)

Feature

3.1.1 (2024-02-26 07:32:26)

Bug Fix

3.1.0 (2023-07-26 12:09:22)

Feature

3.0.2 (2023-07-26 06:19:08)

Bug Fix

3.0.1 (2023-01-02 20:23:00)

Manual release after package updates.

3.0.0 (2022-12-18 09:58:28)

Tests

2.0.0 (2022-02-17 11:16:58)

Bug Fix

1.3.7 (2022-02-10 20:06:59)

Bug Fix

1.3.6 (2021-10-09 10:12:28)

Bug Fix

1.3.5 (2021-05-08 07:22:35)

Bug Fix

1.3.4 (2020-05-12 08:50:47)

Bug Fix

1.3.3 (2020-04-03 10:14:20)

Bug Fix

1.3.2 (2020-04-01 09:10:16)

Bug Fix

  • App: Use statusCode 420 in message if ip is not in range | MP | 995b22dd92a4914fc6272a5e24af588e3c328982
    IP no in range is not typcical error, but more like a preconditon fail. That's why we now use statusCode 420 in addition to the error message. This way you can decide whether you want to treat it as error or as warning.

1.3.1 (2020-03-29 14:04:57)

Bug Fix

Features

1.2.0 (2019-11-08 10:32)

Features

1.1.4 (2019-10-06 12:53)

Bug Fixes

  • Misc: Send IP as parameter if not in production mode | MP (2a5725c)
    For easier debugging it is now possible to just send the IP address as query parameter.

1.1.3 (2019-10-06 09:15)

Bug Fixes

  • Misc: Updated packages / Security fix | MP (8e4c21c)
    Updated packages / Security fix

1.1.2 (2019-07-24 19:28)

Bug Fixes

  • Misc: Updated packages | MP (8cec625)
    Updated packages

1.1.1 (2019-07-03 15:56)

Bug Fixes

  • Misc: Return unique array for ipsToPrivacy | MP (c51318f)
    Make sure to group ips

1.1.0 (2019-07-03 15:20)

Features

  • Misc: New functions to make IPs GDPR ready and to check IPs against a list of IPs | MP (40c2e24)
    New functions to make IPs GDPR ready and to check IPs against a list of IPs

1.0.4 (2019-06-09 17:16)

Bug Fixes

  • Misc: Do not user req.allParams | MP (57e5e4e)
    req.allParams is not available for non-sails APIs

1.0.3 (2019-04-09 16:22)

Bug Fixes

  • Misc: Allow custom IP with X-AdmiralCloud-Test header | MP (89cc08c)
    If you want to set a custom IP for testing or development, use X-AdmiralCloud-Test header as true and send ip with request params.

1.0.2 (2019-01-20 10:54)

Bug Fixes

  • Misc: Minor code cleanup | MP (a65b674)
    Minor code cleanup

1.0.1 (2018-09-22 10:27)

Bug Fixes

  • Misc: Updated packages | MP (6c6014d)
    Updated packages

1.0.0 (2018-08-22 15:39)

Features

  • Misc: Added methods for IP detection and IPs from CIDR | MP (64377dc)
    • Detect IPs from Express-like request object
  • Misc: First commit | MP (617d724)
    First commit