-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (116 loc) · 3.29 KB
/
app.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
// Some simplistic game discovery server for Zank
// https://github.com/theimpossibleastronaut/zank
const dgram = require( 'dgram' );
const crypto = require( 'crypto' );
var gameIdAlgorithm = crypto.getHashes()[ 0 ];
var bindv6 = false;
var bindv4 = false;
var bindv6address = '::0';
var bindv4address = '0.0.0.0';
var bindport = 10101;
var s6, s4 = null;
var args = process.argv.slice( 2 );
args.forEach( function( val, ind, arr ) {
if ( val.indexOf( '=' ) > 0 ) {
var parts = val.split( '=' );
switch ( parts[ 0 ] ) {
case 'bindv6': bindv6 = true; bindv6address = parts[ 1 ].trim(); break;
case 'bindv4': bindv4 = true; bindv4address = parts[ 1 ].trim(); break;
case 'bindport': bindport = true; bindport = parts[ 1 ].trim(); break;
default: break;
}
}
} );
if ( !bindv6 && !bindv4 ) {
console.error( 'No address/protocol bindings, specify bindv6= or bindv4=' );
process.exit( 1 );
}
if ( bindv6 ) {
s6 = dgram.createSocket( {
type: 'udp6',
reuseAddr: true
} );
s6.on( 'error', socketError );
s6.on( 'message', socketMessage );
s6.on( 'listening', socketListening );
s6.bind( bindport, bindv6address );
}
if ( bindv4 ) {
s4 = dgram.createSocket( {
type: 'udp4',
reuseAddr: true
} );
s4.on( 'error', socketError );
s4.on( 'message', socketMessage );
s4.on( 'listening', socketListening );
s4.bind( bindport, bindv4address );
}
function socketError( err ) {
console.log( 'Server error:\n' + err.stack );
process.exit( 1 );
}
function socketMessage( msg, rinfo ) {
console.log( 'Message: "' + msg + '" from ' + rinfo.address + ':' + rinfo.port );
var response = 'ERR';
var result = processMessage( msg.toString(), rinfo );
if ( result === false ) {
response = "ERR";
} else if ( result === true ) {
response = "ACK"
} else {
// Assume processmessage has formatted a message for us.
response = result;
}
this.send( response, rinfo.port, rinfo.address, function( err ) {
if ( err ) {
console.log( 'Client ' + rinfo.address + ':' + rinfo.port + ' left prematurely' );
}
} );
}
function processMessage( msg, rinfo ) {
var msgParts = msg.split( '|' );
if ( msgParts.length !== 2 ) {
return 'ERR;invalid message';
}
var command = msgParts[ 0 ].toUpperCase();
var params = msgParts[ 1 ].trim().split( ';' );
if ( params.length < 1 ) {
return 'ERR;invalid param count';
}
switch( command ) {
case 'REGISTER':
if ( params.length == 2 ) {
var gameIdMessage = 'ACK;';
gameIdMessage += getGameIdFor( rinfo.address, params[ 0 ] );
return gameIdMessage;
}
return 'ERR;invalid param count';
break;
case 'UNREGISTER':
return true;
break;
case 'SETNAME':
return true;
break;
case 'GAMES':
if ( params[ 0 ] == 'list' && params.length == 2 ) {
var gameListMessage = 'ACK;gameslist\n';
gameListMessage += '100.100.100.100;7001;My Zank game\n';
return gameListMessage;
}
return 'ERR;message not understood';
break;
default:
return false;
}
return true;
}
function getGameIdFor( address, port ) {
return crypto.createHash( gameIdAlgorithm )
.update( address + ':' + port )
.digest( 'hex' ).toUpperCase()
.match( /.{1,6}/g ).join( '-' );
}
function socketListening( ) {
console.log( 'Created listener for ' + this.address().family + ' at ' + this.address().address + ':' + this.address().port );
}