2025-03-31 13:41:59 -03:00
|
|
|
import subprocess
|
|
|
|
|
2024-02-14 16:36:01 -03:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2025-03-31 13:41:59 -03:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2025-04-15 10:49:16 -03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2025-03-31 13:41:59 -03:00
|
|
|
|
2025-01-21 13:22:18 -03:00
|
|
|
from user_manager.models import UserAcl
|
2025-03-31 13:41:59 -03:00
|
|
|
from wgwadmlibrary.tools import is_valid_ip_or_hostname
|
|
|
|
from wireguard.models import WireGuardInstance
|
2024-02-14 16:36:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def view_console(request):
|
2025-01-21 13:22:18 -03:00
|
|
|
user_acl = get_object_or_404(UserAcl, user=request.user)
|
|
|
|
|
|
|
|
if not user_acl.enable_console:
|
|
|
|
return render(request, 'access_denied.html', {'page_title': 'Access Denied'})
|
|
|
|
|
2024-02-16 17:14:35 -03:00
|
|
|
wireguard_instances = WireGuardInstance.objects.all().order_by('instance_id')
|
2024-02-14 16:36:01 -03:00
|
|
|
requested_command = request.GET.get('command')
|
|
|
|
command_target = request.GET.get('target', '')
|
|
|
|
if command_target:
|
|
|
|
if not is_valid_ip_or_hostname(command_target):
|
|
|
|
command_target = ''
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title = _('Console') + ': '
|
2024-02-14 16:36:01 -03:00
|
|
|
if requested_command == 'iptables':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += _('iptables list')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'iptables -L -nv ; iptables -t nat -L -nv']
|
|
|
|
elif requested_command == 'ifconfig':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += 'ifconfig'
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'ifconfig']
|
|
|
|
elif requested_command == 'ps':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += _('running processes')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'ps faux']
|
|
|
|
elif requested_command == 'wgshow':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += _('WireGuard show')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'wg show']
|
|
|
|
elif requested_command == 'freem':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += _('Memory usage')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'free -m']
|
|
|
|
elif requested_command == 'route':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += _('Routing table')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'route -n']
|
|
|
|
elif requested_command == 'top':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += 'top'
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'top -b -n 1']
|
|
|
|
elif requested_command == 'ping':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title = 'ping ' + command_target
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'ping -c 4 ' + command_target]
|
|
|
|
elif requested_command == 'traceroute':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += 'traceroute ' + command_target
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = ['bash', '-c', 'traceroute ' + command_target]
|
2024-07-09 14:46:59 -03:00
|
|
|
elif requested_command == 'testdns':
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title += _('DNS container test script')
|
2024-07-09 14:46:59 -03:00
|
|
|
bash_command = ['/app/dns/scripts/test_dns_service.sh']
|
2024-02-14 16:36:01 -03:00
|
|
|
else:
|
2025-04-15 10:49:16 -03:00
|
|
|
page_title = _('Console') + ': ' + _('Invalid command')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = None
|
|
|
|
command_output = ''
|
|
|
|
command_success = False
|
|
|
|
|
|
|
|
if requested_command == 'ping' or requested_command == 'traceroute':
|
|
|
|
if not command_target:
|
2025-04-15 10:49:16 -03:00
|
|
|
command_output = requested_command + ': ' + _('Invalid target')
|
2024-02-14 16:36:01 -03:00
|
|
|
bash_command = None
|
|
|
|
command_success = False
|
2025-01-21 13:22:18 -03:00
|
|
|
|
|
|
|
if user_acl.enable_enhanced_filter and requested_command == 'wgshow':
|
2025-04-15 10:49:16 -03:00
|
|
|
command_output = _('Enhanced filter is enabled. This command is not available.')
|
2025-01-21 13:22:18 -03:00
|
|
|
bash_command = None
|
|
|
|
command_success = False
|
|
|
|
else:
|
|
|
|
if bash_command:
|
|
|
|
try:
|
|
|
|
command_output = subprocess.check_output(bash_command, stderr=subprocess.STDOUT).decode('utf-8')
|
|
|
|
command_success = True
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
command_output = e.output.decode('utf-8')
|
|
|
|
command_success = False
|
2024-02-14 16:36:01 -03:00
|
|
|
|
2025-03-31 13:41:59 -03:00
|
|
|
context = {'page_title': page_title, 'command_output': command_output, 'command_success': command_success}
|
2024-02-14 16:36:01 -03:00
|
|
|
return render(request, 'console/console.html', context)
|