migrate from so.base/Network.js
This commit is contained in:
commit
d2b2f2ec89
16
.prettierrc.js
Normal file
16
.prettierrc.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
对 VSCode Prettier 有效;建议一直要有本配置文件,否则不同版本的 Prettier 的默认配置会不同,例如 TrailingComma
|
||||||
|
对 VSCode Prettier Standard 无效,似乎是集成了不能修改的配置。
|
||||||
|
*/
|
||||||
|
module.exports = {
|
||||||
|
printWidth: 160, // default 80
|
||||||
|
tabWidth: 2, // default 2
|
||||||
|
useTabs: false,
|
||||||
|
semi: false, // default true
|
||||||
|
singleQuote: true, // default false
|
||||||
|
trailingComma: 'es5', // none (default in v 1.*), es5 (default in v2.0.0), all
|
||||||
|
bracketSpacing: true, // default true
|
||||||
|
jsxBracketSameLine: false, // default false
|
||||||
|
arrowParens: 'always', // avoid (default in v1.9.0), always (default since v2.0.0)
|
||||||
|
quoteProps: 'as-needed', // as-needed (default), consistent, preserve
|
||||||
|
}
|
59
index.js
Normal file
59
index.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
const os = require('os')
|
||||||
|
const dns = require('dns')
|
||||||
|
const util = require('util')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
dn2ip: async function (host) {
|
||||||
|
// domain name 2 ip
|
||||||
|
if (typeof host === 'string' && host) {
|
||||||
|
var ip = await util
|
||||||
|
.promisify(dns.resolve)(host, 'A')
|
||||||
|
.catch(function (err) {
|
||||||
|
mylog.warn('WARNING : host cannot resolve to ip: ' + host)
|
||||||
|
return null
|
||||||
|
})
|
||||||
|
if (ip && /^\d+\.\d+\.\d+\.\d+$/.test(ip[0])) {
|
||||||
|
return ip[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
isPrivateIp: function (addr) {
|
||||||
|
return (
|
||||||
|
/^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
|
||||||
|
/^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
|
||||||
|
/^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
|
||||||
|
/^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
|
||||||
|
/^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
|
||||||
|
/^f[cd][0-9a-f]{2}:/i.test(addr) ||
|
||||||
|
/^fe80:/i.test(addr) ||
|
||||||
|
/^::1$/.test(addr) ||
|
||||||
|
/^::$/.test(addr)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
getMyIp: function () {
|
||||||
|
var publicIp = null
|
||||||
|
var privateIp = null
|
||||||
|
var self = this
|
||||||
|
try {
|
||||||
|
var ifaces = os.networkInterfaces()
|
||||||
|
Object.keys(ifaces).forEach(function (ifname) {
|
||||||
|
ifaces[ifname].forEach(function (iface) {
|
||||||
|
if ('IPv4' === iface.family && iface.internal === false) {
|
||||||
|
// console.log('ip='+iface.address)
|
||||||
|
if (self.isPrivateIp(iface.address)) {
|
||||||
|
privateIp = iface.address
|
||||||
|
// console.log('privateIp='+privateIp)
|
||||||
|
} else {
|
||||||
|
publicIp = iface.address
|
||||||
|
// console.log('publicIp='+publicIp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
console.log('ERROR in getMyIP(): ' + e.message)
|
||||||
|
}
|
||||||
|
return publicIp || privateIp
|
||||||
|
},
|
||||||
|
}
|
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "nettool",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.faronear.org/npm/nettool"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user