forked from vircadia/screenshare-prototype
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshare-relay.js
158 lines (128 loc) · 4.8 KB
/
screenshare-relay.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
//
// screenshare-relay.js
//
// Created by Kalila L. on 17 Oct 2020
// Updated by R3ido101 on 28 Jan 2023
// Copyright 2020 Vircadia and contributors.
// Copyright 2023 Overte and contributors.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Use the websocket-relay to serve a raw MPEG-TS over WebSockets. You can use
// ffmpeg to feed the relay. ffmpeg -> websocket-relay -> browser
// Example:
// node websocket-relay yoursecret 8081 8082
// ffmpeg -i <some input> -f mpegts http://localhost:8081/yoursecret
// This is to help it work on Plesk.
if (typeof(PhusionPassenger) != 'undefined') {
PhusionPassenger.configure({ autoInstall: false });
}
import fs from 'fs'
import Express from 'express';
import http from 'http'
import { publicIpv4 } from 'public-ip';
import { WebSocket, WebSocketServer } from 'ws';
import { cwd } from 'process';
var server = Express();
function getArgs() {
const args = process.argv.slice(2);
let params = {};
args.forEach(a => {
const nameValue = a.split("=");
params[nameValue[0]] = nameValue[1];
});
return params;
}
if (process.argv.length < 3) {
console.log(
'Usage: \n' +
'node websocket-relay.js stream_port=<stream-port> stream_secret=<stream-secret> ws_port=<websocket-port> ws_secret=<websocket-secret> client_http_port=<client-http-web-port>'
);
// process.exit();
}
const args = getArgs();
var STREAM_PORT = args.stream_port || 8020;
var STREAM_SECRET = args.stream_secret || "open";
var WEBSOCKET_PORT = args.ws_port || 8021;
var WEBSOCKET_SECRET = args.ws_secret || "open";
var CLIENT_HTTP_PORT = args.client_http_port || 8022;
var RECORD_STREAM = false;
// Websocket Server
var socketServer = new WebSocketServer({path: "/" + WEBSOCKET_SECRET, port: WEBSOCKET_PORT, perMessageDeflate: false});
socketServer.connectionCount = 0;
socketServer.on('connection', function(socket, upgradeReq) {
//RESTRICTED CONNECTION PARAMETERS
//if(client.headers['user-agent'].includes("NT 10.0") == true)
socketServer.connectionCount++;
console.log(
'New WebSocket Client Connection: ',
(upgradeReq || socket.upgradeReq).socket.remoteAddress,
(upgradeReq || socket.upgradeReq).headers['user-agent'],
'('+socketServer.connectionCount+' total)'
);
socket.on('close', function(code, message){
socketServer.connectionCount--;
console.log(
'Disconnected WebSocket ('+socketServer.connectionCount+' total)'
);
});
});
socketServer.broadcast = function(data) {
socketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};
// HTTP Server to accept incoming MPEG-TS Stream from ffmpeg / OBS
var streamServer = http.createServer( function (request, response) {
var params = request.url.substr(1).split('/');
if (params[0] !== STREAM_SECRET) {
console.log(
'Failed Stream Connection: '+ request.socket.remoteAddress + ':' +
request.socket.remotePort + ' - wrong secret.'
);
response.end();
}
response.connection.setTimeout(0);
console.log(
'Stream Connected: ' +
request.socket.remoteAddress + ':' +
request.socket.remotePort
//request.socket.headers['user-agent']
);
request.on('data', function(data){
socketServer.broadcast(data);
if (request.socket.recording) {
request.socket.recording.write(data);
}
});
request.on('end',function(){
console.log('close');
if (request.socket.recording) {
request.socket.recording.close();
}
});
// Record the stream to a local file?
if (RECORD_STREAM) {
var path = 'recordings/' + Date.now() + '.ts';
request.socket.recording = fs.createWriteStream(path);
}
//server.listen('passenger');
}).listen(STREAM_PORT);
// Client HTTP Server
var serverPublicIP;
server.get('/api/wsSource', (req, res, next) => {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ wsSource: 'ws://' + serverPublicIP + ':' + WEBSOCKET_PORT + '/' }));
next();
});
server.use('/', Express.static(cwd() + '/client'));
server.listen(CLIENT_HTTP_PORT);
(async () => {
serverPublicIP = await publicIpv4() || '127.0.0.1';
console.log('Listening for incoming MPEG-TS Stream on http://' + serverPublicIP + ':' + STREAM_PORT + '/' + STREAM_SECRET);
console.log('Awaiting WebSocket connections on ws://' + serverPublicIP + ':' + WEBSOCKET_PORT + '/' + WEBSOCKET_SECRET);
console.log('View the stream through http://' + serverPublicIP + ':' + CLIENT_HTTP_PORT + '/index.html');
})();