Client-Ansible-Setup/proxy_resources/home/mail-authserver.py

51 lines
1.9 KiB
Python
Executable File

# {{ ansible_managed }}
from http import server
class HTTPRequestHandler(server.SimpleHTTPRequestHandler):
def end_headers(self):
# The idea is to use <RCPT TO> value to choose and connect to different mail server
# rcpt_to_header = self.headers['Auth-SMTP-To']
# rcpt_to = rcpt_to_header.split(":")[1][2:-1]
# Print all available headers and their value for debug
for k in self.headers:
print(k)
print("Value of header :", self.headers[k])
port_header = self.headers['X-Auth-Port']
# protocol = protocol_header.split(":")[0][2:-1]
# self.send_custom_headers(rcpt_to)
self.send_custom_headers(port_header)
server.SimpleHTTPRequestHandler.end_headers(self)
def send_custom_headers(self, port):
# This is a simple logic to choose mail server
# if rcpt.split("@")[1] == "eng.abc.com":
# port = "2525"
# elif rcpt.split("@")[1] == "hr.abc.com":
# port = "2526"
# Need to modify/create new program to list all headers that will be provided. If there is a port header/way to distinguish whether it is Implicit or Explicit TLS used we can use that to go to the correct port.
# We will do it based on protocol instead
# Will only allow Explicit TLS for now
# if protocol == "smtp":
# port = "3469"
# elif protocol == "imap":
# port = "6994"
# Authentication handled by backend for now for both IMAP and SMTP, may in the future do authentication here too
self.send_header("Auth-Status", "OK")
# Server IP
self.send_header("Auth-Server","{{ proxy_server_ip }}")
# Server port
self.send_header("Auth-Port", port)
# Debug Prints
print("Return Auth-Port:", port)
if __name__ == '__main__':
server.test(HandlerClass=HTTPRequestHandler)