Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking Pull Request (Dashboard 0.1.9a) #82

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions reddash/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,56 @@
parser.add_argument("--host", dest="host", type=str, default="0.0.0.0")
parser.add_argument("--port", dest="port", type=int, default=42356)
parser.add_argument("--rpc-port", dest="rpcport", type=int, default=6133)
parser.add_argument("--interval", dest="interval", type=int, default=5, help=argparse.SUPPRESS)
parser.add_argument("--interval", dest="interval", type=int, default=10, help=argparse.SUPPRESS)
parser.add_argument("--debug", dest="debug", action="store_true")
parser.add_argument("--development", dest="dev", action="store_true", help=argparse.SUPPRESS)

parser.add_argument("--instance", dest="instance", type=str, default=None)


def main():
args = vars(parser.parse_args())

if args["interval"] != 5:
if args["interval"] != 10:
console.print(
"Detected interval argument. Please note that this argument should only be used if you are explicitly told to use it.",
(
"Detected interval argument. Please note that this argument should "
"only be used if you are explicitly told to use it."
),
style="bold red",
)

if args["dev"]:
console.print(
"Detected development status. Please note that this argument is only for testing. Do NOT use if you are opening this up to other people, as it can cause security issues. Confirm that you wish to run the webserver in developmental status by typing 'I agree', case sensitive, below:\n",
(
"Detected development status. Please note that this argument is only for testing."
" Do not use if you are opening this up to other people, as it can cause security "
"issues. Confirm that you wish to run the webserver in developmental status by "
"typing 'I agree', case sensitive, below:\n",
),
style="bold red",
)
confirm = input("> ")
print("")
if confirm == "I agree":
console.print(
"User has read the warnings and has typed I agree. Launching developmental server...",
"User has read the warnings and has agreed. Launching developmental server.",
style="bold red",
)
else:
console.print(
'User did not type "I agree". Launching production server...', style="bold red",
'User did not type "I agree". Launching production server.', style="bold red",
)
args["dev"] = False

create_app(
args["host"], args["port"], args["rpcport"], args["interval"], args["debug"], args["dev"],
args["host"],
args["port"],
args["rpcport"],
args["interval"],
args["debug"],
args["dev"],
args["instance"],
)


