-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathserver.js
215 lines (199 loc) · 7.83 KB
/
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
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
/* entry point */
import fs from 'fs';
import path from 'path';
import {spawn} from 'child_process';
import exitOnExpipe from 'exit-on-epipe';
import express from 'express';
import zl from './zombie-lord/index.js';
import {MAX_FRAMES} from './zombie-lord/screenShots.js';
import {CONFIG, EXPEDITE, COMMAND_MAX_WAIT,DEBUG,GO_SECURE,sleep,throwAfter} from './common.js';
import {start_ws_server} from './ws-server.js';
import {release,applicationCheck} from './hard/application.js';
const BEGIN_AGAIN = 500;
import {
chrome_port, app_port, cookie,
/*username,*/ token, start_mode
} from './args.js';
const demoBlock = token == 'demotoken';
let ws_started = false;
let lastDebugOrderId = -Infinity;
let server;
let targetSaver;
let licenseValid;
//let zombie_started = false;
if ( GO_SECURE && start_mode == "signup" ) {
const redirector = express();
redirector.get('/*path', (req,res) => {
res.redirect('https://' + req.headers.host + req.url);
});
redirector.listen(80, () => DEBUG.val && console.log('listening on 80 for https redirect'));
}
try {
process.title = "browserbox";
} catch(e) {
console.info(`Could not set process title. Current title: ${process.title}`, e);
}
try {
licenseValid = await applicationCheck();
console.log({licenseValid});
} catch(e) {
console.warn(`Application check error:`, e);
licenseValid = false;
}
if ( ! licenseValid ) {
const stopper = spawn('stop_bbpro', [], {
detached: true,
stdio: 'ignore' // Detach completely from parent's stdio
});
stopper.unref(); // Ensure parent doesn’t wait for child
stopper.on('close', () => setTimeout(() => process.exit(1), 1001) );
}
process.on('uncaughtException', err => {
console.log('ue', err, err.stack);
//zl.life.kill(chrome_port);
//begin();
});
process.on('unhandledRejection', err => {
console.log('ur', err, err.stack);
//zl.life.kill(chrome_port);
//begin();
});
process.on('error', err => {
console.log('e', err, err.stack);
//zl.life.kill(chrome_port);
//begin();
});
process.on('beforeExit', () => {
clearInterval(targetSaver);
});
begin();
async function begin() {
let port;
if ( start_mode !== "signup" ) {
try {
({port} = await zl.life.newZombie({port: chrome_port, /*username*/}));
zl.act.setOptions({demoBlock});
} catch(e) {
console.warn("ZL start error", e);
zl.life.kill(chrome_port);
setTimeout(begin, BEGIN_AGAIN);
}
if ( port !== chrome_port ) {
zl.life.kill(port);
throw Error(`Port must match port allocated: app port ${port}, chrome port ${chrome_port}`);
}
DEBUG.val && console.log({zombie:"gnawing at port ", port});
await sleep(BEGIN_AGAIN);
}
if ( ! ws_started ) {
server = await start_ws_server(
app_port, chrome_port, cookie, token,
);
ws_started = true;
}
if ( ! targetSaver ) {
targetSaver = setInterval(saveTargetCount, 13001);
}
}
export async function releaseLicense() {
await release();
}
export async function timedSend(command, port) {
if ( command.dontWait ) {
command.dontWait = false;
console.warn(`Can't set don't wait outside server`);
}
if ( EXPEDITE.has(command.name) && ! command.definitelyWait ) {
command.dontWait = true;
}
if ( DEBUG.neverWait || command.dontWait ) {
return zl.act.send(command, port);
} else {
const CancelTimer = {};
const Saver = {};
const result = await Promise.race([
zl.act.send(command, port, Saver),
throwAfter(COMMAND_MAX_WAIT, command, port, CancelTimer)
]);
/* The above block introduces bugs where if we wait for commands in a timeout way then the timeout firest
and we never get the result of the command so it is essentially lost
An idea to solve that is to use Saver object that saves the results anyway and they can be consumed from there...
somehow...when they are ready while allowing the next command to be sent without blocking the line
Honestly I don't even know if commands can block the line anymore.
The above DEBUG.neverWait is a new "hack" to get around the issue of this block
A more comprehensive solution would involve implementing this Saver object and somehow later consuming it.
Not hard, just seems unnecessary right now
*/
console.log({result, location: 'commandTimeoutBlock'});
CancelTimer.do();
return result;
}
}
export async function eventSendLoop(events, {Data, Frames, Meta, State, receivesFrames, messageId, connectionId}) {
DEBUG.metaDebug && DEBUG.val && console.log('before loop', messageId, {Meta});
for ( const {command} of events ) {
try {
command.receivesFrames = receivesFrames && ! command.isZombieLordCommand;
command.connectionId = connectionId;
if ( DEBUG.val ) {
if ( command.isBufferedResultsCollectionOnly ) {
DEBUG.brc && console.log(`Sending ${JSON.stringify(command)}...`);
} else {
console.log(`Sending ${JSON.stringify(command)}...`);
}
}
if ( DEBUG.debugKeyEvents && (
command?.name?.startsWith?.("Input.dispatchKey") || command?.name?.startsWith?.("Input.insertText")
) ) {
console.info(`[eventSendLoop]: sending ${command.params.key || command.params.text} (${command.params.type ? command.params.type.slice(3) : 'press -> insert'}) (batch size: ${events.length})`);
//console.log(JSON.stringify(command,null,2));
}
if ( DEBUG.debugCommandOrder ) {
if ( command.debugOrderId <= lastDebugOrderId ) {
console.info(`Command sequence disorder: about to emit ${command.debugOrderId}, but last was ${lastDebugOrderId}`);
console.log(command);
}
lastDebugOrderId = command.debugOrderId;
delete command.debugOrderId;
}
const sendResult = await timedSend(command, chrome_port);
DEBUG.debugSendResult && console.log(JSON.stringify({sendResult}));
if ( sendResult ) {
const {data,frameBuffer,meta,totalBandwidth} = sendResult;
DEBUG.metaDebug && DEBUG.val && console.log('meta 1', {meta});
Data.push(data);
if ( meta ) {
// filter out all but the last resource for each targetId
// to save bandwidth
const latestResourceForTarget = {};
const nonResourceMeta = meta.filter(mi => {
DEBUG.metaDebug && DEBUG.val && console.log({mi});
if ( ! mi.resource ) return true;
latestResourceForTarget[mi.resource.targetId] = mi;
return false;
});
//DEBUG.metaDebug && console.log(JSON.stringify({meta}), JSON.stringify({nonResourceMeta}));
Meta.push(...nonResourceMeta, ...Object.values(latestResourceForTarget));
//Meta.push(...meta);
}
if ( frameBuffer ) {
Frames.push(...frameBuffer);
const mf = MAX_FRAMES();
while(Frames.length > mf) Frames.shift();
}
State.totalBandwidth = totalBandwidth;
} else {
if ( DEBUG.metaDebug || DEBUG.dataDebug ) console.log(`Did not wait to get the meta or data results`);
}
} catch(e) {
console.warn(e);
Data.push({error:e+''});
}
}
DEBUG.metaDebug && DEBUG.val && console.log('after loop', {Meta});
}
// necessary to know if we closed browser with any tabs or not, so we know whether to open a 'home page' tag when we start again
function saveTargetCount() {
const targetCount = zl.act.getTargets(chrome_port).length;
fs.writeFileSync(path.resolve(CONFIG.baseDir, 'targetCount'), `${targetCount}`);
}