Skip to content

Commit 699616e

Browse files
committed
user management
1 parent 106397c commit 699616e

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ venv/
55
.env
66

77
config.yaml
8+
users.yaml
89

910
__pycache__
1011
__pycache__/

blueprints/gate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
@gateway.route('/')
23-
def gateway_fun(code=""):
23+
def gateway_fun():
2424
return render_template(
2525
"index.jinja2",
2626
captcha=configuration.captcha,
@@ -58,4 +58,4 @@ def wrapper(invite):
5858
print("Ratelimit")
5959
return redirect("/")
6060

61-
return invite["code"]#redirect(f"https://discord.gg/{invite['code']}")
61+
return redirect(f"https://discord.gg/{invite['code']}")

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ PyYAML
33
blessed
44
pwinput
55
requests
6+
py-bcrypt

setup.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from blessed import Terminal
44
from pwinput import pwinput
55
from source.conf import Configuration
6+
from source import conf
67
from source.decorators import catch_goodbye
78
from source.utils import goodbye
89

@@ -77,13 +78,59 @@ def interactive_config():
7778
goodbye()
7879

7980

81+
@catch_goodbye()
82+
def manage_users():
83+
users = conf.load_users()
84+
print(term.gray100(" What would you like to do?"))
85+
print(" 1. List users")
86+
print(" 2. Add new user")
87+
print(" 3. Remove user")
88+
print(" 0. Exit")
89+
90+
i = input("\n-- ")
91+
try:
92+
i = int(i)
93+
except ValueError:
94+
manage_users()
95+
96+
if i == 1:
97+
print(f"There are {len(users.users)} registered users")
98+
print("index | username | password")
99+
for index, user in enumerate(users.users):
100+
print(f"[{index}] | {user.username} | {user.password}")
101+
print()
102+
manage_users()
103+
elif i == 2:
104+
print("Enter username: ")
105+
username = input()
106+
print("Enter password: ")
107+
password = pwinput(mask="*", prompt="")
108+
users.add_user(username, password)
109+
conf.save_users(users)
110+
print()
111+
manage_users()
112+
elif i == 3:
113+
print("Enter username: ")
114+
username = input()
115+
users.remove_user(username)
116+
conf.save_users(users)
117+
print()
118+
manage_users()
119+
elif i == 0:
120+
goodbye()
121+
quit(0)
122+
else:
123+
manage_users()
124+
125+
80126
@catch_goodbye()
81127
def welcome():
82128
print(term.home + term.clear + term.move_y(0))
83129
print(term.black_on_orange(' Welcome to f1rewall setup!' + " " * (term.width - 28)))
84130
print(term.gray100(" What would you like to do?"))
85131
print(" 1. Generate settings file (manual setup)")
86132
print(" 2. Run interactive setup " + term.green("[recommended]"))
133+
print(" 3. Manage users (f1rewall analytics access)")
87134
print(" 0. Exit")
88135

89136
i = input("\n-- ")
@@ -96,6 +143,8 @@ def welcome():
96143
generate_config()
97144
elif i == 2:
98145
interactive_config()
146+
elif i == 3:
147+
manage_users()
99148
elif i == 0:
100149
goodbye()
101150
quit(0)

source/conf.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import bcrypt
12
import yaml
23
import os
34

5+
46
class Configuration:
57
class Default:
68
captcha = "recaptcha/hcaptcha"
@@ -27,6 +29,55 @@ def __init__(self):
2729
self.kwargs = Configuration.Default.kwargs
2830

2931

32+
class User:
33+
def __init__(self, username, password):
34+
self.username = username
35+
self.password = password
36+
37+
38+
class Users:
39+
def __init__(self):
40+
self.users = [
41+
42+
]
43+
44+
def add_user(self, username, password):
45+
self.users.append(
46+
User(
47+
username,
48+
bcrypt.hashpw(password, bcrypt.gensalt(12)) # hash password
49+
)
50+
)
51+
52+
def remove_user(self, username):
53+
match = [i for i in self.users if i.username == username]
54+
for i in match:
55+
self.users.remove(i)
56+
57+
def check_login(self, username, password):
58+
match = [i for i in self.users if i.username == username]
59+
60+
if len(match) < 1:
61+
return False
62+
63+
match = match[0]
64+
65+
return bcrypt.checkpw(password, match.password)
66+
67+
3068
def load_conf():
3169
with open("config.yaml", "rt") as f:
3270
return yaml.load(f, Loader=yaml.Loader)
71+
72+
73+
def load_users():
74+
if os.path.exists("users.yaml"):
75+
with open("users.yaml", "rt") as f:
76+
return yaml.load(f, Loader=yaml.Loader)
77+
else:
78+
return Users()
79+
80+
81+
def save_users(data):
82+
with open("users.yaml", "wt+") as f:
83+
yaml.dump(data, f)

source/invites.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def __init__(self):
1212
self.pool = [
1313

1414
]
15-
self.poolSize = 3 # amount of invites to keep in memory
16-
self.poolSizeLimit = 25 # max invites to be scaled to
15+
self.poolSize = 3 # amount of invites to keep in memory
16+
self.poolSizeLimit = 25 # max invites to be scaled to
1717

1818
self.inviteSize = 1
19-
self.inviteSizeLimit = 3 # higher number = more invites/minute at the cost of security
19+
self.inviteSizeLimit = 3 # higher number = more invites/minute at the cost of security
2020

2121
self.poolSizeDefault = self.poolSize
2222

@@ -62,7 +62,7 @@ def check(self):
6262
threading.Thread(target=self.fill).start()
6363
if len(self.pool) == 0:
6464
self.poolSize += 1 if self.poolSize < self.poolSizeLimit + 1 else 0
65-
self.inviteSize += 1 if self.inviteSize < self.inviteSizeLimit+1 else 0
65+
self.inviteSize += 1 if self.inviteSize < self.inviteSizeLimit + 1 else 0
6666
print("::InvitePool:: " + term.green(f"Inflating invite pool to {self.poolSize}"))
6767
print("::InvitePool:: " + term.green(f"Inflating invite size to {self.inviteSize}"))
6868
else:

0 commit comments

Comments
 (0)