-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws-server.js
158 lines (147 loc) · 5.14 KB
/
ws-server.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
require('dotenv').config();
const ws = require('ws');
const { spawn } = require('child_process');
const path = require('path');
const crypto = require('crypto');
const fs = require('fs').promises;
const zlib = require('zlib');
const { promisify } = require('util');
const gunzip = promisify(zlib.gunzip);
const express = require('express');
function safeCompare(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') return false
const key = crypto.randomBytes(32)
const ha = crypto.createHmac('sha256', key).update(a).digest()
const hb = crypto.createHmac('sha256', key).update(b).digest()
return ha.length === hb.length && crypto.timingSafeEqual(Buffer.from(ha), Buffer.from(hb)) && a === b;
}
class CloudscriptRemoteClient {
constructor(socket) {
this.socket = socket;
this.startCloudscript = this.startCloudscript.bind(this);
this.handleMessage = this.handleMessage.bind(this);
this.close = this.close.bind(this);
this.ping = this.ping.bind(this);
this.socket.on('message', this.handleMessage);
this.socket.once('close', this.close);
this.filename = `./cloudscript.${crypto.randomUUID().toString()}.js`;
this.interval = null;
this.titleId = null;
this.titleSecret = null;
this.dataBuffer = "";
}
startCloudscript() {
this.serverInstance = spawn(process.execPath, [path.join(__dirname, 'cloudscript-remote-runner.js'), this.filename, this.titleId, this.titleSecret], {
detached: true,
stdio: 'pipe'
});
this.serverInstance.stdout.on('data', data => {
this.dataBuffer += data.toString();
let boundary = this.dataBuffer.indexOf('\n');
while (boundary !== -1) {
let completeMessage = this.dataBuffer.slice(0, boundary + 1);
this.dataBuffer = this.dataBuffer.slice(boundary + 1);
try {
let response = JSON.parse(completeMessage);
switch (response.type) {
case 'response':
case 'playfab-log':
case 'error-log':
this.socket.send(completeMessage);
return;
default:
break;
}
}
catch (e) {
}
try {
this.socket.send(JSON.stringify({ type: 'log', data: completeMessage }));
}
catch (e) {
}
boundary = this.dataBuffer.indexOf('\n');
}
});
this.serverInstance.stderr.on('data', data => {
try {
this.socket.send(JSON.stringify({ type: 'error', data: data.toString() }));
}
catch (e) {
}
});
this.interval = setInterval(this.ping, 5000);
this.socket.send(JSON.stringify({ type: 'log', data: 'remote server started!' }));
}
async handleMessage(message) {
message = message.toString();
try {
let parsed = JSON.parse(message);
switch (parsed.type) {
case 'request':
if (this.serverInstance != null)
this.serverInstance.stdin.write(JSON.stringify(parsed.data));
break;
case 'create':
let uncompressed = await gunzip(Buffer.from(parsed.data, 'base64'));
await fs.writeFile(this.filename, uncompressed);
this.titleId = parsed.titleId;
this.titleSecret = parsed.titleSecret;
this.startCloudscript();
break;
default:
break;
}
}
catch (e) {
console.error(e);
}
}
ping() {
if (this.serverInstance != null)
this.serverInstance.stdin.write('1');
}
async close() {
try {
if (this.interval != null)
clearInterval(this.interval);
}
catch (e) {
}
try {
if (this.serverInstance != null)
this.serverInstance.kill();
}
catch (e) {
}
try {
if (this.socket != null)
this.socket.close();
}
catch (e) {
}
try {
await fs.unlink(this.filename);
}
catch (e) {
}
this.socket = null;
this.serverInstance = null;
this.interval = null;
}
}
const wss = new ws.WebSocketServer({ port: 8040 });
wss.on('connection', socket => new CloudscriptRemoteClient(socket));
console.log("websocket server started");
const app = express();
app.get('/auth', (req, res) => {
const token = req?.headers?.authorization;
if (typeof token != 'string')
return res.sendStatus(401);
if (!safeCompare(token, process.env['REMOTE_SERVER_AUTH']))
return res.sendStatus(403);
res.sendStatus(200);
});
app.listen(3000, () => {
console.log('Auth server listening on port 3000');
});