-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·164 lines (152 loc) · 5.72 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
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
#! /usr/bin/env node
"use strict";
const fs = require("fs");
const IO = require("socket.io");
const IOClient = require("socket.io-client");
const express = require("express");
// const expressWinston = require("express-winston-2");
const listen_host = process.env['LISTEN_HOST'] || '0.0.0.0';
const listen_port = process.env['LISTEN_PORT'] || 1300;
const server_baseurl = process.env['SERVER_BASEURL'] || 'http://localhost:3000/';
let onExit;
const STDOUT = 1;
const STDERR = 2;
// run GC every minute
if (global.gc) {
setInterval(function() {
global.gc();
}, 60000);
}
process.stdin.resume(); // so the program will not close instantly
function gracefulExit(options, err) {
let msg=false, stack=false;
// 1 is stdout and 2 is stderr
fs.writeSync(STDOUT, "EXIT:"+JSON.stringify(options)+"\n");
if (err) {
if (err.message || err.name) {
fs.writeSync(STDERR, "ERROR:"+( err.name || "")+":"+( err.message || "")+"\n");
msg=true;
}
if (err.stack) {
fs.writeSync(STDERR, "ERROR TRACE:"+err.stack+"\n");
stack=true;
}
if (!msg && !stack) {
fs.writeSync(STDERR, "ERROR OBJECT:"+JSON.stringify(err)+"\n");
}
}
// avoid infinite loop
if (!options.fromExit) {
// cleanup here before exit
if (onExit) onExit();
setTimeout(function() {
process.exit();
}, 1000);
}
}
// do something when app is closing
process.on("exit", gracefulExit.bind(null, {fromExit: true}));
// catches ctrl+c event
process.on("SIGINT", gracefulExit.bind(null, {src: "SIGINT"}));
process.on("SIGTERM", gracefulExit.bind(null, {src: "SIGTERM"}));
async function onConnection(socket) {
console.log("got connection ", JSON.stringify(socket.handshake.headers));
let ua = ((socket.handshake || {}).headers || {})['user-agent'] || '';
let upstream = IOClient(server_baseurl, {
transports: ["websocket"],
transportOptions: {
polling: {extraHeaders: {'user-agent': ua}},
websocket: {extraHeaders: {'user-agent': ua}},
}
});
upstream.on("connect", function(){console.log("upstream connected")});
// await waitConnection(upstream);
// make sure one closes the other
let disconnected = false;
socket.on("disconnect", function(data) {
console.log("client disconnect");
if (disconnected) return;
console.log("sending disconnect to upstream server");
disconnected = true;
upstream.close()
});
upstream.on("disconnect", function(data) {
console.log("server disconnect");
if (disconnected) return;
console.log("sending disconnect to client");
disconnected = true;
if (typeof(socket.close)=='function') socket.close();
});
let onevent = socket.onevent;
socket.onevent = function(packet) {
// eslint-disable-next-line prefer-rest-params
onevent.apply(this, arguments); // original call
if (packet && packet.data &&
packet.data.length && packet.data.length > 0
) {
let stringified=JSON.stringify(packet.data);
let data_len = packet.data.length;
let last_type = data_len?typeof(packet.data[data_len-1]):"";
// info has id, ip and referer
console.log("SOCKET.IO packet event "+packet.data[0], {
category: "socketio",
socketio_info: socket.info,
socketio_event: packet.data[0],
socketio_length: data_len,
socketio_last_type: last_type,
socketio_size: stringified.length,
socketio_data: stringified,
});
upstream.emit.apply(upstream, packet.data);
} else {
console.log("!! no packet");
}
};
let upstream_onevent = upstream.onevent;
upstream.onevent = function(packet) {
// eslint-disable-next-line prefer-rest-params
upstream_onevent.apply(this, arguments); // original call
if (packet && packet.data &&
packet.data.length && packet.data.length > 0
) {
let stringified=JSON.stringify(packet.data);
let data_len = packet.data.length;
let last_type = data_len?typeof(packet.data[data_len-1]):"";
// info has id, ip and referer
console.log("SERVER SOCKET.IO packet event "+packet.data[0], {
category: "socketio",
socketio_info: socket.info,
socketio_event: packet.data[0],
socketio_length: data_len,
socketio_last_type: last_type,
socketio_size: stringified.length,
socketio_data: stringified,
});
socket.emit.apply(socket, packet.data);
} else {
console.log("!! no packet");
}
};
}
function start() {
// Setup basic express server
let app = express();
// app.use(proj.logger.express_res_send_error);
// app.use(expressWinston.logger({winstonInstance: proj.logger, getLogLevel: (status) => (status >= 400)?"error":"info",}));
let server;
server = require("http").createServer(app);
let io = IO(server);
io.on("connection", onConnection);
onExit = function() {
let s=io.sockets.sockets;
s=typeof(s)=='object'?Object.values(s):s;
console.info("disconnecting %d users", s.length);
for (let item of s) item.disconnect();
};
app.use(express.static(__dirname + "/../../public"));
// app.use(expressWinston.errorLogger({winstonInstance: proj.logger,}));
server.listen(listen_port, listen_host, function() {
console.info("Repeater is listening at port ", listen_port);
});
}
start();