-
Notifications
You must be signed in to change notification settings - Fork 0
/
poke.js
192 lines (168 loc) · 5.33 KB
/
poke.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
const aws = require("aws-sdk");
const { exec } = require("child_process");
const dns = require("dns");
const fs = require("fs");
const path = require('path');
const https = require("https");
const decompress = require('decompress');
const archiver = require("archiver");
const crypto = require("crypto");
const { v4: uuidv4 } = require("uuid");
const zlib = require("zlib");
const { parseOutput } = require("./strace-parser");
const { Parser } = require('acorn');
const parseSquishVersion = (codePath) => {
const parsed = Parser.parse(fs.readFileSync(codePath));
const foundGameClasses = parsed.body.filter(n => n.type === 'ClassDeclaration' && n.superClass?.name === 'Game');
if (foundGameClasses.length !== 1) {
throw new Error('Top-level file should have one defined game class');
}
const foundGame = foundGameClasses[0];
const foundConstructors = foundGame.body.body.filter(n => n.key?.name === 'metadata' && n.kind === 'method');
if (foundConstructors.length !== 1) {
throw new Error('Game needs one constructor');
}
const foundConstructor = foundConstructors[0];
let foundSquishVersion;
foundConstructor.value.body.body.forEach(n => {
const squishVersionNodes = n.argument.properties.filter(n => n.key?.name === 'squishVersion');
if (squishVersionNodes.length > 1 || (foundSquishVersion && squishVersionNodes.length == 1)) {
throw new Error('Multiple squish versions found');
}
if (squishVersionNodes.length === 1) {
foundSquishVersion = squishVersionNodes[0].value.value;
}
});
if (!foundSquishVersion) {
throw new Error('No squish version found');
}
return foundSquishVersion;
};
const checkIndex = (path) =>
new Promise((resolve, reject) => {
fs.access(`${path}`, fs.F_OK, (err) => {
if (err) {
reject();
} else {
resolve(path);
}
});
});
const homegamesPoke = (
entryPoint,
squishVersion,
) =>
new Promise((resolve, reject) => {
const cmd =
"strace node tester " +
entryPoint +
" " +
require.resolve("squish-" + squishVersion);
console.log("running command");
console.log(cmd);
dns.resolve("api.homegames.io", "A", (err, hosts) => {
const whitelistedIps = hosts;
let failed = false;
const listeners = {
socket: (line) => {
console.log("doing something with a socket at: " + line);
},
connect: (line) => {
const ipRegex = new RegExp('inet_addr\\("(\\S*)"', "g");
const _match = ipRegex.exec(line);
if (_match) {
const requestedIp = _match[1];
if (whitelistedIps.indexOf(requestedIp) < 0) {
emitEvent(
publishRequest.requestId,
EVENT_TYPE.FAILURE,
`Made network request to unknown IP: ${requestedIp}`,
);
failed = true;
}
}
},
open: (line) => {
// console.log('not sure what to do here yet: ' + line);
},
read: (line) => {
// console.log('not sure what to do here yet: ' + line);
},
};
try {
exec(cmd, { maxBuffer: 1024 * 10000 }, (err, stdout, straceOutput) => {
parseOutput(straceOutput, listeners);
console.log(stdout);
if (failed || err) {
console.error("failed");
console.error(err);
reject("Runtime error");
} else {
resolve();
}
});
} catch (err) {
console.log("is this where it fails");
console.log(err);
}
});
});
const pokeCode = (
indexPath,
codeDir,
squishVersion,
) =>
new Promise((resolve, reject) => {
console.log("i am poking code");
console.log(indexPath);
checkIndex('/thangs/test_unzipped/' + indexPath.path)
.then((entryPoint) => {
console.log('entry point');
console.log(entryPoint);
homegamesPoke(
entryPoint,
squishVersion,
)
.then(() => {
resolve();
})
.catch((err) => {
console.log("Failed homegames poke");
console.log(err);
reject();
});
})
.catch(() => {
reject();
});
});
console.log("about to do this with");
console.log(process.argv[2]);
const writeExitMessage = (msg) => {
console.log(
"AYYYYYYYYYLMAOTHISISTHEEXITMESSAGE:" +
msg +
"::andthatwastheendofthemessage",
);
};
console.log('want me to get thing');
console.log(process.argv[2]);
const codePath = '/thangs/test.zip';//process.argv[2];
const dirPath = '/thangs/test_unzipped';
decompress(codePath, dirPath).then((files) => {
console.log("HER ARE FILES from " + codePath + " to " + dirPath);
console.log(files);
const foundIndex = files.filter(f => f.type === 'file' && f.path.endsWith('index.js'))[0];
const squishVersion = parseSquishVersion(path.join(dirPath, foundIndex.path));
pokeCode(foundIndex, dirPath, squishVersion)
.then(() => {
console.log("just poked!!! updated");
writeExitMessage("success-" + squishVersion);
process.exit(0);
})
.catch((err) => {
console.log("errororor");
console.log(err);
writeExitMessage("ayylmao456");
});
});