forked from BrowserBox/BrowserBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spdy-server.js
72 lines (55 loc) · 2.05 KB
/
spdy-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
import express from 'express';
import spdy from 'spdy';
import fs from 'fs';
import zl from './zombie-lord/api.js';
import path from 'path';
import bodyParser from 'body-parser';
import {DEBUG} from './common.js';
import {timedSend, eventSendLoop} from './server.js';
const options = {
key: fs.readFileSync(path.join(__dirname, "certs", "server.key")),
cert: fs.readFileSync(path.join(__dirname, "certs", "server.crt")),
};
const version = 'v1';
export async function start_spdy_server(port, zombie_port) {
console.log(`Starting SPDY server on port ${port}`);
const app = express();
const server_port = port;
app.use(express.static('public'));
app.use(bodyParser.json({extended:true}));
const {data:{targetInfos}} = await timedSend({
name: "Target.getTargets",
params: {}
}, zombie_port);
const browserTargetId = targetInfos[0].targetId;
function addHandlers() {
app.post(`/api/${version}/zombie`, async (req,res) => {
const Data = [], Frames = [], Meta = [];
const {events} = req.body;
await eventSendLoop(events, {Data, Frames, Meta});
res.type('json');
return res.end(JSON.stringify({data:Data, frameBuffer:Frames, meta:Meta}));
});
app.get(`/api/${version}/tabs`, async (req, res) => {
res.type('json');
let {data:{targetInfos:tabs}} = await timedSend({
name: "Target.getTargets",
params: {}
}, zombie_port);
const activeTarget = zl.act.getActiveTarget(zombie_port);
zl.act.addTargets(tabs, zombie_port);
tabs = (tabs || []).filter(({targetId}) => targetId != browserTargetId);
res.end(JSON.stringify({tabs,activeTarget}));
});
}
const server = spdy.createServer(options, app);
server.listen(server_port, async err => {
if ( err ) {
console.error(err);
process.exit(1);
} else {
console.log({uptime:new Date, message:'spdy server up', server_port});
addHandlers();
}
});
}