-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (204 loc) · 10.1 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const dgram = require('node:dgram');
const crypto = require('node:crypto');
const hash = d => crypto.createHash('sha256').update(d).digest('base64');
const {input,choice,CancelChar} = require('./input');
const apps = require('./apps.json');
const discover = Symbol('discover');
const DISCOVER_PORT = 7545;
choice('Select an app',{
values: ['\x1b[33mDiscover\x1b[39m'].concat(apps.map(a=>`\x1b[36m${a.name}\x1b[39m (${a.roomSize} users)`)),
rv : [discover].concat(apps),
ln : false,
clear : true
}).then(
async appd => {
if (appd == discover) {
let appz = apps.map(a=>({name:a.name,version:hash(fs.readFileSync(path.join(__dirname,a.path))),path:a.path}));
let sock = dgram.createSocket('udp4');
let games = {};
sock.on('listening',()=>{
sock.setBroadcast(true);
});
sock.on('message',(msg,inf)=>{
let dat = JSON.parse(msg.slice('LAN_GAMES'.length));
let k = hash(JSON.stringify([inf.address,dat.host,dat.port]));
games[k] = {
last: Date.now(),
addr: {
addr: inf.address,
name: dat.host,
port: dat.port,
},
appd: dat.appd
};
});
sock.bind(DISCOVER_PORT);
const sin = process.stdin;
const getch = () => new Promise(r=>sin.once('data',r));
sin.setRawMode(true);
process.on('exit',()=>{sin.setRawMode(false);});
let oll = 0;
let inp = '';
let inpm = {m:'',t:-Infinity};
let ri = setInterval(()=>{
let w = '';
w += `\x1b[A\x1b[K`.repeat(oll);
oll = 0;
let inps;
if (inp.startsWith('#')) {
inps = `\x1b[90m#\x1b[39m\x1b[${Object.keys(games).map(k=>k.slice(0,4)).includes(inp.slice(1))?('35'+(Date.now()-Object.entries(games).find(([id,r])=>id.slice(0,4)==inp.slice(1))[1].last<5000?'':';3')):'31'}m${inp.slice(1)}\x1b[39;23m`;
if (Date.now()-inpm.t>5000) {
if (Object.keys(games).map(k=>k.slice(0,4)).includes(inp.slice(1))&&Date.now()-Object.entries(games).find(([id,r])=>id.slice(0,4)==inp.slice(1))[1].last>5000) {
inpm = {
t: Date.now(),
m: '(unreachable)',
ur: true
};
} else {
inpm.ur = false;
}
}
if (Object.keys(games).map(k=>k.slice(0,4)).includes(inp.slice(1))&&Date.now()-Object.entries(games).find(([id,r])=>id.slice(0,4)==inp.slice(1))[1].last>5000) {}
else if (inpm.ur) {
inpm.t = -Infinity;
}
} else {
inps = inp;
}
w += `Join game: ${inps} \x1b[90m${Date.now()-inpm.t<5000?inpm.m:''}\x1b[39m\n`;
oll++;
for (let [id,g] of Object.entries(games)) {
if (Date.now()-g.last < 5000) {
let app = appz.find(a=>a.name==g.appd.name);
let s,m;
if (!app) {
s = '\x1b[31mA\x1b[39m';
m = '\x1b[31m[Cannot find app]\x1b[39m';
} else if (app.version!=g.appd.version) {
s = '\x1b[33mV\x1b[39m';
m = `\x1b[33m[Version missmatch]\x1b[39m`;
} else {
s = '\x1b[32m◈\x1b[39m';
m = '';
}
w += `${s} \x1b[35m${id.slice(0,4)}\x1b[m \x1b[36;1m${g.appd.name}\x1b[39m (?/${g.appd.roomSize}) ${g.addr.addr}\x1b[90m:\x1b[39m${g.addr.port} (${g.addr.name}) ${m}\n`;
oll++;
}
}
process.stdout.write(w);
},20);
;(async()=>{
let join = null;
while (true) {
let k = (await getch()).toString('utf-8');
if (k == '\x03') break;
else if (k == '\x7f' || k == '\x08') {
inp = inp.slice(0,inp.length-1);
}
else if (k == '\r' || k == '\n') {
if (inp.startsWith('#')) {
if (Object.keys(games).map(id=>id.slice(0,4)).includes(inp.slice(1))) {
let g = Object.entries(games).find(([id,g])=>id.slice(0,4)==inp.slice(1))[1];
let app = appz.find(a=>a.name==g.appd.name);
if (!app) {
inpm = {t:Date.now(),m:'(Unknown game)'};
} else if (app.version!=g.appd.version) {
inpm = {t:Date.now(),m:'(Version missmatch)'};
} else {
join = {
name: app.name,
path: app.path,
host: g.addr.addr,
port: g.addr.port,
};
}
} else {
inpm = {t:Date.now(),m:'(Invalid room ID)'};
}
}
if (join) {
break;
}
}
else if (!k.startsWith('\x1b')) {
inp += Array.from(k).map(c=>c.charCodeAt()).filter(c=>c>31).map(c=>String.fromCharCode(c)).join('');
}
}
clearInterval(ri);
if (join) {
let appd = apps.find(a=>a.name==join.name);
let app = require(path.resolve(__dirname,join.path));
app.runClient({host:join.host,port:join.port},()=>{
console.log(`\x1b[A\x1b[K`.repeat(oll)+'Joined \x1b[36m'+appd.name+'\x1b[39m '+join.host+'\x1b[90m:\x1b[39m'+join.port);
});
} else {
process.exit();
}
})();
} else {
let a = await choice(`\x1b[36m${appd.name}\x1b[39m`,{
values: [
'Host',
'Join'
],
rv : [
'host',
'join'
],
ln: false,
clear: true
});
process.stdout.write({'host':'Hosting','join':'Joining'}[a]+` \x1b[36m${appd.name}\x1b[39m`);
let app = require(path.resolve(__dirname,appd.path));
let appver = hash(fs.readFileSync(path.resolve(__dirname,appd.path)));
if (a == 'host') {
app.runServer(
addr => {
console.log(` at ${addr.address} ${addr.port} (${os.hostname()})`);
for (let int of Object.values(os.networkInterfaces())) {
let bcst = (()=>{
let a = int.find(a=>a.family == 'IPv4');
let addr = a.address.split(/\./g);
let netm = a.address.split(/\./g);
return addr.map((p,i)=>(~netm[i]&255)|p).join('.');
})();
let sock = dgram.createSocket('udp4',(msg,inf)=>{
console.log(msg,inf);
});
sock.bind(()=>{
sock.setBroadcast(true);
setInterval(()=>{
let msg = JSON.stringify({
address: addr.address,
port: addr.port,
host: os.hostname(),
appd: {
name: appd.name,
roomSize: appd.roomSize,
version: appver,
},
});
let m = Buffer.concat([
Buffer.from('LAN_GAMES'),
Buffer.from(msg),
]);
sock.send(m,0,m.length,DISCOVER_PORT,bcst);
},1000);
});
}
}
);
} else if (a == 'join') {
console.log();
let addr = await input('Address: '); if (addr == CancelChar) { process.stdout.write('\x1b[2K\x1b[A\x1b[2K\x1b[A'); process.exit(); }
let port = await input('Port: '); if (port == CancelChar) { process.stdout.write('\x1b[2K\x1b[A\x1b[2K\x1b[A\x1b[2K\x1b[A'); process.exit(); }
app.runClient({host:addr,port},()=>{
console.log('\x1b[A\x1b[2K\x1b[A\x1b[2K\x1b[A\x1b[G\x1b[KJoined \x1b[36m'+appd.name+'\x1b[39m '+addr+'\x1b[90m:\x1b[39m'+port);
});
}
}
}
);