From 3f5e930fd40b8815aa9c8f0d6fc3da4925c12a19 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Sat, 4 May 2019 13:49:46 +0930 Subject: [PATCH] Add SSL support to email notification. --- auto_rx/auto_rx.py | 8 ++++---- auto_rx/autorx/config.py | 16 ++++++++++++++++ auto_rx/autorx/email_notification.py | 15 +++++++++++++-- auto_rx/autorx/web.py | 10 ++++++++-- auto_rx/station.cfg.example | 8 ++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/auto_rx/auto_rx.py b/auto_rx/auto_rx.py index b5700b1..d455ae9 100644 --- a/auto_rx/auto_rx.py +++ b/auto_rx/auto_rx.py @@ -66,10 +66,6 @@ exporter_functions = [] # This list will hold references to the exporter add fun temporary_block_list = {} -# Scan Result Queue -# Scan results are processed asynchronously from the main scanner object. -#scan_results = Queue() - def allocate_sdr(check_only = False, task_description = ""): """ Allocate an un-used SDR for a task. @@ -559,6 +555,10 @@ def main(): _email_notification = EmailNotification( smtp_server = config['email_smtp_server'], + smtp_port = config['email_smtp_port'], + smtp_ssl = config['email_smtp_ssl'], + smtp_login = config['email_smtp_login'], + smtp_password = config['email_smtp_password'], mail_from = config['email_from'], mail_to = config['email_to'], mail_subject = config['email_subject'] diff --git a/auto_rx/autorx/config.py b/auto_rx/autorx/config.py index ac8e21f..0b61337 100644 --- a/auto_rx/autorx/config.py +++ b/auto_rx/autorx/config.py @@ -43,6 +43,10 @@ def read_auto_rx_config(filename): # Email Settings 'email_enabled': False, 'email_smtp_server': 'localhost', + 'email_smtp_port': 25, + 'email_smtp_ssl': False, + 'email_smtp_login': 'None', + 'email_smtp_password': 'None', 'email_from': 'sonde@localhost', 'email_to': None, 'email_subject': " Sonde launch detected on : ", @@ -144,6 +148,10 @@ def read_auto_rx_config(filename): try: auto_rx_config['email_enabled'] = config.getboolean('email', 'email_enabled') auto_rx_config['email_smtp_server'] = config.get('email', 'smtp_server') + auto_rx_config['email_smtp_port'] = config.get('email', 'smtp_port') + auto_rx_config['email_smtp_ssl'] = config.getboolean('email', 'smtp_ssl') + auto_rx_config['email_smtp_login'] = config.get('email', 'smtp_login') + auto_rx_config['email_smtp_password'] = config.get('email', 'smtp_password') auto_rx_config['email_from'] = config.get('email', 'from') auto_rx_config['email_to'] = config.get('email', 'to') auto_rx_config['email_subject'] = config.get('email', 'subject') @@ -270,12 +278,14 @@ def read_auto_rx_config(filename): auto_rx_config['experimental_decoders']['M10'] = config.getboolean('advanced', 'm10_experimental') auto_rx_config['experimental_decoders']['DFM'] = config.getboolean('advanced', 'dfm_experimental') # When LMS6 support is added, that will have to be added in here. + auto_rx_config['web_debug'] = config.getboolean('web', 'web_debug') except: logging.error("Config - Missing new advanced decoder settings, using defaults.") auto_rx_config['rs41_drift_tweak'] = False auto_rx_config['decoder_spacing_limit'] = 15000 auto_rx_config['decoder_stats'] = False + auto_rx_config['web_debug'] = False @@ -331,6 +341,12 @@ def read_auto_rx_config(filename): else: # Create a global copy of the configuration file at this point global_config = copy.deepcopy(auto_rx_config) + + # Excise some sensitive parameters from the global config. + global_config.pop('email_smtp_login') + global_config.pop('email_smtp_password') + global_config.pop('email_smtp_server') + return auto_rx_config diff --git a/auto_rx/autorx/email_notification.py b/auto_rx/autorx/email_notification.py index 350365c..7e91bda 100644 --- a/auto_rx/autorx/email_notification.py +++ b/auto_rx/autorx/email_notification.py @@ -32,9 +32,13 @@ class EmailNotification(object): # We require the following fields to be present in the input telemetry dict. REQUIRED_FIELDS = [ 'id', 'lat', 'lon', 'alt', 'type', 'freq'] - def __init__(self, smtp_server = 'localhost', mail_from = None, mail_to = None, mail_subject = None): + def __init__(self, smtp_server = 'localhost', smtp_port=25, smtp_ssl=False, smtp_login="None", smtp_password="None", mail_from = None, mail_to = None, mail_subject = None): """ Init a new E-Mail Notification Thread """ self.smtp_server = smtp_server + self.smtp_port = smtp_port + self.smtp_ssl = smtp_ssl + self.smtp_login = smtp_login + self.smtp_password = smtp_password self.mail_from = mail_from self.mail_to = mail_to self.mail_subject = mail_subject @@ -118,7 +122,14 @@ class EmailNotification(object): msg['To'] = self.mail_to msg["Date"] = formatdate() - s = smtplib.SMTP(self.smtp_server) + if self.smtp_ssl: + s = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port) + else: + s = smtplib.SMTP(self.smtp_server, self.smtp_port) + + if self.smtp_login != "None": + s.login(self.smtp_login, self.smtp_password) + s.sendmail(msg['From'], msg['To'], msg.as_string()) s.quit() diff --git a/auto_rx/autorx/web.py b/auto_rx/autorx/web.py index b556f29..dc06cf6 100644 --- a/auto_rx/autorx/web.py +++ b/auto_rx/autorx/web.py @@ -138,7 +138,10 @@ def shutdown_flask(shutdown_key): @app.route('/start_decoder', methods=['POST']) def flask_start_decoder(): """ Inject a scan result, which will cause a decoder to be started if there - are enough resources (SDRs) to do so. """ + are enough resources (SDRs) to do so. + Example: + curl -d "type=DFM&freq=403240000" -X POST http://localhost:5000/start_decoder + """ if request.method == 'POST' and autorx.config.global_config['web_debug']: _type = str(request.form['type']) _freq = float(request.form['freq']) @@ -154,7 +157,10 @@ def flask_start_decoder(): @app.route('/stop_decoder', methods=['POST']) def flask_stop_decoder(): - """ Request that a decoder process be halted. """ + """ Request that a decoder process be halted. + Example: + curl -d "freq=403250000" -X POST http://localhost:5000/stop_decoder + """ if request.method == 'POST' and autorx.config.global_config['web_debug']: _freq = float(request.form['freq']) diff --git a/auto_rx/station.cfg.example b/auto_rx/station.cfg.example index 8d9a705..066010d 100644 --- a/auto_rx/station.cfg.example +++ b/auto_rx/station.cfg.example @@ -228,6 +228,10 @@ payload_summary_port = 55672 [email] email_enabled = False smtp_server = localhost +smtp_port = 25 +smtp_ssl = False +smtp_login = None +smtp_password = None from = sonde@localhost to = someone@example.com # Custom subject field. The following fields can be included: @@ -288,6 +292,10 @@ web_port = 5000 # Note: The higher this number, the more data the client will need to load in on startup archive_age = 120 +# Enable some additional debug endpoints. These are currently only used for development testing purposes. +# Do not set this to True on an internet-facing auto_rx instance!!! +web_debug = False + ################## # DEBUG SETTINGS #