Skip to content

Commit

Permalink
Added disable Web
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Dec 13, 2024
1 parent 99a2491 commit 3c9b2b9
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/Pode.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
'New-PodeMiddleware',
'Add-PodeBodyParser',
'Remove-PodeBodyParser',
'Test-PodeMiddleware',

# sessions
'Enable-PodeSessionMiddleware',
Expand Down
22 changes: 16 additions & 6 deletions src/Private/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,14 @@ function New-PodeContext {
$ctx.Server.OpenAPI = Initialize-PodeOpenApiTable -DefaultDefinitionTag $ctx.Server.Web.OpenApi.DefaultDefinitionTag

$ctx.Server.AllowedActions = @{
Suspend = $true
Restart = $true
Timeout = @{
Suspend = $true
Restart = $true
Disable = $true
DisableSettings = @{
ServiceRecoveryTime = 3600
MiddlewareName = '__Pode_Midleware_Code_503'
}
Timeout = @{
Suspend = 30
Resume = 30
}
Expand Down Expand Up @@ -927,9 +932,14 @@ function Set-PodeServerConfiguration {
}

$Context.Server.AllowedActions = @{
Suspend = [bool](Protect-PodeValue -Value $Configuration.AllowedActions.Suspend -Default $Context.Server.AllowedActions.Suspend)
Restart = [bool](Protect-PodeValue -Value $Configuration.AllowedActions.Restart -Default $Context.Server.AllowedActions.Restart)
Timeout = @{
Suspend = [bool](Protect-PodeValue -Value $Configuration.AllowedActions.Suspend -Default $Context.Server.AllowedActions.Suspend)
Restart = [bool](Protect-PodeValue -Value $Configuration.AllowedActions.Restart -Default $Context.Server.AllowedActions.Restart)
Disable = [bool](Protect-PodeValue -Value $Configuration.AllowedActions.Disable -Default $Context.Server.AllowedActions.Disable)
DisableSettings = @{
ServiceRecoveryTime = [int](Protect-PodeValue -Value $Configuration.AllowedActions.DisableSettings.ServiceRecoveryTime -Default $Context.Server.AllowedActions.DisableSettings.ServiceRecoveryTime)
MiddlewareName = (Protect-PodeValue -Value $Configuration.AllowedActions.DisableSettings.MiddlewareName -Default $Context.Server.AllowedActions.DisableSettings.MiddlewareName)
}
Timeout = @{
Suspend = [int](Protect-PodeValue -Value $Configuration.AllowedActions.Timeout.Suspend -Default $Context.Server.AllowedActions.Timeout.Suspend)
Resume = [int](Protect-PodeValue -Value $Configuration.AllowedActions.Timeout.Resume -Default $Context.Server.AllowedActions.Timeout.Resume)
}
Expand Down
7 changes: 5 additions & 2 deletions src/Private/Endpoints.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ function Show-PodeEndPointConsoleInfo {

# Write a horizontal divider line to the console.
Write-PodeHostDivider -Force $true

$disabled = ! (Test-PodeServerIsEnabled)
# Display each endpoint with extracted protocol
$PodeContext.Server.EndpointsInfo | ForEach-Object {
# Extract protocol from the URL
Expand All @@ -465,10 +465,13 @@ function Show-PodeEndPointConsoleInfo {
$flags += 'DualMode'
}

if ($disabled -and ('HTTP', 'HTTPS' -contains $protocol)) {
$flags += 'Disabled'
}
$flagString = if ($flags.Length -gt 0) { "[$($flags -join ',')]" } else { [string]::Empty }

# Display endpoint details
Write-PodeHost " - $protocolLabel : $($_.Url) $flagString" -ForegroundColor $endpointsColor -Force:$Force
Write-PodeHost " - $protocolLabel : $($_.Url) `t$flagString" -ForegroundColor $endpointsColor -Force:$Force
}

# Footer
Expand Down
13 changes: 13 additions & 0 deletions src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,19 @@ function Test-PodeSuspendPressed {
}


function Test-PodeDisablePressed {
param(
[Parameter()]
$Key = $null
)

if ($PodeContext.Server.Console.DisableConsoleInput ) {
return $false
}

return (Test-PodeKeyPressed -Key $Key -Character 'd')
}


<#
.SYNOPSIS
Expand Down
102 changes: 83 additions & 19 deletions src/Private/Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ function Show-PodeConsoleInfo {
$ClearHost,

[switch]
$Force
$Force,

[switch]
$ShowTopSeparator
)


Expand Down Expand Up @@ -229,9 +232,6 @@ function Show-PodeConsoleInfo {
[System.ConsoleColor]::Gray
}




if ($PodeContext.Server.Console.Quiet -and !$Force) {
return
}
Expand All @@ -244,7 +244,7 @@ function Show-PodeConsoleInfo {
$noHeaderNewLine = $false
$ctrlH = !$showHelp
$footerSeparator = $false
$topSeparator = $false
$topSeparator = $ShowTopSeparator.IsPresent
$headerSeparator = $true
break
}
Expand Down Expand Up @@ -299,7 +299,7 @@ function Show-PodeConsoleInfo {
$noHeaderNewLine = $false
$ctrlH = !$showHelp
$footerSeparator = $false
$topSeparator = $false
$topSeparator = $ShowTopSeparator.IsPresent
$headerSeparator = $true
break
}
Expand All @@ -321,7 +321,7 @@ function Show-PodeConsoleInfo {
$noHeaderNewLine = $false
$ctrlH = $false
$footerSeparator = $false
$topSeparator = $false
$topSeparator = $ShowTopSeparator.IsPresent
$headerSeparator = $true
break
}
Expand All @@ -333,7 +333,7 @@ function Show-PodeConsoleInfo {
if ($ClearHost -or $PodeContext.Server.Console.ClearHost) {
Clear-Host
}
elseif ($topSeparator) {
elseif ($topSeparator ) {
# Write a horizontal divider line to the console.
Write-PodeHostDivider -Force $true
}
Expand Down Expand Up @@ -368,6 +368,7 @@ function Show-PodeConsoleInfo {
$Podelocale.SuspendServerMessage
}

$enableOrDisable = if (Test-PodeServerIsEnabled) { 'Disable Server' } else { 'Enable Server' }
# Print help section
Write-PodeHost $Podelocale.serverControlCommandsTitle -ForegroundColor $helpHeaderColor -Force:$Force

Expand All @@ -376,14 +377,25 @@ function Show-PodeConsoleInfo {
Write-PodeHostDivider -Force $true
}

Write-PodeHost ' Ctrl+C : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$($Podelocale.GracefullyTerminateMessage)" -ForegroundColor $helpDescriptionColor -Force:$Force
if (!$PodeContext.Server.Console.DisableTermination) {
Write-PodeHost ' Ctrl+C : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$($Podelocale.GracefullyTerminateMessage)" -ForegroundColor $helpDescriptionColor -Force:$Force
}

if ($PodeContext.Server.AllowedActions.Restart) {
Write-PodeHost ' Ctrl+R : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$($Podelocale.RestartServerMessage)" -ForegroundColor $helpDescriptionColor -Force:$Force
}

Write-PodeHost ' Ctrl+R : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$($Podelocale.RestartServerMessage)" -ForegroundColor $helpDescriptionColor -Force:$Force
if ($PodeContext.Server.AllowedActions.Suspend) {
Write-PodeHost ' Ctrl+U : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$resumeOrSuspend" -ForegroundColor $helpDescriptionColor -Force:$Force
}

Write-PodeHost ' Ctrl+U : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$resumeOrSuspend" -ForegroundColor $helpDescriptionColor -Force:$Force
if (($serverState -eq 'Running') -and $PodeContext.Server.AllowedActions.Disable) {
Write-PodeHost ' Ctrl+D : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$enableOrDisable" -ForegroundColor $helpDescriptionColor -Force:$Force
}

Write-PodeHost ' Ctrl+H : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost 'Hide Help' -ForegroundColor $helpDescriptionColor -Force:$Force
Expand All @@ -395,12 +407,14 @@ function Show-PodeConsoleInfo {

Write-PodeHost ' ----' -ForegroundColor $helpDividerColor -Force:$Force

Write-PodeHost ' Ctrl+E : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$(if ($PodeContext.Server.Console.ShowEndpoints) { 'Hide' } else { 'Show' }) Endpoints" -ForegroundColor $helpDescriptionColor -Force:$Force
if ($serverState -eq 'Running') {
Write-PodeHost ' Ctrl+E : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$(if ($PodeContext.Server.Console.ShowEndpoints) { 'Hide' } else { 'Show' }) Endpoints" -ForegroundColor $helpDescriptionColor -Force:$Force

if (Test-PodeOAEnabled) {
Write-PodeHost ' Ctrl+O : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$(if ($PodeContext.Server.Console.ShowOpenAPI) { 'Hide' } else { 'Show' }) OpenAPI" -ForegroundColor $helpDescriptionColor -Force:$Force
if (Test-PodeOAEnabled) {
Write-PodeHost ' Ctrl+O : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Write-PodeHost "$(if ($PodeContext.Server.Console.ShowOpenAPI) { 'Hide' } else { 'Show' }) OpenAPI" -ForegroundColor $helpDescriptionColor -Force:$Force
}
}

Write-PodeHost ' Ctrl+L : ' -ForegroundColor $helpKeyColor -NoNewLine -Force:$Force
Expand Down Expand Up @@ -793,3 +807,53 @@ function Resume-PodeServerInternal {



<#
.SYNOPSIS
Enables new requests by removing the middleware that blocks requests when the Pode Watchdog service is active.
.DESCRIPTION
This function checks if the middleware associated with the Pode Watchdog client is present, and if so, it removes it to allow new requests.
This effectively re-enables access to the service by removing the request blocking.
.NOTES
This function is used internally to manage Watchdog monitoring and may change in future releases of Pode.
#>
function Enable-PodeServer {

# Check if the Watchdog middleware exists and remove it if found to allow new requests
if (! (Test-PodeServerIsEnabled)) {
Remove-PodeMiddleware -Name $PodeContext.Server.AllowedActions.DisableSettings.MiddlewareName
}
}

<#
.SYNOPSIS
Disables new requests by adding middleware that blocks incoming requests when the Pode Watchdog service is active.
.DESCRIPTION
This function adds middleware to the Pode server to block new incoming requests while the Pode Watchdog client is active.
It responds to all new requests with a 503 Service Unavailable status and sets a 'Retry-After' header, indicating when the service will be available again.
.NOTES
This function is used internally to manage Watchdog monitoring and may change in future releases of Pode.
#>
function Disable-PodeServer {

if (Test-PodeServerIsEnabled) {
# Add middleware to block new requests and respond with 503 Service Unavailable
Add-PodeMiddleware -Name $PodeContext.Server.AllowedActions.DisableSettings.MiddlewareName -ScriptBlock {
# Set HTTP response header for retrying after a certain time (RFC7231)
Set-PodeHeader -Name 'Retry-After' -Value $PodeContext.Server.AllowedActions.DisableSettings.ServiceRecoveryTime

# Set HTTP status to 503 Service Unavailable
Set-PodeResponseStatus -Code 503

# Stop further processing
return $false
}
}
}

function Test-PodeServerIsEnabled {
return !(Test-PodeMiddleware -Name $PodeContext.Server.AllowedActions.DisableSettings.MiddlewareName)
}
15 changes: 12 additions & 3 deletions src/Public/Core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,17 @@ function Start-PodeServer {
elseif ( Test-PodeHelpPressed -Key $key) {
Clear-PodeKeyPressed
$PodeContext.Server.Console.ShowHelp = !$PodeContext.Server.Console.ShowHelp
Show-PodeConsoleInfo -ClearHost
Show-PodeConsoleInfo -ShowTopSeparator
}
elseif ( Test-PodeOpenAPIPressed -Key $key) {
Clear-PodeKeyPressed
$PodeContext.Server.Console.ShowOpenAPI = !$PodeContext.Server.Console.ShowOpenAPI
Show-PodeConsoleInfo -ClearHost
Show-PodeConsoleInfo -ShowTopSeparator
}
elseif ( Test-PodeEndpointsPressed -Key $key) {
Clear-PodeKeyPressed
$PodeContext.Server.Console.ShowEndpoints = !$PodeContext.Server.Console.ShowEndpoints
Show-PodeConsoleInfo -ClearHost
Show-PodeConsoleInfo -ShowTopSeparator
}
elseif ( Test-PodeClearPressed -Key $key) {
Clear-PodeKeyPressed
Expand All @@ -337,6 +337,15 @@ function Start-PodeServer {
$PodeContext.Server.Console.Quiet = !$PodeContext.Server.Console.Quiet
Show-PodeConsoleInfo -ClearHost -Force
}
elseif (( (Get-PodeServerState) -eq 'Running') -and (Test-PodeDisablePressed -Key $key)) {
Clear-PodeKeyPressed
if(Test-PodeServerIsEnabled){
Disable-PodeServer
}else{
Enable-PodeServer
}
Show-PodeConsoleInfo -ShowTopSeparator
}
elseif ( Test-PodeTerminationPressed -Key $key) {
Clear-PodeKeyPressed
break
Expand Down
33 changes: 33 additions & 0 deletions src/Public/Middleware.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,37 @@ function Use-PodeMiddleware {
)

Use-PodeFolder -Path $Path -DefaultPath 'middleware'
}



<#
.SYNOPSIS
Checks if a specific middleware is registered in the Pode server.
.DESCRIPTION
This function verifies whether a middleware with the specified name is registered in the Pode server by checking the `PodeContext.Server.Middleware` collection.
It returns `$true` if the middleware exists, otherwise it returns `$false`.
.PARAMETER Name
The name of the middleware to check for.
.OUTPUTS
[boolean]
Returns $true if the middleware with the specified name is found, otherwise returns $false.
.EXAMPLE
Test-PodeMiddleware -Name 'BlockEverything'
This command checks if a middleware named 'BlockEverything' is registered in the Pode server.
#>
function Test-PodeMiddleware {
param(
[Parameter(Mandatory = $true)]
[string]
$Name
)

# Check if the specified middleware exists in the Pode server's middleware collection
return (($PodeContext.Server.Middleware | Where-Object { $_.Name -ieq $Name } | Measure-Object).Count -gt 0)
}

0 comments on commit 3c9b2b9

Please sign in to comment.