Skip to content

Commit

Permalink
fix: No longer double reporting MFA server errors and gracefully hand…
Browse files Browse the repository at this point in the history
…ling address in use errors
  • Loading branch information
steilerDev committed Jul 6, 2023
1 parent 5587f94 commit c916e17
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/src/app/error/codes/mfa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export const STARTUP_FAILED: ErrorStruct = buildErrorStruct(
name, prefix, `STARTUP_FAILED`, `Unable to start MFA server`,
);

export const SERVER_ERR: ErrorStruct = buildErrorStruct(
name, prefix, `SERVER_ERR`, `HTTP Server Error`,
);

export const ADDR_IN_USE_ERR: ErrorStruct = buildErrorStruct(
name, prefix, `ADDR_IN_USE`, `HTTP Server could not start, because address/port is in use`,
);

export const RESEND_REQUEST_FAILED: ErrorStruct = buildErrorStruct(
name, prefix, `RESEND_REQUEST_FAILED`, `Resending request failed`,
);
Expand Down
5 changes: 3 additions & 2 deletions app/src/app/event/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {getLogger, logFilePath} from "../../lib/logger.js";
import {iCPSError} from "../error/error.js";
import {randomUUID} from "crypto";
import {EventHandler} from './event-handler.js';
import {AUTH_ERR, ERR_SIGINT, ERR_SIGTERM, LIBRARY_ERR} from '../error/error-codes.js';
import {AUTH_ERR, ERR_SIGINT, ERR_SIGTERM, LIBRARY_ERR, MFA_ERR} from '../error/error-codes.js';
import {iCPSAppOptions} from '../factory.js';
import * as SYNC_ENGINE from '../../lib/sync-engine/constants.js';
import {SyncEngine} from '../../lib/sync-engine/sync-engine.js';
Expand All @@ -28,8 +28,9 @@ export const WARN_EVENT = `warn`;
const reportDenyList = [
ERR_SIGINT.code,
ERR_SIGTERM.code,
MFA_ERR.ADDR_IN_USE_ERR.code, // Only happens if port/address is in use
// LIBRARY_ERR.LOCKED.code,
AUTH_ERR.UNAUTHORIZED.code,
AUTH_ERR.UNAUTHORIZED.code, // Only happens if username/password don't match
];

const BACKTRACE_SUBMISSION = {
Expand Down
1 change: 0 additions & 1 deletion app/src/lib/icloud/icloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class iCloud extends EventEmitter {
this.mfaServer = new MFAServer(app.options.port);
this.mfaServer.on(MFA_SERVER.EVENTS.MFA_RECEIVED, this.submitMFA.bind(this));
this.mfaServer.on(MFA_SERVER.EVENTS.MFA_RESEND, this.resendMFA.bind(this));
this.mfaServer.on(HANDLER_EVENT, this.emit.bind(this, HANDLER_EVENT));

// ICloud Auth object
this.auth = new iCloudAuth(app.options.username, app.options.password, app.options.trustToken, app.options.dataDir);
Expand Down
9 changes: 9 additions & 0 deletions app/src/lib/icloud/mfa/mfa-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export class MFAServer extends EventEmitter {

this.logger.debug(`Preparing MFA server on port ${this.port}`);
this.server = http.createServer(this.handleRequest.bind(this));
this.server.on(`error`, err => {
const icpsErr = Object.hasOwn(err, `code`) && (err as any).code === `EADDRINUSE`
? new iCPSError(MFA_ERR.ADDR_IN_USE_ERR).addContext(`port`, this.port)
: new iCPSError(MFA_ERR.SERVER_ERR);

icpsErr.addCause(err);

this.emit(HANDLER_EVENT, icpsErr);
});

// Default MFA request always goes to device
this.mfaMethod = new MFAMethod();
Expand Down

0 comments on commit c916e17

Please sign in to comment.