mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Adds some http redirect support. User is redirected to https login screen on attempted connection.
This commit is contained in:
parent
bd27e619b4
commit
42bdc055aa
85
app/classes/web/http_handler.py
Normal file
85
app/classes/web/http_handler.py
Normal file
@ -0,0 +1,85 @@
|
||||
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.full_url())
|
||||
port = 443
|
||||
db_port = helper.get_setting('https_port')
|
||||
if url[len(url)-1] == '/':
|
||||
url = url.strip(url[len(url)-1])
|
||||
|
||||
url_list = url.split('/')
|
||||
new_url_list = url_list[2].split(':')
|
||||
|
||||
if new_url_list[0] != "":
|
||||
url = 'https://' + new_url_list[0]
|
||||
else:
|
||||
url = 'https://' + url_list[2]
|
||||
|
||||
if url_list[0] != "":
|
||||
primary_url = url + ":"+str(port)+"/"
|
||||
backup_url = url + ":" +str(db_port) +"/"
|
||||
else:
|
||||
primary_url = url +":" +str(port)
|
||||
backup_url = url + ":"+str(db_port)
|
||||
|
||||
try:
|
||||
resp = requests.get(primary_url)
|
||||
resp.raise_for_status()
|
||||
url = primary_url
|
||||
except Exception as err:
|
||||
url = backup_url
|
||||
self.redirect(url)
|
||||
|
||||
|
||||
class HTTPHandlerPage(BaseHandler):
|
||||
def get(self, page):
|
||||
url = str(self.request.full_url())
|
||||
port = 443
|
||||
db_port = helper.get_setting('https_port')
|
||||
if url[len(url)-1] == '/':
|
||||
url = url.strip(url[len(url)-1])
|
||||
|
||||
url_list = url.split('/')
|
||||
new_url_list = url_list[2].split(':')
|
||||
|
||||
if new_url_list[0] != "":
|
||||
url = 'https://' + new_url_list[0]
|
||||
else:
|
||||
url = 'https://' + url_list[2]
|
||||
|
||||
if url_list[0] != "":
|
||||
primary_url = url + ":"+str(port)+"/"
|
||||
backup_url = url + ":" +str(db_port) +"/"
|
||||
else:
|
||||
primary_url = url +":" +str(port)
|
||||
backup_url = url + ":"+str(db_port)
|
||||
|
||||
try:
|
||||
resp = requests.get(primary_url)
|
||||
resp.raise_for_status()
|
||||
url = primary_url
|
||||
except Exception as err:
|
||||
url = backup_url
|
||||
self.redirect(url)
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user