diff --git a/app/add.py b/app/add.py index 47aad8e2..da3ddb01 100644 --- a/app/add.py +++ b/app/add.py @@ -19,7 +19,7 @@ get_config_var = roxy_wi_tools.GetConfigVar() env = Environment(loader=FileSystemLoader('templates/'), autoescape=True) template = env.get_template('add.html') form = common.form -serv = form.getvalue('serv') +serv = common.is_ip_or_dns(form.getvalue('serv')) print('Content-type: text/html\n') @@ -75,7 +75,7 @@ elif form.getvalue('mode') is not None: ip = "" force_close = form.getvalue('force_close') balance = "" - mode = " mode " + form.getvalue('mode') + "\n" + mode = f" mode {form.getvalue('mode')}\n" maxconn = "" options_split = "" ssl = "" @@ -94,34 +94,34 @@ elif form.getvalue('mode') is not None: health_check = form.getvalue('health_check') if health_check == 'option httpchk' and form.getvalue('checks_http_domain') is not None: health_check = health_check + ' GET ' + form.getvalue('checks_http_path') + ' "HTTP/1.0\\r\\nHost: ' + form.getvalue('checks_http_domain') + '"' - balance += " " + health_check + "\n" + balance += f" {health_check}\n" if form.getvalue('ip') is not None: ip = form.getlist('ip') if new_listener is not None: - name = "listen " + new_listener + name = f"listen {new_listener}" end_name = new_listener elif new_frontend is not None: - name = "frontend " + new_frontend + name = f"frontend {new_frontend}" end_name = new_frontend elif new_backend is not None: - name = "backend " + new_backend + name = f"backend {new_backend}" end_name = new_backend else: print('error: The name cannot be empty') sys.exit() if form.getvalue('backends') is not None: - backend = " default_backend " + form.getvalue('backends') + "\n" + backend = f" default_backend { form.getvalue('backends')}\n" if form.getvalue('maxconn'): - maxconn = " maxconn " + form.getvalue('maxconn') + "\n" + maxconn = f" maxconn {form.getvalue('maxconn')}\n" if form.getvalue('ssl') == "https" and form.getvalue('mode') != "tcp": cert_path = sql.get_setting('cert_path') if form.getvalue('cert') is not None: - ssl = "ssl crt " + cert_path + form.getvalue('cert') + ssl = f"ssl crt {cert_path}{form.getvalue('cert')}" if form.getvalue('ssl-dis-check') is None: if form.getvalue('ssl-check') == "ssl-check": ssl_check = " ssl verify none" @@ -138,32 +138,32 @@ elif form.getvalue('mode') is not None: continue else: port_value = port[i] - bind += " bind *:" + port_value + " " + ssl + "\n" + bind += f" bind *:{port_value} {ssl}\n" else: if port[i] == 'IsEmptY': print('error: IP cannot be bind without a port') sys.exit() else: port_value = port[i] - bind += " bind " + ip[i] + ":" + port_value + " " + ssl + "\n" + bind += f" bind {ip[i]}:{port_value} {ssl}\n" i += 1 if form.getvalue('default-check') == "1": if form.getvalue('check-servers') == "1": - check = " check inter " + form.getvalue('inter') + " rise " + form.getvalue('rise') + " fall " + form.getvalue('fall') + ssl_check + check = f" check inter {form.getvalue('inter')} rise {form.getvalue('rise')} fall {form.getvalue('fall')}{ssl_check}" else: check = "" else: if form.getvalue('check-servers') != "1": check = "" else: - check = " check" + ssl_check + check = f" check{ssl_check}" if form.getvalue('option') is not None: options = form.getvalue('option') i = options.split("\n") for j in i: - options_split += " " + j + "\n" + options_split += f" {j}\n" if force_close == "1": options_split += " option http-server-close\n" @@ -181,9 +181,9 @@ elif form.getvalue('mode') is not None: 'blacklist') + " }\n" if form.getvalue('cookie'): - cookie = " cookie " + form.getvalue('cookie_name') + cookie = f" cookie {form.getvalue('cookie_name')}" if form.getvalue('cookie_domain'): - cookie += " domain " + form.getvalue('cookie_domain') + cookie += f" domain {form.getvalue('cookie_domain')}" if form.getvalue('rewrite'): rewrite = form.getvalue('rewrite') else: @@ -204,10 +204,10 @@ elif form.getvalue('mode') is not None: dynamic = form.getvalue('dynamic') else: dynamic = "" - cookie += " " + rewrite + " " + prefix + " " + nocache + " " + postonly + " " + dynamic + "\n" + cookie += f" {rewrite} {prefix} {nocache} {postonly} {dynamic}\n" options_split += cookie if form.getvalue('dynamic'): - options_split += " dynamic-cookie-key " + form.getvalue('dynamic-cookie-key') + "\n" + options_split += f" dynamic-cookie-key {form.getvalue('dynamic-cookie-key')}\n" if form.getvalue('acl_if'): acl_if = form.getlist('acl_if') @@ -324,27 +324,27 @@ elif form.getvalue('mode') is not None: if compression == "1" or cache == "2": filter_com = " filter compression\n" if cache == "2": - cache_s = " http-request cache-use " + end_name + "\n http-response cache-store " + end_name + "\n" - cache_set = "cache " + end_name + "\n total-max-size 4\n max-age 240\n" + cache_s = f" http-request cache-use {end_name}\n http-response cache-store {end_name}\n" + cache_set = f"cache {end_name}\n total-max-size 4\n max-age 240\n" if compression == "1": compression_s = " compression algo gzip\n compression type text/html text/plain text/css\n" waf = "" if form.getvalue('waf') is not None: - waf = " filter spoe engine modsecurity config " + haproxy_dir + "/waf.conf\n" + waf = f" filter spoe engine modsecurity config {haproxy_dir}/waf.conf\n" waf += " http-request deny if { var(txn.modsec.code) -m int gt 0 }\n" - config_add = "\n" + name + "\n" + bind + mode + maxconn + balance + options_split + cache_s + filter_com + \ - compression_s + waf + acl + backend + servers_split + "\n" + cache_set + "\n" + config_add = f"\n{name}\n{bind}{mode}{maxconn}{balance}{options_split}{cache_s}{filter_com}{compression_s}" \ + f"{waf}{acl}{backend}{servers_split}\n{cache_set}\n" if form.getvalue('new_userlist') is not None: - name = "userlist " + form.getvalue('new_userlist') + "\n" + name = f"userlist {form.getvalue('new_userlist')}\n" new_userlist_groups = "" if form.getvalue('userlist-group') is not None: groups = form.getlist('userlist-group') for group in groups: - new_userlist_groups += " group " + group + "\n" + new_userlist_groups += f" group {group}\n" new_users_list = "" if form.getvalue('userlist-user') is not None: @@ -355,10 +355,10 @@ if form.getvalue('new_userlist') is not None: for user in users: try: - group = ' groups ' + userlist_user_group[i] + group = f' groups {userlist_user_group[i]}' except Exception: group = '' - new_users_list += " user " + user + " insecure-password " + passwords[i] + group + "\n" + new_users_list += f" user {user} insecure-password { passwords[i]} {group}\n" i += 1 config_add = "\n" + name + new_userlist_groups + new_users_list @@ -396,18 +396,22 @@ if form.getvalue('generateconfig') is None and serv is not None: try: with open(cfg, "a") as conf: conf.write(config_add) - except IOError: - print("error: Can't read import config file") - - roxywi_common.logging(serv, "add.py add new %s" % name) + except IOError as e: + print(f"error: Can't read import config file {e}") output = config_mod.master_slave_upload_and_restart(serv, cfg, just_save="save") + + try: + roxywi_common.logging(serv, f"add.py add new {name}") + except Exception: + pass + if output: print(output) else: print(name) - except Exception: - pass + except Exception as e: + print(e) else: print(config_add) diff --git a/app/modules/alerting/alerting.py b/app/modules/alerting/alerting.py index f55d4382..c10baaf2 100644 --- a/app/modules/alerting/alerting.py +++ b/app/modules/alerting/alerting.py @@ -246,16 +246,17 @@ def add_slack_channel(token: str, channel: str, group: str, page: str) -> None: login=1) -def delete_telegram_channel(telegram, channel_id) -> None: +def delete_telegram_channel(channel_id) -> None: + telegram = sql.select_telegram(id=channel_id) for t in telegram: telegram_name = t.token if sql.delete_telegram(channel_id): print("Ok") - roxywi_common.logging('Roxy-WI server', f'The Telegram channel {telegram_name} has been deleted ', roxywi=1, - login=1) + roxywi_common.logging('Roxy-WI server', f'The Telegram channel {telegram_name} has been deleted ', roxywi=1, login=1) -def delete_slack_channel(slack, channel_id) -> None: +def delete_slack_channel(channel_id) -> None: + slack = sql.select_slack(id=channel_id) for t in slack: slack_name = t.chanel_name if sql.delete_slack(channel_id): diff --git a/app/modules/config/runtime.py b/app/modules/config/runtime.py index f3d70002..012e565c 100644 --- a/app/modules/config/runtime.py +++ b/app/modules/config/runtime.py @@ -14,6 +14,28 @@ get_date = roxy_wi_tools.GetDate(time_zone) get_config_var = roxy_wi_tools.GetConfigVar() +def show_frontend_backend() -> None: + haproxy_sock_port = int(sql.get_setting('haproxy_sock_port')) + backend = common.checkAjaxInput(form.getvalue('ipbackend')) + cmd = 'echo "show servers state"|nc %s %s |grep "%s" |awk \'{print $4}\'' % (serv, haproxy_sock_port, backend) + output, stderr = server_mod.subprocess_execute(cmd) + for i in output: + if i == ' ': + continue + i = i.strip() + print(i + '
') + + +def show_server() -> None: + haproxy_sock_port = int(sql.get_setting('haproxy_sock_port')) + backend = common.checkAjaxInput(form.getvalue('ipbackend')) + backend_server = common.checkAjaxInput(form.getvalue('backend_server')) + cmd = 'echo "show servers state"|nc %s %s |grep "%s" |grep "%s" |awk \'{print $5":"$19}\' |head -1' % ( + serv, haproxy_sock_port, backend, backend_server) + output, stderr = server_mod.subprocess_execute(cmd) + print(output[0]) + + def get_all_stick_table(): hap_sock_p = sql.get_setting('haproxy_sock_port') cmd = 'echo "show table"|nc %s %s |awk \'{print $3}\' | tr -d \'\n\' | tr -d \'[:space:]\'' % (serv, hap_sock_p) diff --git a/app/options.py b/app/options.py index 2970caf7..41fcb4e1 100644 --- a/app/options.py +++ b/app/options.py @@ -91,24 +91,14 @@ if form.getvalue('ip_select') is not None: runtime.show_backends(serv) if form.getvalue('ipbackend') is not None and form.getvalue('backend_server') is None: - haproxy_sock_port = int(sql.get_setting('haproxy_sock_port')) - backend = common.checkAjaxInput(form.getvalue('ipbackend')) - cmd = 'echo "show servers state"|nc %s %s |grep "%s" |awk \'{print $4}\'' % (serv, haproxy_sock_port, backend) - output, stderr = server_mod.subprocess_execute(cmd) - for i in output: - if i == ' ': - continue - i = i.strip() - print(i + '
') + import modules.config.runtime as runtime + + runtime.show_frontend_backend() if form.getvalue('ipbackend') is not None and form.getvalue('backend_server') is not None: - haproxy_sock_port = int(sql.get_setting('haproxy_sock_port')) - backend = common.checkAjaxInput(form.getvalue('ipbackend')) - backend_server = common.checkAjaxInput(form.getvalue('backend_server')) - cmd = 'echo "show servers state"|nc %s %s |grep "%s" |grep "%s" |awk \'{print $5":"$19}\' |head -1' % ( - serv, haproxy_sock_port, backend, backend_server) - output, stderr = server_mod.subprocess_execute(cmd) - print(output[0]) + import modules.config.runtime as runtime + + runtime.show_server() if form.getvalue('backend_ip') is not None: import modules.config.runtime as runtime @@ -180,8 +170,8 @@ if form.getvalue('session_delete_id') is not None: runtime.delete_session() if form.getvalue("change_pos") is not None: - pos = form.getvalue('change_pos') - server_id = form.getvalue('pos_server_id') + pos = common.checkAjaxInput(form.getvalue('change_pos')) + server_id = common.checkAjaxInput(form.getvalue('pos_server_id')) sql.update_server_pos(pos, server_id) if form.getvalue('show_ip') is not None and serv is not None: @@ -1558,17 +1548,15 @@ if form.getvalue('telegramdel') is not None: import modules.alerting.alerting as alerting channel_id = common.checkAjaxInput(form.getvalue('telegramdel')) - telegram = sql.select_telegram(id=channel_id) - alerting.delete_telegram_channel(telegram, channel_id) + alerting.delete_telegram_channel(channel_id) if form.getvalue('slackdel') is not None: import modules.alerting.alerting as alerting channel_id = common.checkAjaxInput(form.getvalue('slackdel')) - slack = sql.select_slack(id=channel_id) - alerting.delete_slack_channel(slack, channel_id) + alerting.delete_slack_channel(channel_id) if form.getvalue('updatetoken') is not None: token = common.checkAjaxInput(form.getvalue('updatetoken'))