Expand Down
138 changes: 83 additions & 55 deletions reddash/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,24 @@
Copyright (c) 2020 - NeuroAssassin
"""
# Flask related libraries
from flask import Flask, url_for, session, render_template
from flask import Flask
from flask_babel import Babel
from waitress import serve

# Terminal related libraries
from rich import console, traceback as rtb, progress, logging as richlogging
from rich import console, traceback as rtb, logging as richlogging
import logging

# Secret key libraries
from cryptography import fernet
import base64

# Background thread libraries
import threading
import time
import websocket
import traceback

# Base libraries
from datetime import datetime
from babel import Locale
import json
import sys
import os
import threading

# Relative imports
from reddash.app.constants import Lock, DEFAULTS, WS_URL, ALLOWED_LOCALES
from reddash.app.data_manager import LoadManager
from reddash.app.tasks import TaskManager
from reddash.app.utils import (
register_blueprints,
Expand All @@ -49,45 +41,31 @@

console = console.Console()
oldexcepthook = rtb.install()
progress_bar = progress.Progress(
"{task.description}", progress.TextColumn("{task.fields[status]}\n")
)

logging.basicConfig(format="%(message)s", handlers=[richlogging.RichHandler(console=progress_bar)])
logging.basicConfig(
format="%(message)s", handlers=[richlogging.RichHandler(console=console, rich_tracebacks=True)]
)
queuelog.setLevel(logging.ERROR)

# Base variable setup
app = Flask("reddash", static_folder="app/base/static")
lock = Lock()
lock = threading.Lock()
babel = Babel()


def create_app(host, port, rpcport, interval, debug, dev):
url = f"{WS_URL}{rpcport}"

# Session encoding
fernet_key = fernet.Fernet.generate_key()
secret_key = base64.urlsafe_b64decode(fernet_key)

# JWT encoding
jwt_fernet_key = fernet.Fernet.generate_key()
jwt_secret_key = base64.urlsafe_b64decode(jwt_fernet_key)
def create_app(host, port, rpcport, interval, debug, dev, instance):
if debug:
log.setLevel(logging.DEBUG)
dashlog.setLevel(logging.DEBUG)

# Initialize websocket variables
app.ws_url = url
app.ws = None
app.lock = lock
app.rpcport = str(rpcport)
app.rpcversion = 0
app.interval = interval

# Initialize core variables
app.task_manager = TaskManager(app, console, progress_bar)
app.dashlog = dashlog
app.progress = progress_bar
app.console = console
app.task_manager = TaskManager(app, console)
app.running = True
app.variables = {}
app.commanddata = {}
app.cooldowns = {
"serverprefix": {},
"adminroles": {},
Expand All @@ -101,25 +79,65 @@ def create_app(host, port, rpcport, interval, debug, dev):
"removedefaultrule": {},
"fetchaliases": {},
}
app.blacklisted = []

# Initialize config
app.data = LoadManager(instance=instance)
app.data.initialize()

app.config.from_object(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config["BABEL_TRANSLATION_DIRECTORIES"] = "app/translations"

app.config["WEBSOCKET_HOST"] = "localhost"
app.config["WEBSOCKET_PORT"] = rpcport
app.config["WEBSOCKET_INTERVAL"] = interval
app.config["RPC_CONNECTED"] = False
app.config["LAST_RPC_EVENT"] = datetime.utcnow()
app.config["LAUNCH"] = datetime.utcnow()

app.config["LANGUAGES"] = [
"en",
"af_ZA",
"ar_SA",
"bg_BG",
"ca_ES",
"cs_CZ",
"da_DK",
"de_DE",
"el_GR",
"es_ES",
"fi_FI",
"fr_FR",
"he_IL",
"hu_HU",
"id_ID",
"it_IT",
"ja_JP",
"ko_KR",
"nl_NL",
"nb_NO",
"pl_PL",
"pt_BR",
"pt_PT",
"ro_RO",
"ru_RU",
"sk_SK",
"sv_SE",
"tr_TR",
"uk_UA",
"vi_VN",
"zh_CN",
"zh_HK",
"zh_TW",
]
locale_dict = {}
for locale in ALLOWED_LOCALES:
l = Locale.parse(locale)
lang = l.get_language_name(locale)
if territory := l.get_territory_name(locale):
lang = f"{l.get_language_name(locale)} - {l.get_territory_name(locale)}"
locale_dict[locale] = lang

app.config["LANGUAGES"] = ALLOWED_LOCALES
for locale in app.config["LANGUAGES"]:
loc = Locale.parse(locale)
lang = loc.get_language_name(locale)
if territory := loc.get_territory_name(locale):
lang = f"{lang} - {territory}"
locale_dict[locale] = lang
app.config["LOCALE_DICT"] = locale_dict
app.secret_key = secret_key
app.jwt_secret_key = jwt_secret_key

babel = Babel(app)

Expand All @@ -129,22 +147,32 @@ def create_app(host, port, rpcport, interval, debug, dev):
add_constants(app)
initialize_babel(app, babel)

if debug:
log.setLevel(logging.DEBUG)
dashlog.setLevel(logging.DEBUG)
# Initialize security
# Session encoding
fernet_key = app.data.core["secret_key"]
secret_key = base64.urlsafe_b64decode(fernet_key)

# JWT encoding
jwt_fernet_key = app.data.core["jwt_secret_key"]
jwt_secret_key = base64.urlsafe_b64decode(jwt_fernet_key)

app.secret_key = secret_key
app.jwt_secret_key = jwt_secret_key

kwargs = {"host": host, "port": port, "dev": dev, "debug": debug}
progress_tasks = startup_message(app, progress_bar, kwargs)
startup_message(app, kwargs)

app.task_manager.start_tasks(progress_tasks)
app.task_manager.start_tasks()

if dev:
app.run(host=host, port=port, debug=debug)
else:
serve(app, host=host, port=port, _quiet=True)

app.dashlog.fatal("Shutting down...")
dashlog.fatal("Shutting down...")
app.running = False
app.task_manager.stop_tasks()

app.dashlog.fatal("Webserver terminated")
dashlog.fatal("Webserver terminated")
# For some reason, this exits on the same line sometimes. Let's print an extra line
print("")
13 changes: 6 additions & 7 deletions reddash/app/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from reddash.app import app
from reddash.app.api import blueprint
from flask import render_template, redirect, url_for, session, request, jsonify, Response, g
from flask_babel import _, refresh
from jinja2 import TemplateNotFound
from flask import render_template, redirect, url_for, session, request, jsonify, g
from flask_babel import _
import traceback
import websocket
import json
import time
import random
import logging
import datetime

Expand All @@ -34,12 +30,15 @@ def get_result(app, requeststr):
def getservers():
if not session.get("id"):
return jsonify({"status": 0, "message": _("Not logged in")})

page = request.args.get("page", 0)

try:
requeststr = {
"jsonrpc": "2.0",
"id": 0,
"method": "DASHBOARDRPC__GET_USERS_SERVERS",
"params": [str(g.id)],
"params": [str(g.id), str(page)],
}
with app.lock:
return get_result(app, requeststr)
Expand Down
Loading