rename all base.xxx to basend-xxx
This commit is contained in:
		
							parent
							
								
									49fa78f5a6
								
							
						
					
					
						commit
						68e8e549fa
					
				
							
								
								
									
										70
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								index.js
									
									
									
									
									
								
							@ -1,7 +1,63 @@
 | 
				
			|||||||
const tool4net = require('./tool4net.js')
 | 
					const os = require('os')
 | 
				
			||||||
const tool4log = require('./tool4log')
 | 
					const dns = require('dns')
 | 
				
			||||||
 | 
					const util = require('util')
 | 
				
			||||||
module.exports = {
 | 
					
 | 
				
			||||||
  tool4net,
 | 
					module.exports = {
 | 
				
			||||||
  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
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										13
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								package.json
									
									
									
									
									
								
							@ -1,18 +1,9 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "base.tool",
 | 
					  "name": "basend-netinfo",
 | 
				
			||||||
  "version": "1.0.0",
 | 
					  "version": "1.0.0",
 | 
				
			||||||
  "description": "",
 | 
					  "description": "",
 | 
				
			||||||
  "main": "index.js",
 | 
					  "main": "index.js",
 | 
				
			||||||
  "scripts": {
 | 
					 | 
				
			||||||
    "test": "echo \"Error: no test specified\" && exit 1"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "repository": {
 | 
					 | 
				
			||||||
    "type": "git",
 | 
					 | 
				
			||||||
    "url": "https://git.faronear.org/npm/base.tool"
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  "author": "",
 | 
					  "author": "",
 | 
				
			||||||
  "license": "ISC",
 | 
					  "license": "ISC",
 | 
				
			||||||
  "dependencies": {
 | 
					  "dependencies": {}
 | 
				
			||||||
    "colors": "^1.4.0"
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										41
									
								
								tool4log.js
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								tool4log.js
									
									
									
									
									
								
							@ -1,41 +0,0 @@
 | 
				
			|||||||
const colors = require('colors')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// HBuilder 内置环境的 console 不支持颜色。为了检查是否支持颜色,可测试 uniCloud 是否存在(不存在说明在自己的server环境里),或 require('supports-color'),相应的返回不同的 ccXXX 函数。
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module.exports = typeof uniCloud === 'undefined' ? {
 | 
					 | 
				
			||||||
  cclog(...args) {
 | 
					 | 
				
			||||||
    console.log({timeiso: new Date().toJSON()}, ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccinfo(...args) {
 | 
					 | 
				
			||||||
    console.info({timeiso: new Date().toJSON()}, ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccerror(...args) { // console.error will appear in pm2's error log
 | 
					 | 
				
			||||||
    console.error({timeiso: new Date().toJSON()}, ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccwarn(...args) { // console.warn will appear in pm2's error log
 | 
					 | 
				
			||||||
    console.warn({timeiso: new Date().toJSON()}, ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccdebug(...args) {
 | 
					 | 
				
			||||||
    if ('development'===process.env.NODE_ENV) { // 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
 | 
					 | 
				
			||||||
      console.log({timeiso: new Date().toJSON()}, ...args)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
} : {
 | 
					 | 
				
			||||||
  cclog(...args) {
 | 
					 | 
				
			||||||
    console.log(colors.blue({timeiso: new Date().toJSON()}), ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccinfo(...args) {
 | 
					 | 
				
			||||||
    console.info(colors.green({timeiso: new Date().toJSON()}), ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccerror(...args) { // console.error will appear in pm2's error log
 | 
					 | 
				
			||||||
    console.error(colors.red({timeiso: new Date().toJSON()}), ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccwarn(...args) { // console.warn will appear in pm2's error log
 | 
					 | 
				
			||||||
    console.warn(colors.yellow({timeiso: new Date().toJSON()}), ...args)
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  ccdebug(...args) {
 | 
					 | 
				
			||||||
    if ('development'===process.env.NODE_ENV) { // 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
 | 
					 | 
				
			||||||
      console.log(colors.rainbow({timeiso: new Date().toJSON()}), ...args)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										63
									
								
								tool4net.js
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								tool4net.js
									
									
									
									
									
								
							@ -1,63 +0,0 @@
 | 
				
			|||||||
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