add pure tls smptp

This commit is contained in:
Balakrishnan Balasubramanian 2018-12-25 15:37:01 -05:00
parent 98b4ea7c95
commit 2d83267b55
2 changed files with 32 additions and 7 deletions

View File

@ -10,7 +10,7 @@ import sys
from argparse import ArgumentParser from argparse import ArgumentParser
from pathlib import Path from pathlib import Path
from .smtp import create_smtp_server from .smtp import create_smtp_server_starttls, create_smtp_server_tls
from .pop3 import create_pop_server from .pop3 import create_pop_server
@ -33,6 +33,8 @@ def parse_args():
# Hardcoded args # Hardcoded args
args.host = '0.0.0.0' args.host = '0.0.0.0'
args.smtp_port = 25 args.smtp_port = 25
args.smtp_port_tls = 465
args.smtp_port_submission = 587
args.pop_port = 995 args.pop_port = 995
args.smtputf8 = True args.smtputf8 = True
args.debug = True args.debug = True
@ -66,9 +68,15 @@ def drop_privileges():
async def a_main(args, tls_context): async def a_main(args, tls_context):
pop_server = await create_pop_server( pop_server = await create_pop_server(
args.mail_dir_path, port=args.pop_port, host=args.host, context=tls_context, password_hash=args.password_hash) args.mail_dir_path, port=args.pop_port, host=args.host, context=tls_context, password_hash=args.password_hash)
smtp_server = await create_smtp_server(args.mail_dir_path, port=args.smtp_port, host=args.host, context=tls_context) smtp_server_starttls = await create_smtp_server_starttls(
args.mail_dir_path, port=args.smtp_port, host=args.host, context=tls_context)
smtp_server_tls = await create_smtp_server_tls(
args.mail_dir_path, port=args.smtp_port_tls, host=args.host, context=tls_context)
drop_privileges() drop_privileges()
await asyncio.gather(pop_server.serve_forever(), smtp_server.serve_forever()) await asyncio.gather(
pop_server.serve_forever(),
smtp_server_starttls.serve_forever(),
smtp_server_tls.serve_forever())
def main(): def main():

View File

@ -31,7 +31,7 @@ class MailboxCRLF(Mailbox):
self.mailbox = MaildirCRLF(mail_dir) self.mailbox = MaildirCRLF(mail_dir)
def protocol_factory(dirpath: Path, context: ssl.SSLContext = None): def protocol_factory_starttls(dirpath: Path, context: ssl.SSLContext = None):
logging.info("Got smtp client cb") logging.info("Got smtp client cb")
try: try:
handler = MailboxCRLF(dirpath) handler = MailboxCRLF(dirpath)
@ -46,14 +46,31 @@ def protocol_factory(dirpath: Path, context: ssl.SSLContext = None):
return smtp return smtp
async def create_smtp_server(dirpath: Path, port: int, host="", context: ssl.SSLContext = None): def protocol_factory(dirpath: Path):
logging.info("Got smtp client cb")
try:
handler = MailboxCRLF(dirpath)
smtp = SMTP(handler=handler, data_size_limit=DATA_SIZE_DEFAULT, enable_SMTPUTF8=True)
except Exception as e:
logging.error("Something went wrong", e)
raise
return smtp
async def create_smtp_server_starttls(dirpath: Path, port: int, host="", context: ssl.SSLContext = None):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
return await loop.create_server(partial(protocol_factory, dirpath, context), return await loop.create_server(partial(protocol_factory_starttls, dirpath, context),
host=host, port=port, start_serving=False) host=host, port=port, start_serving=False)
async def create_smtp_server_tls(dirpath: Path, port: int, host="", context: ssl.SSLContext = None):
loop = asyncio.get_event_loop()
return await loop.create_server(partial(protocol_factory, dirpath),
host=host, port=port, ssl=context, start_serving=False)
async def a_main(*args, **kwargs): async def a_main(*args, **kwargs):
server = await create_smtp_server(*args, **kwargs) server = await create_smtp_server_starttls(*args, **kwargs)
await server.serve_forever() await server.serve_forever()