Skip to content

Commit e3d755e

Browse files
authored
Merge pull request #44 from robswc/password-protection
add password protection for GUI page
2 parents d90125a + 2c81274 commit e3d755e

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
![demopic](https://user-images.githubusercontent.com/38849824/160300853-ef6fe753-36d6-41a9-9bd2-8a06f7add71d.png)
22

33
![](https://img.shields.io/github/license/robswc/tradingview-webhooks-bot?style=for-the-badge)
4-
![](https://img.shields.io/github/repo-size/robswc/tradingview-webhooks-bot?style=for-the-badge)
54
![](https://img.shields.io/github/commit-activity/y/robswc/tradingview-webhooks-bot?style=for-the-badge)
65
![](https://img.shields.io/twitter/follow/robswc?style=for-the-badge)
76

requirements.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@
3232
@app.route("/", methods=["GET"])
3333
def dashboard():
3434
if request.method == 'GET':
35+
36+
# check if gui key file exists
37+
try:
38+
with open('.gui_key', 'r') as key_file:
39+
gui_key = key_file.read().strip()
40+
# check that the gui key from file matches the gui key from request
41+
if gui_key == request.args.get('guiKey', None):
42+
pass
43+
else:
44+
return 'Access Denied', 401
45+
46+
# if gui key file does not exist, the tvwb.py did not start gui in closed mode
47+
except FileNotFoundError:
48+
logger.warning('GUI key file not found. Open GUI mode detected.')
49+
50+
# serve the dashboard
3551
action_list = am.get_all()
3652
return render_template(
3753
template_name_or_list='dashboard.html',

src/tvwb.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from logging import getLogger, DEBUG
23

34
import typer
@@ -16,9 +17,60 @@
1617
logger = get_logger(__name__)
1718

1819

19-
@app.command()
20-
def start():
21-
run('gunicorn --bind 0.0.0.0:5000 wsgi:app'.split(' '))
20+
@app.command('start')
21+
def start(
22+
open_gui: bool = typer.Option(
23+
default=False,
24+
help='Determines whether the GUI should be served at the root path, or behind a unique key.',
25+
),
26+
host: str = typer.Option(
27+
default='0.0.0.0'
28+
),
29+
port: int = typer.Option(
30+
default=5000
31+
)
32+
):
33+
34+
def clear_gui_key():
35+
try:
36+
os.remove('.gui_key')
37+
except FileNotFoundError:
38+
pass
39+
40+
def generate_gui_key():
41+
import secrets
42+
if os.path.exists('.gui_key'):
43+
pass
44+
else:
45+
open('.gui_key', 'w').write(secrets.token_urlsafe(24))
46+
47+
def read_gui_key():
48+
return open('.gui_key', 'r').read()
49+
50+
def print_gui_info():
51+
if open_gui:
52+
print('GUI is set to [OPEN] - it will be served at the root path.')
53+
print(f'\n\tView GUI dashboard here: http://{host}:{port}\n')
54+
else:
55+
print('GUI is set to [CLOSED] - it will be served at the path /?guiKey=<unique_key>')
56+
print(f'\n\tView GUI dashboard here: http://{host}:{port}?guiKey={read_gui_key()}\n')
57+
print('To run the GUI in [OPEN] mode (for development purposes only), run the following command: tvwb start --open-gui')
58+
gui_modes_url = 'https://github.com/robswc/tradingview-webhooks-bot/discussions/43'
59+
print(f'To learn more about GUI modes, visit: {gui_modes_url}')
60+
61+
62+
def run_server():
63+
run(f'gunicorn --bind {host}:{port} wsgi:app'.split(' '))
64+
65+
# clear gui key if gui is set to open, else generate key
66+
if open_gui:
67+
clear_gui_key()
68+
else:
69+
generate_gui_key()
70+
71+
print_gui_info()
72+
run_server()
73+
2274

2375

2476
@app.command('action:create')
@@ -156,5 +208,6 @@ def shell():
156208
run(f'python3 tvwb.py {cmd}'.split(' '))
157209
cmd = typer.prompt("Enter TVWB command (q) to exit")
158210

211+
159212
if __name__ == "__main__":
160213
app()

0 commit comments

Comments
 (0)