Version 1.0.0

parents
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
# Typescript builds
/lib
ISC License
Copyright (c) 2019 [TelcoDB.net](https://telcodb.net)
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import { CymruClient } from "./src";
// Create an instance of CymruClient
const cymruClient = new CymruClient();
console.log(cymruClient.getTypeOfQuery("AS6939"));
console.log(cymruClient.getTypeOfQuery("45.76.38.7"));
console.log(cymruClient.getTypeOfQuery("2001:19f0:5001:8f0:5400:1ff:fe34:85c5"));
console.log(cymruClient.getDnsName("2001:19f0:5001:8f0:5400:1ff:fe34:85c5"));
console.log(cymruClient.getDnsName("45.76.38.7"));
console.log(cymruClient.getDnsName("AS6939"));
cymruClient.getData("AS6939")
.then(result => console.log(result))
cymruClient.getData("45.76.38.7")
.then(result => console.log(result))
cymruClient.getData("2001:19f0:5001:8f0:5400:1ff:fe34:85c5")
.then(result => console.log(result))
{
"watch": ["src", "dev-cli.ts"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./dev-cli.ts"
}
\ No newline at end of file
{
"name": "cymru-ip2asn",
"version": "1.0.0",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://gitlab.lelux.fi/TelcoDB/libs/cymru-ip2asn.git"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": ["lib/**/*"],
"author": {
"name": "TelcoDB",
"email": "npm@telcodb.net",
"url": "https://telcodb.net/"
},
"bugs": {
"url": "https://gitlab.lelux.fi/TelcoDB/libs/cymru-ip2asn/issues"
},
"homepage": "https://gitlab.lelux.fi/TelcoDB/libs/cymru-ip2asn",
"scripts": {
"dev": "nodemon",
"build": "tsc",
"prepare": "rm -rf lib && yarn build",
"postversion" : "git push && git push --tags"
},
"dependencies": {
"@types/node": "^12.7.5",
"ip-reverser": "https://gitlab.lelux.fi/TelcoDB/libs/ip-reverser.git#1.0.0",
"ipaddr.js": "^1.9.1",
"is-asn": "https://gitlab.lelux.fi/TelcoDB/libs/is-asn.git#1.0.0",
"is-ip": "^3.1.0",
"txt-chunk-processor": "https://gitlab.lelux.fi/TelcoDB/libs/txt-chunk-processor.git#1.0.0",
"typescript": "^3.6.3"
},
"devDependencies": {
"nodemon": "^1.19.2",
"ts-node": "^8.4.1"
}
}
import ipReverser from "ip-reverser";
import isIp from "is-ip";
import isAsn from "is-asn";
import txtChunkProcessor from "txt-chunk-processor";
const { Resolver } = require('dns').promises;
const resolver = new Resolver();
// Data parsers
interface IpDataInterface {
asNumber: number,
announcementRange: string,
countryCode: string,
rir: string,
assignmentDate: string,
}
function ipDataParser(rawData: string[]): IpDataInterface[] {
const finalData: IpDataInterface[] = [];
rawData.forEach((x: string) => {
const arr = x.split(" | ");
// TODO: Do some checking on the data
const data: IpDataInterface = {
asNumber: parseInt(arr[0]),
announcementRange: arr[1],
countryCode: arr[2],
rir: arr[3],
assignmentDate: arr[4],
}
finalData.push(data);
})
return finalData;
}
interface AsnDataInterface {
asNumber: number,
countryCode: string,
rir: string,
assignmentDate: string,
// Can be at least the following formats:
// "EUNET-FINLAND, FI" (ripencc, 6667)
// "DNA, FI" (ripencc, 16086)
// "ELISA-AS Helsinki, Finland, FI'" (ripencc, 719)
// "HURRICANE - Hurricane Electric LLC, US" (arin, 6939)
rawDesc: string}
function asnDataParser(rawData: string[]): AsnDataInterface[] {
const finalData: AsnDataInterface[] = [];
rawData.forEach((x: string) => {
const arr = x.split(" | ");
// TODO: Do some checking on the data
const data: AsnDataInterface = {
asNumber: parseInt(arr[0]),
countryCode: arr[1],
rir: arr[2],
assignmentDate: arr[3],
rawDesc: arr[4],
}
finalData.push(data);
})
return finalData;
}
// Visible client
enum QueryType {
IPAddress = "ip",
ASN = "asn",
}
class CymruClient {
getTypeOfQuery(query: string): QueryType {
if(isIp(query)) {
return QueryType.IPAddress;
} else if(isAsn(query)) {
return QueryType.ASN;
} else {
throw "Not an IPv4/IPv6 address or an ASN (in format like AS6939)";
}
}
getDnsName(query: string, queryType?: QueryType): string {
queryType = queryType ? queryType : this.getTypeOfQuery(query);
if(queryType === QueryType.IPAddress) {
return ipReverser(query, "origin.asn.cymru.com", "origin6.asn.cymru.com");
} else if(queryType === QueryType.ASN) {
return `${query}.asn.cymru.com`;
} else {
throw "Unknown query type";
}
}
fetchRawData(query: string): Promise<[[], QueryType]> {
const queryType = this.getTypeOfQuery(query);
return new Promise((resolve, reject) => {
resolver.resolveTxt(this.getDnsName(query, queryType))
.then(rawResult => txtChunkProcessor(rawResult))
.then(result => resolve([result, queryType]))
.catch(reject);
});
}
parseRawData(query: string): Promise<IpDataInterface[] | AsnDataInterface[]> {
return new Promise((resolve, reject) => {
this.fetchRawData(query)
.then(x => {
const rawData: [] = x[0];
const queryType: QueryType = x[1];
if(queryType === QueryType.IPAddress) {
const data: IpDataInterface[] = ipDataParser(rawData);
return data;
} else if(queryType === QueryType.ASN) {
const data: AsnDataInterface[] = asnDataParser(rawData);
return data;
} else {
throw "Unknown query type";
}
})
.then(x => resolve(x))
.catch(reject);
});
}
getData(query: string) {
return new Promise((resolve, reject) => {
this.parseRawData(query)
.then(x => resolve(x))
.catch(reject);
});
}
}
export default CymruClient;
export { CymruClient };
export * from "./cymru-client";
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"esModuleInterop": true,
"outDir": "./lib",
"sourceMap": true,
"removeComments": true,
"declaration": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment