Mercurial > templog
annotate web/templog.py @ 226:da769023bf08
Fix race setting umask
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 28 Feb 2015 00:14:02 +0800 |
parents | 59379b2bd056 |
children | 88dfac30d065 |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python2.7 |
2 | |
28 | 3 import binascii |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
4 import json |
28 | 5 import hmac |
29 | 6 import zlib |
69 | 7 from datetime import datetime, timedelta |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
8 import time |
69 | 9 import urllib |
10 import sys | |
182 | 11 import os |
185 | 12 import traceback |
13 import fcntl | |
28 | 14 |
27 | 15 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
16 from bottle import route, request, response |
27 | 17 |
28 | 18 import config |
19 import log | |
185 | 20 import secure |
191 | 21 import atomicfile |
28 | 22 |
69 | 23 DATE_FORMAT = '%Y%m%d-%H.%M' |
24 ZOOM_SCALE = 2.0 | |
25 | |
27 | 26 @route('/update', method='post') |
27 def update(): | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
28 js_enc = request.forms.data |
28 | 29 mac = request.forms.hmac |
30 | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
31 if hmac.new(config.HMAC_KEY, js_enc).hexdigest() != mac: |
85 | 32 raise bottle.HTTPError(code = 403, output = "Bad key") |
28 | 33 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
34 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
28 | 35 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
36 params = json.loads(js) |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
37 |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
38 log.parse(params) |
28 | 39 |
40 return "OK" | |
27 | 41 |
42 @route('/graph.png') | |
43 def graph(): | |
69 | 44 length_minutes = int(request.query.length) |
45 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
46 start = end - timedelta(minutes=length_minutes) | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
47 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
48 response.set_header('Content-Type', 'image/png') |
69 | 49 start_epoch = time.mktime(start.timetuple()) |
50 return log.graph_png(start_epoch, length_minutes * 60) | |
27 | 51 |
188 | 52 @route('/set/update', method='post') |
194 | 53 def set_update(): |
188 | 54 post_json = json.loads(request.forms.data) |
55 | |
56 csrf_blob = post_json['csrf_blob'] | |
57 | |
194 | 58 if not secure.check_csrf_blob(csrf_blob): |
59 bottle.response.status = 403 | |
60 return "Bad csrf" | |
61 | |
62 ret = log.update_params(post_json['params']) | |
63 if not ret is True: | |
64 bottle.response.status = 403 | |
65 return ret | |
66 | |
67 return "Good" | |
188 | 68 |
182 | 69 @route('/set') |
70 def set(): | |
211
59379b2bd056
key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents:
202
diff
changeset
|
71 allowed = ["false", "true"][secure.check_user_hash(config.ALLOWED_USERS)] |
202
6dd157a12035
Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents:
194
diff
changeset
|
72 response.set_header('Cache-Control', 'no-cache') |
185 | 73 return bottle.template('set', |
74 inline_data = log.get_params(), | |
189 | 75 csrf_blob = secure.get_csrf_blob(), |
76 allowed = allowed) | |
182 | 77 |
78 @route('/set_current.json') | |
79 def set_fresh(): | |
80 response.set_header('Content-Type', 'application/javascript') | |
81 return log.get_current() | |
82 | |
27 | 83 @route('/') |
84 def top(): | |
69 | 85 |
86 minutes = int(request.query.get('length', 26*60)) | |
87 | |
88 if 'end' in request.query: | |
89 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
90 else: | |
91 end = datetime.now() | |
92 | |
93 if 'zoom' in request.query: | |
94 orig_start = end - timedelta(minutes=minutes) | |
95 orig_end = end | |
96 xpos = int(request.query.x) | |
77 | 97 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 98 |
77 | 99 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 100 click_time = orig_start \ |
77 | 101 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 102 minutes = int(minutes / ZOOM_SCALE) |
103 | |
104 end = click_time + timedelta(minutes=minutes/2) | |
105 else: | |
106 # zoom out | |
107 minutes = int(minutes*ZOOM_SCALE) | |
108 end += timedelta(minutes=minutes/2) | |
109 | |
110 if end > datetime.now(): | |
111 end = datetime.now() | |
112 | |
113 request.query.replace('length', minutes) | |
114 request.query.replace('end', end.strftime(DATE_FORMAT)) | |
115 | |
116 urlparams = urllib.urlencode(request.query) | |
117 return bottle.template('top', urlparams=urlparams, | |
118 end = end.strftime(DATE_FORMAT), | |
119 length = minutes) | |
27 | 120 |
103 | 121 @route('/debug') |
122 def debuglog(): | |
123 response.set_header('Content-Type', 'text/plain') | |
124 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
125 |
182 | 126 @route('/env') |
127 def env(): | |
128 response.set_header('Content-Type', 'text/plain') | |
189 | 129 #return '\n'.join(traceback.format_stack()) |
130 return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
182 | 131 #return str(request.environ) |
132 #yield "\n" | |
133 #var_lookup = environ['mod_ssl.var_lookup'] | |
134 #return var_lookup("SSL_SERVER_I_DN_O") | |
135 | |
136 @bottle.get('/<filename:re:.*\.js>') | |
137 def javascripts(filename): | |
185 | 138 response.set_header('Cache-Control', "public, max-age=1296000") |
182 | 139 return bottle.static_file(filename, root='static') |
140 | |
185 | 141 secure.setup_csrf() |
182 | 142 |
27 | 143 def main(): |
61 | 144 #bottle.debug(True) |
145 #bottle.run(reloader=True) | |
146 bottle.run(server='cgi', reloader=True) | |
40 | 147 #bottle.run(port=9999, reloader=True) |
27 | 148 |
149 if __name__ == '__main__': | |
150 main() | |
151 |