From af62ff0fc20f3fb32410c2bdce5fef9a9f0dc8d2 Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Tue, 14 Aug 2018 11:59:21 +0600 Subject: [PATCH] v3.0 Show config and compare page improved --- app/funct.py | 84 ++--------------------------- app/options.py | 60 ++++++++++++++------- app/ovw.py | 29 +--------- app/scripts/syn_flood_protect.sh | 8 ++- app/templates/ajax/compare.html | 31 +++++++++++ app/templates/ajax/config_show.html | 77 ++++++++++++++++++++++++++ install.sh | 2 +- 7 files changed, 161 insertions(+), 130 deletions(-) create mode 100644 app/templates/ajax/compare.html create mode 100644 app/templates/ajax/config_show.html diff --git a/app/funct.py b/app/funct.py index ca0c2750..99a93640 100644 --- a/app/funct.py +++ b/app/funct.py @@ -231,53 +231,6 @@ def get_config(serv, cfg, **kwargs): ssh += str(e) return ssh -def show_config(cfg): - print('
') - try: - conf = open(cfg, "r") - except IOError: - print('
Can\'t read import config file
') - i = 0 - for line in conf: - i = i + 1 - if not line.find("global"): - print('' + line + '
') - continue - if not line.find("defaults"): - print('
' + line + '
') - continue - if not line.find("listen"): - print('
' + line + '
') - continue - if not line.find("frontend"): - print('
' + line + '
') - continue - if not line.find("backend"): - print('
' + line + '
') - continue - if not line.find("cache"): - print('
' + line + '
') - continue - if "acl" in line or "option" in line or "server" in line: - if "timeout" not in line and "default-server" not in line and "#use_backend" not in line: - print('') - print(i) - print('' + line + '
') - continue - if "#" in line: - print('') - print(i) - print(line + '
') - continue - if line.__len__() < 1: - print('
') - if line.__len__() > 1: - print('') - print(i) - print('' + line + '
') - print('
') - conf.close - def diff_config(oldcfg, cfg): import subprocess log_path = get_config_var('main', 'log_path') @@ -317,7 +270,7 @@ def install_haproxy(serv, **kwargs): " STATS_USER="+stats_user+" STATS_PASS="+stats_password ] upload(serv, tmp_config_path, script) - ssh_command(serv, commands) + ssh_command(serv, commands, print_out="1") if kwargs.get('syn_flood') == "1": syn_flood_protect(serv) @@ -339,7 +292,7 @@ def syn_flood_protect(serv, **kwargs): commands = [ "chmod +x "+tmp_config_path+script, tmp_config_path+script+ " "+enable ] upload(serv, tmp_config_path, script) - ssh_command(serv, commands) + ssh_command(serv, commands, print_out="1") os.system("rm -f %s" % script) @@ -428,37 +381,6 @@ def check_haproxy_config(serv): else: return False ssh.close() - -def compare(stdout): - i = 0 - minus = 0 - plus = 0 - total_change = 0 - - print('
') - print('
') - - for line in stdout: - i = i + 1 - - if i is 1: - print('
' + line + '
') - elif i is 2: - print(line + '
') - elif line.find("-") == 0 and i is not 1: - print('
' + line + '
') - minus = minus + 1 - elif line.find("+") == 0 and i is not 2: - print('
' + line + '
') - plus = plus + 1 - elif line.find("@") == 0: - print('
' + line + '
') - else: - print('
' + line + '
') - - total_change = minus + plus - print('
Total change: %s, additions: %s & deletions: %s
' % (total_change, minus, plus)) - print('
') def show_log(stdout): i = 0 @@ -501,6 +423,8 @@ def ssh_command(serv, commands, **kwargs): show_log(stdout) elif kwargs.get("server_status") == "1": server_status(stdout) + elif kwargs.get('print_out'): + print(stdout.read().decode(encoding='UTF-8')) else: return stdout.read().decode(encoding='UTF-8') diff --git a/app/options.py b/app/options.py index 25d32e6a..ee380787 100644 --- a/app/options.py +++ b/app/options.py @@ -261,10 +261,35 @@ if form.getvalue('servaction') is not None: funct.logging(serv, action) if act == "showCompareConfigs": - ovw.show_compare_configs(serv) + import glob + from jinja2 import Environment, FileSystemLoader + env = Environment(loader=FileSystemLoader('templates/ajax')) + template = env.get_template('/show_compare_configs.html') + left = form.getvalue('left') + right = form.getvalue('right') + + output_from_parsed_template = template.render(serv = serv, + right = right, + left = left, + return_files = funct.get_files()) + + print(output_from_parsed_template) if serv is not None and form.getvalue('right') is not None: - ovw.comapre_show() + import subprocess + from jinja2 import Environment, FileSystemLoader + left = form.getvalue('left') + right = form.getvalue('right') + hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') + cmd='diff -ub %s%s %s%s' % (hap_configs_dir, left, hap_configs_dir, right) + env = Environment(loader=FileSystemLoader('templates/ajax'),extensions=['jinja2.ext.loopcontrols']) + template = env.get_template('compare.html') + + output, stderr = funct.subprocess_execute(cmd) + template = template.render(stdout=output) + + print(template) + print(stderr) if serv is not None and act == "configShow": hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') @@ -275,27 +300,24 @@ if serv is not None and act == "configShow": else: cfg = hap_configs_dir + form.getvalue('configver') - print("

