From 2e5eeaf674a3ae224a97136a66d462e7eeea580f Mon Sep 17 00:00:00 2001 From: ndale Date: Fri, 17 Nov 2023 17:27:03 +0200 Subject: [PATCH 1/8] first commit --- jsmon.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/jsmon.py b/jsmon.py index ee271ab..7fa43d0 100755 --- a/jsmon.py +++ b/jsmon.py @@ -7,15 +7,22 @@ import json import difflib import jsbeautifier +import smtplib from decouple import config +from email.mime.text import MIMEText TELEGRAM_TOKEN = config("JSMON_TELEGRAM_TOKEN", default="CHANGEME") TELEGRAM_CHAT_ID = config("JSMON_TELEGRAM_CHAT_ID", default="CHANGEME") SLACK_TOKEN = config("JSMON_SLACK_TOKEN", default="CHANGEME") SLACK_CHANNEL_ID = config("JSMON_SLACK_CHANNEL_ID", default="CHANGEME") +EMAIL_SENDER = config("JSMON_EMAIL_FROM", default="CHANGEME") +EMAIL_RECEIVER = config("JSMON_EMAIL_TO", default="CHANGEME") +EMAIL_PASSWORD = config("JSMON_EMAIL_PASSWORD", default="CHANGEME") NOTIFY_SLACK = config("JSMON_NOTIFY_SLACK", default=False, cast=bool) NOTIFY_TELEGRAM = config("JSMON_NOTIFY_TELEGRAM", default=False, cast=bool) +NOTIFY_EMAIL = config("JSMON_NOTIFY_EMAIL", default=True, cast=bool) + if NOTIFY_SLACK: from slack import WebClient from slack.errors import SlackApiError @@ -154,19 +161,24 @@ def notify(endpoint, prev, new): if NOTIFY_SLACK: notify_slack(endpoint, prev, new, diff, prevsize, newsize) + if NOTIFY_EMAIL: + pass + // TODO mpla mpla def main(): print("JSMon - Web File Monitor") - if not(NOTIFY_SLACK or NOTIFY_TELEGRAM): - print("You need to setup Slack or Telegram Notifications of JSMon to work!") + if not(NOTIFY_SLACK or NOTIFY_TELEGRAM or NOTIFY_EMAIL): + print("You need to setup Slack or Telegram Notifications or set up a GMail account of JSMon to work!") exit(1) if NOTIFY_TELEGRAM and "CHANGEME" in [TELEGRAM_TOKEN, TELEGRAM_CHAT_ID]: print("Please Set Up your Telegram Token And Chat ID!!!") if NOTIFY_SLACK and "CHANGEME" in [SLACK_TOKEN, SLACK_CHANNEL_ID]: print("Please Set Up your Sllack Token And Channel ID!!!") + if NOTIFY_EMAIL and "CHANGEME" in [EMAIL_SENDER, EMAIL_RECEIVER, EMAIL_PASSWORD]: + print("Please Set Up your GMail account!!!") allendpoints = get_endpoint_list('targets') From 2d0ca38806786278dfa185f1f4e3a0c5d7ae0fce Mon Sep 17 00:00:00 2001 From: ndale Date: Sun, 26 Nov 2023 19:46:42 +0200 Subject: [PATCH 2/8] added email feature --- jsmon.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/jsmon.py b/jsmon.py index 7fa43d0..ba061f9 100755 --- a/jsmon.py +++ b/jsmon.py @@ -152,18 +152,34 @@ def notify_slack(endpoint,prev, new, diff, prevsize,newsize): assert e.response["error"] # str like 'invalid_auth', 'channel_not_found' print(f"Got an error: {e.response['error']}") +def notify_email(endpoint,prev, new, diff, prevsize,newsize): + try: + subject = "[JSmon] {} has been updated! View message body to check changes.".format(endpoint) + body = diff + sender = EMAIL_SENDER + recipients = [EMAIL_RECEIVER] + password = EMAIL_PASSWORD + msg = MIMEText(body) + msg['Subject'] = subject + msg['From'] = sender + msg['To'] = ', '.join(recipients) + with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server: + smtp_server.login(sender, password) + smtp_server.sendmail(sender, recipients, msg.as_string()) + except: + printf("An exception sending email.") + + def notify(endpoint, prev, new): diff = get_diff(prev,new) prevsize = get_file_stats(prev).st_size newsize = get_file_stats(new).st_size if NOTIFY_TELEGRAM: notify_telegram(endpoint, prev, new, diff, prevsize, newsize) - if NOTIFY_SLACK: notify_slack(endpoint, prev, new, diff, prevsize, newsize) if NOTIFY_EMAIL: - pass - // TODO mpla mpla + notify_email(endpoint, prev, new, diff, prevsize, newsize) def main(): From 647efa54f59748cae46012c3b8962c5e60629736 Mon Sep 17 00:00:00 2001 From: ndale Date: Sun, 26 Nov 2023 20:13:26 +0200 Subject: [PATCH 3/8] edited README.md --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c3091fb..5fb8731 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,17 @@ You need to set up your Slack or Telegram token in the Environment, e.g. by crea `touch .env` With The Contents: ``` -JSMON_NOTIFY_TELEGRAM=True -JSMON_TELEGRAM_TOKEN=YOUR TELEGRAM TOKEN -JSMON_TELEGRAM_CHAT_ID=YOUR TELEGRAM CHAT ID +#JSMON_NOTIFY_TELEGRAM=False +#JSMON_TELEGRAM_TOKEN=YOUR TELEGRAM TOKEN +#JSMON_TELEGRAM_CHAT_ID=YOUR TELEGRAM CHAT ID #JSMON_NOTIFY_SLACK=True #JSMON_SLACK_TOKEN=sometoken #JSMON_SLACK_CHANNEL_ID=somechannel +#Added email support, just enable these settings +JSMON_NOTIFY_EMAIL=True +JSMON_EMAIL_FROM=SENDER'S EMAIL +JSMON_EMAIL_TO=RECIPIENT'S EMAIL +JSMON_EMAIL_PASSWORD=SENDER'S PASSWORD ``` To Enable slack, uncomment the slack lines in the env and add your token. From 48b3fbfeaaf618cab247e9671c93275d16345373 Mon Sep 17 00:00:00 2001 From: ndale Date: Thu, 30 Nov 2023 20:10:07 +0200 Subject: [PATCH 4/8] Fixed yahoo mail --- README.md | 1 + jsmon.json | 1 - jsmon.py | 37 ++++++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 16 deletions(-) delete mode 100644 jsmon.json diff --git a/README.md b/README.md index 5fb8731..3085694 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ With The Contents: JSMON_NOTIFY_EMAIL=True JSMON_EMAIL_FROM=SENDER'S EMAIL JSMON_EMAIL_TO=RECIPIENT'S EMAIL +# This is yahoo's app password. You need to define in your yahoo account a password for jsmon app JSMON_EMAIL_PASSWORD=SENDER'S PASSWORD ``` To Enable slack, uncomment the slack lines in the env and add your token. diff --git a/jsmon.json b/jsmon.json deleted file mode 100644 index 0967ef4..0000000 --- a/jsmon.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/jsmon.py b/jsmon.py index ba061f9..f7437c3 100755 --- a/jsmon.py +++ b/jsmon.py @@ -153,21 +153,28 @@ def notify_slack(endpoint,prev, new, diff, prevsize,newsize): print(f"Got an error: {e.response['error']}") def notify_email(endpoint,prev, new, diff, prevsize,newsize): - try: - subject = "[JSmon] {} has been updated! View message body to check changes.".format(endpoint) - body = diff - sender = EMAIL_SENDER - recipients = [EMAIL_RECEIVER] - password = EMAIL_PASSWORD - msg = MIMEText(body) - msg['Subject'] = subject - msg['From'] = sender - msg['To'] = ', '.join(recipients) - with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server: - smtp_server.login(sender, password) - smtp_server.sendmail(sender, recipients, msg.as_string()) - except: - printf("An exception sending email.") + #try: + subject = "[JSmon] {} has been updated! View message body to check changes.".format(endpoint) + body = diff + sender = EMAIL_SENDER + recipients = [EMAIL_RECEIVER] + password = EMAIL_PASSWORD + msg = MIMEText(body) + msg['Subject'] = subject + msg['From'] = sender + msg['To'] = ', '.join(recipients) + with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server: + + # Authentication + smtp_server.login(sender, password) + + smtp_server.sendmail(sender, recipients, msg.as_string()) + + # Termination + # smtp_server.quit() + #except smtplib.SMTPResponseException as e: + print("An exception sending email.".format(e.smtp_error)) + print("An exception sending email.".format(e.smtp_code)) def notify(endpoint, prev, new): From 729f479808fc2a94c24777ee91bdb40888a3b5ad Mon Sep 17 00:00:00 2001 From: ndale Date: Thu, 30 Nov 2023 20:17:12 +0200 Subject: [PATCH 5/8] Fixing jsmon deletion --- jsmon.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 jsmon.json diff --git a/jsmon.json b/jsmon.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/jsmon.json @@ -0,0 +1 @@ +{} \ No newline at end of file From a827a6ab5c517a0ad35462b06507518cdcbf5038 Mon Sep 17 00:00:00 2001 From: ndale Date: Thu, 30 Nov 2023 20:27:56 +0200 Subject: [PATCH 6/8] Fixed yahoo mail --- jsmon.py | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/jsmon.py b/jsmon.py index f7437c3..fe2f03c 100755 --- a/jsmon.py +++ b/jsmon.py @@ -153,28 +153,25 @@ def notify_slack(endpoint,prev, new, diff, prevsize,newsize): print(f"Got an error: {e.response['error']}") def notify_email(endpoint,prev, new, diff, prevsize,newsize): - #try: - subject = "[JSmon] {} has been updated! View message body to check changes.".format(endpoint) - body = diff - sender = EMAIL_SENDER - recipients = [EMAIL_RECEIVER] - password = EMAIL_PASSWORD - msg = MIMEText(body) - msg['Subject'] = subject - msg['From'] = sender - msg['To'] = ', '.join(recipients) - with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server: - + try: + subject = "[JSmon] {} has been updated! View message body to check changes.".format(endpoint) + body = diff + sender = EMAIL_SENDER + recipients = [EMAIL_RECEIVER] + password = EMAIL_PASSWORD + msg = MIMEText(body) + msg['Subject'] = subject + msg['From'] = sender + msg['To'] = ', '.join(recipients) + with smtplib.SMTP_SSL('smtp.mail.yahoo.com', 465) as smtp_server: # Authentication - smtp_server.login(sender, password) - - smtp_server.sendmail(sender, recipients, msg.as_string()) - + smtp_server.login(sender, password) + smtp_server.sendmail(sender, recipients, msg.as_string()) # Termination - # smtp_server.quit() - #except smtplib.SMTPResponseException as e: - print("An exception sending email.".format(e.smtp_error)) - print("An exception sending email.".format(e.smtp_code)) + smtp_server.quit() + print("An email was sent to {}".format(recipients)) + except (Exception, e) as e: + print("An exception sending email.".format(e)) def notify(endpoint, prev, new): @@ -194,14 +191,14 @@ def main(): if not(NOTIFY_SLACK or NOTIFY_TELEGRAM or NOTIFY_EMAIL): - print("You need to setup Slack or Telegram Notifications or set up a GMail account of JSMon to work!") + print("You need to setup Slack or Telegram Notifications or set up a Yahoo account of JSMon to work!") exit(1) if NOTIFY_TELEGRAM and "CHANGEME" in [TELEGRAM_TOKEN, TELEGRAM_CHAT_ID]: print("Please Set Up your Telegram Token And Chat ID!!!") if NOTIFY_SLACK and "CHANGEME" in [SLACK_TOKEN, SLACK_CHANNEL_ID]: print("Please Set Up your Sllack Token And Channel ID!!!") if NOTIFY_EMAIL and "CHANGEME" in [EMAIL_SENDER, EMAIL_RECEIVER, EMAIL_PASSWORD]: - print("Please Set Up your GMail account!!!") + print("Please Set Up your Yahoo account!!!") allendpoints = get_endpoint_list('targets') From f223773903e640edebf852496909766ffeae31e8 Mon Sep 17 00:00:00 2001 From: Nikos Dalezios Date: Thu, 30 Nov 2023 20:30:13 +0200 Subject: [PATCH 7/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3085694..03d8bf1 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,11 @@ With The Contents: #JSMON_NOTIFY_SLACK=True #JSMON_SLACK_TOKEN=sometoken #JSMON_SLACK_CHANNEL_ID=somechannel -#Added email support, just enable these settings +#Added yahoo email support, just enable these settings and to define in your yahoo account a password for jsmon app JSMON_NOTIFY_EMAIL=True JSMON_EMAIL_FROM=SENDER'S EMAIL JSMON_EMAIL_TO=RECIPIENT'S EMAIL -# This is yahoo's app password. You need to define in your yahoo account a password for jsmon app +# This is yahoo's app password. JSMON_EMAIL_PASSWORD=SENDER'S PASSWORD ``` To Enable slack, uncomment the slack lines in the env and add your token. From 1d3d86ca7e5c97a862d58db03e189bfb4d4fdcbb Mon Sep 17 00:00:00 2001 From: ndale Date: Sat, 2 Dec 2023 21:06:27 +0200 Subject: [PATCH 8/8] Changed mimetype to html --- jsmon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsmon.py b/jsmon.py index fe2f03c..8be2279 100755 --- a/jsmon.py +++ b/jsmon.py @@ -159,7 +159,7 @@ def notify_email(endpoint,prev, new, diff, prevsize,newsize): sender = EMAIL_SENDER recipients = [EMAIL_RECEIVER] password = EMAIL_PASSWORD - msg = MIMEText(body) + msg = MIMEText(body, "html") msg['Subject'] = subject msg['From'] = sender msg['To'] = ', '.join(recipients)