changeset 28:e3e0ed7758f9

hmac
author Matt Johnston <matt@ucc.asn.au>
date Tue, 12 Jun 2012 00:35:23 +0800
parents dbbd503119ba
children 048143905092
files server/ts.py web/config.py web/index.py web/log.py
diffstat 4 files changed, 41 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/server/ts.py	Tue Jun 12 00:09:09 2012 +0800
+++ b/server/ts.py	Tue Jun 12 00:35:23 2012 +0800
@@ -16,6 +16,10 @@
 import httplib
 import time
 import traceback
+import binascii
+import hmac
+
+import config
 
 from utils import monotonic_time, retry, readline, crc16
 
@@ -105,6 +109,14 @@
 
     return int(next_wake)
 
+def send_results(lines):
+    enc_lines = binascii.b2a_base64('\n'.join(lines))
+    hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
+
+    url_data = urllib.url_encode( ('lines', enc_lines), ('hmac', mac) )
+    con = urllib2.urlopen(config.UPDATE_URL, url_data)
+
+
 def do_comms(sock):
     print "do_comms"
     d = None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/config.py	Tue Jun 12 00:35:23 2012 +0800
@@ -0,0 +1,5 @@
+
+DATA_PATH = 'data'
+
+HMAC_KEY = 'a hmac key'
+
--- a/web/index.py	Tue Jun 12 00:09:09 2012 +0800
+++ b/web/index.py	Tue Jun 12 00:35:23 2012 +0800
@@ -1,11 +1,27 @@
 #!/usr/bin/env python2.7
 
+import binascii
+import hmac
+
 import bottle
 from bottle import route, request
 
+import config
+import log
+
 @route('/update', method='post')
 def update():
-    return "Done"
+    enc_lines = request.forms.lines
+    mac = request.forms.hmac
+
+    if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
+        raise HTTPError(code = 403, output = "Bad key")
+
+    lines = binascii.a2b_base64(enc_lines).split('\n')
+
+    log.parse(lines)
+
+    return "OK"
 
 @route('/graph.png')
 def graph():
--- a/web/log.py	Tue Jun 12 00:09:09 2012 +0800
+++ b/web/log.py	Tue Jun 12 00:35:23 2012 +0800
@@ -2,10 +2,10 @@
 import os
 import sys
 
-DATA_PATH='data'
+import config
 
 def sensor_rrd_path(s):
-    return '%s/sensor_%s.rrd' % s
+    return '%s/sensor_%s.rrd' % (config.DATA_PATH, s)
 
 def create_rrd(sensor_id):
     rrdtool.create(sensor_rrd_path(sensor_id), '-s', '300',
@@ -38,8 +38,12 @@
     for s in sensors:
         meas.append([])
 
+    def val_scale(v):
+        # convert decidegrees to degrees
+        return 0.1 * v
+
     for n in xrange(num_measurements):
-        vals = [int(entries["meas%d" % n].strip().split())]
+        vals = [val_scale(int(entries["meas%d" % n].strip().split()))]
         if len(vals) != num_sensors:
             raise Exception("Wrong number of sensors for measurement %d" % n)
         # we make an array of values for each sensor