-
Notifications
You must be signed in to change notification settings - Fork 1
/
nmp-proxy.js
142 lines (100 loc) · 3.09 KB
/
nmp-proxy.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
// the basis is taken the blame file Andrew Kelley
// https://github.com/PrismarineJS/node-minecraft-protocol/blob/master/examples/proxy/proxy.js
var mc = require('minecraft-protocol');
var states = mc.states;
var util = require('util');
//var usleep = require('sleep').usleep;
// подключаю свои библиотеки
var Servers = require('./lib/servers');
var mc_chat = require('./lib/mc_chat');
var test_bot = require('./lib/test_bot');
var commands = require('./lib/commands');
var packet_listeners = require('./lib/packet_listeners');
var users = require('./lib/users');
var storages = require('./lib/storages');
// подключаю свои плагины
var warps = require('./plugins/warps');
var printAllIds = false;
var printIdWhitelist = {};
var printIdBlacklist = {};
var options = {
'server': {
checkTimeoutInterval: 30 * 1000,
'online-mode': false,
port: 25566
},
'client': {
host: '127.0.0.1',
port: 25565,
username: 'test',
'online-mode': false
}
}
var srv = mc.createServer(options.server);
/*var bot1 = test_bot.botCreate(mc, states,{
host: '127.0.0.1',
port: 25565,
username: 'test_bot',
'online-mode': false
});
commands.bots["25565"] = bot1;
var bot2 = test_bot.botCreate(mc, states,{
host: '127.0.0.1',
port: 25564,
username: 'test_bot',
'online-mode': false
});
commands.bots["25564"] = bot2;*/
srv.on('login', loginOnServer);
function loginOnServer(client, port) {
if( !users.list[client.username] )
users.newuser(client.username,{});
client.ext = {};
var user = users.list[client.username];
var flags = {
"endedClient": false,
"endedTargetClient": false
};
client.ext.flags = flags;
client.ext.user = user;
client.ext.servername = user.pos.cur.server;
var addr = client.socket.remoteAddress;
console.log('Incoming connection', '(' + addr + ')');
client.on('end', function() {
endedClient = true;
console.log('Connection closed by client', '(' + addr + ')');
if(!flags.endedTargetClient )
client.ext.server.end("End");
});
client.on('error', function() {
endedClient = true;
console.log('Connection error by client', '(' + addr + ')');
if(!flags.endedTargetClient )
client.ext.server.end("Error");
});
client.ext.server = mc.createClient({
host: options.client.host,
port: options.client.port,
username: client.username,
'online-mode': false
});
client.on('packet', function(packet) {
if(client.ext.server.state == states.PLAY && packet.state == states.PLAY) {
if(!flags.endedTargetClient){
var listener = "client_"+packet.id;
if( !packet_listeners.emit(listener,client,packet) )
client.ext.server.write(packet.id, packet);
}
}
});
Servers.reinit(client, client.ext.server);
};
function shouldDump(id, direction) {
if(matches(printIdBlacklist[id])) return false;
if(printAllIds) return true;
if(matches(printIdWhitelist[id])) return true;
return false;
function matches(result) {
return result != null && result.indexOf(direction) !== -1;
}
}