From f012064574aa544a439581f535e139e859977fd9 Mon Sep 17 00:00:00 2001 From: Saksham Singh Date: Wed, 2 Oct 2024 00:50:45 +0530 Subject: [PATCH] SSH server option (#2745) * Squashed commit of the following: commit c674d5eb605b549d8d8b247749954a98197277b4 Author: Saksham Singh Date: Wed Sep 11 19:34:44 2024 +0530 desc update commit 3d8bf2bdc9c382a9f087b9ac6e63cb8604c99698 Author: Saksham Singh Date: Wed Sep 11 19:30:23 2024 +0530 Added Razer Block Added the razer block by chris titus under the Fixes Section * Moved razer-block from features to tweaks * Updated tweaks.json * Added option to enable ssh server under config tab * Revert "Added option to enable ssh server under config tab" This reverts commit ade414284d6e489f5a7a2d858d5abc2b2ebe0f5a. * Added Option to enable ssh server under config tab * updated docs * Removed #2715 from this PR * update variable name * Fixed authorized_keys file creation issue auth key file was not being created due to incomplete file path from the $env:HOMEPATH and also corrected the ssh directory to .ssh --- config/feature.json | 10 ++- docs/userguide.md | 4 + functions/private/Invoke-WinUtilSSHServer.ps1 | 81 +++++++++++++++++++ functions/public/Invoke-WPFButton.ps1 | 1 + 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 functions/private/Invoke-WinUtilSSHServer.ps1 diff --git a/config/feature.json b/config/feature.json index 10835451f3..137cd9ee81 100644 --- a/config/feature.json +++ b/config/feature.json @@ -313,5 +313,13 @@ "Order": "a083_", "Type": "Button", "ButtonWidth": "300" - } + }, + "WPFWinUtilSSHServer": { + "Content": "Enable OpenSSH Server", + "category": "Remote Access", + "panel": "2", + "Order": "a084_", + "Type": "Button", + "ButtonWidth": "300" + }, } diff --git a/docs/userguide.md b/docs/userguide.md index de80eb1900..47a49473d8 100644 --- a/docs/userguide.md +++ b/docs/userguide.md @@ -142,6 +142,10 @@ Open old-school Windows panels directly from WinUtil. Following Panels are avail * System Properties * User Accounts +### Remote Access + +Enables OpenSSH server on your windows machine. + ## Updates --- diff --git a/functions/private/Invoke-WinUtilSSHServer.ps1 b/functions/private/Invoke-WinUtilSSHServer.ps1 new file mode 100644 index 0000000000..7185af7ed3 --- /dev/null +++ b/functions/private/Invoke-WinUtilSSHServer.ps1 @@ -0,0 +1,81 @@ +function Invoke-WinUtilSSHServer { + <# + .SYNOPSIS + Enables OpenSSH server to remote into your windows device + #> + + # Get the latest version of OpenSSH Server + $FeatureName = Get-WindowsCapability -Online | Where-Object { $_.Name -like "OpenSSH.Server*" } + + # Install the OpenSSH Server feature if not already installed + if ($FeatureName.State -ne "Installed") { + Write-Host "Enabling OpenSSH Server" + Add-WindowsCapability -Online -Name $FeatureName.Name + } + + # Sets up the OpenSSH Server service + Write-Host "Starting the services" + Start-Service -Name sshd + Set-Service -Name sshd -StartupType Automatic + + # Sets up the ssh-agent service + Start-Service 'ssh-agent' + Set-Service -Name 'ssh-agent' -StartupType 'Automatic' + + # Confirm the required services are running + $SSHDaemonService = Get-Service -Name sshd + $SSHAgentService = Get-Service -Name 'ssh-agent' + + if ($SSHDaemonService.Status -eq 'Running') { + Write-Host "OpenSSH Server is running." + } else { + try { + Write-Host "OpenSSH Server is not running. Attempting to restart..." + Restart-Service -Name sshd -Force + Write-Host "OpenSSH Server has been restarted successfully." + } catch { + Write-Host "Failed to restart OpenSSH Server: $_" + } + } + if ($SSHAgentService.Status -eq 'Running') { + Write-Host "ssh-agent is running." + } else { + try { + Write-Host "ssh-agent is not running. Attempting to restart..." + Restart-Service -Name sshd -Force + Write-Host "ssh-agent has been restarted successfully." + } catch { + Write-Host "Failed to restart ssh-agent : $_" + } + } + + #Adding Firewall rule for port 22 + Write-Host "Setting up firewall rules" + $firewallRule = (Get-NetFirewallRule -Name 'sshd').Enabled + if ($firewallRule) { + Write-Host "Firewall rule for OpenSSH Server (sshd) already exists." + } else { + New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 + Write-Host "Firewall rule for OpenSSH Server created and enabled." + } + + # Check for the authorized_keys file + $sshFolderPath = "$env:HOMEDRIVE\$env:HOMEPATH\.ssh" + $authorizedKeysPath = "$sshFolderPath\authorized_keys" + + if (-not (Test-Path -Path $sshFolderPath)) { + Write-Host "Creating ssh directory..." + New-Item -Path $sshFolderPath -ItemType Directory -Force + } + + if (-not (Test-Path -Path $authorizedKeysPath)) { + Write-Host "Creating authorized_keys file..." + New-Item -Path $authorizedKeysPath -ItemType File -Force + Write-Host "authorized_keys file created at $authorizedKeysPath." + } else { + Write-Host "authorized_keys file already exists at $authorizedKeysPath." + } + Write-Host "OpenSSH server was successfully enabled." + Write-Host "The config file can be located at C:\ProgramData\ssh\sshd_config " + Write-Host "Add your public keys to this file -> $authorizedKeysPath" +} diff --git a/functions/public/Invoke-WPFButton.ps1 b/functions/public/Invoke-WPFButton.ps1 index 2773ccb571..ee4d0421d5 100644 --- a/functions/public/Invoke-WPFButton.ps1 +++ b/functions/public/Invoke-WPFButton.ps1 @@ -58,5 +58,6 @@ function Invoke-WPFButton { "WPFCloseButton" {Invoke-WPFCloseButton} "MicrowinScratchDirBT" {Invoke-ScratchDialog} "WPFWinUtilPSProfile" {Invoke-WinUtilpsProfile} + "WPFWinUtilSSHServer" {Invoke-WinUtilSSHServer} } }