def logging(serv, action, **kwargs): import sql log_path = get_config_var('main', 'log_path') try: IP = cgi.escape(os.environ["REMOTE_ADDR"]) cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_uuid = cookie.get('uuid') login = sql.get_user_name_by_uuid(user_uuid.value) except: pass if kwargs.get('alerting') != 1: mess = get_data( 'date_in_log' ) + " from " + IP + " user: "******" " + action + " for: " + serv + "\n" log = open(log_path + "/config_edit-" + get_data('logs') + ".log", "a") else: mess = get_data('date_in_log') + action + "\n" log = open(log_path + "/checker-" + get_data('logs') + ".log", "a") try: log.write(mess) log.close except IOError: print( '<center><div class="alert alert-danger">Can\'t read write log. Please chech log_path in config</div></center>' ) pass
def diff_config(oldcfg, cfg): import http.cookies import sql cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) log_path = get_config_var('main', 'log_path') user_group = get_user_group() diff = "" date = get_data('date_in_log') cmd = "/bin/diff -ub %s %s" % (oldcfg, cfg) try: user_uuid = cookie.get('uuid') login = sql.get_user_name_by_uuid(user_uuid.value) except Exception: login = '' output, stderr = subprocess_execute(cmd) for line in output: diff += date + " user: "******", group: " + user_group + " " + line + "\n" try: log = open(log_path + "/config_edit-" + get_data('logs') + ".log", "a") log.write(diff) log.close() except IOError: print( '<center><div class="alert alert-danger">Can\'t read write change to log. %s</div></center>' % stderr) pass
def send_cookie(login): session_ttl = int() session_ttl = sql.get_setting('session_ttl') session_ttl = int(session_ttl) expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl) user_uuid = str(uuid.uuid4()) user_token = str(uuid.uuid4()) c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) c["uuid"] = user_uuid c["uuid"]["path"] = "/app/" c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") print(c) sql.write_user_uuid(login, user_uuid) sql.write_user_token(login, user_token) try: funct.logging('locahost', sql.get_user_name_by_uuid(user_uuid) + ' log in', haproxywi=1, login=1) except: pass print("Content-type: text/html\n") print('ok') sys.exit()
def send_cookie(login): session_ttl = int() session_ttl = sql.get_setting('session_ttl') session_ttl = int(session_ttl) expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl) user_uuid = str(uuid.uuid4()) user_token = str(uuid.uuid4()) sql.write_user_uuid(login, user_uuid) sql.write_user_token(login, user_token) id = sql.get_user_id_by_uuid(user_uuid) user_groups = sql.select_user_groups(id, limit=1) c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) c["uuid"] = user_uuid c["uuid"]["path"] = "/" # c["uuid"]["sameSite"] = "Strict" c["uuid"]["Secure"] = "True" c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") c["group"] = user_groups c["group"]["path"] = "/" # c["group"]["sameSite"] = "Strict" c["group"]["Secure"] = "True" c["group"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") print(c) try: funct.logging('locahost', ' ' + sql.get_user_name_by_uuid(user_uuid) + ' log in', haproxywi=1) except: pass print("Content-type: text/html\n") print('ok') sys.exit()
def check_login(**kwargs): import sql cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_uuid = cookie.get('uuid') ref = os.environ.get("SCRIPT_NAME") if user_uuid is not None: if sql.get_user_name_by_uuid(user_uuid.value) is None: print('<meta http-equiv="refresh" content="0; url=login.py?ref=%s">' % ref) else: print('<meta http-equiv="refresh" content="0; url=login.py?ref=%s">' % ref)
def send_cookie(login): session_ttl = sql.get_setting('session_ttl') session_ttl = int(session_ttl) expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl) user_uuid = str(uuid.uuid4()) user_token = str(uuid.uuid4()) sql.write_user_uuid(login, user_uuid) sql.write_user_token(login, user_token) id = sql.get_user_id_by_uuid(user_uuid) try: cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_group_id = cookie.get('group') user_group_id = user_group_id.value if sql.check_user_group(id, user_group_id): user_groups = user_group_id else: user_groups = sql.select_user_groups(id, limit=1) except: user_groups = sql.select_user_groups(id, limit=1) c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) c["uuid"] = user_uuid c["uuid"]["path"] = "/app" # c["uuid"]["samesite"] = "Strict" c["uuid"]["Secure"] = "True" c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") c["group"] = user_groups c["group"]["path"] = "/app" # c["group"]["samesite"] = "Strict" c["group"]["Secure"] = "True" c["group"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") print(c.output()) try: groups = sql.select_groups(id=user_groups) for g in groups: if g[0] == int(user_groups): user_group = g[1] except: user_group = '' try: user_name = sql.get_user_name_by_uuid(user_uuid) funct.logging('localhost', ' user: '******', group: ' + user_group + ' log in', haproxywi=1) except: pass print("Content-type: text/html\n") print('ok') sys.exit()
def logging(serv, action, **kwargs): import sql import http.cookies log_path = get_config_var('main', 'log_path') login = '' if not os.path.exists(log_path): os.makedirs(log_path) try: IP = cgi.escape(os.environ["REMOTE_ADDR"]) except: IP = '' try: cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_uuid = cookie.get('uuid') login = sql.get_user_name_by_uuid(user_uuid.value) except: login = '' if kwargs.get('alerting') == 1: mess = get_data('date_in_log') + action + "\n" log = open(log_path + "/checker-" + get_data('logs') + ".log", "a") elif kwargs.get('metrics') == 1: mess = get_data('date_in_log') + action + "\n" log = open(log_path + "/metrics-" + get_data('logs') + ".log", "a") elif kwargs.get('keep_alive') == 1: mess = get_data('date_in_log') + action + "\n" log = open(log_path + "/keep_alive-" + get_data('logs') + ".log", "a") elif kwargs.get('haproxywi') == 1: if kwargs.get('login'): mess = get_data( 'date_in_log' ) + " from " + IP + " user: "******" " + action + " for: " + serv + "\n" else: mess = get_data('date_in_log') + action + "\n" log = open(log_path + "/haproxy-wi-" + get_data('logs') + ".log", "a") else: mess = get_data( 'date_in_log' ) + " from " + IP + " user: "******" " + action + " for: " + serv + "\n" log = open(log_path + "/config_edit-" + get_data('logs') + ".log", "a") try: log.write(mess) log.close except IOError as e: print( '<center><div class="alert alert-danger">Can\'t write log. Please check log_path in config %e</div></center>' % e) pass
def get_users_params(**kwargs): import http.cookies import sql cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) role = sql.get_user_role_by_uuid(user_id.value) token = sql.get_token(user_id.value) if kwargs.get('virt'): servers = sql.get_dick_permit(virt=1) elif kwargs.get('disable'): servers = sql.get_dick_permit(disable=0) else: servers = sql.get_dick_permit() return user, user_id, role, token, servers
def check_login(**kwargs): import sql import http.cookies cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_uuid = cookie.get('uuid') ref = os.environ.get("SCRIPT_NAME") sql.delete_old_uuid() if user_uuid is not None: sql.update_last_act_user(user_uuid.value) if sql.get_user_name_by_uuid(user_uuid.value) is None: print('<meta http-equiv="refresh" content="0; url=login.py?ref=%s">' % ref) return False else: print('<meta http-equiv="refresh" content="0; url=login.py?ref=%s">' % ref) return False
def logging(serv, action): import sql dateFormat = "%b %d %H:%M:%S" now_utc = datetime.now(timezone(get_config_var('main', 'time_zone'))) IP = cgi.escape(os.environ["REMOTE_ADDR"]) cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_uuid = cookie.get('uuid') login = sql.get_user_name_by_uuid(user_uuid.value) mess = now_utc.strftime(dateFormat) + " from " + IP + " user: "******" " + action + " for: " + serv + "\n" log_path = get_config_var('main', 'log_path') try: log = open(log_path + "/config_edit-"+get_data('logs')+".log", "a") log.write(mess) log.close except IOError: print('<center><div class="alert alert-danger">Can\'t read write log. Please chech log_path in config</div></center>') pass if get_config_var('telegram', 'enable') == "1": telegram_send_mess(mess)
#!/usr/bin/env python3 import funct, sql import os, http.cookies from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader('templates/')) template = env.get_template('hapservers.html') print('Content-type: text/html\n') funct.check_login() try: cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) users = sql.select_users() groups = sql.select_groups() token = sql.get_token(user_id.value) servers = sql.get_dick_permit() cmd = "ps ax |grep -e 'keep_alive.py' |grep -v grep |wc -l" keep_alive, stderr = funct.subprocess_execute(cmd) except: pass haproxy_sock_port = sql.get_setting('haproxy_sock_port') haproxy_config_path = sql.get_setting('haproxy_config_path') commands = ["ls -l %s |awk '{ print $6\" \"$7\" \"$8}'" % haproxy_config_path] servers_with_status1 = [] out1 = "" for s in servers: servers_with_status = list() cmd = 'echo "show info" |nc %s %s -w 1 |grep -e "Ver\|Uptime:\|Process_num"' % (
if ref is None: ref = "/index.html" if form.getvalue('error'): error_log = '<div class="alert alert-danger">Somthing wrong :( I\'m sad about this, but try again!</div><br /><br />' try: if sql.get_setting('session_ttl'): session_ttl = sql.get_setting('session_ttl') except: error = '<center><div class="alert alert-danger">Can not find "session_ttl" parametr. Check into settings, "main" section</div>' pass try: role = sql.get_user_role_by_uuid(user_id.value) user = sql.get_user_name_by_uuid(user_id.value) except: role = "" user = "" pass if form.getvalue('logout'): try: sql.delete_uuid(user_id.value) except: pass print("Set-cookie: uuid=; expires=Wed May 18 03:33:20 2003; path=/app/; httponly") print("Content-type: text/html\n") print('<meta http-equiv="refresh" content="0; url=/app/login.py">') if login is not None and password is not None:
if ref is None: ref = "/index.html" if form.getvalue('error'): error_log = '<div class="alert alert-danger">Somthing wrong :( I\'m sad about this, but try again!</div><br /><br />' try: if sql.get_setting('session_ttl'): session_ttl = sql.get_setting('session_ttl') except: error = '<center><div class="alert alert-danger">Can not find "session_ttl" parametr. Check into settings, "main" section</div>' pass try: role = sql.get_user_role_by_uuid(user_id.value) user = sql.get_user_name_by_uuid(user_id.value) except: role = "" user = "" pass if form.getvalue('logout'): try: sql.delete_uuid(user_id.value) except: pass print( "Set-cookie: uuid=; expires=Wed May 18 03:33:20 2003; path=/app/; httponly" ) print("Content-type: text/html\n") print('<meta http-equiv="refresh" content="0; url=/app/login.py">')