forked from jacobwgillespie/heroku-minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.coffee
71 lines (53 loc) · 2.32 KB
/
server.coffee
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
WebSocketServer = require("websocket").server
http = require("http")
net = require('net')
urlParse = require('url').parse
webserver_port = process.env.MC_HEROKU_SERVER_PORT || 8080
minecraft_server_host = "0.0.0.0"
minecraft_server_port = 25566
httpServer = http.createServer (request, response) ->
console.log "Received request for #{request.url}"
response.writeHead 404, {'Content-Type': 'text/plain'}
response.end('This is not exactly an HTTP server.\n')
httpServer.listen webserver_port, ->
console.log "Socket proxy server is listening on port #{webserver_port}"
webSocketServer = new WebSocketServer
httpServer: httpServer
autoAcceptConnections: no
originIsAllowed = (origin) -> yes
webSocketServer.on "request", (request) ->
url = urlParse(request.resource, true)
args = url.pathname.split("/").slice(1)
action = args.shift()
# params = url.query
unless action is 'tunnel'
console.log "Rejecting request for #{action} with 404"
request.reject(404)
return
console.log "Trying to create a TCP to WebSocket tunnel for #{minecraft_server_host}:#{minecraft_server_port}"
webSocketConnection = request.accept()
console.log "#{webSocketConnection.remoteAddress} connected - Protocol Version #{webSocketConnection.websocketVersion}"
tcpSocketConnection = new net.Socket()
tcpSocketConnection.on "error", (err) ->
webSocketConnection.send JSON.stringify
status: "error"
details: "Upstream socket error; " + err
tcpSocketConnection.on "data", (data) ->
webSocketConnection.send data
tcpSocketConnection.on "close", ->
webSocketConnection.close()
tcpSocketConnection.connect minecraft_server_port, minecraft_server_host, ->
webSocketConnection.on "message", (msg) ->
if msg.type is 'utf8'
console.log "received utf message: #{msg.utf8Data}"
# tcpSocketConnection.write msg.binaryData
else
# console.log "received binary message of length #{msg.binaryData.length}"
tcpSocketConnection.write msg.binaryData
console.log "Upstream socket connected for #{webSocketConnection.remoteAddress}"
webSocketConnection.send JSON.stringify
status: "ready"
details: "Upstream socket connected"
webSocketConnection.on "close", ->
tcpSocketConnection.destroy()
console.log "#{webSocketConnection.remoteAddress} disconnected"