forked from summertriangle-dev/sparklebox
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
103 lines (87 loc) · 3.55 KB
/
app.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import locale
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
import tornado.httpserver
import tornado.ioloop
import tornado.web
import os
import tornado.options
import json
import ipaddress
import functools
import subprocess
import user_agents
from collections import namedtuple
import models
import dispatch
import endpoints
import api_endpoints
import enums
import starlight
import analytics
import webutil
from starlight import private_data_path
def early_init():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
if not os.environ.get("DISABLE_HTTPS_ENFORCEMENT", "") and not os.environ.get("DEV", ""):
# production mode: force https usage due to local storage issues
# also we don't want the NSA knowing you play chinese cartoon games
def _swizzle_RequestHandler_prepare(self):
if self.request.protocol != "https":
self.redirect(
"https://{0}{1}".format(self.request.host, self.request.uri))
tornado.web.RequestHandler.prepare = _swizzle_RequestHandler_prepare
if os.environ.get("BEHIND_CLOUDFLARE") == "1":
cloudflare_ranges = []
with open("cloudflare.txt", "r") as cf:
for line in cf:
cloudflare_ranges.append(ipaddress.ip_network(line.strip()))
_super_RequestHandler_prepare2 = tornado.web.RequestHandler.prepare
def _swizzle_RequestHandler_prepare2(self):
for net in cloudflare_ranges:
if ipaddress.ip_address(self.request.remote_ip) in net:
if "CF-Connecting-IP" in self.request.headers:
self.request.remote_ip = self.request.headers[
"CF-Connecting-IP"]
break
_super_RequestHandler_prepare2(self)
tornado.web.RequestHandler.prepare = _swizzle_RequestHandler_prepare2
_super_RequestHandler_prepare3 = tornado.web.RequestHandler.prepare
def _swizzle_RequestHandler_prepare3(self):
self.request.is_low_bandwidth = 0
if "User-Agent" in self.request.headers:
ua = user_agents.parse(self.request.headers["User-Agent"])
if ua.is_mobile or ua.is_tablet:
self.request.is_low_bandwidth = 1
_super_RequestHandler_prepare3(self)
tornado.web.RequestHandler.prepare = _swizzle_RequestHandler_prepare3
def main():
starlight.init()
early_init()
in_dev_mode = os.environ.get("DEV")
image_server = os.environ.get("IMAGE_HOST", "")
tornado.options.parse_command_line()
application = tornado.web.Application(dispatch.ROUTES,
template_path="webui",
static_path="static",
image_host=image_server,
debug=in_dev_mode,
is_dev=in_dev_mode,
tle=models.TranslationEngine(starlight),
enums=enums,
starlight=starlight,
tlable=webutil.tlable,
webutil=webutil,
analytics=analytics.Analytics(),
# Change every etag when the server restarts, in case we change what the output looks like.
instance_random=os.urandom(8))
http_server = tornado.httpserver.HTTPServer(application, xheaders=1)
addr = os.environ.get("ADDRESS", "0.0.0.0")
port = int(os.environ.get("PORT", 5000))
http_server.listen(port, addr)
print("Current APP_VER:", os.environ.get("VC_APP_VER",
"1.9.1 (warning: Truth updates will fail in the future if an accurate VC_APP_VER "
"is not set. Export VC_APP_VER to suppress this warning.)"))
print("Ready.")
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()