Merge branch 'http_redirect' into 'dev'

Adds support for http redirects to https

See merge request crafty-controller/crafty-commander!61
This commit is contained in:
Andrew 2021-08-24 22:33:27 +00:00
commit 754da44b5b
2 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,57 @@
import sys
import json
import logging
import tornado.web
import tornado.escape
import requests
from app.classes.shared.helpers import helper
from app.classes.web.base_handler import BaseHandler
from app.classes.shared.console import console
from app.classes.shared.models import Users, fn, db_helper
logger = logging.getLogger(__name__)
try:
import bleach
except ModuleNotFoundError as e:
logger.critical("Import Error: Unable to load {} module".format(e.name), exc_info=True)
console.critical("Import Error: Unable to load {} module".format(e.name))
sys.exit(1)
class HTTPHandler(BaseHandler):
def get(self):
url = str(self.request.host)
port = 443
url_list = url.split(":")
if url_list[0] != "":
url = 'https://' + url_list[0]
else:
url = 'https://' + url
db_port = helper.get_setting('https_port')
try:
resp = requests.get(url + ":" + str(port))
resp.raise_for_status()
except Exception as err:
port = db_port
self.redirect(url+":"+str(port))
class HTTPHandlerPage(BaseHandler):
def get(self, page):
url = str(self.request.host)
port = 443
url_list = url.split(":")
if url_list[0] != "":
url = 'https://' + url_list[0]
else:
url = 'https://' + url
db_port = helper.get_setting('https_port')
try:
resp = requests.get(url + ":" + str(port))
resp.raise_for_status()
except Exception as err:
port = db_port
self.redirect(url+":"+str(port))

View File

@ -28,6 +28,7 @@ try:
from app.classes.web.static_handler import CustomStaticHandler
from app.classes.shared.translation import translation
from app.classes.web.upload_handler import UploadHandler
from app.classes.web.http_handler import HTTPHandler, HTTPHandlerPage
except ModuleNotFoundError as e:
logger.critical("Import Error: Unable to load {} module".format(e, e.name))
@ -147,8 +148,30 @@ class Webserver:
static_handler_class=CustomStaticHandler,
serve_traceback=debug_errors,
)
HTTPhanders = [(r'/', HTTPHandler, handler_args),
(r'/public/(.*)', HTTPHandlerPage, handler_args),
(r'/panel/(.*)', HTTPHandlerPage, handler_args),
(r'/server/(.*)', HTTPHandlerPage, handler_args),
(r'/ajax/(.*)', HTTPHandlerPage, handler_args),
(r'/api/stats/servers', HTTPHandlerPage, handler_args),
(r'/api/stats/node', HTTPHandlerPage, handler_args),
(r'/ws', HTTPHandlerPage, handler_args),
(r'/upload', HTTPHandlerPage, handler_args)]
HTTPapp = tornado.web.Application(
HTTPhanders,
template_path=os.path.join(helper.webroot, 'templates'),
static_path=os.path.join(helper.webroot, 'static'),
debug=debug_errors,
cookie_secret=cookie_secret,
xsrf_cookies=True,
autoreload=False,
log_function=self.log_function,
default_handler_class = HTTPHandler,
login_url="/login",
serve_traceback=debug_errors,
)
self.HTTP_Server = tornado.httpserver.HTTPServer(app)
self.HTTP_Server = tornado.httpserver.HTTPServer(HTTPapp)
self.HTTP_Server.listen(http_port)
self.HTTPS_Server = tornado.httpserver.HTTPServer(app, ssl_options=cert_objects)