emailjs 5.0.5 → 5.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth-admission.d.ts +13 -0
- package/dist/auth-admission.js +112 -0
- package/dist/auth-admission.js.map +1 -0
- package/dist/auth-admission.test.d.ts +1 -0
- package/dist/auth-admission.test.js +51 -0
- package/dist/auth-admission.test.js.map +1 -0
- package/dist/client.auth-interruption.integration.test.d.ts +1 -0
- package/dist/client.auth-interruption.integration.test.js +197 -0
- package/dist/client.auth-interruption.integration.test.js.map +1 -0
- package/dist/client.auth-starttls.integration.test.d.ts +1 -0
- package/dist/client.auth-starttls.integration.test.js +95 -0
- package/dist/client.auth-starttls.integration.test.js.map +1 -0
- package/dist/client.d.ts +5 -1
- package/dist/client.data-ownership.test.d.ts +1 -0
- package/dist/client.data-ownership.test.js +193 -0
- package/dist/client.data-ownership.test.js.map +1 -0
- package/dist/client.integration.test.js +160 -3
- package/dist/client.integration.test.js.map +1 -1
- package/dist/client.js +99 -55
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +130 -10
- package/dist/client.test.js.map +1 -1
- package/dist/connection.auth-ownership.test.d.ts +1 -0
- package/dist/connection.auth-ownership.test.js +395 -0
- package/dist/connection.auth-ownership.test.js.map +1 -0
- package/dist/connection.body-ownership.test.d.ts +1 -0
- package/dist/connection.body-ownership.test.js +73 -0
- package/dist/connection.body-ownership.test.js.map +1 -0
- package/dist/connection.cached-login.test.d.ts +1 -0
- package/dist/connection.cached-login.test.js +290 -0
- package/dist/connection.cached-login.test.js.map +1 -0
- package/dist/connection.d.ts +15 -7
- package/dist/connection.errors.integration.test.js +86 -32
- package/dist/connection.errors.integration.test.js.map +1 -1
- package/dist/connection.greeting-ownership.test.d.ts +1 -0
- package/dist/connection.greeting-ownership.test.js +76 -0
- package/dist/connection.greeting-ownership.test.js.map +1 -0
- package/dist/connection.integration.test.js +108 -41
- package/dist/connection.integration.test.js.map +1 -1
- package/dist/connection.js +282 -185
- package/dist/connection.js.map +1 -1
- package/dist/connection.login-boundaries.test.d.ts +1 -0
- package/dist/connection.login-boundaries.test.js +151 -0
- package/dist/connection.login-boundaries.test.js.map +1 -0
- package/dist/connection.test.js +68 -2
- package/dist/connection.test.js.map +1 -1
- package/dist/response.js +19 -20
- package/dist/response.js.map +1 -1
- package/dist/response.test.js +17 -2
- package/dist/response.test.js.map +1 -1
- package/dist/smtp-auth.d.ts +13 -0
- package/dist/smtp-auth.js +40 -0
- package/dist/smtp-auth.js.map +1 -0
- package/dist/smtp-data.d.ts +18 -0
- package/dist/smtp-data.js +75 -0
- package/dist/smtp-data.js.map +1 -0
- package/dist/smtp-session.d.ts +9 -0
- package/dist/smtp-session.js +31 -0
- package/dist/smtp-session.js.map +1 -0
- package/dist/smtp-transport.d.ts +33 -0
- package/dist/smtp-transport.js +170 -0
- package/dist/smtp-transport.js.map +1 -0
- package/dist/tls-upgrade.d.ts +5 -0
- package/dist/tls-upgrade.js +30 -0
- package/dist/tls-upgrade.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SMTPConnection } from './connection.js';
|
|
2
|
+
type Result = (err?: Error | null) => void;
|
|
3
|
+
interface DataBody {
|
|
4
|
+
write(chunk: string): void;
|
|
5
|
+
end(): void;
|
|
6
|
+
abort(err: Error): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class DataTransfer {
|
|
9
|
+
private readonly smtp;
|
|
10
|
+
private readonly stillAttached;
|
|
11
|
+
constructor(smtp: SMTPConnection, stillAttached: () => boolean);
|
|
12
|
+
private disconnected;
|
|
13
|
+
private untilClose;
|
|
14
|
+
private runDataCommand;
|
|
15
|
+
open(done: Result): void;
|
|
16
|
+
stream(produce: (body: DataBody) => void, done: Result): void;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { SMTPError, SMTPErrorStates } from './error.js';
|
|
2
|
+
export class DataTransfer {
|
|
3
|
+
smtp;
|
|
4
|
+
stillAttached;
|
|
5
|
+
constructor(smtp, stillAttached) {
|
|
6
|
+
this.smtp = smtp;
|
|
7
|
+
this.stillAttached = stillAttached;
|
|
8
|
+
}
|
|
9
|
+
disconnected() {
|
|
10
|
+
return SMTPError.create('connection closed during DATA transfer', SMTPErrorStates.CONNECTIONCLOSED);
|
|
11
|
+
}
|
|
12
|
+
untilClose(done, start) {
|
|
13
|
+
let finished = false;
|
|
14
|
+
const finish = (err) => {
|
|
15
|
+
if (finished)
|
|
16
|
+
return;
|
|
17
|
+
finished = true;
|
|
18
|
+
this.smtp.off('close', onClose);
|
|
19
|
+
done(err);
|
|
20
|
+
};
|
|
21
|
+
const onClose = () => {
|
|
22
|
+
// A final reply may already be in its logger. Let it settle first.
|
|
23
|
+
if (!this.stillAttached())
|
|
24
|
+
queueMicrotask(() => finish(this.disconnected()));
|
|
25
|
+
};
|
|
26
|
+
this.smtp.on('close', onClose);
|
|
27
|
+
if (this.stillAttached())
|
|
28
|
+
start(finish);
|
|
29
|
+
else
|
|
30
|
+
finish(this.disconnected());
|
|
31
|
+
}
|
|
32
|
+
// DATA and its terminator determine the delivery result. RSET is only
|
|
33
|
+
// cleanup: whether it replies or the socket closes, report the original error.
|
|
34
|
+
runDataCommand(send, done) {
|
|
35
|
+
this.untilClose((err) => {
|
|
36
|
+
if (!err || !this.stillAttached())
|
|
37
|
+
return done(err);
|
|
38
|
+
this.untilClose(() => done(err), (reply) => this.smtp.rset(reply));
|
|
39
|
+
}, send);
|
|
40
|
+
}
|
|
41
|
+
open(done) {
|
|
42
|
+
this.runDataCommand((reply) => this.smtp.data(reply), (err) => done(err || (this.stillAttached() ? null : this.disconnected())));
|
|
43
|
+
}
|
|
44
|
+
stream(produce, done) {
|
|
45
|
+
let acceptingChunks = true;
|
|
46
|
+
this.untilClose((err) => {
|
|
47
|
+
acceptingChunks = false;
|
|
48
|
+
if (err)
|
|
49
|
+
return done(err);
|
|
50
|
+
this.runDataCommand((reply) => this.smtp.data_end(reply), done);
|
|
51
|
+
}, (finish) => produce({
|
|
52
|
+
write: (chunk) => {
|
|
53
|
+
if (!acceptingChunks)
|
|
54
|
+
return;
|
|
55
|
+
if (this.stillAttached())
|
|
56
|
+
this.smtp.message(chunk);
|
|
57
|
+
else
|
|
58
|
+
finish(this.disconnected());
|
|
59
|
+
},
|
|
60
|
+
end: () => {
|
|
61
|
+
if (acceptingChunks)
|
|
62
|
+
finish(null);
|
|
63
|
+
},
|
|
64
|
+
abort: (err) => {
|
|
65
|
+
if (!acceptingChunks)
|
|
66
|
+
return;
|
|
67
|
+
acceptingChunks = false;
|
|
68
|
+
if (this.stillAttached())
|
|
69
|
+
this.smtp.close();
|
|
70
|
+
finish(err);
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=smtp-data.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smtp-data.js","sourceRoot":"","sources":["../src/smtp-data.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AASvD,MAAM,OAAO,YAAY;IAEN;IACA;IAFlB,YACkB,IAAoB,EACpB,aAA4B;QAD5B,SAAI,GAAJ,IAAI,CAAgB;QACpB,kBAAa,GAAb,aAAa,CAAe;IAC3C,CAAC;IAEI,YAAY;QACnB,OAAO,SAAS,CAAC,MAAM,CACtB,wCAAwC,EACxC,eAAe,CAAC,gBAAgB,CAChC,CAAA;IACF,CAAC;IAEO,UAAU,CAAC,IAAY,EAAE,KAA+B;QAC/D,IAAI,QAAQ,GAAG,KAAK,CAAA;QACpB,MAAM,MAAM,GAAW,CAAC,GAAG,EAAE,EAAE;YAC9B,IAAI,QAAQ;gBAAE,OAAM;YACpB,QAAQ,GAAG,IAAI,CAAA;YACf,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;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M,CAAC,OAAiC,EAAE,IAAY;QACrD,IAAI,eAAe,GAAG,IAAI,CAAA;QAC1B,IAAI,CAAC,UAAU,CACd,CAAC,GAAG,EAAE,EAAE;YACP,eAAe,GAAG,KAAK,CAAA;YACvB,IAAI,GAAG;gBAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAA;YACzB,IAAI,CAAC,cAAc,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,CAAA;QAChE,CAAC,EACD,CAAC,MAAM,EAAE,EAAE,CACV,OAAO,CAAC;YACP,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE;gBAChB,IAAI,CAAC,eAAe;oBAAE,OAAM;gBAC5B,IAAI,IAAI,CAAC,aAAa,EAAE;oBAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;;oBAC7C,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,CAAA;YACjC,CAAC;YACD,GAAG,EAAE,GAAG,EAAE;gBACT,IAAI,eAAe;oBAAE,MAAM,CAAC,IAAI,CAAC,CAAA;YAClC,CAAC;YACD,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE;gBACd,IAAI,CAAC,eAAe;oBAAE,OAAM;gBAC5B,eAAe,GAAG,KAAK,CAAA;gBACvB,IAAI,IAAI,CAAC,aAAa,EAAE;oBAAE,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAA;gBAC3C,MAAM,CAAC,GAAG,CAAC,CAAA;YACZ,CAAC;SACD,CAAC,CACH,CAAA;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SMTPConnection } from './connection.js';
|
|
2
|
+
import type { SmtpTransport } from './smtp-transport.js';
|
|
3
|
+
export interface SmtpSession {
|
|
4
|
+
close(): void;
|
|
5
|
+
closeWhenIdle(done: () => void): void;
|
|
6
|
+
}
|
|
7
|
+
export declare function startSession(connection: SMTPConnection, currentTransport: () => SmtpTransport | null): void;
|
|
8
|
+
export declare function endSession(connection: SMTPConnection): void;
|
|
9
|
+
export declare function getSession(connection: SMTPConnection): SmtpSession | undefined;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// One logical login session, including the switch from plaintext to STARTTLS.
|
|
2
|
+
const sessions = new WeakMap();
|
|
3
|
+
export function startSession(connection, currentTransport) {
|
|
4
|
+
const session = {
|
|
5
|
+
close() {
|
|
6
|
+
if (sessions.get(connection) === session)
|
|
7
|
+
connection.close(true);
|
|
8
|
+
},
|
|
9
|
+
closeWhenIdle(done) {
|
|
10
|
+
const transport = sessions.get(connection) === session ? currentTransport() : null;
|
|
11
|
+
if (!transport)
|
|
12
|
+
return done();
|
|
13
|
+
transport.whenIdle(() => {
|
|
14
|
+
// STARTTLS may have moved the same session to a TLS transport.
|
|
15
|
+
if (sessions.get(connection) === session &&
|
|
16
|
+
currentTransport() !== transport)
|
|
17
|
+
return session.closeWhenIdle(done);
|
|
18
|
+
session.close();
|
|
19
|
+
done();
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
sessions.set(connection, session);
|
|
24
|
+
}
|
|
25
|
+
export function endSession(connection) {
|
|
26
|
+
sessions.delete(connection);
|
|
27
|
+
}
|
|
28
|
+
export function getSession(connection) {
|
|
29
|
+
return sessions.get(connection);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=smtp-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smtp-session.js","sourceRoot":"","sources":["../src/smtp-session.ts"],"names":[],"mappings":"AAQA,8EAA8E;AAC9E,MAAM,QAAQ,GAAG,IAAI,OAAO,EAA+B,CAAA;AAE3D,MAAM,UAAU,YAAY,CAC3B,UAA0B,EAC1B,gBAA4C;IAE5C,MAAM,OAAO,GAAgB;QAC5B,KAAK;YACJ,IAAI,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,OAAO;gBAAE,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACjE,CAAC;QACD,aAAa,CAAC,IAAI;YACjB,MAAM,SAAS,GACd,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;YACjE,IAAI,CAAC,SAAS;gBAAE,OAAO,IAAI,EAAE,CAAA;YAC7B,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE;gBACvB,+DAA+D;gBAC/D,IACC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,OAAO;oBACpC,gBAAgB,EAAE,KAAK,SAAS;oBAEhC,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAA;gBACnC,OAAO,CAAC,KAAK,EAAE,CAAA;gBACf,IAAI,EAAE,CAAA;YACP,CAAC,CAAC,CAAA;QACH,CAAC;KACD,CAAA;IACD,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;AAClC,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,UAA0B;IACpD,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,UAA0B;IACpD,OAAO,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;AAChC,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Socket } from 'net';
|
|
2
|
+
type ReplyHandler = (err: Error | null | undefined, ...rest: unknown[]) => void;
|
|
3
|
+
/** The write and reply path for one socket. A retired transport is never rebound. */
|
|
4
|
+
export declare class SmtpTransport {
|
|
5
|
+
readonly socket: Socket;
|
|
6
|
+
private readonly log;
|
|
7
|
+
private readonly isAttached;
|
|
8
|
+
private readonly greetingAccepted;
|
|
9
|
+
private readonly auth;
|
|
10
|
+
private greetingPaused;
|
|
11
|
+
private writeCount;
|
|
12
|
+
get isAuthenticated(): boolean;
|
|
13
|
+
private get available();
|
|
14
|
+
get allowsGreeting(): boolean;
|
|
15
|
+
get allowsLogin(): boolean;
|
|
16
|
+
private pauseGreetingWhile;
|
|
17
|
+
private readonly pendingReplyHandlers;
|
|
18
|
+
private pendingOperations;
|
|
19
|
+
private finishCleanup?;
|
|
20
|
+
private get idle();
|
|
21
|
+
beginOperation(): () => void;
|
|
22
|
+
retryAfter(delay: number, retry: () => void): void;
|
|
23
|
+
constructor(socket: Socket, log: (entry: unknown) => void, isAttached: (transport: SmtpTransport) => boolean, greetingAccepted: (verb: 'EHLO' | 'HELO') => void);
|
|
24
|
+
waitForReply(handler: ReplyHandler, canWait?: () => boolean): ReplyHandler | null;
|
|
25
|
+
failPendingReplies(): void;
|
|
26
|
+
writeBody(data: string): void;
|
|
27
|
+
private write;
|
|
28
|
+
whenIdle(done: () => void): void;
|
|
29
|
+
trySendRequest(request: string, onReply: ReplyHandler, { starting }?: {
|
|
30
|
+
starting?: 'auth' | 'greeting';
|
|
31
|
+
}): boolean;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { SMTPError, SMTPErrorStates } from './error.js';
|
|
2
|
+
import { AuthAdmission, requestVerb } from './auth-admission.js';
|
|
3
|
+
/** The write and reply path for one socket. A retired transport is never rebound. */
|
|
4
|
+
export class SmtpTransport {
|
|
5
|
+
socket;
|
|
6
|
+
log;
|
|
7
|
+
isAttached;
|
|
8
|
+
greetingAccepted;
|
|
9
|
+
auth = new AuthAdmission();
|
|
10
|
+
greetingPaused = false;
|
|
11
|
+
writeCount = 0;
|
|
12
|
+
get isAuthenticated() {
|
|
13
|
+
return this.auth.isAuthenticated;
|
|
14
|
+
}
|
|
15
|
+
get available() {
|
|
16
|
+
return this.idle && !this.greetingPaused;
|
|
17
|
+
}
|
|
18
|
+
get allowsGreeting() {
|
|
19
|
+
return this.available && this.auth.allowsGreeting;
|
|
20
|
+
}
|
|
21
|
+
get allowsLogin() {
|
|
22
|
+
return this.available && this.auth.allowsLogin;
|
|
23
|
+
}
|
|
24
|
+
// Logging and registering a listener can both run caller code mid-command.
|
|
25
|
+
// Neither greeting nor login may start until control returns to us.
|
|
26
|
+
pauseGreetingWhile(fn) {
|
|
27
|
+
const wasPaused = this.greetingPaused;
|
|
28
|
+
this.greetingPaused = true;
|
|
29
|
+
try {
|
|
30
|
+
fn();
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
this.greetingPaused = wasPaused;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
pendingReplyHandlers = new Set();
|
|
37
|
+
pendingOperations = 0;
|
|
38
|
+
finishCleanup;
|
|
39
|
+
get idle() {
|
|
40
|
+
return this.pendingReplyHandlers.size === 0 && this.pendingOperations === 0;
|
|
41
|
+
}
|
|
42
|
+
// A greylist delay or TLS handshake outlives the reply that started it.
|
|
43
|
+
beginOperation() {
|
|
44
|
+
this.pendingOperations++;
|
|
45
|
+
return () => {
|
|
46
|
+
this.pendingOperations--;
|
|
47
|
+
if (this.idle)
|
|
48
|
+
this.finishCleanup?.();
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
retryAfter(delay, retry) {
|
|
52
|
+
const finish = this.beginOperation();
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
try {
|
|
55
|
+
retry();
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
finish();
|
|
59
|
+
}
|
|
60
|
+
}, delay);
|
|
61
|
+
}
|
|
62
|
+
constructor(socket, log, isAttached, greetingAccepted) {
|
|
63
|
+
this.socket = socket;
|
|
64
|
+
this.log = log;
|
|
65
|
+
this.isAttached = isAttached;
|
|
66
|
+
this.greetingAccepted = greetingAccepted;
|
|
67
|
+
}
|
|
68
|
+
// Banners and commands both wait for one reply on this socket.
|
|
69
|
+
waitForReply(handler, canWait = () => this.isAttached(this)) {
|
|
70
|
+
const completeOnce = (err, ...rest) => {
|
|
71
|
+
if (!this.pendingReplyHandlers.delete(completeOnce))
|
|
72
|
+
return;
|
|
73
|
+
try {
|
|
74
|
+
handler(err, ...rest);
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
if (this.idle)
|
|
78
|
+
this.finishCleanup?.();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
this.pauseGreetingWhile(() => this.socket.once('response', completeOnce));
|
|
82
|
+
// A newListener hook may have closed this socket or sent a command.
|
|
83
|
+
if (!canWait()) {
|
|
84
|
+
this.socket.off('response', completeOnce);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
this.pendingReplyHandlers.add(completeOnce);
|
|
88
|
+
return completeOnce;
|
|
89
|
+
}
|
|
90
|
+
failPendingReplies() {
|
|
91
|
+
for (const completeOnce of this.pendingReplyHandlers) {
|
|
92
|
+
this.socket.off('response', completeOnce);
|
|
93
|
+
// A response or peer error already being dispatched gets first claim.
|
|
94
|
+
queueMicrotask(() => completeOnce(SMTPError.create('connection has closed', SMTPErrorStates.CONNECTIONCLOSED)));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
writeBody(data) {
|
|
98
|
+
this.pauseGreetingWhile(() => this.log(data));
|
|
99
|
+
if (!this.isAttached(this))
|
|
100
|
+
return;
|
|
101
|
+
if (this.pendingReplyHandlers.size)
|
|
102
|
+
this.auth.overlap();
|
|
103
|
+
this.auth.untrackedWrite(data);
|
|
104
|
+
this.write(data);
|
|
105
|
+
}
|
|
106
|
+
write(data) {
|
|
107
|
+
if (!this.isAttached(this))
|
|
108
|
+
return;
|
|
109
|
+
this.writeCount++;
|
|
110
|
+
this.socket.write(data);
|
|
111
|
+
}
|
|
112
|
+
// Called once by the client whose setup failed, never by a direct login.
|
|
113
|
+
whenIdle(done) {
|
|
114
|
+
if (this.idle)
|
|
115
|
+
return done();
|
|
116
|
+
const finish = () => {
|
|
117
|
+
this.finishCleanup = undefined;
|
|
118
|
+
this.socket.off('close', onClose);
|
|
119
|
+
done();
|
|
120
|
+
};
|
|
121
|
+
const onClose = () => {
|
|
122
|
+
// A detached socket can't receive another reply, but its operation
|
|
123
|
+
// can still owe the caller a timer or handshake callback.
|
|
124
|
+
if (!this.pendingOperations)
|
|
125
|
+
finish();
|
|
126
|
+
};
|
|
127
|
+
this.finishCleanup = finish;
|
|
128
|
+
this.socket.once('close', onClose);
|
|
129
|
+
}
|
|
130
|
+
trySendRequest(request, onReply, { starting } = {}) {
|
|
131
|
+
const initialWriteCount = this.writeCount;
|
|
132
|
+
const canStillStart = () => {
|
|
133
|
+
if (this.writeCount !== initialWriteCount)
|
|
134
|
+
return false;
|
|
135
|
+
if (starting === 'auth')
|
|
136
|
+
return this.allowsLogin;
|
|
137
|
+
// A fallback HELO can be inside its own greylist retry timer, but
|
|
138
|
+
// it must never write over another command's pending reply.
|
|
139
|
+
return (this.pendingReplyHandlers.size === 0 &&
|
|
140
|
+
!this.greetingPaused &&
|
|
141
|
+
this.auth.allowsGreeting);
|
|
142
|
+
};
|
|
143
|
+
const canStillSendRequest = () => this.isAttached(this) &&
|
|
144
|
+
this.socket.writable &&
|
|
145
|
+
(!starting || canStillStart());
|
|
146
|
+
if (!canStillSendRequest())
|
|
147
|
+
return false;
|
|
148
|
+
const verb = requestVerb(request);
|
|
149
|
+
this.pauseGreetingWhile(() => this.log(request));
|
|
150
|
+
if (!canStillSendRequest())
|
|
151
|
+
return false;
|
|
152
|
+
const waiting = this.waitForReply((err, data, message) => {
|
|
153
|
+
if (err)
|
|
154
|
+
return onReply(err);
|
|
155
|
+
const greeting = this.auth.observeReply(verb, Number(data?.code));
|
|
156
|
+
if (greeting && this.isAttached(this))
|
|
157
|
+
this.greetingAccepted(greeting);
|
|
158
|
+
this.pauseGreetingWhile(() => this.log(data));
|
|
159
|
+
onReply(null, data, message);
|
|
160
|
+
}, canStillSendRequest);
|
|
161
|
+
if (!waiting)
|
|
162
|
+
return false;
|
|
163
|
+
this.auth.observeRequest(request);
|
|
164
|
+
if (this.pendingReplyHandlers.size > 1)
|
|
165
|
+
this.auth.overlap();
|
|
166
|
+
this.write(request);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=smtp-transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"smtp-transport.js","sourceRoot":"","sources":["../src/smtp-transport.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AACvD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAIhE,qFAAqF;AACrF,MAAM,OAAO,aAAa;IA4Df;IACQ;IACA;IACA;IA9DD,IAAI,GAAG,IAAI,aAAa,EAAE,CAAA;IACnC,cAAc,GAAG,KAAK,CAAA;IACtB,UAAU,GAAG,CAAC,CAAA;IAEtB,IAAI,eAAe;QAClB,OAAO,IAAI,CAAC,IAAI,CAAC,eAAe,CAAA;IACjC,CAAC;IAED,IAAY,SAAS;QACpB,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,cAAc,CAAA;IACzC,CAAC;IAED,IAAI,cAAc;QACjB,OAAO,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,IAAI,CAAC,cAAc,CAAA;IAClD,CAAC;IACD,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,IAAI,CAAC,WAAW,CAAA;IAC/C,CAAC;IAED,2EAA2E;IAC3E,oEAAoE;IAC5D,kBAAkB,CAAC,EAAc;QACxC,MAAM,SAAS,GAAG,IAAI,CAAC,cAAc,CAAA;QACrC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAA;QAC1B,IAAI,CAAC;YACJ,EAAE,EAAE,CAAA;QACL,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,cAAc,GAAG,SAAS,CAAA;QAChC,CAAC;IACF,CAAC;IACgB,oBAAoB,GAAG,IAAI,GAAG,EAAgB,CAAA;IACvD,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;QAC3D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACnB,OAAO,IAAI,CAAA;IACZ,CAAC;CACD"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Socket } from 'net';
|
|
2
|
+
import { type SecureContextOptions, type TLSSocket } from 'tls';
|
|
3
|
+
export declare function startTlsHandshake(socket: Socket, relayHost: string, options: SecureContextOptions & {
|
|
4
|
+
rejectUnauthorized?: boolean;
|
|
5
|
+
}, timeout: number, onHandshake: (err: Error | null, tlsSocket: TLSSocket) => void): TLSSocket;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isIP } from 'net';
|
|
2
|
+
import { connect, createSecureContext, } from 'tls';
|
|
3
|
+
import { SMTPError, SMTPErrorStates } from './error.js';
|
|
4
|
+
export function startTlsHandshake(socket, relayHost, options, timeout, onHandshake) {
|
|
5
|
+
// Verify relayHost even for IP addresses, which are not sent in SNI.
|
|
6
|
+
const sniServerName = isIP(relayHost) ? undefined : relayHost;
|
|
7
|
+
const tlsSocket = connect({
|
|
8
|
+
socket,
|
|
9
|
+
host: relayHost,
|
|
10
|
+
servername: sniServerName,
|
|
11
|
+
secureContext: createSecureContext(options),
|
|
12
|
+
rejectUnauthorized: options.rejectUnauthorized !== false,
|
|
13
|
+
});
|
|
14
|
+
const finishHandshake = (err) => {
|
|
15
|
+
tlsSocket.off('secureConnect', onSecureConnect);
|
|
16
|
+
tlsSocket.off('error', finishHandshake);
|
|
17
|
+
tlsSocket.off('close', closed);
|
|
18
|
+
tlsSocket.off('timeout', timedOut);
|
|
19
|
+
onHandshake(err, tlsSocket);
|
|
20
|
+
};
|
|
21
|
+
const onSecureConnect = () => finishHandshake(null);
|
|
22
|
+
const closed = () => finishHandshake(SMTPError.create('connection closed during STARTTLS handshake', SMTPErrorStates.CONNECTIONCLOSED));
|
|
23
|
+
const timedOut = () => tlsSocket.destroy(SMTPError.create('timed out during STARTTLS handshake', SMTPErrorStates.TIMEDOUT));
|
|
24
|
+
tlsSocket.once('secureConnect', onSecureConnect);
|
|
25
|
+
tlsSocket.once('error', finishHandshake);
|
|
26
|
+
tlsSocket.once('close', closed);
|
|
27
|
+
tlsSocket.setTimeout(timeout, timedOut);
|
|
28
|
+
return tlsSocket;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=tls-upgrade.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tls-upgrade.js","sourceRoot":"","sources":["../src/tls-upgrade.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAe,MAAM,KAAK,CAAA;AACvC,OAAO,EACN,OAAO,EACP,mBAAmB,GAGnB,MAAM,KAAK,CAAA;AACZ,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEvD,MAAM,UAAU,iBAAiB,CAChC,MAAc,EACd,SAAiB,EACjB,OAAgE,EAChE,OAAe,EACf,WAA8D;IAE9D,qEAAqE;IACrE,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAA;IAC7D,MAAM,SAAS,GAAG,OAAO,CAAC;QACzB,MAAM;QACN,IAAI,EAAE,SAAS;QACf,UAAU,EAAE,aAAa;QACzB,aAAa,EAAE,mBAAmB,CAAC,OAAO,CAAC;QAC3C,kBAAkB,EAAE,OAAO,CAAC,kBAAkB,KAAK,KAAK;KACxD,CAAC,CAAA;IAEF,MAAM,eAAe,GAAG,CAAC,GAAiB,EAAE,EAAE;QAC7C,SAAS,CAAC,GAAG,CAAC,eAAe,EAAE,eAAe,CAAC,CAAA;QAC/C,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,eAAe,CAAC,CAAA;QACvC,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;QAC9B,SAAS,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;QAClC,WAAW,CAAC,GAAG,EA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