forked from SirMcPotato/RMMV-MMO-webkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameServer.js
138 lines (121 loc) · 3.62 KB
/
GameServer.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
/*
* @Author: Vinxce
* @Date: 2015-10-27 08:05:27
* @Last Modified by: Vinxce
* @Last Modified time: 2015-11-06 02:31:35
*/
var crypto = require('crypto');
var Account = require('./models/Account');
var handshakePrivateSeed = "c2aaf41755945b640071dd13342923331d004ae4";
var handshakePublicSeed = "331e32c90d6f17b50f3070369aa58d902d379c50";
var clientVersion = "1.0.1a";
var io = null;
function isHandshakedSession(socket, noerr, noclose) {
if (!socket.request.session.handshakeOk) {
if (!noerr) {
socket.emit('mmo_error', {
code: 2,
msg: "Session not handshaked"
});
}
if (!noclose) {
socket.disconnect('Session not handshaked');
}
return false;
}
return true;
}
function bindHandshakeEvents(socket) {
socket.on('handshakeResult', function(data){
if (data.clientVersion != clientVersion) {
socket.emit('mmo_error', {
code: 4,
msg: "Invalid game version."
});
}
if (crypto.createHash('sha1').update(clientVersion+socket.request.session.handshakeHash + handshakePublicSeed).digest('hex') == data.hash) {
//User logged in
socket.request.session.handshakeOk = true;
socket.emit('handshakeOk', {});
} else {
socket.emit('mmo_error', {
code: 1,
msg: "Invalid handshake result"
});
}
});
if (!socket.request.session.handshakeHash) {
// Handshake procedure not started, send token.
socket.request.session.handshakeHash = crypto.createHash('sha1').update(handshakePrivateSeed + 'd28cb767c4272d8ab91000283c67747cb2ef7cd1').digest('hex');
socket.emit('handshake', {
hash: socket.request.session.handshakeHash
});
}
}
function processLoginSuccess(socket, account) {
if (account.socketId != socket.id) {
if (io.sockets.connected[account.socketId]) {
console.log("Duplicate account connection, closing older sockets.", socket.id, account.socketId);
io.sockets.connected[account.socketId].emit('mmo_error', {
code: 3,
msg: 'Logged from another place.'
});
io.sockets.connected[account.socketId].disconnect('Logged from another place.');
}
}
Account.findOneAndUpdate({_id: account._id}, { socketId: socket.id }, function(err, account){
// todo?
});
}
function bindLoginEvents(socket) {
socket.on('loginQuery', function(data){
isHandshakedSession(socket);
Account.findByName(data.username, function(err, account){
if (err) {
return socket.emit('loginResponse', {
err: err.msg
});
}
if (!account) {
return socket.emit('loginResponse', {
err: "Invalid username"
});
}
crypto.pbkdf2(data.password, account.salt, 25000, 512, function(err, hashRaw){
var hpass = new Buffer(hashRaw, 'binary').toString('hex');
if (account.hash == hpass) {
socket.request.session.user = account;
processLoginSuccess(socket, account);
return socket.emit('loginResponse', {
msg: data.username
});
}
return socket.emit('loginResponse', {
err: "Invalid password"
});
})
});
});
}
module.exports = function(app, sessionMiddleware) {
io = require('socket.io')(app.server);
io.use(function(socket, next){
socket.request.originalUrl = socket.request.url;
sessionMiddleware(socket.request, socket.request.res, next);
});
io.on('connection', function(socket){
console.log(' - User connected : ', socket.id);
socket.on('disconnect', function(data){
if (socket.request.session.user) {
Account.findOneAndUpdate({id: socket.request.session.user.id}, { socketId: null }, function(err, account){
// todo?
});
}
socket.request.session = {};
console.log(" - User disconnected");
});
bindHandshakeEvents(socket);
bindLoginEvents(socket);
});
console.log("Socket.io running.")
}