2018-01-15 06:16:04 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import cgi
|
|
|
|
import html
|
|
|
|
import os
|
2018-04-23 04:49:23 +00:00
|
|
|
import sys
|
2018-01-15 06:16:04 +00:00
|
|
|
import funct
|
|
|
|
import http.cookies
|
2018-04-16 07:01:44 +00:00
|
|
|
import sql
|
2018-04-23 04:49:23 +00:00
|
|
|
import create_db
|
2018-05-02 11:11:22 +00:00
|
|
|
import datetime
|
|
|
|
import uuid
|
|
|
|
from configparser import ConfigParser, ExtendedInterpolation
|
2018-01-15 06:16:04 +00:00
|
|
|
|
|
|
|
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
|
|
|
form = cgi.FieldStorage()
|
|
|
|
ref = form.getvalue('ref')
|
|
|
|
login = form.getvalue('login')
|
|
|
|
password = form.getvalue('pass')
|
|
|
|
|
2018-05-02 11:11:22 +00:00
|
|
|
path_config = "haproxy-webintarface.config"
|
|
|
|
config = ConfigParser(interpolation=ExtendedInterpolation())
|
|
|
|
config.read(path_config)
|
|
|
|
|
2018-01-15 06:16:04 +00:00
|
|
|
def login_page(error):
|
|
|
|
if error == "error":
|
2018-05-02 11:11:22 +00:00
|
|
|
funct.head("Login page")
|
2018-04-23 04:49:23 +00:00
|
|
|
printError = "<h2>Login page. Enter please</h2><br /><br /><b style='color: red'>Somthing wrong :( I'm sad about this, but try again!</b><br /><br />"
|
2018-01-15 06:16:04 +00:00
|
|
|
else:
|
2018-02-10 14:16:33 +00:00
|
|
|
printError = "<h2>Login page. Enter please</h2><br /><br />"
|
2018-05-02 11:11:22 +00:00
|
|
|
|
2018-04-23 04:49:23 +00:00
|
|
|
if create_db.check_db():
|
|
|
|
if create_db.create_table():
|
2018-04-25 12:23:05 +00:00
|
|
|
print('<div class="alert alert-success">DB was created<br />')
|
2018-04-23 04:49:23 +00:00
|
|
|
create_db.update_all()
|
|
|
|
print('<br />Now you can login, default: admin/admin</div>')
|
2018-05-02 11:11:22 +00:00
|
|
|
create_db.update_all_silent()
|
2018-01-25 09:04:57 +00:00
|
|
|
|
|
|
|
ref = form.getvalue('ref')
|
|
|
|
if ref is None:
|
|
|
|
ref = "/index.html"
|
2018-04-23 04:49:23 +00:00
|
|
|
|
2018-04-13 06:48:21 +00:00
|
|
|
print('<center><form name="auth" action="login.py" class="form-horizontal" method="get">')
|
2018-01-15 06:16:04 +00:00
|
|
|
print(printError)
|
2018-02-10 14:16:33 +00:00
|
|
|
print('<label for="login">Login: </label> <input type="text" name="login" required class="form-control"><br /><br />')
|
|
|
|
print('<label for="pass">Pass: </label> <input type="password" name="pass" required class="form-control"><br /><br />')
|
2018-01-15 06:16:04 +00:00
|
|
|
print('<input type="hidden" value="%s" name="ref">' % ref)
|
2018-01-29 05:13:18 +00:00
|
|
|
print('<button type="submit" name="Login" value="Enter">Sign Up</button>')
|
|
|
|
print('</form></center>')
|
2018-01-31 09:22:14 +00:00
|
|
|
|
2018-05-02 11:11:22 +00:00
|
|
|
try:
|
|
|
|
if config.get('main', 'session_ttl'):
|
|
|
|
session_ttl = config.getint('main', 'session_ttl')
|
|
|
|
except:
|
|
|
|
print('<center><div class="alert alert-danger">Can not find "session_ttl" parametr. Check into config, "main" section</div>')
|
|
|
|
|
2018-01-25 09:04:57 +00:00
|
|
|
if form.getvalue('logout') is not None:
|
2018-05-02 11:11:22 +00:00
|
|
|
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
|
|
|
user_id = cookie.get('uuid')
|
|
|
|
try:
|
|
|
|
sql.delete_uuid(user_id.value)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
print("Set-cookie: uuid=; expires=Wed May 18 03:33:20 2003; path=/app/; httponly")
|
2018-01-31 09:22:14 +00:00
|
|
|
print("Content-type: text/html\n")
|
2018-05-02 11:11:22 +00:00
|
|
|
print('<meta http-equiv="refresh" content="0; url=/app/login.py">')
|
2018-01-25 09:04:57 +00:00
|
|
|
|
2018-01-15 06:16:04 +00:00
|
|
|
if login is None:
|
2018-04-23 04:49:23 +00:00
|
|
|
funct.head("Login page")
|
2018-01-15 06:16:04 +00:00
|
|
|
login_page("n")
|
2018-01-25 09:04:57 +00:00
|
|
|
|
2018-01-15 06:16:04 +00:00
|
|
|
if login is not None and password is not None:
|
2018-05-02 11:11:22 +00:00
|
|
|
if form.getvalue('ref') is None:
|
|
|
|
ref = "/index.html"
|
|
|
|
|
2018-04-16 07:01:44 +00:00
|
|
|
USERS = sql.select_users()
|
2018-05-02 11:11:22 +00:00
|
|
|
session_ttl = config.getint('main', 'session_ttl')
|
|
|
|
expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl)
|
|
|
|
user_uuid = str(uuid.uuid4())
|
|
|
|
|
2018-04-16 07:01:44 +00:00
|
|
|
for users in USERS:
|
|
|
|
if login in users[1] and password == users[3]:
|
2018-01-31 09:22:14 +00:00
|
|
|
c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
|
2018-05-02 11:11:22 +00:00
|
|
|
c["uuid"] = user_uuid
|
|
|
|
c["uuid"]["path"] = "/app/"
|
|
|
|
c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
2018-01-31 09:22:14 +00:00
|
|
|
print(c)
|
2018-05-02 11:11:22 +00:00
|
|
|
sql.write_user_uuid(login, user_uuid)
|
|
|
|
|
|
|
|
print("Content-type: text/html\n")
|
2018-01-15 06:16:04 +00:00
|
|
|
print('<html><head><title>Redirecting</title><meta charset="UTF-8">')
|
|
|
|
print('<link href="/style.css" rel="stylesheet">')
|
|
|
|
print('<meta http-equiv="refresh" content="0; url=%s">' % ref)
|
2018-04-23 04:49:23 +00:00
|
|
|
sys.exit()
|
2018-01-25 09:04:57 +00:00
|
|
|
login_page("error")
|
|
|
|
|
2018-04-23 16:20:19 +00:00
|
|
|
funct.footer()
|