This repository has been archived by the owner on Aug 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
Β·143 lines (123 loc) Β· 4.12 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
/**
* Cauldron.js - Minecraft Server in your browser
*
* @version 0.1.0
* @copyright Copyright vantezzen (https://github.com/vantezzen)
* @link https://github.com/vantezzen/cauldron-js
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
// Import libraries
const chalk = require('chalk')
const express = require('express')
const http = require('http')
const mc = require('minecraft-protocol')
const path = require('path')
const ip = require('ip')
const debug = require('debug')('cauldron:backend')
// Display welcome message
console.log(chalk.cyan('Cauldron.js - Minecraft Server in your browser'))
// Setup express server
const app = express()
app.use(express.static(path.join(__dirname, '/client')))
const server = http.createServer(app)
// HTTP server routes
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/client/index.html'))
})
// Setup socket.io server
const io = require('socket.io')(server, {
cookie: false
})
const servers = {} // List of minecraft servers and clients connected
let nextPort = 25500 // Port used for next server
const availiblePorts = [] // Ports that are lower than `port` but are availible again
const stopServer = (id, reason) => {
// Disconnect all clients and show custom message
Object.keys(servers[id].server.clients).forEach(clientId => {
const client = servers[id].server.clients[clientId]
client.end(reason)
})
// Close server
servers[id].server.close()
// Make port availible for next server
availiblePorts.push(servers[id].port)
delete servers[id]
debug('Server stopped, opening port for new server')
}
io.on('connection', socket => {
debug('New socket connection')
// Create new minecraft proxy server
socket.on('create server', (version, motd, callback) => {
// Only create one server per socket
if (servers[socket.id]) callback(0) // eslint-disable-line
// Get availible port for current server
let port
if (availiblePorts.length > 0) {
port = availiblePorts.shift()
} else {
port = nextPort
nextPort++
}
// Setup minecraft proxy server
const mcserver = mc.createServer({
'online-mode': false,
encryption: true,
port,
version,
motd
})
// Add server to servers list
servers[socket.id] = {
server: mcserver,
socket,
port,
clients: {}
}
// Proxy all calls to the browser via socket.io
mcserver.on('login', client => {
servers[socket.id].clients[client.id] = client
client.on('packet', (data, metadata) => {
if (metadata.name) {
socket.emit('event', metadata.name, data, metadata, client.id, client.uuid)
}
})
client.on('end', reason => {
socket.emit('client disconnected', client.id, reason)
})
socket.emit('login', client)
})
debug('Started new MC proxy server on port ' + port)
// Inform client about new server IP
const location = process.env.LOCATION || ip.address()
callback(`${location}:${port}`) // eslint-disable-line
})
// Write a packet to a minecraft client
socket.on('write', (client, type, data) => {
if (servers[socket.id] &&
servers[socket.id].clients[client] &&
servers[socket.id].clients[client].write) {
servers[socket.id].clients[client].write(type, data)
}
})
socket.on('motd', motd => {
if (servers[socket.id] && servers[socket.id].server) {
servers[socket.id].server.motd = motd
}
})
socket.on('stop', () => {
if (servers[socket.id] && servers[socket.id].server) {
stopServer(socket.id, 'You stopped the server. Please restart the server to continue playing.')
}
})
// Stop proxy server when socket disconnected
socket.on('disconnect', () => {
if (servers[socket.id] && servers[socket.id].server) {
stopServer(socket.id, 'You closed your browser tab - this will stop your Minecraft server. Please reopen the tab to restart your server.')
}
})
})
// Start express server
const expressport = process.env.PORT || 3000
server.listen(expressport, function () {
console.log(`Webserver listening on port ${expressport}`)
})