mybase 1.0.39 → 1.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/mybase.js +37 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ var { isLocal } = require('mybase')
|
|
|
33
33
|
### isMochaRunning
|
|
34
34
|
### ip2int(), int2ip()
|
|
35
35
|
### ensureProperty(obj,'tier.tier',defaultValue)
|
|
36
|
-
|
|
36
|
+
### Geoip2Paths() : boolean | object
|
|
37
37
|
### arrayRandomItem(array,defaultValue=false)
|
|
38
38
|
### maxmindOpen(geoipFile) : <Promise>
|
|
39
39
|
|
package/mybase.js
CHANGED
|
@@ -2,6 +2,8 @@ const aesjs = require("aes-js")
|
|
|
2
2
|
const debug = require('debug')('mybase')
|
|
3
3
|
const os = require('os')
|
|
4
4
|
const fs = require('fs')
|
|
5
|
+
const path = require('path')
|
|
6
|
+
|
|
5
7
|
const net = require('net')
|
|
6
8
|
const chalk = require('chalk')
|
|
7
9
|
const punycode = require('punycode');
|
|
@@ -56,6 +58,22 @@ function validIp(str) {
|
|
|
56
58
|
return false
|
|
57
59
|
}
|
|
58
60
|
|
|
61
|
+
function Geoip2Paths() {
|
|
62
|
+
function firstExisting(paths) {
|
|
63
|
+
if (Array.isArray(paths))
|
|
64
|
+
for(let fpath of paths)
|
|
65
|
+
if (fs.existsSync(fpath)) return fpath
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
country:firstExisting(['/opt/geoip2/GeoIP2-Country.mmdb','/usr/local/var/GeoIP/GeoIP2-Country.mmdb','/var/lib/GeoIP/GeoIP2-Country.mmdb','/usr/share/GeoIP/GeoIP2-Country.mmdb',path.join(__dirname, 'assets/GeoIP2-Country.mmdb')]),
|
|
71
|
+
city:firstExisting(['/opt/geoip2/GeoIP2-City.mmdb','/usr/local/var/GeoIP/GeoIP2-City.mmdb','/var/lib/GeoIP/GeoIP2-City.mmdb','/usr/share/GeoIP/GeoIP2-City.mmdb',path.join(__dirname, 'assets/GeoIP2-City.mmdb')]),
|
|
72
|
+
isp:firstExisting(['/opt/geoip2/GeoIP2-ISP.mmdb','/usr/local/var/GeoIP/GeoIP2-ISP.mmdb','/var/lib/GeoIP/GeoIP2-ISP.mmdb','/usr/share/GeoIP/GeoIP2-ISP.mmdb',path.join(__dirname, 'assets/GeoIP2-ISP.mmdb')]),
|
|
73
|
+
ct:firstExisting(['/opt/geoip2/GeoIP2-Connection-Type.mmdb','/usr/local/var/GeoIP/GeoIP2-Connection-Type.mmdb','/var/lib/GeoIP/GeoIP2-Connection-Type.mmdb','/usr/share/GeoIP/GeoIP2-Connection-Type.mmdb',path.join(__dirname, 'assets/GeoIP2-Connection-Type.mmdb')]),
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
59
77
|
function validIpNative(str) {
|
|
60
78
|
return (net.isIP(str)!==0)
|
|
61
79
|
// const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)';
|
|
@@ -447,6 +465,22 @@ function validUUID4(uuid) {
|
|
|
447
465
|
return new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i).test(uuid)
|
|
448
466
|
}
|
|
449
467
|
|
|
468
|
+
function promiseTimeout(ms, promise) {
|
|
469
|
+
// Create a promise that rejects in <ms> milliseconds
|
|
470
|
+
let timeout = new Promise((resolve, reject) => {
|
|
471
|
+
let id = setTimeout(() => {
|
|
472
|
+
clearTimeout(id);
|
|
473
|
+
reject('TIMEDOUT')
|
|
474
|
+
}, ms)
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
// Returns a race between our timeout and the passed in promise
|
|
478
|
+
return Promise.race([
|
|
479
|
+
promise,
|
|
480
|
+
timeout
|
|
481
|
+
])
|
|
482
|
+
}
|
|
483
|
+
|
|
450
484
|
async function portCheck_tcp (port, {timeout = 1000, host='127.0.0.1'} = {}) {
|
|
451
485
|
const promise = new Promise(((resolve, reject) => {
|
|
452
486
|
const socket = new net.Socket();
|
|
@@ -563,5 +597,7 @@ module.exports = {
|
|
|
563
597
|
portCheck_tcp,
|
|
564
598
|
asJSON,
|
|
565
599
|
ensureProperty,
|
|
566
|
-
arrayRandomItem
|
|
600
|
+
arrayRandomItem,
|
|
601
|
+
promiseTimeout,
|
|
602
|
+
Geoip2Paths
|
|
567
603
|
}
|