1 | # Range Check v3.1.0
|
2 | [](https://badge.fury.io/js/range_check) [](https://travis-ci.org/keverw/range_check)
|
3 |
|
4 | This is a simple module to validate IP address, check IP address version, check if IP is within a range.
|
5 |
|
6 | This started out as `range_check` but it does much more than just checking ranges but since it's already got a large amount of downloads (37,115 downloads in the last month as of this writing) I'll keep the name the same even though I kinda want to change it to something better.
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | - [Install](#install)
|
13 | - [IP Functions](#ip-functions)
|
14 | - [Check if IP is valid](#check-if-ip-is-valid)
|
15 | - [Check IP version](#check-ip-version)
|
16 | - [Version](#version)
|
17 | - [isV4](#isv4)
|
18 | - [isV6](#isv6)
|
19 | - [Range Functions](#range-functions)
|
20 | - [Check if range is valid](#check-if-range-is-valid)
|
21 | - [Check if IP is within range](#check-if-ip-is-within-range)
|
22 | - [Check if IP is private](#check-if-ip-is-private)
|
23 | - [Check if IP is in range or private](#check-if-ip-is-in-range-or-private)
|
24 | - [storeIP](#storeip)
|
25 | - [searchIP](#searchip)
|
26 | - [displayIP](#displayip)
|
27 | - [Contributing](#contributing)
|
28 | - [Dependencies](#dependencies)
|
29 |
|
30 |
|
31 |
|
32 | ## Install
|
33 |
|
34 | `npm install range_check` or `yarn add range_check`
|
35 |
|
36 | You can then import the functions as needed or require the entire `range_check` package depending on your own projects configuration.
|
37 |
|
38 | ## IP Functions
|
39 | ### Check if IP is valid
|
40 |
|
41 | ```typescript
|
42 | console.log(isIP('10.0.1.5')); //returns true or false
|
43 | ```
|
44 |
|
45 | ### Check IP version
|
46 | #### Version
|
47 | ```typescript
|
48 | console.log(version('10.0.1.5')); //returns 4
|
49 | console.log(version('2001:4860:8006::62')); //returns 6
|
50 | console.log(version('foo')); //returns 0 as invalid IP address
|
51 | ```
|
52 |
|
53 | #### isV4
|
54 | ```typescript
|
55 | console.log(isV4('10.0.1.5')); //true
|
56 | console.log(isV4('foo')); //false
|
57 | console.log(isV4('123::123')); //false
|
58 | ```
|
59 |
|
60 | #### isV6
|
61 | ```typescript
|
62 | console.log(isV6('123::123')); //true
|
63 | console.log(isV6('foo')); //false
|
64 | console.log(isV6('10.0.1.5')); //false
|
65 | ```
|
66 |
|
67 | ## Range Functions
|
68 |
|
69 | ### Check if range is valid
|
70 |
|
71 | You can use isRange if you want to validate an entire range.
|
72 |
|
73 | ```typescript
|
74 | console.log(isRange('2001:db8::/32')); //true
|
75 | console.log(isRange('10.0.0.0/8')); // true
|
76 | console.log(isRange('qwerty')); // false
|
77 |
|
78 | ```
|
79 |
|
80 | ### Check if IP is within range
|
81 | ```typescript
|
82 | console.log(inRange('10.0.1.5', '10.0.0.0/8')); //returns true
|
83 |
|
84 | console.log(inRange('192.0.1.5', '10.0.0.0/8')); //returns false
|
85 |
|
86 | console.log(inRange('2001:db8:1234::1', '2001:db8::/32')); //returns true
|
87 | ```
|
88 |
|
89 | You can also give a list of ranges
|
90 |
|
91 | ```typescript
|
92 | console.log(inRange('192.168.1.1', ['10.0.0.0/8', '192.0.0.0/8'])); //returns true
|
93 |
|
94 | ```
|
95 |
|
96 | ### Check if IP is private
|
97 | ```typescript
|
98 | console.log(isPrivateIP('10.0.0.1')); //returns true
|
99 | console.log(isPrivateIP('192.168.1.1')); //returns true
|
100 | console.log(isPrivateIP('172.16.0.1')); //returns true
|
101 | console.log(isPrivateIP('8.8.8.8')); //returns false
|
102 | console.log(isPrivateIP('fd00::1')); //returns true (IPv6 ULA)
|
103 | console.log(isPrivateIP('2001:db8::1')); //returns false
|
104 | ```
|
105 |
|
106 | This function checks if an IP address is private. It returns true for:
|
107 | - IPv4 private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16)
|
108 | - IPv4 loopback addresses (127.0.0.0/8)
|
109 | - IPv6 unique local addresses (fd00::/8)
|
110 | - IPv6 loopback address (::1)
|
111 |
|
112 | ### Check if IP is in range or private
|
113 | ```typescript
|
114 | console.log(isIPInRangeOrPrivate('192.168.1.1')); // returns true (private IP)
|
115 | console.log(isIPInRangeOrPrivate('8.8.8.8')); // returns false (public IP, no range specified)
|
116 | console.log(isIPInRangeOrPrivate('8.8.8.8', { ranges: '8.8.8.0/24' })); // returns true
|
117 | console.log(isIPInRangeOrPrivate('10.0.0.1', { allowAnyPrivate: false, ranges: '8.8.8.0/24' })); // returns false
|
118 | ```
|
119 |
|
120 | This function checks if an IP address is either within a specified range or is a private IP. It's particularly useful for scenarios where you need to determine if a request is coming from a local server or a specific set of allowed IPs.
|
121 |
|
122 | Options:
|
123 | - `ranges`: A string or array of strings representing IP ranges to check against.
|
124 | - `allowAnyPrivate`: Boolean to determine if any private IP should be allowed. Defaults to true.
|
125 |
|
126 | If no options are provided, the function will return true for any private IP and false for public IPs.
|
127 |
|
128 | Use case example: This function can be used in server configurations to easily allow local calls or calls from specific IP ranges, while blocking others. For instance, it can be used in middleware for setting trace IDs. This allows you to automatically set trace IDs for requests from private networks or specific IP ranges, which can be useful for debugging and tracking requests across microservices in a distributed system.
|
129 |
|
130 | ### storeIP
|
131 | This function is useful to get a consistent IP address such for storing it in a database or when searching in a database after being stored using this. So if a V6 address was sent compacted or not, or if you searched by either version this function would make sure you get a consistent IP address for both versions. Also the possibly of saving a few bytes.
|
132 |
|
133 | If an V6 addressed is mapped as v4 is given it will convert it to V4, If any other V6 address is given it is __abbreviated__ and plain V4 addresses are left alone. Returns null if a invalid IP
|
134 |
|
135 | ```typescript
|
136 | console.log(storeIP('foo')); //null
|
137 | console.log(storeIP('::ffff:127.0.0.1')); //127.0.0.1
|
138 | console.log(storeIP('2001:0000:0111:0000:0011:0000:0001:0000')); //2001:0:111:0:11:0:1:0
|
139 | console.log(storeIP('2001:0001:0000:0001:0000:0000:0000:0000')); //2001:1:0:1::
|
140 | console.log(storeIP('0000:0000:0000:0000:0000:0000:0000:0000')); //::
|
141 | console.log(storeIP('0000:0000:0000:0000:0000:0000:0000:0001')); //::1
|
142 | console.log(storeIP('2041:0000:140F:0000:0000:0000:875B:131B')); //2041:0:140F::875B:131B
|
143 | console.log(storeIP('2001:0001:0002:0003:0004:0005:0006:0007')); //2001:1:2:3:4:5:6:7
|
144 | console.log(storeIP('127.0.0.1')); //127.0.0.1
|
145 | ```
|
146 |
|
147 | ### searchIP
|
148 | Same function as `storeIP`, just a clearer name when you are using it for search instead
|
149 |
|
150 | ### displayIP
|
151 | This function is useful for displaying IP addresses, such as after grabbing it back from the database when using `storeIP`
|
152 |
|
153 | If an V6 addressed mapped as v4 is given it will convert it to V4, If any other V6 address is given it is __normalized__ into the longer version and plain V4 addresses are left alone. Returns a empty string if a invalid IP
|
154 |
|
155 | ```typescript
|
156 | console.log(displayIP(null)); // ''
|
157 | console.log(displayIP('::ffff:127.0.0.1')); //'127.0.0.1'
|
158 | console.log(displayIP('2001:0:111:0:11:0:1:0')); //'2001:0000:0111:0000:0011:0000:0001:0000'
|
159 | console.log(displayIP('2001:1:0:1::')); //'2001:0001:0000:0001:0000:0000:0000:0000'
|
160 | console.log(displayIP('::')); //'0000:0000:0000:0000:0000:0000:0000:0000'
|
161 | console.log(displayIP('::1')); //'0000:0000:0000:0000:0000:0000:0000:0001'
|
162 | console.log(displayIP('2041:0:140F::875B:131B')); //'2041:0000:140F:0000:0000:0000:875B:131B'
|
163 | console.log(displayIP('2001:1:2:3:4:5:6:7')); //'2001:0001:0002:0003:0004:0005:0006:0007'
|
164 | console.log(displayIP('127.0.0.1')); //'127.0.0.1'
|
165 |
|
166 | ```
|
167 |
|
168 | ## Contributing
|
169 |
|
170 | This project is using this starter library [bun-lib-starter](https://github.com/maxam2017/bun-lib-starter/tree/main)
|
171 | Currently to use NPM to publish, must have that installed with Node, along side `Bun`
|
172 |
|
173 | ```sh
|
174 | cd into the repo
|
175 |
|
176 | pre-commit install
|
177 |
|
178 | bun install
|
179 | ```
|
180 |
|
181 | - `bun run test`: Run test suite
|
182 | - `bun run build`: Generate bundles and typings
|
183 | - `bun run format`: Format source files, readme, etc
|
184 |
|
185 |
|
186 | ## Dependencies
|
187 | * ipaddr.js - [https://github.com/whitequark/ipaddr.js](https://github.com/whitequark/ipaddr.js)
|
188 | * ip6 - [https://github.com/elgs/ip6](https://github.com/elgs/ip6) |
\ | No newline at end of file |