Config from %s

" % serv) - print('

' - 'Expand all' - 'Edit' - '

') - print('
') - funct.show_config(cfg) + try: + conf = open(cfg, "r") + except IOError: + print('
Can\'t read import config file
') + + from jinja2 import Environment, FileSystemLoader + env = Environment(loader=FileSystemLoader('templates/ajax'),extensions=['jinja2.ext.loopcontrols']) + template = env.get_template('config_show.html') + + template = template.render(conf=conf, + view=form.getvalue('view'), + serv=serv, + configver=form.getvalue('configver')) + print(template) if form.getvalue('configver') is None: os.system("/bin/rm -f " + cfg) - else: - print('
') - print('
') - print('' % serv) - print('' % form.getvalue('configver')) - print('') - if form.getvalue('view') is None: - print("") - print("") - print('
') if form.getvalue('master'): master = form.getvalue('master') diff --git a/app/ovw.py b/app/ovw.py index bf2a8640..3acf8235 100644 --- a/app/ovw.py +++ b/app/ovw.py @@ -163,31 +163,4 @@ def get_map(serv): output, stderr = funct.subprocess_execute(cmd) print(stderr) - print('map' % date) - -def show_compare_configs(serv): - import glob - from jinja2 import Environment, FileSystemLoader - env = Environment(loader=FileSystemLoader('templates/ajax')) - template = env.get_template('/show_compare_configs.html') - left = form.getvalue('left') - right = form.getvalue('right') - - output_from_parsed_template = template.render(serv = serv, - right = right, - left = left, - return_files = funct.get_files()) - - print(output_from_parsed_template) - -def comapre_show(): - import subprocess - left = form.getvalue('left') - right = form.getvalue('right') - hap_configs_dir = funct.get_config_var('configs', 'haproxy_save_configs_dir') - cmd='diff -ub %s%s %s%s' % (hap_configs_dir, left, hap_configs_dir, right) - - output, stderr = funct.subprocess_execute(cmd) - - funct.compare(output) - print(stderr) \ No newline at end of file + print('map' % date) \ No newline at end of file diff --git a/app/scripts/syn_flood_protect.sh b/app/scripts/syn_flood_protect.sh index 458544db..b232dbaa 100644 --- a/app/scripts/syn_flood_protect.sh +++ b/app/scripts/syn_flood_protect.sh @@ -1,17 +1,21 @@ #!/bin/bash if [[ $1 == "enable" ]]; then - sudo bash -c cat <> /etc/sysctl.conf + if grep -q "net.ipv4.tcp_syncookie = 1" /etc/sysctl.conf; then + echo "SYN flood protectd allready enabled" + else + sudo bash -c cat <> /etc/sysctl.conf # Protection SYN flood net.ipv4.tcp_syncookies = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.tcp_max_syn_backlog = 1024 EOF - + sudo sysctl -w net.ipv4.tcp_syncookies=1 sudo sysctl -w net.ipv4.conf.all.rp_filter=1 sudo sysctl -w net.ipv4.tcp_max_syn_backlog=1024 sudo sysctl -w net.ipv4.tcp_synack_retries=3 + fi fi if [[ $1 == "disable" ]]; then diff --git a/app/templates/ajax/compare.html b/app/templates/ajax/compare.html new file mode 100644 index 00000000..e9039fde --- /dev/null +++ b/app/templates/ajax/compare.html @@ -0,0 +1,31 @@ + +
+
+ {% set plus = 0 %} + {% set minus = 0 %} + {% set total_change = 0 %} + {% for line in stdout %} + + {% if loop.index0 == 0 %} +
{{ line }}
+ {% elif loop.index0 == 1 %} + {{ line }}
+ {% elif line.startswith('-') and loop.index0 > 1 %} +
{{ line }}
+ {% set minus = minus + 1 %} + {% elif line.startswith('+') and loop.index0 > 2 %} +
{{ line }}
+ {% set plus = plus + 1 %} + {% elif line.startswith('@') %} +
{{ line }}
+ {% else %} +
{{ line }}
+ {% endif %} + {% set total_change = minus + plus %} + {% if loop.last %} +
Total change: {{ total_change }}, additions: {{ plus }} & deletions: {{ minus }}
+ {% endif %} + {% endfor %} + +
+
\ No newline at end of file diff --git a/app/templates/ajax/config_show.html b/app/templates/ajax/config_show.html new file mode 100644 index 00000000..4b49f9ce --- /dev/null +++ b/app/templates/ajax/config_show.html @@ -0,0 +1,77 @@ +
+

