Skip to content

Commit

Permalink
#52: More tests, and support for tcp/smtp
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Jul 3, 2018
1 parent b83ff4b commit 083c907
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 8 deletions.
3 changes: 3 additions & 0 deletions examples/mail-server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Import-Module "$($path)/src/Pode.psm1" -ErrorAction Stop
# create a server, and start listening on port 25
Server -Smtp {

# allow the local ip
access allow ip 127.0.0.1

# setup an smtp handler
handler 'smtp' {
param($session)
Expand Down
3 changes: 3 additions & 0 deletions examples/tcp-server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Import-Module "$($path)/src/Pode.psm1" -ErrorAction Stop
# create a server, and start listening on port 8999
Server -Tcp -Port 8999 {

# allow the local ip
access allow ip 127.0.0.1

# setup a tcp handler
handler 'tcp' {
param($session)
Expand Down
11 changes: 11 additions & 0 deletions src/Tools/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ function Test-IPAddress
}
}

function ConvertTo-IPEndpoint
{
param (
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
$Endpoint
)

return [System.Net.IPAddress]::Parse(([System.Net.IPEndPoint]$Endpoint).Address.ToString())
}

function Test-IPAddressLocal
{
param (
Expand Down
29 changes: 27 additions & 2 deletions src/Tools/Server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ function Server
-Interval $Interval -ServerRoot $MyInvocation.PSScriptRoot -ServerType $ServerType `
-DisableLogging:$DisableLogging -FileMonitor:$FileMonitor

# set ad efault port for the server type
Set-PodePortForServerType

# parse ip:port to listen on (if both have been supplied)
if (!(Test-Empty $IP) -or $Port -gt 0) {
listen -IPPort "$($IP):$($Port)" -Type $PodeSession.ServerType
if (!(Test-Empty $IP) -or $PodeSession.IP.Port -gt 0) {
listen -IPPort "$($IP):$($PodeSession.IP.Port)" -Type $PodeSession.ServerType
}

# set it so ctrl-c can terminate
Expand Down Expand Up @@ -246,4 +249,26 @@ function Get-PodeServerType
}

return 'SCRIPT'
}

function Set-PodePortForServerType
{
if ($PodeSession.IP.Port -gt 0) {
return
}

switch ($PodeSession.ServerType.ToUpperInvariant())
{
'SMTP' {
$PodeSession.IP.Port = 25
}

'HTTP' {
$PodeSession.IP.Port = 8080
}

'HTTPS' {
$PodeSession.IP.Port = 8443
}
}
}
18 changes: 15 additions & 3 deletions src/Tools/SmtpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,22 @@ function Start-SmtpServer
{
$task = $listener.AcceptTcpClientAsync()
$task.Wait($PodeSession.Tokens.Cancellation.Token)
$client = $task.Result

# ensure the request ip is allowed
if (!(Test-IPAccess -IP (ConvertTo-IPEndpoint -Endpoint $client.Client.RemoteEndPoint))) {
try {
$client.Close()
$client.Dispose()
} catch { }
}

$PodeSession.Tcp.Client = $task.Result
$PodeSession.Smtp = @{}
Invoke-ScriptBlock -ScriptBlock $process
# deal with smtp call
else {
$PodeSession.Tcp.Client = $client
$PodeSession.Smtp = @{}
Invoke-ScriptBlock -ScriptBlock $process
}
}
}
catch [System.OperationCanceledException] {
Expand Down
11 changes: 8 additions & 3 deletions src/Tools/TcpServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ function Start-TcpServer
{
$task = $listener.AcceptTcpClientAsync()
$task.Wait($PodeSession.Tokens.Cancellation.Token)
$client = $task.Result

$PodeSession.Tcp.Client = $client
$PodeSession.Tcp.Lockable = $PodeSession.Lockable
Invoke-ScriptBlock -ScriptBlock (Get-PodeTcpHandler -Type 'TCP') -Arguments $PodeSession.Tcp -Scoped
# ensure the request ip is allowed and deal with the tcp call
if (Test-IPAccess -IP (ConvertTo-IPEndpoint -Endpoint $client.Client.RemoteEndPoint)) {
$PodeSession.Tcp.Client = $client
$PodeSession.Tcp.Lockable = $PodeSession.Lockable
Invoke-ScriptBlock -ScriptBlock (Get-PodeTcpHandler -Type 'TCP') -Arguments $PodeSession.Tcp -Scoped
}

# close the connection
if ($client -ne $null -and $client.Connected) {
try {
$client.Close()
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/Tools/Server.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,44 @@ Describe 'Get-PodeServerType' {
Get-PodeServerType | Should Be 'SCRIPT'
}
}
}

Describe 'Set-PodePortForServerType' {
Context 'Valid parameters supplied' {
It 'Uses 25 for smtp' {
$PodeSession = @{ 'IP' = @{ 'Port' = 0 }; 'ServerType' = 'SMTP' }
Set-PodePortForServerType
$PodeSession.IP.Port | Should Be 25
}

It 'Uses 8080 for http' {
$PodeSession = @{ 'IP' = @{ 'Port' = 0 }; 'ServerType' = 'HTTP' }
Set-PodePortForServerType
$PodeSession.IP.Port | Should Be 8080
}

It 'Uses 8443 for https' {
$PodeSession = @{ 'IP' = @{ 'Port' = 0 }; 'ServerType' = 'HTTPS' }
Set-PodePortForServerType
$PodeSession.IP.Port | Should Be 8443
}

It 'Uses 0 for tcp' {
$PodeSession = @{ 'IP' = @{ 'Port' = 0 }; 'ServerType' = 'TCP' }
Set-PodePortForServerType
$PodeSession.IP.Port | Should Be 0
}

It 'Uses 0 for script' {
$PodeSession = @{ 'IP' = @{ 'Port' = 0 }; 'ServerType' = 'SCRIPT' }
Set-PodePortForServerType
$PodeSession.IP.Port | Should Be 0
}

It 'Uses 0 for service' {
$PodeSession = @{ 'IP' = @{ 'Port' = 0 }; 'ServerType' = 'SERVICE' }
Set-PodePortForServerType
$PodeSession.IP.Port | Should Be 0
}
}
}

0 comments on commit 083c907

Please sign in to comment.