Mercurial > templog
annotate web/templog.py @ 301:47c259458160
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 06 Jul 2019 19:17:21 +0800 |
parents | 8441916e3095 |
children | 87c20b8c5472 |
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 | |
240 | 14 import hashlib |
28 | 15 |
27 | 16 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
17 from bottle import route, request, response |
27 | 18 |
28 | 19 import config |
20 import log | |
185 | 21 import secure |
191 | 22 import atomicfile |
28 | 23 |
69 | 24 DATE_FORMAT = '%Y%m%d-%H.%M' |
25 ZOOM_SCALE = 2.0 | |
26 | |
237 | 27 class TemplogBottle(bottle.Bottle): |
28 def run(*args, **argm): | |
29 argm['server'] = 'gevent' | |
30 super(TemplogBottle, self).run(*args, **argm) | |
31 | |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
32 bottle.default_app.push(TemplogBottle()) |
237 | 33 |
34 secure.setup_csrf() | |
35 | |
27 | 36 @route('/update', method='post') |
37 def update(): | |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
38 js_enc = request.forms.data |
28 | 39 mac = request.forms.hmac |
40 | |
240 | 41 h = hmac.new(config.HMAC_KEY, js_enc.strip(), hashlib.sha256).hexdigest() |
42 if h != mac: | |
85 | 43 raise bottle.HTTPError(code = 403, output = "Bad key") |
28 | 44 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
45 js = zlib.decompress(binascii.a2b_base64(js_enc)) |
28 | 46 |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
47 params = json.loads(js) |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
48 |
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
103
diff
changeset
|
49 log.parse(params) |
28 | 50 |
51 return "OK" | |
27 | 52 |
244 | 53 def make_graph(length, end): |
54 length_minutes = int(length) | |
55 end = datetime.strptime(end, DATE_FORMAT) | |
56 start = end - timedelta(minutes=length_minutes) | |
57 | |
58 start_epoch = time.mktime(start.timetuple()) | |
59 return log.graph_png(start_epoch, length_minutes * 60) | |
60 | |
61 def encode_data(data, mimetype): | |
62 return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip()) | |
63 | |
27 | 64 @route('/graph.png') |
65 def graph(): | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
66 response.set_header('Content-Type', 'image/png') |
250 | 67 minutes, endstr = get_request_zoom() |
68 return make_graph(minutes, endstr) | |
27 | 69 |
188 | 70 @route('/set/update', method='post') |
194 | 71 def set_update(): |
291
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
72 if not secure.check_cookie(config.ALLOWED_USERS): |
275
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
73 # the "Save" button should be disabled if the cert wasn't |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
74 # good |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
75 response.status = 403 |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
76 return "No cert, dodginess" |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
77 |
188 | 78 post_json = json.loads(request.forms.data) |
79 | |
80 csrf_blob = post_json['csrf_blob'] | |
81 | |
194 | 82 if not secure.check_csrf_blob(csrf_blob): |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
83 response.status = 403 |
194 | 84 return "Bad csrf" |
85 | |
86 ret = log.update_params(post_json['params']) | |
87 if not ret is True: | |
275
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
88 response.status = 409 # Conflict |
194 | 89 return ret |
90 | |
91 return "Good" | |
188 | 92 |
182 | 93 @route('/set') |
94 def set(): | |
291
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
95 cookie_hash = secure.init_cookie() |
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
96 allowed = ["false", "true"][secure.check_cookie(config.ALLOWED_USERS)] |
202
6dd157a12035
Add url link, improve atomicfile
Matt Johnston <matt@ucc.asn.au>
parents:
194
diff
changeset
|
97 response.set_header('Cache-Control', 'no-cache') |
291
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
98 if request.query.fake: |
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
99 inline_data = log.fake_params() |
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
100 else: |
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
101 inline_data = log.get_params() |
280
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
102 if not inline_data: |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
103 response.status = 503 # Service Unavailable |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
104 return bottle.template('noparamsyet') |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
105 |
185 | 106 return bottle.template('set', |
280
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
107 inline_data = inline_data, |
189 | 108 csrf_blob = secure.get_csrf_blob(), |
291
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
109 allowed = allowed, |
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
110 cookie_hash = cookie_hash, |
f7261dd970da
- replace ssl client certs with cookies
Matt Johnston <matt@ucc.asn.au>
parents:
280
diff
changeset
|
111 email = urllib.quote(config.EMAIL)) |
182 | 112 |
250 | 113 def get_request_zoom(): |
114 """ returns (length, end) tuple. | |
115 length is in minutes, end is a DATE_FORMAT string """ | |
69 | 116 minutes = int(request.query.get('length', 26*60)) |
117 | |
118 if 'end' in request.query: | |
119 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
120 else: | |
121 end = datetime.now() | |
122 | |
123 if 'zoom' in request.query: | |
124 orig_start = end - timedelta(minutes=minutes) | |
125 orig_end = end | |
249 | 126 scale = float(request.query.scaledwidth) / config.GRAPH_WIDTH |
127 xpos = int(request.query.x) / scale | |
77 | 128 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 129 |
77 | 130 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 131 click_time = orig_start \ |
77 | 132 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 133 minutes = int(minutes / ZOOM_SCALE) |
134 | |
264 | 135 end = click_time + timedelta(minutes=minutes//2) |
69 | 136 else: |
137 # zoom out | |
138 minutes = int(minutes*ZOOM_SCALE) | |
264 | 139 end += timedelta(minutes=minutes//2) |
69 | 140 |
141 if end > datetime.now(): | |
142 end = datetime.now() | |
250 | 143 |
144 endstr = end.strftime(DATE_FORMAT) | |
145 return (minutes, endstr) | |
146 | |
147 @route('/') | |
148 def top(): | |
149 minutes, endstr = get_request_zoom() | |
150 | |
69 | 151 request.query.replace('length', minutes) |
250 | 152 request.query.replace('end', endstr) |
69 | 153 |
154 urlparams = urllib.urlencode(request.query) | |
250 | 155 graphdata = encode_data(make_graph(minutes, endstr), 'image/png') |
69 | 156 return bottle.template('top', urlparams=urlparams, |
250 | 157 end = endstr, |
244 | 158 length = minutes, |
159 graphwidth = config.GRAPH_WIDTH, | |
160 graphdata = graphdata) | |
27 | 161 |
103 | 162 @route('/debug') |
163 def debuglog(): | |
164 response.set_header('Content-Type', 'text/plain') | |
165 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
166 |
182 | 167 @route('/env') |
168 def env(): | |
169 response.set_header('Content-Type', 'text/plain') | |
189 | 170 #return '\n'.join(traceback.format_stack()) |
171 return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
182 | 172 #return str(request.environ) |
173 #yield "\n" | |
174 #var_lookup = environ['mod_ssl.var_lookup'] | |
175 #return var_lookup("SSL_SERVER_I_DN_O") | |
176 | |
275
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
177 @route('/h') |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
178 def headers(): |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
179 response.set_header('Content-Type', 'text/plain') |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
180 return '\n'.join("%s: %s" % x for x in request.headers.items()) |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
181 |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
182 @route('/get_settings') |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
183 def get_settings(): |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
184 response.set_header('Cache-Control', 'no-cache') |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
185 req_etag = request.headers.get('etag', None) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
186 if req_etag: |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
187 # wait for it to change |
258
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
188 # XXX this is meant to return True if it has been woken up |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
189 # but it isn't working. Instead compare epochtag below. |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
190 log.fridge_settings.wait(req_etag, timeout=config.LONG_POLL_TIMEOUT) |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
191 |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
192 contents, epoch_tag = log.fridge_settings.get() |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
193 if epoch_tag == req_etag: |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
194 response.status = 304 |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
195 return "Nothing happened" |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
196 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
197 response.set_header('Content-Type', 'application/json') |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
198 return json.dumps({'params': contents, 'epoch_tag': epoch_tag}) |
252 | 199 |
182 | 200 @bottle.get('/<filename:re:.*\.js>') |
201 def javascripts(filename): | |
185 | 202 response.set_header('Cache-Control', "public, max-age=1296000") |
182 | 203 return bottle.static_file(filename, root='static') |
204 | |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
205 |
27 | 206 def main(): |
252 | 207 """ for standalone testing """ |
61 | 208 #bottle.debug(True) |
209 #bottle.run(reloader=True) | |
210 bottle.run(server='cgi', reloader=True) | |
40 | 211 #bottle.run(port=9999, reloader=True) |
27 | 212 |
213 if __name__ == '__main__': | |
214 main() | |
215 |