![](/img/trans.png)
[英]How do I detect whether a given IP is in a given subnet in Javascript?
[英]How do you check whether the given IP is internal or not
您如何仅使用 javascript 检查给定 IP 是否为内部 IP?
例如,如果给定的 IP 为 192.168.1.1,则脚本应验证此 IP 并警告这是内部 IP 还是外部 IP。
如果您的意思是私人,请确保它在以下范围之一:
专用IP地址范围
可用IP的范围和数量如下:
10.0.0.0 - 10.255.255.255地址:16,777,216
172.16.0.0 - 172.31.255.255地址:1,048,576
192.168.0.0 - 192.168.255.255地址:65,536
这样的功能应该有所帮助:
function isPrivateIP(ip) {
var parts = ip.split('.');
return parts[0] === '10' ||
(parts[0] === '172' && (parseInt(parts[1], 10) >= 16 && parseInt(parts[1], 10) <= 31)) ||
(parts[0] === '192' && parts[1] === '168');
}
内部IP如下:
10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255
为他们写正确的正则表达式。
/10\.\d+\.\d+\.\d+/
/192\.168\.\d+\.\d+/
我留给你找出172.xxx范围的正确正则表达式。
您可以使用ipaddr.js
库并检查它是否返回"private"
:
const ipaddrJs = require('ipaddr.js');
ipaddrJs.parse('192.168.5.1').range()
> 'private'
https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js
如果您使用Node.js,请查看此处: https : //www.npmjs.com/package/ipaddr.js
试试这个吧!
var id = "10.10.10.10";
if (/^(10)\.(.*)\.(.*)\.(.*)$/.test(id)){
//10.x.x.x
}else if (/^(172)\.(1[6-9]|2[0-9]|3[0-1])\.(.*)\.(.*)$/.test(id)){
//172.16.x.x - 172.31.255.255
}else if (/^(192)\.(168)\.(.*)\.(.*)$/.test(id)){
//192.168.x.x
}else {
//else
}
要包含 IPv6 地址并使用一个函数检查任何 IP 地址,您可以使用以下命令:
var net = require('net') // requires node.js
ipIsPrivate(ip) {
if (ip.substring(0,7) === "::ffff:")
ip = ip.substring(7);
if (net.isIPv4(ip)) {
// 10.0.0.0 - 10.255.255.255 || 172.16.0.0 - 172.31.255.255 || 192.168.0.0 - 192.168.255.255
return /^(10)\.(.*)\.(.*)\.(.*)$/.test(ip) || /^(172)\.(1[6-9]|2[0-9]|3[0-1])\.(.*)\.(.*)$/.test(ip) || /^(192)\.(168)\.(.*)\.(.*)$/.test(ip)
}
// else: ip is IPv6
const firstWord = ip.split(":").find(el => !!el); //get first not empty word
// The original IPv6 Site Local addresses (fec0::/10) are deprecated. Range: fec0 - feff
if (/^fe[c-f][0-f]$/.test(firstWord))
return true;
// These days Unique Local Addresses (ULA) are used in place of Site Local.
// Range: fc00 - fcff
else if (/^fc[0-f]{2}$/.test(firstWord))
return true;
// Range: fd00 - fcff
else if (/^fd[0-f]{2}$/.test(firstWord))
return true;
// Link local addresses (prefixed with fe80) are not routable
else if (firstWord === "fe80")
return true;
// Discard Prefix
else if (firstWord === "100")
return true;
// Any other IP address is not Unique Local Address (ULA)
return false;
}
如果检查应包括本地主机( 127.0.0.1
、 ::ffff:127.0.0.1
和::1
),请使用:
var net = require('net') // requires node.js
ipIsPrivateOrLocalhost(ip) {
if (ip.substring(0,7) === "::ffff:")
ip = ip.substring(7);
if (net.isIPv4(ip)) {
// check localhost
if (ip === '127.0.0.1')
return true;
// 10.0.0.0 - 10.255.255.255 || 172.16.0.0 - 172.31.255.255 || 192.168.0.0 - 192.168.255.255
return /^(10)\.(.*)\.(.*)\.(.*)$/.test(ip) || /^(172)\.(1[6-9]|2[0-9]|3[0-1])\.(.*)\.(.*)$/.test(ip) || /^(192)\.(168)\.(.*)\.(.*)$/.test(ip)
}
// else: ip is IPv6
const firstWord = ip.split(":").find(el => !!el); //get first not empty word
// equivalent of 127.0.0.1 in IPv6
if (ip === "::1")
return true;
// The original IPv6 Site Local addresses (fec0::/10) are deprecated. Range: fec0 - feff
else if (/^fe[c-f][0-f]$/.test(firstWord))
return true;
// These days Unique Local Addresses (ULA) are used in place of Site Local.
// Range: fc00 - fcff
else if (/^fc[0-f]{2}$/.test(firstWord))
return true;
// Range: fd00 - fcff
else if (/^fd[0-f]{2}$/.test(firstWord))
return true;
// Link local addresses (prefixed with fe80) are not routable
else if (firstWord === "fe80")
return true;
// Discard Prefix
else if (firstWord === "100")
return true;
// Any other IP address is not Unique Local Address (ULA)
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.