From c1f3fe4a97b2cc498a2da4858b9b27a7c1093ca5 Mon Sep 17 00:00:00 2001 From: Pavel Loginov Date: Sun, 26 Jul 2020 18:26:04 +0200 Subject: [PATCH] v4.4.0.0 Changelog: https://haproxy-wi.org/changelog.py#4_4 --- app/create_db.py | 45 ++- app/funct.py | 11 +- app/options.py | 76 ++--- app/templates/include/smon_server.html | 7 +- inc/script.js | 112 ++++--- inc/toastr.css | 228 +++++++++++++ inc/toastr.js | 2 + inc/toastr.js.map | 1 + inc/users.js | 422 ++++++++++--------------- inc/waf.js | 46 ++- 10 files changed, 609 insertions(+), 341 deletions(-) create mode 100644 inc/toastr.css create mode 100644 inc/toastr.js create mode 100644 inc/toastr.js.map diff --git a/app/create_db.py b/app/create_db.py index d968db1b..86f1578c 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -166,8 +166,8 @@ def update_db_v_31(**kwargs): sql.append("INSERT INTO settings (param, value, section, `desc`) values('proxy', '', 'main', 'Proxy server. Use proto://ip:port');") sql.append("INSERT INTO settings (param, value, section, `desc`) values('session_ttl', '5', 'main', 'Time to live users sessions. In days');") sql.append("INSERT INTO settings (param, value, section, `desc`) values('token_ttl', '5', 'main', 'Time to live users tokens. In days');") - sql.append("INSERT INTO settings (param, value, section, `desc`) values('tmp_config_path', '/tmp/', 'main', 'Temp store configs, for check');") - sql.append("INSERT INTO settings (param, value, section, `desc`) values('cert_path', '/etc/ssl/certs/', 'main', 'Path to SSL dir');") + sql.append("INSERT INTO settings (param, value, section, `desc`) values('tmp_config_path', '/tmp/', 'main', 'Temp store configs, for check. Path must exist');") + sql.append("INSERT INTO settings (param, value, section, `desc`) values('cert_path', '/etc/ssl/certs/', 'main', 'Path to SSL dir. Folder owner must be a user which set in the SSH settings. Path must exist');") sql.append("INSERT INTO settings (param, value, section, `desc`) values('lists_path', 'lists', 'main', 'Path to black/white lists. This is a relative path, begins with $HOME_HAPROXY-WI');") sql.append("INSERT INTO settings (param, value, section, `desc`) values('local_path_logs', '/var/log/haproxy.log', 'logs', 'Logs save locally, enabled by default');") sql.append("INSERT INTO settings (param, value, section, `desc`) values('syslog_server_enable', '0', 'logs', 'If exist syslog server for HAproxy logs, enable this option');") @@ -475,9 +475,14 @@ def update_db_v_4_3(**kwargs): def update_db_v_4_3_0(**kwargs): con, cur = get_cur() - sql = """ - insert OR IGNORE into user_groups(user_id, user_group_id) select id, groups from user; - """ + if mysql_enable == '1': + sql = """ + insert OR IGNORE into user_groups(user_id, user_group_id) select user.id, user.groups from user; + """ + else: + sql = """ + insert OR IGNORE into user_groups(user_id, user_group_id) select id, groups from user; + """ try: cur.execute(sql) con.commit() @@ -567,7 +572,7 @@ def update_db_v_4_4(**kwargs): except sqltool.Error as e: if kwargs.get('silent') != 1: if e.args[0] == 'duplicate column name: pos' or e == " 1060 (42S21): Duplicate column name 'pos' ": - print('DB was update to 4.4.0') + print('Updating... go to version 4.4.2') else: print("An error occurred:", e) return False @@ -575,6 +580,32 @@ def update_db_v_4_4(**kwargs): return True cur.close() con.close() + + +def update_db_v_4_4_2(**kwargs): + con, cur = get_cur() + sql = """CREATE TABLE IF NOT EXISTS `waf_rules` (`id` INTEGER NOT NULL, + serv varchar(64), + `rule_name` varchar(64), + `rule_file` varchar(64), + `desc` varchar(1024), + `en` INTEGER DEFAULT 1, + UNIQUE(serv, rule_name), + PRIMARY KEY(`id`) ); """ + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + if kwargs.get('silent') != 1: + if e.args[0] == 'duplicate column name: version' or e == "1060 (42S21): Duplicate column name 'version' ": + print('DB was update to 4.4.2') + else: + print("DB was update to 4.4.2") + return False + else: + return True + cur.close() + con.close() def update_ver(**kwargs): @@ -609,6 +640,7 @@ def update_all(): update_db_v_4_3_1() update_db_v_4_3_2() update_db_v_4_4() + update_db_v_4_4_2() update_ver() @@ -632,6 +664,7 @@ def update_all_silent(): update_db_v_4_3_1(silent=1) update_db_v_4_3_2(silent=1) update_db_v_4_4(silent=1) + update_db_v_4_4_2(silent=1) update_ver() diff --git a/app/funct.py b/app/funct.py index 2b181c07..11024417 100644 --- a/app/funct.py +++ b/app/funct.py @@ -523,6 +523,7 @@ def waf_install(serv, **kwargs): stderr = ssh_command(serv, commands, print_out="1") sql.insert_waf_metrics_enable(serv, "0") + sql.insert_waf_rules(serv) def install_nginx(serv): @@ -607,18 +608,23 @@ def upload(serv, path, file, **kwargs): try: ssh = ssh_connect(serv) except Exception as e: - error = e + error = e.args logging('localhost', str(e.args[0]), haproxywi=1) + print(' Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) pass try: sftp = ssh.open_sftp() except Exception as e: + error = e.args logging('localhost', str(e.args[0]), haproxywi=1) + print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) try: file = sftp.put(file, full_path) except Exception as e: + error = e.args + print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) logging('localhost', ' Cannot upload '+file+' to '+full_path+' to server: '+serv+' Error: '+str(e.args), haproxywi=1) pass @@ -628,6 +634,7 @@ def upload(serv, path, file, **kwargs): except Exception as e: error = e.args logging('localhost', str(error[0]), haproxywi=1) + print('Cannot upload '+file+' to '+full_path+' to server: '+serv+' error: '+str(e.args)) pass return str(error) @@ -903,7 +910,7 @@ def ssh_command(serv, commands, **kwargs): ssh.close() except: logging('localhost', ' '+str(ssh), haproxywi=1) - return "
"+str(ssh)+"X
" + return "error: "+str(ssh) pass diff --git a/app/options.py b/app/options.py index 3ee54630..d593f752 100644 --- a/app/options.py +++ b/app/options.py @@ -26,17 +26,17 @@ if act == "checkrestart": if not sql.check_token_exists(form.getvalue("token")): - print('
Your token has been expired
') + print('error: Your token has been expired') sys.exit() if form.getvalue('getcerts') is not None and serv is not None: cert_path = sql.get_setting('cert_path') - commands = [ "ls -1t "+cert_path+" |grep pem" ] + commands = ["sudo ls -1t "+cert_path+" |grep pem"] try: funct.ssh_command(serv, commands, ip="1") - except: - print('
Can not connect to the server
') + except Exception as e: + print('error: Cannot connect to the server: ' + str(e)) if form.getvalue('checkSshConnect') is not None and serv is not None: @@ -50,7 +50,7 @@ if form.getvalue('getcert') is not None and serv is not None: try: funct.ssh_command(serv, commands, ip="1") except: - print('
Can not connect to the server
') + print('error: Can not connect to the server') if serv and form.getvalue('ssl_cert'): @@ -61,24 +61,24 @@ if serv and form.getvalue('ssl_cert'): os.makedirs(cert_local_dir) if form.getvalue('ssl_name') is None: - print('
Please enter desired name
') + print('error: Please enter desired name') else: name = form.getvalue('ssl_name') + '.pem' try: with open(name, "w") as ssl_cert: ssl_cert.write(form.getvalue('ssl_cert')) - except IOError: - print('
Can\'t save ssl keys file. Check ssh keys path in config
') - else: - print('
SSL file was upload to %s into: %s %s
' % (serv, cert_path, name)) + except IOError as e : + print('error: Can\'t save ssl keys file. Check ssh keys path in config '+e.args[0]) MASTERS = sql.is_master(serv) for master in MASTERS: if master[0] != None: funct.upload(master[0], cert_path, name) try: - funct.upload(serv, cert_path, name) + error = funct.upload(serv, cert_path, name) + if error == '': + print('success: SSL file has been uploaded to %s into: %s%s' % (serv, cert_path, '/'+name)) except Exception as e: funct.logging('localhost', e.args[0], haproxywi=1) try: @@ -86,7 +86,7 @@ if serv and form.getvalue('ssl_cert'): except OSError as e: funct.logging('localhost', e.args[0], haproxywi=1) - funct.logging(serv, "add.py#ssl upload new ssl cert %s" % name) + funct.logging(serv, "add.py#ssl uploaded a new SSL cert %s" % name, haproxywi=1, login=1) if form.getvalue('backend') is not None: @@ -200,7 +200,7 @@ if form.getvalue('maxconn_frontend') is not None: cmd = 'string=`grep %s %s -n -A5 |grep maxcon -n |awk -F":" \'{print $2}\'|awk -F"-" \'{print $1}\'` && sed -Ei "$( echo $string)s/[0-9]+/%s/g" %s' % (frontend, cfg, maxconn, cfg) output, stderr = funct.subprocess_execute(cmd) stderr = funct.master_slave_upload_and_restart(serv, cfg, just_save='save') - print('Maxconn for %s has been set to %s ' % (frontend, maxconn)) + print('success: Maxconn for %s has been set to %s ' % (frontend, maxconn)) else: print('error: '+output[0]) @@ -272,9 +272,9 @@ if form.getvalue('action_hap') is not None and serv is not None: commands = [ "sudo systemctl %s haproxy" % action ] funct.ssh_command(serv, commands) funct.logging(serv, 'HAProxy was '+action+'ed', haproxywi=1, login=1) - print("HAproxy was %s" % action) + print("success: HAproxy was %s" % action) else: - print("Bad config, check please") + print("error: Bad config, check please") if form.getvalue('action_nginx') is not None and serv is not None: @@ -284,9 +284,9 @@ if form.getvalue('action_nginx') is not None and serv is not None: commands = [ "sudo systemctl %s nginx" % action ] funct.ssh_command(serv, commands) funct.logging(serv, 'Nginx was '+action+'ed', haproxywi=1, login=1) - print("Nginx was %s" % action) + print("success: Nginx was %s" % action) else: - print("Bad config, check please") + print("error: Bad config, check please") if form.getvalue('action_waf') is not None and serv is not None: @@ -354,7 +354,7 @@ if act == "overviewHapservers": try: print(funct.ssh_command(serv, commands)) except: - print('Cannot get last date') + print('error: Cannot get last date') if act == "overview": @@ -538,17 +538,17 @@ if serv is not None and act == "stats": try: response = requests.get('http://%s:%s/%s' % (serv, stats_port, stats_page), auth=(haproxy_user, haproxy_pass)) except requests.exceptions.ConnectTimeout: - print('Oops. Connection timeout occured!') + print('error: Oops. Connection timeout occured!') except requests.exceptions.ReadTimeout: - print('Oops. Read timeout occured') + print('error: Oops. Read timeout occured') except requests.exceptions.HTTPError as errh: - print ("Http Error:",errh) + print ("error: Http Error:",errh) except requests.exceptions.ConnectionError as errc: - print ('
Error Connecting: %s
' % errc) + print ('error: Error Connecting: %s' % errc) except requests.exceptions.Timeout as errt: - print ("Timeout Error:",errt) + print ("error: Timeout Error:",errt) except requests.exceptions.RequestException as err: - print ("OOps: Something Else",err) + print ("error: OOps: Something Else",err) data = response.content if form.getvalue('service') == 'nginx': @@ -627,11 +627,11 @@ if serv is not None and act == "showMap": error = funct.get_config(serv, cfg) if error: - print('
'+error+'
') + print(error) try: conf = open(cfg, "r") except IOError: - print('
Can\'t read import config file
') + print('error: Can\'t read import config file') node = "" line_new2 = [1,""] @@ -703,7 +703,7 @@ if serv is not None and act == "showMap": plt.savefig("map.png") plt.show() except Exception as e: - print('
' + str(e) + '
') + print(str(e)) cmd = "rm -f "+os.path.dirname(os.getcwd())+"/map*.png && mv map.png "+os.path.dirname(os.getcwd())+"/map"+date+".png" output, stderr = funct.subprocess_execute(cmd) @@ -1339,7 +1339,7 @@ if form.getvalue('get_hap_v'): if form.getvalue('get_nginx_v'): - cmd = [ "/usr/sbin/nginx -v" ] + cmd = [ '/usr/sbin/nginx -v' ] print(funct.ssh_command(serv, cmd)) @@ -1355,7 +1355,7 @@ if form.getvalue('bwlists'): file.close print(file_read) except IOError: - print('
Cat\'n read '+form.getvalue('color')+' list
') + print('error: Cat\'n read '+form.getvalue('color')+' list , ') if form.getvalue('bwlists_create'): @@ -1365,13 +1365,13 @@ if form.getvalue('bwlists_create'): list = os.path.dirname(os.getcwd())+"/"+sql.get_setting('lists_path')+"/"+form.getvalue('group')+"/"+color+"/"+list_name try: open(list, 'a').close() - print('
'+color+' list was created
') + print(color) try: funct.logging(server[1], 'has created '+color+' list '+list_name, haproxywi=1, login=1) except: pass except IOError as e: - print('
Cat\'n create new '+color+' list. %s
' % e) + print('error: Cat\'n create new '+color+' list. %s , ' % e) if form.getvalue('bwlists_save'): @@ -1382,7 +1382,7 @@ if form.getvalue('bwlists_save'): with open(list, "w") as file: file.write(form.getvalue('bwlists_content')) except IOError as e: - print('
Cat\'n save '+color+' list. %s
' % e) + print('error: Cat\'n save '+color+' list. %s , ' % e) path = sql.get_setting('haproxy_dir')+"/"+color servers = [] @@ -1404,9 +1404,9 @@ if form.getvalue('bwlists_save'): funct.ssh_command(serv, ["sudo chown $(whoami) "+path]) error = funct.upload(serv, path+"/"+bwlists_save, list, dir='fullpath') if error: - print('
Upload fail: %s
' % error) + print('error: Upload fail: %s , ' % error) else: - print('
Edited '+color+' list was uploaded to '+serv+'
') + print('success: Edited '+color+' list was uploaded to '+serv+' , ') try: funct.logging(serv, 'has edited '+color+' list '+bwlists_save, haproxywi=1, login=1) except: @@ -1468,7 +1468,7 @@ if form.getvalue('change_waf_mode'): funct.logging(serv, 'Was changed WAF mod to '+waf_mode, haproxywi=1, login=1) -error_mess = 'All fields must be completed X' +error_mess = 'error: All fields must be completed' if form.getvalue('newuser') is not None: @@ -1731,9 +1731,9 @@ if form.getvalue('ssh_cert'): with open(ssh_keys, "w") as conf: conf.write(form.getvalue('ssh_cert')) except IOError: - print('
Can\'t save ssh keys file. Check ssh keys path in config
') + print('error: Can\'t save ssh keys file. Check ssh keys path in config') else: - print('
Ssh key was save into: %s
' % ssh_keys) + print('success: Ssh key was save into: %s ' % ssh_keys) try: cmd = 'chmod 600 %s' % ssh_keys @@ -1867,6 +1867,7 @@ if form.getvalue('smondel') is not None: if sql.delete_smon(id, user_group): print('Ok') + funct.logging('SMON','Has been delete server from SMON ', haproxywi=1, login=1) if form.getvalue('showsmon') is not None: @@ -1907,6 +1908,7 @@ if form.getvalue('updateSmonIp') is not None: sys.exit() if sql.update_smon(id, ip, port, body, telegram, group, desc, en): print("Ok") + funct.logging('SMON','Has been update the server '+ip+' to SMON ', haproxywi=1, login=1) if form.getvalue('showBytes') is not None: diff --git a/app/templates/include/smon_server.html b/app/templates/include/smon_server.html index e429f60b..1ef102a3 100644 --- a/app/templates/include/smon_server.html +++ b/app/templates/include/smon_server.html @@ -32,10 +32,15 @@ {% set id = 'smon-body-' + s.0|string() %} {% if s.5 != 'None' %} + {{s.5}} + {% else %} + + {% endif %} + {#{% if s.5 != 'None' %} {{ input(id, value=s.5, size='10') }} {% else %} {{ input(id, size='10') }} - {% endif %} + {% endif %} #}