NatLee / telepy

A Django-based web application for managing and monitoring the reverse SSH tunnels or jump servers.
MIT License
6 stars 1 forks source link

[Feature] script template #28

Closed h-alice closed 6 months ago

h-alice commented 6 months ago

I simplify the script generator of src/tunnels/views.py

By putting the script in an isolated templates folder.

Here is the rendered results from test, please help me review them.

#!/bin/bash

echo "[+] Script start"

autossh \\
-M 6769 \\
-o "ServerAliveInterval 30" \\
-o "ServerAliveCountMax 3" \\
-o "StrictHostKeyChecking=no" \\
-o "UserKnownHostsFile=/dev/null" \\
-p 9000 \\
-NR '*:8000:localhost:22' \\
telepy@example.com
# PowerShell script

echo "[+] Script start"

### Configuration

# The {server_domain} is a placeholder for template rendering.
$sshUserHost = "telepy@example.com"    

# The {reverse_port} and 22 are placeholders for template rendering.
$sshRemoteForward = "*:8000:localhost:22"

# The {reverse_server_ssh_port} is a placeholder for template rendering.
$reverseServerSSHPort = "9000"

$sshOptions = "-o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o StrictHostKeyChecking=no"
$sshCommand = "ssh $sshOptions -p $reverseServerSSHPort -NR $sshRemoteForward $sshUserHost"

# Add-Type for PowerManagement to prevent sleep
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class PowerManagement {{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern uint SetThreadExecutionState(uint esFlags);
    public const uint ES_CONTINUOUS = 0x80000000;
    public const uint ES_SYSTEM_REQUIRED = 0x00000001;
    public const uint ES_DISPLAY_REQUIRED = 0x00000002;
}}
"@

# Function to write messages with timestamp
function Write-TimestampedMessage {{
    param([string]$Message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    echo "[$timestamp] $Message"
}}

# Function to enable or disable sleep prevention
function Prevent-Sleep {{
    param([bool]$Enable)
    if ($Enable) {{
        [PowerManagement]::SetThreadExecutionState([PowerManagement]::ES_CONTINUOUS -bor [PowerManagement]::ES_SYSTEM_REQUIRED -bor [PowerManagement]::ES_DISPLAY_REQUIRED)
        Write-TimestampedMessage "Sleep prevention activated."
    }} else {{
        [PowerManagement]::SetThreadExecutionState([PowerManagement]::ES_CONTINUOUS)
        Write-TimestampedMessage "Sleep prevention deactivated."
    }}
}}

# Prevent sleep initially
Prevent-Sleep -Enable $true

try {{
    while ($true) {{
        Write-TimestampedMessage "Starting SSH Reverse Tunnel."
        try {{
            Invoke-Expression $sshCommand
        }} catch {{
            Write-TimestampedMessage "Error executing SSH command: $_"
        }}
        Write-TimestampedMessage "SSH command exited. Restarting in 5 seconds..."
        Start-Sleep -Seconds 5
    }}
}} finally {{
    Prevent-Sleep -Enable $false
    Write-TimestampedMessage "Sleep prevention disabled."
    echo "[+] Script end"
    Write-Host "Press any key to continue..."
    $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}}