-
Notifications
You must be signed in to change notification settings - Fork 21
/
hsts.py
43 lines (35 loc) · 1.32 KB
/
hsts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import tornado.httpserver
import tornado.ioloop
import tornado.web
class HSTSCookie(tornado.web.RequestHandler):
def get(self):
self.set_header('Access-Control-Allow-Origin', '*')
if self.request.protocol == 'https':
self.set_header('Strict-Transport-Security', 'max-age=31536000')
self.write(self.request.protocol)
self.write('<br>')
self.write(self.request.host)
class Home(tornado.web.RequestHandler):
def get(self):
self.set_header('Access-Control-Allow-Origin', '*')
self.write('<html><head><script src="hsts.js"></script></head><body></body></html>')
class HSTSScript(tornado.web.RequestHandler):
def get(self):
with open('hsts.js', 'r') as f:
script = f.read()
script = script.replace('[HOSTNAME]', 'bugben.com')
self.write(script)
application = tornado.web.Application([
(r'/', Home),
(r'/h.gif', HSTSCookie),
(r'/hsts.js', HSTSScript),
])
if __name__ == '__main__':
https_server = tornado.httpserver.HTTPServer(application, ssl_options={
"certfile": "keys/bugben.com.key.crt",
"keyfile": "keys/bugben.com.key",
})
http_server = tornado.httpserver.HTTPServer(application)
https_server.listen(443)
http_server.listen(80)
tornado.ioloop.IOLoop.instance().start()