This repository was archived by the owner on Nov 6, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
chromium/background-scripts Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change 22
33( function ( exports ) {
44
5+ /**
6+ * Parse and convert literal IP address into numerical IP address.
7+ * @param {string } ip
8+ * @returns {number }
9+ */
510const parseIp = ip => {
611 if ( ! / ^ [ 0 - 9 . ] + $ / . test ( ip ) ) {
712 return - 1 ;
813 }
914
15+ /** @type {string[] } */
1016 const octets = ip . split ( '.' ) ;
1117
1218 if ( octets . length !== 4 ) {
@@ -26,14 +32,21 @@ const parseIp = ip => {
2632 return - 1 ;
2733 }
2834
29- ipN = ( ipN << 8 ) | octet ;
35+ ipN = ( ipN << 8 ) | octetN ;
3036 }
3137
3238 return ipN >>> 0 ;
3339} ;
3440
41+ /**
42+ * Check if the numeric IP address is within a certain range.
43+ * @param {number } ip
44+ * @param {number[] } range
45+ * @returns {boolean }
46+ */
3547const isIpInRange = ( ip , [ rangeIp , mask ] ) => ( ip & mask ) >>> 0 === rangeIp ;
3648
49+ // A list of local IP address ranges
3750const localRanges = [
3851 [ /* 0.0.0.0 */ 0x00000000 , /* 255.255.255.255 */ 0xffffffff ] ,
3952 [ /* 127.0.0.0 */ 0x7f000000 , /* 255.0.0.0 */ 0xff000000 ] ,
@@ -42,6 +55,11 @@ const localRanges = [
4255 [ /* 192.168.0.0 */ 0xc0a80000 , /* 255.255.0.0 */ 0xffff0000 ] ,
4356] ;
4457
58+ /**
59+ * Check if the numeric IP address is inside the local IP address ranges.
60+ * @param {number } ip
61+ * @returns {boolean }
62+ */
4563const isLocalIp = ip => localRanges . some ( range => isIpInRange ( ip , range ) ) ;
4664
4765Object . assign ( exports , {
You can’t perform that action at this time.
0 commit comments