Config from {{serv}}

+

+ Expand all + {% if not view %} + Edit + {% endif %} +

+
+
+ {% set i = 0 -%} + {% for line in conf %} + {% set i = i + loop.index0 %} + {% if line.startswith('global') %} + {{ line }}
+ {% continue %} + {% endif %} + {% if line.startswith('defaults') %} + {{ line }}
+ {% continue %} + {% endif %} + {% if line.startswith('listen') %} + {{ line }}
+ {% continue %} + {% endif %} + {% if line.startswith('frontend') %} + {{ line }}
+ {% continue %} + {% endif %} + {% if line.startswith('backend') %} + {{ line }}
+ {% continue %} + {% endif %} + {% if line.startswith('cache') %} + {{ line }}
+ {% continue %} + {% endif %} + {% if "acl" in line or "option" in line or "server" in line %} + {% if "timeout" not in line and "default-server" not in line and "#use_backend" not in line and "#" not in line%} + + + {{ i }} + + {{ line }} +
+ {% continue %} + {% endif %} + {% endif %} + + {% if line|length == 1 %} +
+ {% endif %} + + {% if line|length > 1 %} + + {{ i }} + {{ line }} +
+ {% endif %} + {% endfor %} +
+
+
+ {% if configver %} +
+
+
+ + + + {% if not view %} + + + {% endif %} +
+
+ {% endif %} diff --git a/install.sh b/install.sh index fd0bc267..e0b55a2d 100644 --- a/install.sh +++ b/install.sh @@ -40,7 +40,7 @@ echo "" echo "################################" if hash apt-get 2>/dev/null; then - apt-get install git net-tools lshw dos2unix apache2 gcc netcat python3-pip gcc-c++ -y + apt-get install git net-tools lshw dos2unix apache2 gcc netcat python3-pip g++ -y HTTPD_CONFIG="/etc/apache2/apache2.conf" HAPROXY_WI_VHOST_CONF="/etc/apache2/sites-enabled/haproxy-wi.conf" HTTPD_NAME="apache2"