Skip to content

Commit

Permalink
fix(reconnect): Force close previous connection if reconnect was requ…
Browse files Browse the repository at this point in the history
…ested
  • Loading branch information
andris9 committed Mar 24, 2024
1 parent 69f6c32 commit ec0baf1
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Account {
accountData.oauth2 && accountData.oauth2.provider && accountData.oauth2.provider !== accountData.type
? accountData.oauth2.provider
: undefined,
state: !runIndex || runIndex <= accountData.runIndex ? accountData.state : 'init',
state: !runIndex || runIndex <= accountData.runIndex || ['init', 'unset'].includes(accountData.state) ? accountData.state : 'init',

webhooks: accountData.webhooks || undefined,
proxy: accountData.proxy || undefined,
Expand Down Expand Up @@ -447,7 +447,7 @@ class Account {

let accountData = this.unserializeAccountData(result);

accountData.state = !runIndex || runIndex <= accountData.runIndex ? accountData.state : 'init';
accountData.state = !runIndex || runIndex <= accountData.runIndex || ['init', 'unset'].includes(accountData.state) ? accountData.state : 'init';

if (requireValid && !['connected', 'connecting', 'syncing'].includes(accountData.state)) {
let err;
Expand Down Expand Up @@ -1874,7 +1874,7 @@ class Account {

if (data.reconnect) {
await this.call({
cmd: 'update',
cmd: 'reconnect',
account: this.account,
timeout: this.timeout
});
Expand Down
4 changes: 4 additions & 0 deletions lib/api-client/base-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class BaseClient {
return null;
}

async reconnect() {
return null;
}

async subconnections() {
return [];
}
Expand Down
23 changes: 23 additions & 0 deletions lib/autodetect-imap-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ async function resolveUsingMX(email, domain) {
return await resolveUsingAutoconfig(email, 'zone.ee', 'mx');
}

// AWS WorkMail
let awsMatch = exchange.match(/inbound-smtp\.([^.]+)\.amazonaws.com/);
if (awsMatch) {
let region = awsMatch[1].toLowerCase().trim();
Expand All @@ -359,6 +360,7 @@ async function resolveUsingMX(email, domain) {
};
}

// Zoho EU
if (/^mx\d*\.zoho\.eu$/i.test(exchange)) {
// Zoho custom domain in EU
return {
Expand All @@ -376,6 +378,7 @@ async function resolveUsingMX(email, domain) {
};
}

// Zoho international
if (/^mx\d*\.zoho\.com$/i.test(exchange)) {
// Zoho custom domain not in EU
return {
Expand All @@ -393,6 +396,7 @@ async function resolveUsingMX(email, domain) {
};
}

// MS365
if (/\bprotection\.outlook\.com$/i.test(exchange)) {
// outlook
// as autodiscovery is currently (2021-11-17) closed use a fixed response
Expand Down Expand Up @@ -427,6 +431,25 @@ async function resolveUsingMX(email, domain) {
};
}

// Alibaba Mail
if (/^mx\d*\.sg\.aliyun\.com$/i.test(exchange)) {
// Naver.com
return {
imap: {
host: 'imap.sg.aliyun.com',
port: 993,
secure: true
},
smtp: {
host: 'smtp.sg.aliyun.com',
port: 465,
secure: true
},
_source: 'mx'
};
}

// Naver (kr)
if (/^mx\d*\.naver\.com$/i.test(exchange)) {
// Naver.com
return {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -7008,7 +7008,7 @@ ${Buffer.from(data.content, 'base64url').toString('base64')}
try {
request.logger.info({ msg: 'Request reconnect for logging', account });
try {
await call({ cmd: 'update', account });
await call({ cmd: 'reconnect', account });
} catch (err) {
request.logger.error({ msg: 'Reconnect request failed', action: 'request_reconnect', account, err });
}
Expand Down
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,7 @@ async function onCommand(worker, message) {
case 'sync':
case 'pause':
case 'resume':
case 'reconnect':
if (assigned.has(message.account)) {
let assignedWorker = assigned.get(message.account);
call(assignedWorker, message)
Expand Down
28 changes: 28 additions & 0 deletions workers/imap.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ class ConnectionHandler {
async assignConnection(account, runIndex) {
logger.info({ msg: 'Assigned account to worker', account });

if (!this.runIndex) {
this.runIndex = runIndex;
}

if (!runIndex && this.runIndex) {
runIndex = this.runIndex;
}

let accountLogger = await this.getAccountLogger(account);
let secret = await getSecret();
let accountObject = new Account({
Expand Down Expand Up @@ -278,6 +286,25 @@ class ConnectionHandler {
}
}

async reconnectConnection(account) {
logger.info({ msg: 'Account reconnection requested', account });
if (this.accounts.has(account)) {
let accountObject = this.accounts.get(account);
if (accountObject.connection) {
accountObject.connection.accountLogger.log({
level: 'info',
t: Date.now(),
cid: accountObject.connection.cid,
msg: 'Account reconnection requested'
});

await accountObject.connection.close();
}

await this.assignConnection(account);
}
}

async listMessages(message) {
if (!this.accounts.has(message.account)) {
return NO_ACTIVE_HANDLER_RESP;
Expand Down Expand Up @@ -634,6 +661,7 @@ class ConnectionHandler {
case 'sync':
case 'pause':
case 'resume':
case 'reconnect':
return await this[`${message.cmd}Connection`](message.account);

case 'assign':
Expand Down

0 comments on commit ec0baf1

Please sign in to comment.