- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
var utils = require('util');
module.exports = class Client {
constructor(Socket) {
this.Socket = Socket;
this.TLSSocket = require('tls');
this.XmlParser = new require('xml2js').Parser();
this.XmlBuilder = require('xmlbuilder');
this.Client = this;
this.Authorized = false;
this.OnlineId = '1';
this.Socket.on('data', (Packet) => this.OnData(Packet));
this.Player = null;
this.Status = 0;
}
// Авторизация.
OnData(Packet) {
if (Packet[0] == 0xad || Packet[1] == 0xde || Packet[2] == 0xed || Packet[3] == 0xfe) {
var PacketBuffer = Buffer.alloc(Number(Packet.readBigInt64LE(4)));
Packet.copy(PacketBuffer, 0, 12);
var Query = PacketBuffer.toString();
console.log('[CLIENT] ',Query);
this.XmlParser.parseString(PacketBuffer.toString(), (err, result) => {
if (result)
{
if (result.starttls && !this.TLSSocket.Authorized && !this.Authorized) {
this.Send(this.XmlBuilder.create({
proceed: {
'@xmlns': 'urn:ietf:params:xml:ns:xmpp-tls'
}
}, {
headless: true
}).end({
pretty: false
}));
this.TLSSocket = new this.TLSSocket.TLSSocket(this.Socket, {
cert: global.Cert,
key: global.CertKey,
ca: global.CertBundle,
minVersion: 'TLSv1',
isServer: true
})
this.TLSSocket.once('secure', () => {
this.TLSSocket.Authorized = true;
console.log('TLS Connection established!');
});
this.TLSSocket.on('data', (Packet)=>this.OnData(Packet));
}
else if (result.iq && result.iq.bind) {
this.Send(this.XmlBuilder.create({
iq: {
'@id': result.iq.$.id,
'@type': 'result',
bind: {
'@xmlns': 'urn:ietf:params:xml:ns:xmpp-bind',
jid: this.OnlineId
}
}
}, {
headless: true
}).end({
pretty: false
}));
} else if (result.iq && result.iq.session) {
this.Send(this.XmlBuilder.create({
iq: {
'@id': result.iq.$.id,
'@type': 'result',
'@to': this.OnlineId,
session: {
'@xmlns': 'urn:ietf:params:xml:ns:xmpp-session'
}
}
}, {
headless: true
}).end({
pretty: false
}));
}
else if (result.iq && result.iq.query) {
var QueryName = Object.keys(result.iq.query[0]).filter(function (str) {
return str != '_' && str != '$'
})[0];
var QueryFunction = global.PacketFactory[QueryName];
if (QueryFunction) {
var Stanza = result.iq.query[0][QueryName][0];
console.log(`\x1b[32mQueryname: ${QueryName} ${utils.inspect(Stanza.$)}\x1b[0m`);
global.PacketFactory[QueryName].handle(this, Stanza, result.iq.$.to, result.iq.$.id);
}