-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
116 lines (114 loc) · 3.05 KB
/
util.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const os = require('os');
const process = require('process');
class AirId {
static isEqual(id1, id2) {
return id1.airId === id2.airId
}
airId = null;
constructor(airId, host = null, sessionId = null) {
if (airId instanceof AirId) {
this.airId = airId.str
}
else if (!host && !sessionId && airId.split(':').length > 1) {
this.airId = airId
}
else if (host && airId.split(':').length == 1) {
//airId is uid
if (sessionId) {
this.airId = `${airId}:${host}:${sessionId}`
}
else {
this.airId = `${airId}:${host}`
}
}
}
parse() {
var ids = this.airId.split(':');
return {
uid: ids[0],
host: ids[1],
sessionId: ids[2] || null
}
}
get str() {
return this.airId;
}
get host() {
return this.airId.split(':')[1]
}
set host(str) {
this.airId = `${this.airId.split(':')[0]}:${str}:${this.airId.split(':')[1]}`
}
get uid() {
return this.airId.split(':')[0]
}
set uid(str) {
this.airId = `${str}:${this.airId.split(':')[0]}:${this.airId.split(':')[1]}`
}
get sessionId() {
return this.airId.split(':')[2] || null
}
set sessionId(str) {
if (str)
this.airId = `${this.airId.split(':')[0]}:${this.airId.split(':')[1]}:${str}`
else
this.airId = `${this.airId.split(':')[0]}:${this.airId.split(':')[1]}`
}
get isLocal() {
if (this.sessionId) {
return this.sessionId.split('#').length == 2
}
return null
}
get ipAddr() {
if (this.isLocal) {
return this.sessionId.split('#')[0]
}
return null
}
get port() {
if (this.isLocal) {
return this.sessionId.split('#')[1]
}
return null
}
}
module.exports = {
c: {
FRAME_SIZE: 64535, //65535
VERSION: '1.1'
},
getIpAddrs() {
var network = os.networkInterfaces();
var obj = {}
Object.keys(network).forEach((connName) => {
network[connName].forEach((conn) => {
if (conn.family === 'IPv4' && conn.address !== '127.0.0.1' && !conn.internal) {
obj[connName] = conn.address;
}
})
})
return obj;
},
getIpAddr() {
//This will return the most probable IP address of WiFi
var ips = this.getIpAddrs();
if (ips['Wi-Fi']) return ips['Wi-Fi']
if (ips['Wi-Fi']) return ips['en0']
return ips[Object.keys(ips)[0]]
},
getDeviceName() {
if (process.env['COMPUTERNAME'])
return process.env['COMPUTERNAME'];
return os.hostname().split('.')[0].split('-').join(' ');
},
parseAirId(airId) {
var ids = airId.split(':');
return {
uid: ids[0],
host: ids[1],
sessionId: ids[2]
}
},
AirId
}