website-debug/TestURL.ps1

150 lines
6.2 KiB
PowerShell

# Check if the URL is passed as a parameter
param (
[string]$url
)
if (-not $url) {
Write-Error "Please provide a URL as a parameter."
exit 1
}
# Define the servers to send logs and notifications
$hastebinUrl = "https://haste.nixc.us/documents"
$ntfyServer = "https://ntfy.nixc.us/bnHobG80sO5ZF4SB"
$ntfyCredentials = "admin:tiZcgc8Waf9mg5A4n2aJ4Qk2RyYnwEHDT"
# Function to perform the URL and network tests
function Test-Url {
param ($url)
$result = @{
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") # Explicitly convert to string format to avoid epoch time
Url = $url.Trim()
StatusCode = ""
ResponseTime = ""
Error = ""
PingIPv4 = ""
PingIPv6 = ""
Traceroute = ""
NsLookupDefault = ""
NsLookupCloudflare = ""
NsLookupGoogle = ""
NsLookupIBM = ""
NameserverDefault = ""
}
# Test HTTP status and response time
Write-Host "Starting HTTP status check..."
try {
$startTime = Get-Date
$response = Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 10
$endTime = Get-Date
$result.StatusCode = $response.StatusCode
$result.ResponseTime = [math]::Round(($endTime - $startTime).TotalMilliseconds, 2) # Time in milliseconds
Write-Host "HTTP status check completed."
} catch {
$result.Error = $_.Exception.Message.Trim()
Write-Host "HTTP status check failed: $($_.Exception.Message)"
}
# Perform Ping test for IPv4
Write-Host "Starting Ping test for IPv4..."
try {
$pingIPv4 = Test-Connection -ComputerName $url -Count 4 -ErrorAction Stop
$result.PingIPv4 = ($pingIPv4 | Select-Object -First 1).Address.ToString().Trim() + ": " + [math]::Round(($pingIPv4 | Measure-Object ResponseTime -Average).Average, 2) + " ms avg"
Write-Host "Ping test for IPv4 completed."
} catch {
$result.PingIPv4 = "Ping (IPv4) failed: " + $_.Exception.Message.Trim()
Write-Host "Ping test for IPv4 failed: $($_.Exception.Message)"
}
# Check if IPv6 is available and perform Ping test for IPv6
Write-Host "Checking IPv6 availability..."
try {
$pingIPv6Available = Test-Connection -ComputerName $url -Count 1 -ErrorAction Stop -IPv6
Write-Host "IPv6 is available, starting Ping test for IPv6..."
try {
$pingIPv6 = Test-Connection -ComputerName $url -Count 4 -ErrorAction Stop -IPv6
$result.PingIPv6 = ($pingIPv6 | Select-Object -First 1).Address.ToString().Trim() + ": " + [math]::Round(($pingIPv6 | Measure-Object ResponseTime -Average).Average, 2) + " ms avg"
Write-Host "Ping test for IPv6 completed."
} catch {
$result.PingIPv6 = "Ping (IPv6) failed: " + $_.Exception.Message.Trim()
Write-Host "Ping test for IPv6 failed: $($_.Exception.Message)"
}
} catch {
$result.PingIPv6 = "IPv6 not available"
Write-Host "IPv6 is not available."
}
# Perform Traceroute
Write-Host "Starting Traceroute..."
try {
$traceroute = tracert.exe -d $url 2>&1 | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
$result.Traceroute = ($traceroute -join " ") -replace '\s+', ' '
Write-Host "Traceroute completed."
} catch {
$result.Traceroute = "Traceroute failed: " + $_.Exception.Message.Trim()
Write-Host "Traceroute failed: $($_.Exception.Message)"
}
# Function to perform nslookup with a specific DNS server
function Perform-NsLookup {
param ($dnsServer, $label)
try {
$outputFile = "$label.txt"
$process = Start-Process nslookup.exe -ArgumentList "$url $dnsServer" -NoNewWindow -RedirectStandardOutput $outputFile -PassThru
$process.WaitForExit()
$nslookup = Get-Content $outputFile | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
# Extract the nameserver information if using the default
if ($label -eq "Default") {
$serverLine = $nslookup | Select-String -Pattern '^Server:'
$addressLine = $nslookup | Select-String -Pattern '^Address:'
$result.NameserverDefault = "Server: $($serverLine -replace 'Server:\s*','') Address: $($addressLine -replace 'Address:\s*','')"
}
$result["NsLookup$label"] = ($nslookup -join " ") -replace '\s+', ' '
Remove-Item $outputFile -Force -ErrorAction SilentlyContinue # Clean up output file
Write-Host "NSLookup ($label) completed."
} catch {
$result["NsLookup$label"] = "NSLookup ($label) failed: " + $_.Exception.Message.Trim()
Write-Host "NSLookup ($label) failed: $($_.Exception.Message)"
}
}
# Perform nslookup with default DNS, Cloudflare, Google, and IBM DNS servers
Perform-NsLookup "Default"
Perform-NsLookup "1.1.1.1" "Cloudflare"
Perform-NsLookup "8.8.8.8" "Google"
Perform-NsLookup "9.9.9.9" "IBM"
return $result
}
# Test the URL and save the result as a log
Write-Host "Starting URL test..."
$log = Test-Url -url $url | ConvertTo-Json -Depth 10
Write-Host "URL test completed. Sending results to Hastebin..."
# Send the log to Hastebin server
try {
$prettyLog = $log -replace '\n', ' ' # Replace newline characters with spaces
$hasteResponse = Invoke-RestMethod -Uri $hastebinUrl -Method Post -Body $prettyLog -ContentType "application/json"
$hasteKey = $hasteResponse.key
$hasteLink = "https://haste.nixc.us/" + $hasteKey
Write-Host "Results successfully sent to Hastebin."
} catch {
Write-Error "Failed to send log to Hastebin: $_"
exit 1
}
# Send a notification to ntfy server with the Hastebin link and test settings
Write-Host "Sending notification to ntfy server..."
try {
$ntfyMessage = "URL test completed. Log available at: $hasteLink`nTested URL: $url"
$ntfyResponse = Invoke-RestMethod -Uri $ntfyServer -Method Post -Body $ntfyMessage -Headers @{ Authorization = "Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($ntfyCredentials)))" }
Write-Host "Notification sent to ntfy."
} catch {
Write-Error "Failed to send notification to ntfy: $_"
exit 1
}