简体   繁体   English

IP 地址到 Int

[英]IP address to Int

So I am having an issue with converting IPv4 and IPv6 to Int, I however have found this handy Go script which does it, however I need it for NodeJS.所以我在将 IPv4 和 IPv6 转换为 Int 时遇到问题,但是我发现这个方便的 Go 脚本可以做到这一点,但是我需要它用于 NodeJS。

I have tried the plugins in NPM including ip2int but it seems to be broken, and only work for IPv4.我已经尝试过 NPM 中的插件,包括ip2int ,但它似乎已损坏,仅适用于 IPv4。

I am wondering if anyone would know how to convert GO into Javascript for nodeJS.我想知道是否有人知道如何将 GO 转换为 nodeJS 的 Javascript。 As I know the following code works in Go.据我所知,以下代码适用于 Go。

package main

import (
    "fmt"
    "math/big"
    "net"
)

func Ip2Int(ip net.IP) *big.Int {
    i := big.NewInt(0)
    i.SetBytes(ip)
    return i
}

func main() {
    fmt.Println(Ip2Int(net.ParseIP("20.36.77.12").To4()).String())
    fmt.Println(Ip2Int(net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334").To16()).String())
}

The reason why we need to parse the IP addresses to int is so that when we receive the IP address we can search to see if that IP address is inside one of the ranges of IP addresses that we have collected. The reason why we need to parse the IP addresses to int is so that when we receive the IP address we can search to see if that IP address is inside one of the ranges of IP addresses that we have collected.

For example RAPD returns例如 RAPD 返回

 "startAddress": "35.192.0.0",
  "endAddress": "35.207.255.255",

so if we convert both of those to int we can store them in our DB and do a gte or lte search and see if the IP address is in between any of the stored values.因此,如果我们将这两个都转换为 int,我们可以将它们存储在我们的数据库中并进行gtelte搜索,看看 IP 地址是否在任何存储值之间。

See this solution .请参阅此解决方案

For IPv4 use:对于 IPv4 使用:

ipv4.split('.').reduce(function(int, value) { return int * 256 + +value })

For IPv6 you first need to parse the hex values and the hex values represent 2 bytes:对于 IPv6,您首先需要解析十六进制值,十六进制值代表 2 个字节:

ipv6.split(':').map(str => Number('0x'+str)).reduce(function(int, value) { return int * 65536 + +value })

I've also added the ipv6 solution to the referenced gist.我还在引用的要点中添加了 ipv6 解决方案。

const IPv4ToInt = ipv4 => ipv4.split(".").reduce((a, b) => a << 8 | b) >>> 0;

and a simple IPv6 implementation:和一个简单的 IPv6 实现:

const IPv6ToBigInt = ipv6 => BigInt("0x" + ipv6.toLowerCase().split(":").map(v => v.padStart(4, 0)).join(""));

or a more extensive one或更广泛的

 // normalizes several IPv6 formats to the long version // in this format a string sort is equivalent to a numeric sort const normalizeIPv6 = (ipv6) => { // for embedded IPv4 in IPv6. // like "::ffff:127.0.0.1" ipv6 = ipv6.replace(/\d+\.\d+\.\d+\.\d+$/, ipv4 => { const [a, b, c, d] = ipv4.split("."); return (a << 8 | b).toString(16) + ":" + (c << 8 | d).toString(16) }); // shortened IPs // like "2001:db8::1428:57ab" ipv6 = ipv6.replace("::", ":".repeat(10 - ipv6.split(":").length)); return ipv6.toLowerCase().split(":").map(v => v.padStart(4, 0)).join(":"); } const IPv6ToBigInt = ipv6 => BigInt("0x" + normalizeIPv6(ipv6).replaceAll(":", "")); // tests [ "2001:0db8:85a3:08d3:1319:8a2e:0370:7344", "2001:0db8:85a3:08d3:1319:8a2e:0370:7345", // check precision "2001:db8:0:8d3:0:8a2e:70:7344", "2001:db8:0:0:0:0:1428:57ab", "2001:db8::1428:57ab", "2001:0db8:0:0:8d3:0:0:0", "2001:db8:0:0:8d3::", "2001:db8::8d3:0:0:0", "::ffff:127.0.0.1", "::ffff:7f00:1" ].forEach(ip => console.log({ ip, normalized: normalizeIPv6(ip), bigInt: IPv6ToBigInt(ip).toString() // toString() or SO console doesn't print them. }));
 .as-console-wrapper{top:0;max-height:100%!important}

Javascript (vanilla) program: Javascript(原版)程序:

function ipToInt(value) {
  return Number(value.replace(/[a-z]|:|\./g, ""))
}

const ip1 = "2001:0db8:0:0:8d3:0:0:0"
const ip2 = "35.192.0.0"

console.log("IPv6", ipToInt(ip1))
console.log("IPv4", ipToInt(ip2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM