forked from w3gh/ghost.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
82 lines (64 loc) · 2.02 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as fs from 'fs';
import * as path from 'path';
const startTime = Date.now();
export function getTime() {
return Math.floor(Date.now() / 1000);
}
export function getTicks() {
return Date.now() - startTime;
}
export function getTimezone() {
return Math.abs(new Date(Date.now()).getTimezoneOffset());
}
export function networkInterfaces() {
const os = require('os');
const ifaces = os.networkInterfaces();
const faces = [];
Object.keys(ifaces).forEach(function (ifname) {
let alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
faces.push({name: ifname + ':' + alias, address: iface.address});
// this single interface has multiple ipv4 addresses
} else {
// this interface has only one ipv4 adress
faces.push({name: ifname, address: iface.address});
}
++alias;
});
});
return faces;
}
export function localIP() {
return require('ip').address();
}
export function ipToBuffer(ip:string) {
return require('ip').toBuffer(ip);
}
export function isNameValid(name) {
return name.length && name.length <= 15
}
export function resolveLibraryPath(name: string) {
const platform = process.platform;
const cwd = process.cwd();
let libPath = null;
if (platform === 'win32') {
libPath = `${name}.dll`;
} else if (platform === 'linux') {
libPath = `lib${name}.so`;
} else if (platform === 'darwin') {
libPath = `lib${name}.dylib`;
} else {
throw new Error(`unsupported plateform for ${name}`);
}
let libName = path.resolve(cwd, libPath);
if (!fs.existsSync(libName)) {
console.error(`${libName} not found, fallback to libstorm`);
return `lib${name}`;
}
return libName;
}