Skip to content

Commit

Permalink
user management
Browse files Browse the repository at this point in the history
  • Loading branch information
MiranDaniel committed Mar 10, 2022
1 parent 106397c commit 699616e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ venv/
.env

config.yaml
users.yaml

__pycache__
__pycache__/
Expand Down
4 changes: 2 additions & 2 deletions blueprints/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


@gateway.route('/')
def gateway_fun(code=""):
def gateway_fun():
return render_template(
"index.jinja2",
captcha=configuration.captcha,
Expand Down Expand Up @@ -58,4 +58,4 @@ def wrapper(invite):
print("Ratelimit")
return redirect("/")

return invite["code"]#redirect(f"https://discord.gg/{invite['code']}")
return redirect(f"https://discord.gg/{invite['code']}")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ PyYAML
blessed
pwinput
requests
py-bcrypt
49 changes: 49 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from blessed import Terminal
from pwinput import pwinput
from source.conf import Configuration
from source import conf
from source.decorators import catch_goodbye
from source.utils import goodbye

Expand Down Expand Up @@ -77,13 +78,59 @@ def interactive_config():
goodbye()


@catch_goodbye()
def manage_users():
users = conf.load_users()
print(term.gray100(" What would you like to do?"))
print(" 1. List users")
print(" 2. Add new user")
print(" 3. Remove user")
print(" 0. Exit")

i = input("\n-- ")
try:
i = int(i)
except ValueError:
manage_users()

if i == 1:
print(f"There are {len(users.users)} registered users")
print("index | username | password")
for index, user in enumerate(users.users):
print(f"[{index}] | {user.username} | {user.password}")
print()
manage_users()
elif i == 2:
print("Enter username: ")
username = input()
print("Enter password: ")
password = pwinput(mask="*", prompt="")
users.add_user(username, password)
conf.save_users(users)
print()
manage_users()
elif i == 3:
print("Enter username: ")
username = input()
users.remove_user(username)
conf.save_users(users)
print()
manage_users()
elif i == 0:
goodbye()
quit(0)
else:
manage_users()


@catch_goodbye()
def welcome():
print(term.home + term.clear + term.move_y(0))
print(term.black_on_orange(' Welcome to f1rewall setup!' + " " * (term.width - 28)))
print(term.gray100(" What would you like to do?"))
print(" 1. Generate settings file (manual setup)")
print(" 2. Run interactive setup " + term.green("[recommended]"))
print(" 3. Manage users (f1rewall analytics access)")
print(" 0. Exit")

i = input("\n-- ")
Expand All @@ -96,6 +143,8 @@ def welcome():
generate_config()
elif i == 2:
interactive_config()
elif i == 3:
manage_users()
elif i == 0:
goodbye()
quit(0)
Expand Down
51 changes: 51 additions & 0 deletions source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import bcrypt
import yaml
import os


class Configuration:
class Default:
captcha = "recaptcha/hcaptcha"
Expand All @@ -27,6 +29,55 @@ def __init__(self):
self.kwargs = Configuration.Default.kwargs


class User:
def __init__(self, username, password):
self.username = username
self.password = password


class Users:
def __init__(self):
self.users = [

]

def add_user(self, username, password):
self.users.append(
User(
username,
bcrypt.hashpw(password, bcrypt.gensalt(12)) # hash password
)
)

def remove_user(self, username):
match = [i for i in self.users if i.username == username]
for i in match:
self.users.remove(i)

def check_login(self, username, password):
match = [i for i in self.users if i.username == username]

if len(match) < 1:
return False

match = match[0]

return bcrypt.checkpw(password, match.password)


def load_conf():
with open("config.yaml", "rt") as f:
return yaml.load(f, Loader=yaml.Loader)


def load_users():
if os.path.exists("users.yaml"):
with open("users.yaml", "rt") as f:
return yaml.load(f, Loader=yaml.Loader)
else:
return Users()


def save_users(data):
with open("users.yaml", "wt+") as f:
yaml.dump(data, f)
8 changes: 4 additions & 4 deletions source/invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def __init__(self):
self.pool = [

]
self.poolSize = 3 # amount of invites to keep in memory
self.poolSizeLimit = 25 # max invites to be scaled to
self.poolSize = 3 # amount of invites to keep in memory
self.poolSizeLimit = 25 # max invites to be scaled to

self.inviteSize = 1
self.inviteSizeLimit = 3 # higher number = more invites/minute at the cost of security
self.inviteSizeLimit = 3 # higher number = more invites/minute at the cost of security

self.poolSizeDefault = self.poolSize

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

0 comments on commit 699616e

Please sign in to comment.