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