working on user system

This commit is contained in:
Ross Mountjoy 2020-02-08 09:12:57 -05:00
parent 071d12a285
commit 2d0f53ca4f
7 changed files with 113 additions and 47 deletions

0
config_readme.md Normal file
View File

View File

@ -13,7 +13,9 @@ def inApiLink(ip, endpoint):
class Auth(object): class Auth(object):
def __init__(self, password): def __init__(self, password):
# PiHole's web token is just a double sha256 hash of the utf8 encoded password # PiHole's web token is just a double sha256 hash of the utf8 encoded password
self.token = hashlib.sha256(hashlib.sha256(str(password).encode()).hexdigest().encode()).hexdigest() self.token = hashlib.sha256(
hashlib.sha256(str(password).encode()).hexdigest().encode()
).hexdigest()
self.auth_timestamp = time.time() self.auth_timestamp = time.time()
@ -26,20 +28,33 @@ class PiHole(object):
self.pw = None self.pw = None
def refresh(self): def refresh(self):
rawdata = requests.get("http://" + self.ip_address + "/admin/api.php?summary").json() rawdata = requests.get(
"http://" + self.ip_address + "/admin/api.php?summary"
).json()
if self.auth_data != None: if self.auth_data != None:
topdevicedata = requests.get( topdevicedata = requests.get(
"http://" + self.ip_address + "/admin/api.php?getQuerySources=25&auth=" + self.auth_data.token).json() "http://"
+ self.ip_address
+ "/admin/api.php?getQuerySources=25&auth="
+ self.auth_data.token
).json()
self.top_devices = topdevicedata["top_sources"] self.top_devices = topdevicedata["top_sources"]
self.forward_destinations = requests.get( self.forward_destinations = requests.get(
"http://" + self.ip_address + "/admin/api.php?getForwardDestinations&auth=" + self.auth_data.token).json() "http://"
+ self.ip_address
+ "/admin/api.php?getForwardDestinations&auth="
+ self.auth_data.token
).json()
self.query_types = requests.get( self.query_types = requests.get(
"http://" + self.ip_address + "/admin/api.php?getQueryTypes&auth=" + self.auth_data.token).json()[ "http://"
"querytypes"] + self.ip_address
+ "/admin/api.php?getQueryTypes&auth="
+ self.auth_data.token
).json()["querytypes"]
# Data that is returned is now parsed into vars # Data that is returned is now parsed into vars
self.status = rawdata["status"] self.status = rawdata["status"]
@ -60,14 +75,25 @@ class PiHole(object):
print("Unable to fetch top items. Please authenticate.") print("Unable to fetch top items. Please authenticate.")
exit(1) exit(1)
rawdata = requests.get("http://" + self.ip_address + "/admin/api.php?topItems=" + str( rawdata = requests.get(
count) + "&auth=" + self.auth_data.token).json() "http://"
+ self.ip_address
+ "/admin/api.php?topItems="
+ str(count)
+ "&auth="
+ self.auth_data.token
).json()
self.top_queries = rawdata["top_queries"] self.top_queries = rawdata["top_queries"]
self.top_ads = rawdata["top_ads"] self.top_ads = rawdata["top_ads"]
def getGraphData(self): def getGraphData(self):
rawdata = requests.get("http://" + self.ip_address + "/admin/api.php?overTimeData10mins").json() rawdata = requests.get(
return {"domains": rawdata["domains_over_time"], "ads": rawdata["ads_over_time"]} "http://" + self.ip_address + "/admin/api.php?overTimeData10mins"
).json()
return {
"domains": rawdata["domains_over_time"],
"ads": rawdata["ads_over_time"],
}
def authenticate(self, password): def authenticate(self, password):
self.auth_data = Auth(password) self.auth_data = Auth(password)
@ -79,54 +105,85 @@ class PiHole(object):
if self.auth_data == None: if self.auth_data == None:
print("Unable to get queries. Please authenticate") print("Unable to get queries. Please authenticate")
exit(1) exit(1)
return \ return requests.get(
requests.get("http://" + self.ip_address + "/admin/api.php?getAllQueries&auth=" + self.auth_data.token).json()[ "http://"
"data"] + self.ip_address
+ "/admin/api.php?getAllQueries&auth="
+ self.auth_data.token
).json()["data"]
def enable(self): def enable(self):
if self.auth_data == None: if self.auth_data == None:
print("Unable to enable pihole. Please authenticate") print("Unable to enable pihole. Please authenticate")
exit(1) exit(1)
requests.get("http://" + self.ip_address + "/admin/api.php?enable&auth=" + self.auth_data.token) requests.get(
"http://"
+ self.ip_address
+ "/admin/api.php?enable&auth="
+ self.auth_data.token
)
def disable(self, seconds): def disable(self, seconds):
if self.auth_data == None: if self.auth_data == None:
print("Unable to disable pihole. Please authenticate") print("Unable to disable pihole. Please authenticate")
exit(1) exit(1)
requests.get( requests.get(
"http://" + self.ip_address + "/admin/api.php?disable=" + str(seconds) + "&auth=" + self.auth_data.token) "http://"
+ self.ip_address
+ "/admin/api.php?disable="
+ str(seconds)
+ "&auth="
+ self.auth_data.token
)
def getVersion(self): def getVersion(self):
return requests.get("http://" + self.ip_address + "/admin/api.php?versions").json() return requests.get(
"http://" + self.ip_address + "/admin/api.php?versions"
).json()
def getDBfilesize(self): def getDBfilesize(self):
if self.auth_data == None: if self.auth_data == None:
print("Please authenticate") print("Please authenticate")
exit(1) exit(1)
return float(requests.get( return float(
"http://" + self.ip_address + "/admin/api_db.php?getDBfilesize&auth=" + self.auth_data.token).json()[ requests.get(
"filesize"]) "http://"
+ self.ip_address
+ "/admin/api_db.php?getDBfilesize&auth="
+ self.auth_data.token
).json()["filesize"]
)
def getList(self, list): def getList(self, list):
return requests.get(inApiLink(self.ip_address, "get") + "?list=" + str(list)).json() return requests.get(
inApiLink(self.ip_address, "get") + "?list=" + str(list)
).json()
def add(self, list, domain): def add(self, list, domain):
if self.auth_data == None: if self.auth_data == None:
print("Please authenticate") print("Please authenticate")
exit(1) exit(1)
with requests.session() as s: with requests.session() as s:
s.get("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php") s.get(
requests.post("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php", "http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php"
data={"list": list, "domain": domain, "pw": self.pw}).text )
requests.post(
"http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php",
data={"list": list, "domain": domain, "pw": self.pw},
).text
def sub(self, list, domain): def sub(self, list, domain):
if self.auth_data == None: if self.auth_data == None:
print("Please authenticate") print("Please authenticate")
exit(1) exit(1)
with requests.session() as s: with requests.session() as s:
s.get("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php") s.get(
requests.post("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php", "http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php"
data={"list": list, "domain": domain, "pw": self.pw}).text )
requests.post(
"http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php",
data={"list": list, "domain": domain, "pw": self.pw},
).text
class Platform: class Platform:
@ -139,7 +196,7 @@ class Platform:
def process(self): def process(self):
self.pihole.refresh() self.pihole.refresh()
value_template = render_template_string(self.value_template, **self.pihole.__dict__) value_template = render_template_string(
self.value_template, **self.pihole.__dict__
)
return value_template return value_template

View File

@ -30,7 +30,6 @@ def settings():
config_form = ConfigForm() config_form = ConfigForm()
user_form = UserForm() user_form = UserForm()
# user_form.role.choices = [(role, role) for role in settings_db.roles.split(",")]
with open(os.path.join(user_data_folder, "config.ini"), "r") as config_file: with open(os.path.join(user_data_folder, "config.ini"), "r") as config_file:
config_form.config.data = config_file.read() config_form.config.data = config_file.read()
files_html = load_files_html() files_html = load_files_html()
@ -98,12 +97,11 @@ def edit_user():
if form.validate_on_submit(): if form.validate_on_submit():
if form.password.data != form.confirm_password.data: if form.password.data != form.confirm_password.data:
return jsonify(data={"err": "Passwords don't match"}) return jsonify(data={"err": "Passwords don't match"})
if not form.id.data:
new = True
else:
new = False
add_edit_user( add_edit_user(
form.username.data, form.password.data, user_id=form.id.data, new=new form.username.data,
form.password.data,
user_id=form.id.data,
role=form.role.data,
) )
else: else:
err_str = "" err_str = ""

View File

@ -6,12 +6,12 @@
<form id="edit-user-form"> <form id="edit-user-form">
{{ user_form.hidden_tag() }} {{ user_form.hidden_tag() }}
{# {{ select(#} {{ select(
{# id='user-form-role',#} id='user-form-role',
{# form_obj=user_form.role,#} form_obj=user_form.role,
{# size="s12",#} size="s12",
{# label='Role'#} label='Role'
{# ) }}#} ) }}
{{ input( {{ input(
label="Username", label="Username",
@ -85,4 +85,5 @@
<h5>DashMachine</h5> <h5>DashMachine</h5>
<p class="mb-2">version: {{ version }}</p> <p class="mb-2">version: {{ version }}</p>
</div> </div>
{% endmacro %} {% endmacro %}
{{UserTab()}}

View File

@ -1,6 +1,9 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SelectField from wtforms import StringField, PasswordField, BooleanField, SelectField
from wtforms.validators import DataRequired, Length from wtforms.validators import DataRequired, Length
from dashmachine.settings_system.models import Settings
settings_db = Settings.query.first()
class UserForm(FlaskForm): class UserForm(FlaskForm):
@ -8,10 +11,16 @@ class UserForm(FlaskForm):
password = PasswordField(validators=[DataRequired(), Length(min=8, max=120)]) password = PasswordField(validators=[DataRequired(), Length(min=8, max=120)])
# role = SelectField() role = SelectField(choices=[(role, role) for role in settings_db.roles.split(",")])
id = StringField() id = StringField()
confirm_password = PasswordField() confirm_password = PasswordField()
class LoginForm(FlaskForm):
username = StringField(validators=[DataRequired(), Length(min=1, max=120)])
password = PasswordField(validators=[DataRequired(), Length(min=8, max=120)])
remember = BooleanField() remember = BooleanField()

View File

@ -1,6 +1,6 @@
from flask import render_template, url_for, redirect, Blueprint from flask import render_template, url_for, redirect, Blueprint
from flask_login import login_user, logout_user, current_user from flask_login import login_user, logout_user
from dashmachine.user_system.forms import UserForm from dashmachine.user_system.forms import LoginForm
from dashmachine.user_system.models import User from dashmachine.user_system.models import User
from dashmachine.user_system.utils import add_edit_user from dashmachine.user_system.utils import add_edit_user
from dashmachine import bcrypt from dashmachine import bcrypt
@ -18,7 +18,7 @@ user_system = Blueprint("user_system", __name__)
def login(): def login():
user = User.query.first() user = User.query.first()
form = UserForm() form = LoginForm()
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data.lower()).first() user = User.query.filter_by(username=form.username.data.lower()).first()

View File

@ -32,4 +32,5 @@ SQLAlchemy==1.3.13
urllib3==1.25.8 urllib3==1.25.8
Werkzeug==0.16.1 Werkzeug==0.16.1
WTForms==2.2.1 WTForms==2.2.1
transmissionrpc transmissionrpc
markdown2