-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·222 lines (194 loc) · 9.74 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
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
216
217
218
219
220
221
222
'use strict';
const dgram = require('dgram');
const { createSerializer, createDeserializer } = require('raknet/src/transforms/serializer');
const Observer = require('./source/observer');
const Connector = require('./source/connector');
// Configuration
const CBWX_ANNOUNCE_BROADCAST_DELAY_MS = parseInt(process.env.CBWX_ANNOUNCE_BROADCAST_DELAY_MS || '4000');
const CBWX_ANNOUNCE_BROADCAST_IP = (process.env.CBWX_ANNOUNCE_BROADCAST_IP || '255.255.255.255');
const CBWX_ANNOUNCE_BROADCAST_PORT = parseInt(process.env.CBWX_ANNOUNCE_BROADCAST_PORT || '4445');
const CBWX_ANNOUNCE_LISTEN_PORT = parseInt(process.env.CBWX_ANNOUNCE_LISTEN_PORT || '19132');
const CBWX_MCBE_CONNECT_PORT = parseInt(process.env.CBWX_MCBE_CONNECT_PORT || '19132');
const CBWX_MCJE_CONNECT_PORT = parseInt(process.env.CBWX_MCJE_CONNECT_PORT || '25565');
var broadcaster;
var intervals = [];
// Mapping from container id to connector instance
const connectors = {};
// Observe active docker containers
const observer = new Observer();
// Handle a server being added
observer.on('serverAdded', server => {
console.log(`Server added - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Name:(${server.announceName})`);
if (server.ipAddress) {
if (server.type == "mcbe") { //Bedrock Edition
let internalMCBEPort = CBWX_MCBE_CONNECT_PORT;
// Find the mapping for the internal server port
let portMapping = server.portMappings.find(portMapping => portMapping.privatePort === internalMCBEPort);
// Default to using the only port mapping there is
if (!portMapping && server.portMappings.length === 1) {
portMapping = server.portMappings[0];
}
if (portMapping) {
console.log(`Server running - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Name:(${server.announceName}) IP:(${server.ipAddress}) Internal port:(${portMapping.privatePort}/udp) External port:(${portMapping.publicPort}/udp)`);
const connector = new Connector(server.name, server.type, server.ipAddress, portMapping.privatePort, portMapping.publicPort);
connectors[server.id] = connector;
connector.on('changed', (oldState, newState) => {
console.log(`Server status - Container:(${connector.name}) Type:(${server.type}) ID:(${server.shortid}) Name:(${server.announceName}) IP:(${server.ipAddress}) Internal Port:(${portMapping.privatePort}) Old:(${oldState}) New:(${newState})`);
});
connector.on('error', error => {
console.error(`${connector.name} ${error.message}`);
});
} else {
console.error(`Server ${server.name} has no mapping for internal port ${internalMCBEPort}`);
}
} else if (server.type == "mcje") { //Java Edition
let internalMCJEPort = CBWX_MCJE_CONNECT_PORT;
// Find the mapping for the internal server port
let portMapping = server.portMappings.find(portMapping => portMapping.privatePort === internalMCJEPort);
// Default to using the only port mapping there is
if (!portMapping && server.portMappings.length === 1) {
portMapping = server.portMappings[0];
}
if (portMapping) {
console.log(`Server running - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Name:(${server.announceName}) IP:(${server.ipAddress}) Internal port:(${portMapping.privatePort}) External port:(${portMapping.publicPort})`);
const connector = new Connector(server.name, server.type, server.ipAddress, portMapping.privatePort, portMapping.publicPort);
connectors[server.id] = connector;
// run broadcast function every CBWX_ANNOUNCE_BROADCAST_DELAY_MS
console.log(`Server broadcasting - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Message:([MOTD]${server.announceName}[/MOTD][AD]${portMapping.publicPort}[/AD])`)
intervals[server.shortid] = setInterval(broadcast, CBWX_ANNOUNCE_BROADCAST_DELAY_MS,`${server.announceName}`,`${server.type}`,`${portMapping.publicPort}`);
} else {
console.error(`Server ${server.name} has no mapping for internal port ${internalMCJEPort}`);
}
} else if (server.type == "proxy") { //CWBX Java Proxy
let internalProxyPort = CBWX_MCJE_CONNECT_PORT;
// Find the mapping for the internal server port
let portMapping = server.portMappings.find(portMapping => portMapping.privatePort === internalProxyPort);
// Default to using the only port mapping there is
if (!portMapping && server.portMappings.length === 1) {
portMapping = server.portMappings[0];
}
if (portMapping) {
console.log(`Server running - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Name:(${server.announceName}) IP:(${server.ipAddress}) Internal port:(${portMapping.privatePort}) External port:(${portMapping.publicPort})`);
const connector = new Connector(server.name, server.type, server.ipAddress, portMapping.privatePort, portMapping.publicPort);
connectors[server.id] = connector;
// run broadcast function every CBWX_ANNOUNCE_BROADCAST_DELAY_MS
console.log(`Server broadcasting - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Message:([MOTD]${server.announceName}[/MOTD][AD]${portMapping.publicPort}[/AD])`)
intervals[server.shortid] = setInterval(broadcast, CBWX_ANNOUNCE_BROADCAST_DELAY_MS,`${server.announceName}`,`${server.type}`,`${portMapping.publicPort}`);
} else {
console.error(`Server ${server.name} has no mapping for internal port ${internalProxyPort}`);
}
}
} else {
console.error(`Server ${server.name} has no ip address`);
}
});
// Handle a server being removed
observer.on('serverRemoved', server => {
console.log(`Server removed - Container:(${server.name}) Type:(${server.type}) ID:(${server.shortid}) Name:(${server.announceName})`);
clearInterval(intervals[server.shortid]);
const connector = connectors[server.id];
if (connector) {
connector.close();
delete connectors[server.id];
}
});
// Respond to a ping from a Bedrock Minecraft client
function handleClientPing (socket, host, port, data) {
const parser = createDeserializer(true);
const serializer = createSerializer(true);
parser.on('data', (parsed) => {
if (parsed.data.name === 'unconnected_ping') {
for (const connector of Object.values(connectors)) {
if (connector.remoteServerID !== null) {
const updatedServerName = connector.remoteServerName.replace(connector.privatePort, connector.publicPort);
serializer.write({
name: 'unconnected_pong',
params: {
pingID: parsed.data.params.pingID,
serverID: connector.remoteServerID,
magic: connector.remoteServerMagic,
serverName: updatedServerName
}
});
}
}
} else {
console.error('Received unexpected packet on listen port:', parsed.data.name);
}
});
serializer.on('data', (chunk) => {
socket.send(chunk, 0, chunk.length, port, host);
});
parser.write(data);
}
//Broadcast mcje servers
function broadcast(announceName, type, publicPort) {
let msg = Buffer.from(`[MOTD]${announceName}[/MOTD][AD]${publicPort}[/AD]`);
if (msg) {
if (broadcaster) {
broadcaster.send(msg, 0, msg.length, CBWX_ANNOUNCE_BROADCAST_PORT, CBWX_ANNOUNCE_BROADCAST_IP);
} else {
broadcaster = dgram.createSocket('udp4');
broadcaster.bind(CBWX_ANNOUNCE_BROADCAST_PORT);
broadcaster.on('listening', function () {
broadcaster.setBroadcast(true);
broadcaster.send(msg, 0, msg.length, CBWX_ANNOUNCE_BROADCAST_PORT, CBWX_ANNOUNCE_BROADCAST_IP);
});
broadcaster.on('error', error => {
console.error("Cannot bind broadcaster");
});
}
}
}
// Check configuration
if (CBWX_ANNOUNCE_BROADCAST_IP) {
console.log(`CBWX_ANNOUNCE_BROADCAST_IP=${CBWX_ANNOUNCE_BROADCAST_IP}`);
} else {
console.error("ERROR: No broadcast ip specified, CBWX_ANNOUNCE_BROADCAST_IP cannot be empty");
process.exit(1);
}
if (CBWX_ANNOUNCE_BROADCAST_PORT) {
console.log(`CBWX_ANNOUNCE_BROADCAST_PORT=${CBWX_ANNOUNCE_BROADCAST_PORT}`);
} else {
console.error("ERROR: No broadcast port specified, CBWX_ANNOUNCE_BROADCAST_PORT cannot be empty");
process.exit(1);
}
if (CBWX_ANNOUNCE_LISTEN_PORT) {
console.log(`CBWX_ANNOUNCE_LISTEN_PORT=${CBWX_ANNOUNCE_LISTEN_PORT}`);
} else {
console.error("ERROR: No listen port specified, CBWX_ANNOUNCE_LISTEN_PORT cannot be empty");
process.exit(1);
}
if (CBWX_MCBE_CONNECT_PORT) {
console.log(`CBWX_MCBE_CONNECT_PORT=${CBWX_MCBE_CONNECT_PORT}`);
} else {
console.error("ERROR: No connect port specified, CBWX_MCBE_CONNECT_PORT cannot be empty");
process.exit(1);
}
if (CBWX_MCJE_CONNECT_PORT) {
console.log(`CBWX_MCJE_CONNECT_PORT=${CBWX_MCJE_CONNECT_PORT}`);
} else {
console.error("ERROR: No connect port specified, CBWX_MCJE_CONNECT_PORT cannot be empty");
process.exit(1);
}
// Listen for broadcast pings from minecraft clients
const socket = dgram.createSocket({ type: 'udp4' });
socket.on('listening', () => {
const address = socket.address();
console.log(`Listening for pings at ${address.address}:${address.port}`);
});
socket.on('message', (data, { port, address }) => {
handleClientPing(socket, address, port, data);
});
// Let's go
observer.start();
socket.bind(CBWX_ANNOUNCE_LISTEN_PORT);
// Listen for termination message
process.on('SIGTERM', function onSigterm () {
console.info('Graceful shutdown on SIGTERM');
for (const connector of Object.values(connectors)) {
connector.close();
}
observer.close();
process.exit();
});