Mercurial > templog
annotate web/templog.py @ 631:c57821a60e51 rust
rust work in progress ?
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 06 Jul 2019 18:28:34 +0800 |
parents | 788d713f6b87 |
children | 8441916e3095 |
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(): |
275
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
72 if not secure.check_user_hash(config.ALLOWED_USERS): |
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(): | |
211
59379b2bd056
key fingerprints are case- and whitespace-insensitive.
Matt Johnston <matt@ucc.asn.au>
parents:
202
diff
changeset
|
95 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
|
96 response.set_header('Cache-Control', 'no-cache') |
280
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
97 inline_data = log.get_params() |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
98 if not inline_data: |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
99 response.status = 503 # Service Unavailable |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
100 return bottle.template('noparamsyet') |
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
101 |
185 | 102 return bottle.template('set', |
280
6c14e0573f50
make overshoot_factor floating point
Matt Johnston <matt@ucc.asn.au>
parents:
275
diff
changeset
|
103 inline_data = inline_data, |
189 | 104 csrf_blob = secure.get_csrf_blob(), |
105 allowed = allowed) | |
182 | 106 |
250 | 107 def get_request_zoom(): |
108 """ returns (length, end) tuple. | |
109 length is in minutes, end is a DATE_FORMAT string """ | |
69 | 110 minutes = int(request.query.get('length', 26*60)) |
111 | |
112 if 'end' in request.query: | |
113 end = datetime.strptime(request.query.end, DATE_FORMAT) | |
114 else: | |
115 end = datetime.now() | |
116 | |
117 if 'zoom' in request.query: | |
118 orig_start = end - timedelta(minutes=minutes) | |
119 orig_end = end | |
249 | 120 scale = float(request.query.scaledwidth) / config.GRAPH_WIDTH |
121 xpos = int(request.query.x) / scale | |
77 | 122 xpos -= config.GRAPH_LEFT_MARGIN * config.ZOOM |
69 | 123 |
77 | 124 if xpos >= 0 and xpos < config.GRAPH_WIDTH * config.ZOOM: |
69 | 125 click_time = orig_start \ |
77 | 126 + timedelta(minutes=(float(xpos) / (config.GRAPH_WIDTH * config.ZOOM)) * minutes) |
69 | 127 minutes = int(minutes / ZOOM_SCALE) |
128 | |
264 | 129 end = click_time + timedelta(minutes=minutes//2) |
69 | 130 else: |
131 # zoom out | |
132 minutes = int(minutes*ZOOM_SCALE) | |
264 | 133 end += timedelta(minutes=minutes//2) |
69 | 134 |
135 if end > datetime.now(): | |
136 end = datetime.now() | |
250 | 137 |
138 endstr = end.strftime(DATE_FORMAT) | |
139 return (minutes, endstr) | |
140 | |
141 @route('/') | |
142 def top(): | |
143 minutes, endstr = get_request_zoom() | |
144 | |
69 | 145 request.query.replace('length', minutes) |
250 | 146 request.query.replace('end', endstr) |
69 | 147 |
148 urlparams = urllib.urlencode(request.query) | |
250 | 149 graphdata = encode_data(make_graph(minutes, endstr), 'image/png') |
69 | 150 return bottle.template('top', urlparams=urlparams, |
250 | 151 end = endstr, |
244 | 152 length = minutes, |
153 graphwidth = config.GRAPH_WIDTH, | |
154 graphdata = graphdata) | |
27 | 155 |
103 | 156 @route('/debug') |
157 def debuglog(): | |
158 response.set_header('Content-Type', 'text/plain') | |
159 return log.tail_debug_log() | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
160 |
182 | 161 @route('/env') |
162 def env(): | |
163 response.set_header('Content-Type', 'text/plain') | |
189 | 164 #return '\n'.join(traceback.format_stack()) |
165 return '\n'.join(("%s %s" % k) for k in request.environ.items()) | |
182 | 166 #return str(request.environ) |
167 #yield "\n" | |
168 #var_lookup = environ['mod_ssl.var_lookup'] | |
169 #return var_lookup("SSL_SERVER_I_DN_O") | |
170 | |
275
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
171 @route('/h') |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
172 def headers(): |
9be8464e4295
Oops, we didn't authenticate the parameter update
Matt Johnston <matt@ucc.asn.au>
parents:
258
diff
changeset
|
173 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
|
174 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
|
175 |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
176 @route('/get_settings') |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
177 def get_settings(): |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
178 response.set_header('Cache-Control', 'no-cache') |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
179 req_etag = request.headers.get('etag', None) |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
180 if req_etag: |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
181 # wait for it to change |
258
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
182 # 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
|
183 # 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
|
184 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
|
185 |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
186 contents, epoch_tag = log.fridge_settings.get() |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
187 if epoch_tag == req_etag: |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
188 response.status = 304 |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
255
diff
changeset
|
189 return "Nothing happened" |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
190 |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
191 response.set_header('Content-Type', 'application/json') |
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
192 return json.dumps({'params': contents, 'epoch_tag': epoch_tag}) |
252 | 193 |
182 | 194 @bottle.get('/<filename:re:.*\.js>') |
195 def javascripts(filename): | |
185 | 196 response.set_header('Cache-Control', "public, max-age=1296000") |
182 | 197 return bottle.static_file(filename, root='static') |
198 | |
253
0a1b642e3086
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
250
diff
changeset
|
199 |
27 | 200 def main(): |
252 | 201 """ for standalone testing """ |
61 | 202 #bottle.debug(True) |
203 #bottle.run(reloader=True) | |
204 bottle.run(server='cgi', reloader=True) | |
40 | 205 #bottle.run(port=9999, reloader=True) |
27 | 206 |
207 if __name__ == '__main__': | |
208 main() | |
209 |