@@ -25,6 +25,12 @@ class StreamParser extends EventEmitter {
|
|||||||
this.parse();
|
this.parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.buffer = '';
|
||||||
|
this.depth = 0;
|
||||||
|
this.streamStarted = false;
|
||||||
|
}
|
||||||
|
|
||||||
parse() {
|
parse() {
|
||||||
// Find complete stanzas in buffer
|
// Find complete stanzas in buffer
|
||||||
let startIdx = 0;
|
let startIdx = 0;
|
||||||
@@ -148,6 +154,11 @@ class XMPPStream extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setupSocket() {
|
setupSocket() {
|
||||||
|
// Reset parser for stream restart (after TLS upgrade)
|
||||||
|
if (this.parser) {
|
||||||
|
this.parser.reset();
|
||||||
|
}
|
||||||
|
|
||||||
this.socket.on('data', (data) => {
|
this.socket.on('data', (data) => {
|
||||||
try {
|
try {
|
||||||
if (this.parser) {
|
if (this.parser) {
|
||||||
@@ -308,33 +319,55 @@ class XMPPStream extends EventEmitter {
|
|||||||
});
|
});
|
||||||
this.send(proceed);
|
this.send(proceed);
|
||||||
|
|
||||||
|
// Small delay to ensure data is flushed before upgrading
|
||||||
|
setImmediate(() => {
|
||||||
|
this.upgradeTLS();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
upgradeTLS() {
|
||||||
|
try {
|
||||||
// Get TLS options
|
// Get TLS options
|
||||||
const tlsOptions = this.getTLSOptions();
|
const tlsOptions = this.getTLSOptions();
|
||||||
|
|
||||||
// Upgrade socket to TLS
|
// Remove old data handler - we'll recreate it
|
||||||
const secureSocket = tls.connect({
|
this.socket.removeAllListeners('data');
|
||||||
socket: this.socket,
|
|
||||||
|
// Create TLS socket wrapping the existing plain socket
|
||||||
|
const tlsSocket = new tls.TLSSocket(this.socket, {
|
||||||
...tlsOptions,
|
...tlsOptions,
|
||||||
|
isServer: true,
|
||||||
rejectUnauthorized: false
|
rejectUnauthorized: false
|
||||||
}, () => {
|
});
|
||||||
|
|
||||||
|
tlsSocket.on('secure', () => {
|
||||||
this.logger.debug('TLS upgrade successful');
|
this.logger.debug('TLS upgrade successful');
|
||||||
|
|
||||||
// Update socket and restart stream
|
// Update socket reference
|
||||||
this.socket = secureSocket;
|
this.socket = tlsSocket;
|
||||||
this.secure = true;
|
this.secure = true;
|
||||||
|
|
||||||
// Setup socket handlers for new socket
|
// Re-setup socket handlers with new socket
|
||||||
this.setupSocket();
|
this.setupSocket();
|
||||||
|
|
||||||
// Reset state for stream restart
|
// Reset stream for restart
|
||||||
this.setState('wait-for-stream');
|
this.setState('wait-for-stream');
|
||||||
this.streamId = this.generateStreamId();
|
this.streamId = this.generateStreamId();
|
||||||
});
|
});
|
||||||
|
|
||||||
secureSocket.on('error', (error) => {
|
tlsSocket.on('error', (error) => {
|
||||||
this.logger.error('TLS upgrade error:', error);
|
this.logger.error('TLS upgrade error:', error);
|
||||||
this.close();
|
this.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tlsSocket.on('close', () => {
|
||||||
|
this.handleClose();
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error('Error upgrading TLS:', error);
|
||||||
|
this.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getTLSOptions() {
|
getTLSOptions() {
|
||||||
|
|||||||
Referencia en una nueva incidencia
Block a user