#
# TcpCheck.ps1
#

. '.\Common.ps1'
$endPoints = New-Object System.Collections.Generic.List[Object]
Write-Output "`r`n`r`nTCP CONNECTIVITY TEST:`r`n"
$defaultPorts = @(80,443)
$nonDefaultPortMap = @{
	'204.26.248.58' = @(53310, 53330) #Shares
	
	'57.14.12.254' = @(102) #Unimatic

	'10.232.100.72' = @(50002) #GateReader alternative
	'57.228.112.12' = @(50002) #GateReader primary

	'57.14.12.21' = @(443)
	'57.14.12.24' = @(443)
	'57.14.12.20' = @(443)
	'57.14.13.142' = @(443)
	'57.14.13.143' = @(443)
	'57.14.13.144' = @(443)
	'57.14.13.24' = @(443)

	'57.14.12.122' = @(80)
};
if ($null -ne $ipHashTable) {
	foreach ($ip in $ipHashTable.Keys)
	{
		$ports = @()
		if ($nonDefaultPortMap.ContainsKey($ip)) {
			$ports = $nonDefaultPortMap[$ip]
		}
		else {
			$ports = $defaultPorts
		}
		foreach ($port in $ports) {	
			$thisEndPoint = New-Object PSObject -Property @{
				Host = [string]$ip
				Port = [int]$port
			}
			$endPoints.Add($thisEndPoint);
		}
	}
}

# REMOVED: -Parallel only works in newer powershell versions
# # Asynchronous port check
# $isPortOpenDef = $function:IsPortOpen.ToString()
# $endPoints | ForEach-Object -Parallel {
# 	$function:IsPortOpen = $using:isPortOpenDef
# 	IsPortOpen -hostName $_.Host -port $_.Port
# }

# Synchronous port check
foreach ($endPoint in $endPoints) {
	IsPortOpen -hostName $endPoint.Host -port $endPoint.Port
}