javascript binding with IPv4 and IPv6 supports
This commit is contained in:
parent
54a160e230
commit
086250c666
|
|
@ -77,6 +77,13 @@ target
|
||||||
/binding/nodejs/.nyc_output
|
/binding/nodejs/.nyc_output
|
||||||
/binging/nodejs/package-lock.json
|
/binging/nodejs/package-lock.json
|
||||||
|
|
||||||
|
# Javascript
|
||||||
|
/binding/javascript/tests/unitTests/__snapshots__
|
||||||
|
/binding/javascript/coverage
|
||||||
|
/binding/javascript/node_modules
|
||||||
|
/binding/javascript/.nyc_output
|
||||||
|
/binging/javascript/package-lock.json
|
||||||
|
|
||||||
# maker
|
# maker
|
||||||
## golang
|
## golang
|
||||||
/maker/golang/dbmaker
|
/maker/golang/dbmaker
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
// Copyright 2022 The Ip2Region Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a Apache2.0-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// xdb header class
|
||||||
|
// @Author Lion <chenxin619315@gmail.com>
|
||||||
|
|
||||||
|
const xdb_structure_20 = 2;
|
||||||
|
const xdb_structure_30 = 3;
|
||||||
|
|
||||||
|
class Header {
|
||||||
|
constructor(buff) {
|
||||||
|
this.version = buff.readUInt16LE(0);
|
||||||
|
this.indexPolicy = buff.readUInt16LE(2);
|
||||||
|
this.createdAt = buff.readUInt32LE(4);
|
||||||
|
this.startIndexPtr = buff.readUInt32LE(8);
|
||||||
|
this.endIndexPtr = buff.readUInt32LE(12);
|
||||||
|
|
||||||
|
// since IPv6 supporting
|
||||||
|
this.ipVersion = buff.readUInt16LE(16);
|
||||||
|
this.runtimePtrBytes = buff.readUInt16LE(18);
|
||||||
|
|
||||||
|
// keep the raw data
|
||||||
|
this.buff = buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
version() {
|
||||||
|
return this.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
indexPolicy() {
|
||||||
|
return this.indexPolicy;
|
||||||
|
}
|
||||||
|
|
||||||
|
createdAt() {
|
||||||
|
return this.createdAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
startIndexPtr() {
|
||||||
|
return this.startIndexPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
endIndexPtr() {
|
||||||
|
return this.endIndexPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
ipVersion() {
|
||||||
|
return this.ipVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
runtimePtrBytes() {
|
||||||
|
return this.runtimePtrBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
buff() {
|
||||||
|
return this.buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return `{
|
||||||
|
"version":${this.version},
|
||||||
|
"index_policy":${this.indexPolicy},
|
||||||
|
"start_index_ptr": ${this.startIndexPtr},
|
||||||
|
"end_index_ptr": ${this.endIndexPtr},
|
||||||
|
"ipVersion": ${this.ipVersion},
|
||||||
|
"runtime_ptr_bytes": ${this.runtimePtrBytes}
|
||||||
|
}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
xdb_structure_20, xdb_structure_30,
|
||||||
|
Header
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,31 @@
|
||||||
|
{
|
||||||
|
"name": "ip2region",
|
||||||
|
"version": "3.0.0",
|
||||||
|
"description": "javascript (ES6) binding for ip2region with both IPv4 and IPv6 supported ",
|
||||||
|
"type": "module",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/lionsoul2014/ip2region.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"ip2region",
|
||||||
|
"ip-address",
|
||||||
|
"ip-lookup",
|
||||||
|
"ip-location",
|
||||||
|
"ipv4-lookup",
|
||||||
|
"ipv6-lookup"
|
||||||
|
],
|
||||||
|
"author": "lionsoul2014",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/lionsoul2014/ip2region/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/lionsoul2014/ip2region#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"jest": "^30.2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// Copyright 2022 The Ip2Region Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a Apache2.0-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
const version = require('../version');
|
||||||
|
|
||||||
|
test('const print', () => {
|
||||||
|
console.log("IPv4: ", version.IPv4.toString());
|
||||||
|
console.log("IPv6: ", version.IPv6.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
test("test version from name", () => {
|
||||||
|
let vs = ["v4", "ipv4", "v4x", "v6", "ipv6", "v6x"];
|
||||||
|
vs.forEach(ele => {
|
||||||
|
let v = version.versionFromName(ele);
|
||||||
|
console.log(`versionFrom(${ele})`, v == null ? null : v.toString());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
// Copyright 2022 The Ip2Region Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a Apache2.0-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// util functions
|
||||||
|
// @Author Lion <chenxin619315@gmail.com>
|
||||||
|
|
||||||
|
// parse the specified string ip and return its bytes
|
||||||
|
// @param ip string
|
||||||
|
// @return Buffer
|
||||||
|
function parseIP(ipString) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// bytes ip to humen-readable string ip
|
||||||
|
function ipToString(ipBytes) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// compare two byte ips
|
||||||
|
// returns: -1 if ip1 < ip2, 1 if ip1 > ip2 or 0
|
||||||
|
function ipCompare(ip1, ip2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// load header from xdb file
|
||||||
|
function loadHeader(handle) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadHeaderFromFile(dbPath) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadVectorIndex(handle) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadVectorIndexFromFile(dbPath) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadContent(handle) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadContentFromFile(dbPath) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
// Copyright 2022 The Ip2Region Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a Apache2.0-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// version module
|
||||||
|
// @Author Lion <chenxin619315@gmail.com>
|
||||||
|
|
||||||
|
const header = require('./header');
|
||||||
|
|
||||||
|
const xdb_ipv4_id = 4;
|
||||||
|
const xdb_ipv6_id = 6;
|
||||||
|
|
||||||
|
class Version {
|
||||||
|
constructor(id, name, bytes, indexSize, ip_compare_func) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.bytes = bytes;
|
||||||
|
this.indexSize = indexSize;
|
||||||
|
this.ip_compare_func = ip_compare_func;
|
||||||
|
}
|
||||||
|
|
||||||
|
id() {
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
name() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes() {
|
||||||
|
return this.bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
indexSize() {
|
||||||
|
return this.indexSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
compareFunc() {
|
||||||
|
return this.ip_compare_func;
|
||||||
|
}
|
||||||
|
|
||||||
|
ipCompare(ip1, ip2) {
|
||||||
|
return this.ip_compare_func(ip1, ip2);
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return `{"id": ${this.id}, "name": "${this.name}", "bytes":${this.bytes}, "index_size": ${this.indexSize}}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 14 = 4 + 4 + 2 + 4
|
||||||
|
const IPv4 = new Version(xdb_ipv4_id, "IPv4", 4, 14, function(ip1, ip2){
|
||||||
|
});
|
||||||
|
|
||||||
|
// 38 = 16 + 16 + 2 + 4
|
||||||
|
const IPv6 = new Version(xdb_ipv6_id, "IPv6", 6, 38, function(ip1, ip2){
|
||||||
|
});
|
||||||
|
|
||||||
|
function versionFromName(name) {
|
||||||
|
let n = name.toUpperCase();
|
||||||
|
if (n == "V4" || n == "IPV4") {
|
||||||
|
return IPv4;
|
||||||
|
} else if (n == "V6" || n == "IPV6") {
|
||||||
|
return IPv6;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function versionFromHeader(h) {
|
||||||
|
let v = h.version();
|
||||||
|
|
||||||
|
// old structure with ONLY IPv4 supporting
|
||||||
|
if (v == header.xdb_structure_20) {
|
||||||
|
return IPv4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// structure 3.0 with IPv6 supporting
|
||||||
|
if (v != header.xdb_structure_30) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ipVer = h.ipVersion();
|
||||||
|
if (ipVer == xdb_ipv4_id) {
|
||||||
|
return IPv4;
|
||||||
|
} else if (ipVer == xdb_ipv6_id) {
|
||||||
|
return IPv6;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
xdb_ipv4_id, xdb_ipv6_id,
|
||||||
|
Version, IPv4, IPv6,
|
||||||
|
versionFromName, versionFromHeader
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue