rename base.nettool to base.tool; add tool4net.js and tool4log.js
This commit is contained in:
		
							parent
							
								
									7aa865bd25
								
							
						
					
					
						commit
						cfb66c3eec
					
				
							
								
								
									
										64
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								index.js
									
									
									
									
									
								
							@ -1,63 +1,7 @@
 | 
				
			|||||||
const os = require('os')
 | 
					const tool4net = require('./tool4net.js')
 | 
				
			||||||
const dns = require('dns')
 | 
					const tool4log = require('./tool4log')
 | 
				
			||||||
const util = require('util')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
 | 
					  tool4net,
 | 
				
			||||||
  /*----------------------------------------------------------------
 | 
					  tool4log
 | 
				
			||||||
  * Network tools
 | 
					 | 
				
			||||||
  ----------------------------------------------------------------*/
 | 
					 | 
				
			||||||
  async dn2ip(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(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() {
 | 
					 | 
				
			||||||
    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
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "base.nettool",
 | 
					  "name": "base.tool",
 | 
				
			||||||
  "version": "1.0.0",
 | 
					  "version": "1.0.0",
 | 
				
			||||||
  "description": "",
 | 
					  "description": "",
 | 
				
			||||||
  "main": "index.js",
 | 
					  "main": "index.js",
 | 
				
			||||||
@ -8,8 +8,11 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  "repository": {
 | 
					  "repository": {
 | 
				
			||||||
    "type": "git",
 | 
					    "type": "git",
 | 
				
			||||||
    "url": "https://git.faronear.org/npm/base.nettool"
 | 
					    "url": "https://git.faronear.org/npm/base.tool"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "author": "",
 | 
					  "author": "",
 | 
				
			||||||
  "license": "ISC"
 | 
					  "license": "ISC",
 | 
				
			||||||
 | 
					  "dependencies": {
 | 
				
			||||||
 | 
					    "colors": "^1.4.0"
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								tool4log.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								tool4log.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					const colors = require('colors')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
					  colog(...args) {
 | 
				
			||||||
 | 
					    console.log(colors.green({time: new Date().toJSON()}), ...args)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										63
									
								
								tool4net.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								tool4net.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,63 @@
 | 
				
			|||||||
 | 
					const os = require('os')
 | 
				
			||||||
 | 
					const dns = require('dns')
 | 
				
			||||||
 | 
					const util = require('util')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /*----------------------------------------------------------------
 | 
				
			||||||
 | 
					  * Network tools
 | 
				
			||||||
 | 
					  ----------------------------------------------------------------*/
 | 
				
			||||||
 | 
					  async dn2ip(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(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() {
 | 
				
			||||||
 | 
					    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
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user