@carlgo11/smtp-server
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "@carlgo11/smtp-server", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Simple lightweight SMTP server written in Node.js", | ||
@@ -5,0 +5,0 @@ "main": "src/core/SMTPServer.js", |
@@ -132,3 +132,3 @@ # **SMTP Server** | ||
|--------------|------------------|----------------------------------------------------| | ||
| `connect` | session | Emitted when a client connects. | | ||
| `CONNECT` | session | Emitted when a client connects. | | ||
| `disconnect` | session | Emitted when a client disconnects. | | ||
@@ -135,0 +135,0 @@ | `secure` | session | Emitted when a STARTTLS connection is established. | |
@@ -5,2 +5,3 @@ import Response from '../models/Response.js'; | ||
let commandHandlers = {}; | ||
let unhandledCommands = ['HELO', 'RSET', 'VRFY', 'EXPN', 'HELP', 'NOOP']; | ||
@@ -16,4 +17,4 @@ export function registerCommand(command, handler) { | ||
export function handleCommand(message, session) { | ||
const command = message.trim().split(' ')[0].toUpperCase(); | ||
const args = message.substring(command.length).trim().split(' '); | ||
const command = message.split(' ')[0].toUpperCase(); | ||
const args = message.substring(command.length).split(' '); | ||
const handler = commandHandlers[command]; | ||
@@ -25,5 +26,9 @@ | ||
session.unknownCommands += 1; | ||
if (session.unknownCommands < context.maxUnknownCommands) | ||
session.send(new Response('Command not implemented', 502, [5, 5, 1])); | ||
else { | ||
if (session.unknownCommands < context.maxUnknownCommands) { | ||
if (unhandledCommands.includes(command)) { | ||
session.send(new Response('Command not implemented', 502, [5, 5, 1])); | ||
} else { | ||
session.send(new Response('Unknown command', 500, [5, 5, 2])); | ||
} | ||
} else { | ||
session.send(new Response('Too many unknown commands.', 421, [4, 7, 0])); | ||
@@ -30,0 +35,0 @@ session.socket.end(); |
@@ -11,4 +11,5 @@ import {hostname} from 'os'; | ||
this.maxUnknownCommands = 3; | ||
this.timeout = 60 * 1000; | ||
// Default hooks | ||
this.onConnect = () => {}; | ||
this.onConnect = async () => {}; | ||
this.onDisconnect = () => {}; | ||
@@ -15,0 +16,0 @@ this.onEHLO = async () => true; |
@@ -17,7 +17,5 @@ import net from 'net'; | ||
import DATA from '../commands/DATA.js'; | ||
import HELO from '../commands/HELO.js'; | ||
const activeSessions = new Set(); | ||
registerCommand('HELO', HELO); | ||
registerCommand('EHLO', EHLO); | ||
@@ -37,2 +35,3 @@ registerCommand('STARTTLS', STARTTLS); | ||
const session = new Session(socket); | ||
socket.setTimeout(context.timeout); | ||
@@ -47,3 +46,3 @@ // Add session to active sessions | ||
events.emit('CONNECT', session); | ||
context.onConnect(session); | ||
await context.onConnect(session); | ||
@@ -57,3 +56,7 @@ // Greet the client | ||
Logger.debug(`C: ${message}`, session.id); | ||
handleCommand(message, session); | ||
if (data.length > 512) | ||
session.send(new Response('Line too long', 500, [5, 5, 2])); | ||
else | ||
handleCommand(message, session); | ||
}); | ||
@@ -60,0 +63,0 @@ |
@@ -28,3 +28,6 @@ import tls from 'tls'; | ||
handleCommand(message, session); | ||
if (data.length > 512) | ||
session.send(new Response('Line too long', 500, [5, 5, 2])); | ||
else | ||
handleCommand(message, session); | ||
} | ||
@@ -67,6 +70,6 @@ }); | ||
Logger.info( | ||
`Connection upgraded to ${session.tls.version} (${session.tls.cipher})`, | ||
`Connection upgraded to ${tlsSocket.getProtocol()} (${tlsSocket.getCipher().standardName})`, | ||
session.id); | ||
events.emit('SECURE'); | ||
// context.onSecure(session).then(r => r); | ||
context.onSecure(session).then(r => r); | ||
}); | ||
@@ -73,0 +76,0 @@ |
@@ -38,3 +38,3 @@ import os from 'os'; | ||
* | ||
* @param {String|Error} message | ||
* @param {String|Error|Response} message | ||
* @param code {Number|Array} | ||
@@ -41,0 +41,0 @@ */ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67528
616
19