From acebf9bcabbd71f323719e991f3db5575d7da6f1 Mon Sep 17 00:00:00 2001 From: Ankit Rajpoot <anktir.42@gmail.com> Date: Thu, 29 Nov 2018 21:49:51 +0530 Subject: [PATCH] Fixed authentication check. Added ipmailer script to send mails when public ip changes. Added phonecharger script. Added styling for the homepage. --- consts.py | 2 +- db.py | 5 +++-- ipmailer.py | 18 ++++++++++++++++++ phonecharger.py | 16 ++++++++++++++++ static/styles.css | 10 ++++++++++ webserver.py | 26 +++++++++++++++++--------- webtemplates/index.html | 1 - 7 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 ipmailer.py create mode 100644 phonecharger.py diff --git a/consts.py b/consts.py index cd2136f..cafb438 100644 --- a/consts.py +++ b/consts.py @@ -15,4 +15,4 @@ CHARGER_GPIO = 2 CHARGER_RUNTIME = 10800 # 3 Hours -CHARGER_OFFTIME = 10800 # 3 Hours \ No newline at end of file +CHARGER_OFFTIME = 10800 # 3 Hours diff --git a/db.py b/db.py index c698ac0..74532b7 100644 --- a/db.py +++ b/db.py @@ -22,8 +22,9 @@ def logPumpRun(entry): def getLatestPumpRun(): with rlock: pumpRun = Query() - if (len(pumpDB.search(pumpRun.event == 'Pump Run')) > 0): - return pumpDB.all()[-1] + searchResult = pumpDB.search(pumpRun.event == "Pump Run") + if (len(searchResult) > 0): + return searchResult[-1] else: return None diff --git a/ipmailer.py b/ipmailer.py new file mode 100644 index 0000000..bce49a6 --- /dev/null +++ b/ipmailer.py @@ -0,0 +1,18 @@ +import smtplib +import json +import urllib.request + +config = {} +ip = urllib.request.urlopen("https://api.ipify.org").read().decode("utf-8") + +try: + with open("../config.json", "r") as configJson: + config = json.load(configJson) +finally: + if ("wanip" in config) is False or config["wanip"] != ip: + #Update IP + urllib.request.urlopen("https://www.randomgarage.com/postip.php?ip=" + ip) + config["wanip"] = ip + + with open("../config.json", "w") as configJson: + json.dump(fp = configJson, obj = config) diff --git a/phonecharger.py b/phonecharger.py new file mode 100644 index 0000000..c005dca --- /dev/null +++ b/phonecharger.py @@ -0,0 +1,16 @@ +# +# +# +import gpiozero +import time + +chargeRelay = gpiozero.OutputDevice(1, active_high = False, + initial_value = False) + +while True: + # Charge it for 3 hours + chargeRelay.on() + time.sleep(10800) + # Switch off power for 20 minutes + chargeRelay.off() + time.sleep(1200) diff --git a/static/styles.css b/static/styles.css index d2c5bd1..0227086 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1,5 +1,15 @@ html { font-size: 1em; + background: antiquewhite; +} + +body { + width: 50%; + margin: auto; + background: white; + padding: 10px; + border-left: 1px solid lightblue; + border-right: 1px solid lightblue; } table { diff --git a/webserver.py b/webserver.py index 577cf9b..9ac6090 100644 --- a/webserver.py +++ b/webserver.py @@ -15,7 +15,8 @@ class Index: def GET(self): - if web.ctx.env.get('HTTP_AUTHORIZATION') is None: + auth = web.ctx.env.get('HTTP_AUTHORIZATION') + if auth is None or validateUser(auth) is False: web.seeother('/login') else: temp = os.popen("vcgencmd measure_temp").readline().replace('temp=', '') @@ -23,7 +24,8 @@ def GET(self): class Logs: def GET(self, logcat, since): - if web.ctx.env.get('HTTP_AUTHORIZATION') is None: + auth = web.ctx.env.get('HTTP_AUTHORIZATION') + if auth is None or validateUser(auth) is False: web.seeother('/login') elif(logcat == 'pump'): if (since == 'all'): @@ -48,12 +50,10 @@ def GET(self): if auth is None: authreq = True else: - auth = re.sub('^Basic ','',auth) - username,password = base64.decodestring(auth).split(':') - if (username,password) in users: - raise web.seeother('/') - else: - authreq = True + if validateUser(auth): + raise web.seeother('/') + else: + authreq = True if authreq: web.header('WWW-Authenticate','Basic realm="WaterPI"') web.ctx.status = '401 Unauthorized' @@ -61,12 +61,20 @@ def GET(self): class Operations: def GET(self, operation): - if web.ctx.env.get('HTTP_AUTHORIZATION') is None: + auth = web.ctx.env.get('HTTP_AUTHORIZATION') + if auth is None or validateUser(auth) is False: web.seeother('/login') elif(operation == 'runpump'): os.popen("python3 pumpcontroller.py runnow &") return "Success" +def validateUser(auth): + auth = re.sub('^Basic ','',auth) + username,password = base64.decodestring(auth).split(':') + if (username, password) in users: + return True + else: + return False def startServer(): app = web.application(urls, globals()) diff --git a/webtemplates/index.html b/webtemplates/index.html index 4f348a2..944ab0b 100644 --- a/webtemplates/index.html +++ b/webtemplates/index.html @@ -96,7 +96,6 @@ <h3>System Status</h3> <button onclick="window.open('/operation/runpump')">Run Pump Now</button> <br /> - <button onclick="window.open('https://www.youtube.com/watch?v=9xeAiB_-MD0&feature=youtu.be')">Start Live Feed</button> <h3>Recent Pump Logs (Auto-updates every 30secs)</h3> <div id="pump-logs-div">