-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (117 loc) · 4.04 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
const net = require('net');
const os = require('os');
const commandLineArgs = require('command-line-args');
const express = require('express');
const robot = require('robotjs');
const app = express();
app.use(express.json());
// https://xkcd.com/221/
const multipartBoundary = 'ffb88cad-a6a7-4546-8620-b1422d38919d';
const screenToTcp = require('./build/Release/screenToTcp');
const options = commandLineArgs([
{ name: 'width', type: Number, defaultValue: 1920 },
{ name: 'height', type: Number, defaultValue: 1080 },
{ name: 'framerate', type: Number, defaultValue: 30 },
{ name: 'quality', type: Number, defaultValue: 65 },
{ name: 'extend', type: Boolean, defaultValue: false },
{ name: 'extend-width', type: Number, defaultValue: 1920 },
{ name: 'extend-height', type: Number, defaultValue: 1080 },
{ name: 'allow-input', type: Boolean, defaultValue: false },
{ name: 'tcp-port', type: Number, defaultValue: 12802 },
{ name: 'http-port', type: Number, defaultValue: 8080 }
]);
const promises = [];
promises.push(new Promise((resolve, reject) => {
const tcpServer = net.createServer((socket) => {
console.log('tcp connection');
resolve();
socket.on('data', (data) => {
responses.forEach((res) => {
res.write(data);
});
});
socket.on('close', () => {
responses.forEach((res) => {
res.end();
});
});
});
tcpServer.listen(options['tcp-port'], () => {
console.log('tcp listening', {
port: tcpServer.address().port
});
const { screenWidth, screenHeight, screenX, screenY, width, height } = screenToTcp.start(options.width, options.height, options.framerate, options.quality, options.extend, options['extend-width'], options['extend-height'], options['tcp-port'], multipartBoundary);
const scaleCoordinates = (x, y) => {
return {
x: x / width * screenWidth + screenX,
y: y / height * screenHeight + screenY
};
};
if (options['allow-input']) {
app.post('/move', (req, res) => {
const { x, y, down } = req.body;
if (typeof x !== 'number' || typeof y !== 'number' || typeof down !== 'boolean') {
res.sendStatus(400);
return;
}
const scaled = scaleCoordinates(x, y);
if (down) {
robot.dragMouse(scaled.x, scaled.y);
} else {
robot.moveMouse(scaled.x, scaled.y);
}
res.sendStatus(204);
});
app.post('/click', (req, res) => {
const { x, y, down, right } = req.body;
if (typeof down !== 'boolean' || typeof right !== 'boolean') {
res.sendStatus(400);
return;
}
if (typeof x === 'number' && typeof y === 'number') {
const scaled = scaleCoordinates(x, y);
robot.moveMouse(scaled.x, scaled.y);
}
robot.mouseToggle(down ? 'down' : 'up', right ? 'right' : 'left');
res.sendStatus(204);
});
}
});
}));
const responses = new Set();
app.get('/', (req, res) => {
if (req.query.no_redirect !== 'true') {
res.redirect('/view');
return;
}
res.set({
'Cache-Control': 'max-age=0, no-cache, must-revalidate',
'Content-Type': `multipart/x-mixed-replace; boundary=${multipartBoundary}`
});
responses.add(res);
res.on('close', () => {
responses.delete(res);
});
});
app.get('/view', (req, res) => {
res.sendFile(`${__dirname}/res/view.html`);
});
promises.push(new Promise((resolve, reject) => {
const httpServer = app.listen(options['http-port'], () => {
const { port } = httpServer.address();
console.log('http listening', {
port
});
resolve(port);
});
}));
Promise.all(promises).then((values) => {
const port = values[1];
const networkInterfaces = os.networkInterfaces();
const ips = Object.values(networkInterfaces)
.flat()
.filter(({ family, internal }) => !internal && family === 'IPv4')
.map(({ address }) => address);
const urls = ['localhost', ...ips].map((ip) => `http://${ip}:${port}/view`);
console.log(`\n✅ ready to rock! Get started at:\n\n${urls.join('\n')}`);
});