Skip to content

Instantly share code, notes, and snippets.

@ScottSturdivant
Created March 15, 2024 21:42
Show Gist options
  • Save ScottSturdivant/aade858273552ba6eea629b1267d1d8f to your computer and use it in GitHub Desktop.
Save ScottSturdivant/aade858273552ba6eea629b1267d1d8f to your computer and use it in GitHub Desktop.
aiosmtpd proxy
# Mail command:
# $ echo "asdf" | mail -S smtp=mail.example.com:25 -s "Hello!" -v me@example.com
Resolving host mail.example.com . . . done.
Connecting to 10.41.58.120:25 . . . connected.
2024-03-15 21:35:45,609 mail.log INFO ('10.41.52.189', 56430) handling connection
2024-03-15 21:35:45,609 mail.log DEBUG ('10.41.52.189', 56430) waiting PROXY handshake
2024-03-15 21:35:45,609 mail.debug DEBUG Waiting for PROXY signature
2024-03-15 21:35:45,609 mail.debug DEBUG PROXY version 1
2024-03-15 21:35:45,609 mail.debug DEBUG Get all PROXYv1 handshake
2024-03-15 21:35:45,609 mail.debug DEBUG Got PROXYv1 handshake
2024-03-15 21:35:45,609 mail.log INFO ('10.41.52.189', 56430) valid PROXY handshake
2024-03-15 21:35:45,609 root DEBUG Proxy data: ProxyData(version=1, command=None, family=<AF.INET: 1>, protocol=<PROTO.STREAM: 1>, src_addr=IPv4Address('10.41.58.120'), dst_addr=IPv4Address('10.41.52.189'), src_port=31816, dst_port=25, rest=b'', whole_raw=bytearray(b'PROXY TCP4 10.41.58.120 10.41.52.189 31816 25\r\n'), tlv_start=None, error='', _tlv=None)
2024-03-15 21:35:45,609 mail.log DEBUG ('10.41.52.189', 56430) handle_PROXY returned True
2024-03-15 21:35:45,609 mail.log DEBUG ('10.41.52.189', 56430) << b'220 mail-relay Python SMTP 1.4.4.post2'
2024-03-15 21:35:45,610 mail.log DEBUG _handle_client readline: b'\r\n'
2024-03-15 21:35:45,610 mail.log INFO ('10.41.52.189', 56430) >> b''
2024-03-15 21:35:45,610 mail.log DEBUG ('10.41.52.189', 56430) << b'500 Error: bad syntax'
2024-03-15 21:35:45,610 mail.log DEBUG _handle_client readline: b'\r\n'
2024-03-15 21:35:45,610 mail.log INFO ('10.41.52.189', 56430) >> b''
2024-03-15 21:35:45,610 mail.log DEBUG ('10.41.52.189', 56430) << b'500 Error: bad syntax'
2024-03-15 21:35:45,610 mail.log DEBUG _handle_client readline: b'\x00\r\n'
2024-03-15 21:35:45,610 mail.log INFO ('10.41.52.189', 56430) >> b'\x00'
2024-03-15 21:35:45,610 mail.log WARNING ('10.41.52.189', 56430) unrecognised:
2024-03-15 21:35:45,610 mail.log DEBUG ('10.41.52.189', 56430) << b'500 Error: command "\x00" not recognized'
2024-03-15 21:35:45,610 mail.log INFO ('10.41.52.189', 56430) connection lost
2024-03-15 21:35:45,610 mail.log INFO ('10.41.52.189', 56430) Connection lost during _handle_client()
#!/usr/bin/env python3
import time
import boto3
import logging
import sys
from aiosmtpd.controller import Controller
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
class EmailForwarder:
async def handle_PROXY(self, server, session, envelope, proxy_data):
logger.debug(f'Proxy data: {proxy_data}')
return True
async def handle_DATA(self, server, session, envelope):
logger.debug(f'server: {server}')
logger.debug(f'session: {session}')
logger.debug(f'envelope: {envelope}')
logger.debug(f'raw: {envelope.original_content}')
return '250 Message accepted for delivery'
if __name__ == '__main__':
handler = EmailForwarder()
controller = Controller(
handler,
hostname='0.0.0.0',
port=10025,
ready_timeout=10,
proxy_protocol_timeout=5.0
)
# Run the event loop in a separate thread.
try:
controller.start()
logger.info("Server is running")
except:
logger.exception("Failed to start server.")
controller.stop()
sys.exit(1)
while True:
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment