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):
def __init__(self, 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()
@ -26,20 +28,33 @@ class PiHole(object):
self.pw = None
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:
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.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(
"http://" + self.ip_address + "/admin/api.php?getQueryTypes&auth=" + self.auth_data.token).json()[
"querytypes"]
"http://"
+ self.ip_address
+ "/admin/api.php?getQueryTypes&auth="
+ self.auth_data.token
).json()["querytypes"]
# Data that is returned is now parsed into vars
self.status = rawdata["status"]
@ -60,14 +75,25 @@ class PiHole(object):
print("Unable to fetch top items. Please authenticate.")
exit(1)
rawdata = requests.get("http://" + self.ip_address + "/admin/api.php?topItems=" + str(
count) + "&auth=" + self.auth_data.token).json()
rawdata = requests.get(
"http://"
+ self.ip_address
+ "/admin/api.php?topItems="
+ str(count)
+ "&auth="
+ self.auth_data.token
).json()
self.top_queries = rawdata["top_queries"]
self.top_ads = rawdata["top_ads"]
def getGraphData(self):
rawdata = requests.get("http://" + self.ip_address + "/admin/api.php?overTimeData10mins").json()
return {"domains": rawdata["domains_over_time"], "ads": rawdata["ads_over_time"]}
rawdata = requests.get(
"http://" + self.ip_address + "/admin/api.php?overTimeData10mins"
).json()
return {
"domains": rawdata["domains_over_time"],
"ads": rawdata["ads_over_time"],
}
def authenticate(self, password):
self.auth_data = Auth(password)
@ -79,54 +105,85 @@ class PiHole(object):
if self.auth_data == None:
print("Unable to get queries. Please authenticate")
exit(1)
return \
requests.get("http://" + self.ip_address + "/admin/api.php?getAllQueries&auth=" + self.auth_data.token).json()[
"data"]
return requests.get(
"http://"
+ self.ip_address
+ "/admin/api.php?getAllQueries&auth="
+ self.auth_data.token
).json()["data"]
def enable(self):
if self.auth_data == None:
print("Unable to enable pihole. Please authenticate")
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):
if self.auth_data == None:
print("Unable to disable pihole. Please authenticate")
exit(1)
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):
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):
if self.auth_data == None:
print("Please authenticate")
exit(1)
return float(requests.get(
"http://" + self.ip_address + "/admin/api_db.php?getDBfilesize&auth=" + self.auth_data.token).json()[
"filesize"])
return float(
requests.get(
"http://"
+ self.ip_address
+ "/admin/api_db.php?getDBfilesize&auth="
+ self.auth_data.token
).json()["filesize"]
)
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):
if self.auth_data == None:
print("Please authenticate")
exit(1)
with requests.session() as s:
s.get("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php")
requests.post("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php",
data={"list": list, "domain": domain, "pw": self.pw}).text
s.get(
"http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/add.php"
)
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):
if self.auth_data == None:
print("Please authenticate")
exit(1)
with requests.session() as s:
s.get("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php")
requests.post("http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php",
data={"list": list, "domain": domain, "pw": self.pw}).text
s.get(
"http://" + str(self.ip_address) + "/admin/scripts/pi-hole/php/sub.php"
)
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:
@ -139,7 +196,7 @@ class Platform:
def process(self):
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

View File

@ -30,7 +30,6 @@ def settings():
config_form = ConfigForm()
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:
config_form.config.data = config_file.read()
files_html = load_files_html()
@ -98,12 +97,11 @@ def edit_user():
if form.validate_on_submit():
if form.password.data != form.confirm_password.data:
return jsonify(data={"err": "Passwords don't match"})
if not form.id.data:
new = True
else:
new = False
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:
err_str = ""

View File

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

View File

@ -1,6 +1,9 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SelectField
from wtforms.validators import DataRequired, Length
from dashmachine.settings_system.models import Settings
settings_db = Settings.query.first()
class UserForm(FlaskForm):
@ -8,10 +11,16 @@ class UserForm(FlaskForm):
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()
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()

View File

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

View File

@ -33,3 +33,4 @@ urllib3==1.25.8
Werkzeug==0.16.1
WTForms==2.2.1
transmissionrpc
markdown2