mirror of https://github.com/Aidaho12/haproxy-wi
parent
43efd1587e
commit
0b804af5d9
|
@ -6,8 +6,11 @@ import pika
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
import modules.db.sql as sql
|
import modules.db.sql as sql
|
||||||
|
import modules.common.common as common
|
||||||
import modules.roxywi.common as roxywi_common
|
import modules.roxywi.common as roxywi_common
|
||||||
|
|
||||||
|
error_mess = common.error_mess
|
||||||
|
|
||||||
|
|
||||||
def send_message_to_rabbit(message: str, **kwargs) -> None:
|
def send_message_to_rabbit(message: str, **kwargs) -> None:
|
||||||
rabbit_user = sql.get_setting('rabbitmq_user')
|
rabbit_user = sql.get_setting('rabbitmq_user')
|
||||||
|
@ -215,9 +218,6 @@ def check_email_alert() -> None:
|
||||||
print(f'error: Cannot send a message {e}')
|
print(f'error: Cannot send a message {e}')
|
||||||
|
|
||||||
|
|
||||||
error_mess = 'error: All fields must be completed'
|
|
||||||
|
|
||||||
|
|
||||||
def add_telegram_channel(token: str, channel: str, group: str, page: str) -> None:
|
def add_telegram_channel(token: str, channel: str, group: str, page: str) -> None:
|
||||||
if token is None or channel is None or group is None:
|
if token is None or channel is None or group is None:
|
||||||
print(error_mess)
|
print(error_mess)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import re
|
||||||
import cgi
|
import cgi
|
||||||
|
|
||||||
form = cgi.FieldStorage()
|
form = cgi.FieldStorage()
|
||||||
|
error_mess = 'error: All fields must be completed'
|
||||||
|
|
||||||
def is_ip_or_dns(server_from_request: str) -> str:
|
def is_ip_or_dns(server_from_request: str) -> str:
|
||||||
ip_regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
|
ip_regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
|
||||||
|
|
|
@ -10,6 +10,7 @@ import modules.roxywi.common as roxywi_common
|
||||||
|
|
||||||
form = common.form
|
form = common.form
|
||||||
|
|
||||||
|
|
||||||
def waf_overview(serv, waf_service) -> None:
|
def waf_overview(serv, waf_service) -> None:
|
||||||
env = Environment(
|
env = Environment(
|
||||||
loader=FileSystemLoader('templates/ajax'), autoescape=True,
|
loader=FileSystemLoader('templates/ajax'), autoescape=True,
|
||||||
|
|
|
@ -79,8 +79,7 @@ def ssh_command(server_ip: str, commands: list, **kwargs):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
roxywi_common.logging('Roxy-WI server', f' Something wrong with SSH connection. Probably sudo with password {e}', roxywi=1)
|
roxywi_common.logging('Roxy-WI server', f' Something wrong with SSH connection. Probably sudo with password {e}', roxywi=1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
roxywi_common.logging('Roxy-WI server',
|
roxywi_common.logging('Roxy-WI server', f' Something wrong with SSH connection: {e}', roxywi=1)
|
||||||
f' Something wrong with SSH connection: {e}', roxywi=1)
|
|
||||||
raise Exception(f'error: {e}')
|
raise Exception(f'error: {e}')
|
||||||
|
|
||||||
|
|
||||||
|
|
116
app/options.py
116
app/options.py
|
@ -1657,124 +1657,24 @@ if form.getvalue('updategroup') is not None:
|
||||||
print('error: ' + str(e))
|
print('error: ' + str(e))
|
||||||
|
|
||||||
if form.getvalue('new_ssh'):
|
if form.getvalue('new_ssh'):
|
||||||
user_group = roxywi_common.get_user_group()
|
import modules.server.ssh as ssh_mod
|
||||||
name = common.checkAjaxInput(form.getvalue('new_ssh'))
|
|
||||||
name = f'{name}{user_group}'
|
|
||||||
enable = common.checkAjaxInput(form.getvalue('ssh_enable'))
|
|
||||||
group = common.checkAjaxInput(form.getvalue('new_group'))
|
|
||||||
username = common.checkAjaxInput(form.getvalue('ssh_user'))
|
|
||||||
password = common.checkAjaxInput(form.getvalue('ssh_pass'))
|
|
||||||
page = common.checkAjaxInput(form.getvalue('page'))
|
|
||||||
page = page.split("#")[0]
|
|
||||||
|
|
||||||
if username is None or name is None:
|
ssh_mod.create_ssh_cred()
|
||||||
print(error_mess)
|
|
||||||
else:
|
|
||||||
if sql.insert_new_ssh(name, enable, group, username, password):
|
|
||||||
env = Environment(loader=FileSystemLoader('templates/ajax'), autoescape=True)
|
|
||||||
template = env.get_template('/new_ssh.html')
|
|
||||||
output_from_parsed_template = template.render(groups=sql.select_groups(), sshs=sql.select_ssh(name=name),
|
|
||||||
page=page)
|
|
||||||
print(output_from_parsed_template)
|
|
||||||
roxywi_common.logging('Roxy-WI server', 'A new SSH credentials ' + name + ' has created', roxywi=1, login=1)
|
|
||||||
|
|
||||||
if form.getvalue('sshdel') is not None:
|
if form.getvalue('sshdel') is not None:
|
||||||
lib_path = get_config.get_config_var('main', 'lib_path')
|
import modules.server.ssh as ssh_mod
|
||||||
sshdel = common.checkAjaxInput(form.getvalue('sshdel'))
|
|
||||||
name = ''
|
|
||||||
ssh_enable = 0
|
|
||||||
ssh_key_name = ''
|
|
||||||
|
|
||||||
for sshs in sql.select_ssh(id=sshdel):
|
ssh_mod.delete_ssh_key()
|
||||||
ssh_enable = sshs.enable
|
|
||||||
name = sshs.name
|
|
||||||
ssh_key_name = f'{lib_path}/keys/{sshs.name}.pem'
|
|
||||||
|
|
||||||
if ssh_enable == 1:
|
|
||||||
cmd = f'rm -f {ssh_key_name}'
|
|
||||||
try:
|
|
||||||
server_mod.subprocess_execute(cmd)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if sql.delete_ssh(sshdel):
|
|
||||||
print("Ok")
|
|
||||||
roxywi_common.logging('Roxy-WI server', f'The SSH credentials {name} has deleted', roxywi=1, login=1)
|
|
||||||
|
|
||||||
if form.getvalue('updatessh'):
|
if form.getvalue('updatessh'):
|
||||||
ssh_id = common.checkAjaxInput(form.getvalue('id'))
|
import modules.server.ssh as ssh_mod
|
||||||
name = common.checkAjaxInput(form.getvalue('name'))
|
|
||||||
enable = common.checkAjaxInput(form.getvalue('ssh_enable'))
|
|
||||||
group = common.checkAjaxInput(form.getvalue('group'))
|
|
||||||
username = common.checkAjaxInput(form.getvalue('ssh_user'))
|
|
||||||
password = common.checkAjaxInput(form.getvalue('ssh_pass'))
|
|
||||||
new_ssh_key_name = ''
|
|
||||||
|
|
||||||
if username is None:
|
ssh_mod.update_ssh_key()
|
||||||
print(error_mess)
|
|
||||||
else:
|
|
||||||
lib_path = get_config.get_config_var('main', 'lib_path')
|
|
||||||
|
|
||||||
for sshs in sql.select_ssh(id=ssh_id):
|
|
||||||
ssh_enable = sshs.enable
|
|
||||||
ssh_key_name = f'{lib_path}/keys/{sshs.name}.pem'
|
|
||||||
new_ssh_key_name = f'{lib_path}/keys/{name}.pem'
|
|
||||||
|
|
||||||
if ssh_enable == 1:
|
|
||||||
cmd = f'mv {ssh_key_name} {new_ssh_key_name}'
|
|
||||||
cmd1 = f'chmod 600 {new_ssh_key_name}'
|
|
||||||
try:
|
|
||||||
server_mod.subprocess_execute(cmd)
|
|
||||||
server_mod.subprocess_execute(cmd1)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
sql.update_ssh(ssh_id, name, enable, group, username, password)
|
|
||||||
roxywi_common.logging('Roxy-WI server', f'The SSH credentials {name} has been updated ', roxywi=1, login=1)
|
|
||||||
|
|
||||||
if form.getvalue('ssh_cert'):
|
if form.getvalue('ssh_cert'):
|
||||||
import paramiko
|
import modules.server.ssh as ssh_mod
|
||||||
|
|
||||||
user_group = roxywi_common.get_user_group()
|
ssh_mod.upload_ssh_key()
|
||||||
name = common.checkAjaxInput(form.getvalue('name'))
|
|
||||||
|
|
||||||
try:
|
|
||||||
key = paramiko.pkey.load_private_key(form.getvalue('ssh_cert'))
|
|
||||||
except Exception as e:
|
|
||||||
print(f'error: Cannot save SSH key file: {e}')
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
lib_path = get_config.get_config_var('main', 'lib_path')
|
|
||||||
full_dir = f'{lib_path}/keys/'
|
|
||||||
ssh_keys = f'{name}.pem'
|
|
||||||
|
|
||||||
try:
|
|
||||||
check_split = name.split('_')[1]
|
|
||||||
split_name = True
|
|
||||||
except Exception:
|
|
||||||
split_name = False
|
|
||||||
|
|
||||||
if not os.path.isfile(ssh_keys) and not split_name:
|
|
||||||
name = f'{name}_{user_group}'
|
|
||||||
|
|
||||||
if not os.path.exists(full_dir):
|
|
||||||
os.makedirs(full_dir)
|
|
||||||
|
|
||||||
ssh_keys = f'{full_dir}{name}.pem'
|
|
||||||
|
|
||||||
try:
|
|
||||||
key.write_private_key_file(ssh_keys)
|
|
||||||
except Exception as e:
|
|
||||||
print(f'error: Cannot save SSH key file: {e}')
|
|
||||||
sys.exit()
|
|
||||||
else:
|
|
||||||
print(f'success: SSH key has been saved into: {ssh_keys}')
|
|
||||||
|
|
||||||
try:
|
|
||||||
cmd = f'chmod 600 {ssh_keys}'
|
|
||||||
server_mod.subprocess_execute(cmd)
|
|
||||||
except IOError as e:
|
|
||||||
roxywi_common.logging('Roxy-WI server', e.args[0], roxywi=1)
|
|
||||||
|
|
||||||
roxywi_common.logging("Roxy-WI server", f"A new SSH cert has been uploaded {ssh_keys}", roxywi=1, login=1)
|
|
||||||
|
|
||||||
if form.getvalue('newtelegram'):
|
if form.getvalue('newtelegram'):
|
||||||
import modules.alerting.alerting as alerting
|
import modules.alerting.alerting as alerting
|
||||||
|
|
Loading…
Reference in New Issue