-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-redirects.ps1
More file actions
369 lines (311 loc) · 10.4 KB
/
check-redirects.ps1
File metadata and controls
369 lines (311 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# check-redirects.ps1
# PowerShell 7+ recommended
# WHY-FILE: Resolve and report HTTP redirect behavior for domain portfolios using a reliable HttpClient pipeline.
param(
[ValidateSet("se", "ci", "all", "custom")]
[string]$DomainProfile = "all",
# Used only when DomainProfile = custom
[string[]]$Domains = @(),
# Used for DomainProfiles, or can be overridden
[string]$Canonical = "",
# If empty, script picks a default based on DomainProfile
[string]$OutCsv = "",
[int]$MaxHops = 12,
[int]$TimeoutSec = 20
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# WHY: Progress output can materially slow HTTP loops; enable by changing to "Continue" if desired.
$ProgressPreference = "SilentlyContinue"
Write-Host ("PowerShell: {0}" -f $PSVersionTable.PSVersion)
Write-Host ("Script: {0}" -f $PSCommandPath)
Write-Host ("Params: Canonical='{0}' OutCsv='{1}' DomainProfile='{2}'" -f $Canonical, $OutCsv, $DomainProfile)
Write-Host ""
function Get-HostAddresses([string]$hostname) {
try {
$addrs = [System.Net.Dns]::GetHostAddresses($hostname)
return $addrs | ForEach-Object { $_.IPAddressToString }
}
catch {
return @()
}
}
function Test-TcpPort([string]$hostname, [int]$port, [int]$timeoutSec) {
$client = New-Object System.Net.Sockets.TcpClient
try {
$iar = $client.BeginConnect($hostname, $port, $null, $null)
$ok = $iar.AsyncWaitHandle.WaitOne([TimeSpan]::FromSeconds($timeoutSec))
if (-not $ok) { return $false }
$client.EndConnect($iar) | Out-Null
return $true
}
catch {
return $false
}
finally {
$client.Close()
}
}
function Get-DomainProfileConfig([string]$p) {
switch ($p) {
"se" {
return @{
Name = "se"
Canonical = "https://structuralexplainability.org"
Domains = @(
"structural-explainability.com",
"structural-explainability.org",
"structuralexplainability.com",
"structuralexplainability.org"
)
OutCsv = "./redirect-report-se.csv"
}
}
"ci" {
return @{
Name = "ci"
Canonical = "https://civicinterconnect.org"
Domains = @(
"civicinterconnect.com",
"civicinterconnect.org",
"civicinterconnect.net"
)
OutCsv = "./redirect-report-ci.csv"
}
}
"all" {
return @{
Name = "all"
Canonical = ""
Domains = @()
OutCsv = ""
}
}
"custom" {
return @{
Name = "custom"
Canonical = ""
Domains = @()
OutCsv = "./redirect-report-custom.csv"
}
}
default {
throw ("Unknown DomainProfile '{0}'" -f $p)
}
}
}
function Format-Url-Normal([string]$u) {
# WHY: Normalize trivial variants so canonical comparisons are stable.
try {
$uri = [Uri]$u
$scheme = $uri.Scheme.ToLowerInvariant()
$hostname = $uri.Host.ToLowerInvariant()
# Preserve explicit port only if non-default.
$portPart = ""
if (-not $uri.IsDefaultPort) { $portPart = ":" + $uri.Port }
$path = $uri.AbsolutePath
if ([string]::IsNullOrEmpty($path)) { $path = "/" }
# Normalize: treat empty path and "/" as equivalent for canonical matching.
if ($path -eq "/") {
# Keep query/fragment only if present.
if ([string]::IsNullOrEmpty($uri.Query) -and [string]::IsNullOrEmpty($uri.Fragment)) {
return ("{0}://{1}{2}" -f $scheme, $hostname, $portPart)
}
}
return $uri.AbsoluteUri.Trim()
}
catch {
return $u.Trim()
}
}
function Resolve-AbsoluteUrl([string]$baseUrl, [string]$location) {
try {
$base = [Uri]$baseUrl
$target = [Uri]::new($base, $location)
return $target.AbsoluteUri
}
catch {
return $location
}
}
function New-HttpClient([int]$timeoutSec) {
$handler = New-Object System.Net.Http.HttpClientHandler
$handler.AllowAutoRedirect = $false
# NOTE: If you ever need to tolerate broken TLS for a specific domain portfolio,
# you can add a ServerCertificateCustomValidationCallback here. Do not do so by default.
$client = New-Object System.Net.Http.HttpClient($handler)
$client.Timeout = [TimeSpan]::FromSeconds($timeoutSec)
# WHY: Identify the tool; some redirect providers behave differently by UA.
$client.DefaultRequestHeaders.UserAgent.ParseAdd("redirect-check/1.0")
# WHY: Range keeps payload small when servers ignore HEAD.
$client.DefaultRequestHeaders.TryAddWithoutValidation("Range", "bytes=0-0") | Out-Null
return $client
}
function Get-RedirectStep([System.Net.Http.HttpClient]$client, [string]$url) {
$methods = @([System.Net.Http.HttpMethod]::Head, [System.Net.Http.HttpMethod]::Get)
foreach ($m in $methods) {
$req = New-Object System.Net.Http.HttpRequestMessage($m, $url)
try {
$resp = $client.Send($req, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead)
$status = [int]$resp.StatusCode
$loc = $null
if ($resp.Headers.Location) { $loc = $resp.Headers.Location.ToString() }
$resp.Dispose()
$req.Dispose()
return [pscustomobject]@{
StatusCode = $status
Location = $loc
Error = $null
}
}
catch [System.Threading.Tasks.TaskCanceledException] {
$req.Dispose()
# try next method
continue
}
catch {
$req.Dispose()
return [pscustomobject]@{
StatusCode = $null
Location = $null
Error = $_.Exception.Message
}
}
}
return [pscustomobject]@{
StatusCode = $null
Location = $null
Error = "Timeout"
}
}
function Resolve-RedirectChain([System.Net.Http.HttpClient]$client, [string]$startUrl, [int]$maxHops) {
$current = $startUrl
$hops = 0
$step = Get-RedirectStep -client $client -url $current
$startCode = $step.StatusCode
$startLoc = $step.Location
$startErr = $step.Error
while ($hops -lt $maxHops) {
if ($null -eq $step.StatusCode) { break }
$status = [int]$step.StatusCode
if ($status -ge 300 -and $status -lt 400 -and -not [string]::IsNullOrEmpty($step.Location)) {
$current = Resolve-AbsoluteUrl -baseUrl $current -location $step.Location
$hops += 1
$step = Get-RedirectStep -client $client -url $current
continue
}
break
}
return [pscustomobject]@{
StartUrl = $startUrl
StartCode = $startCode
StartLoc = $startLoc
StartErr = $startErr
FinalUrl = $current
HopCount = $hops
}
}
function Invoke-RedirectReport([string[]]$domains, [string]$canonical, [string]$outCsv, [int]$maxHops, [int]$timeoutSec) {
if (-not $domains -or $domains.Count -eq 0) {
Write-Host "No domains provided for report."
return
}
if ([string]::IsNullOrWhiteSpace($canonical)) {
Write-Host "Canonical URL is required for report."
return
}
$schemes = @("http", "https")
$hostsPrefix = @("", "www.")
$paths = @("", "/")
$tests = New-Object System.Collections.Generic.List[string]
foreach ($d in $domains) {
foreach ($s in $schemes) {
foreach ($p in $hostsPrefix) {
foreach ($path in $paths) {
$tests.Add(("{0}://{1}{2}{3}" -f $s, $p, $d, $path))
}
}
}
}
Write-Host ("Domains count: {0}" -f $domains.Count)
Write-Host ("Domains: {0}" -f ($domains -join ", "))
Write-Host ("URLs to test: {0}" -f $tests.Count)
Write-Host ""
$canonicalNorm = Format-Url-Normal $canonical
$rows = New-Object System.Collections.Generic.List[object]
$client = New-HttpClient -timeoutSec $timeoutSec
try {
$i = 0
foreach ($t in $tests) {
$i++
Write-Host ("[{0}/{1}] {2}" -f $i, $tests.Count, $t)
# === Pre-flight HTTPS diagnostics (before redirect resolution) ===
try {
$uri = [Uri]$t
if ($uri.Scheme -eq "https") {
$hostname = $uri.Host
$ips = Get-HostAddresses -host $hostname
if ($ips.Count -gt 0) {
Write-Host (" DNS: {0}" -f ($ips -join ", "))
}
else {
Write-Host " DNS: (no addresses resolved)"
}
$tcpOk = Test-TcpPort -host $hostname -port 443 -timeoutSec 3
Write-Host (" TCP 443: {0}" -f ($(if ($tcpOk) { "ok" } else { "fail" })))
}
}
catch {
# intentionally ignore diagnostic failures
}
# === Redirect resolution ===
$r = Resolve-RedirectChain -client $client -startUrl $t -maxHops $maxHops
if ($null -eq $r.StartCode) {
Write-Host (" -> ERROR: {0}" -f $r.StartErr)
}
else {
Write-Host (" -> {0} hops={1} final={2}" -f $r.StartCode, $r.HopCount, $r.FinalUrl)
}
$finalNorm = Format-Url-Normal $r.FinalUrl
$match = ($finalNorm -eq $canonicalNorm)
$rows.Add([pscustomobject]@{
StartUrl = $r.StartUrl
StartStatus = $r.StartCode
StartLocation = $r.StartLoc
StartError = $r.StartErr
FinalUrl = $r.FinalUrl
FinalUrlNormalized = $finalNorm
Canonical = $canonical
CanonicalNormalized = $canonicalNorm
MatchesCanonical = $match
HopCount = $r.HopCount
})
}
}
finally {
$client.Dispose()
}
$rows |
Sort-Object MatchesCanonical, StartUrl |
Format-Table StartStatus, MatchesCanonical, HopCount, StartUrl, StartLocation, FinalUrl -AutoSize
$rows | Export-Csv -NoTypeInformation -Encoding UTF8 -Path $outCsv
Write-Host ""
Write-Host ("Wrote: {0}" -f (Resolve-Path -LiteralPath $outCsv))
Write-Host ("Rows collected: {0}" -f $rows.Count)
Write-Host ""
}
# Decide what to run
if ($DomainProfile -eq "all") {
$se = Get-DomainProfileConfig "se"
$ci = Get-DomainProfileConfig "ci"
Write-Host "=== SE report ==="
Invoke-RedirectReport -domains $se.Domains -canonical $se.Canonical -outCsv $se.OutCsv -maxHops $MaxHops -timeoutSec $TimeoutSec
Write-Host "=== CI report ==="
Invoke-RedirectReport -domains $ci.Domains -canonical $ci.Canonical -outCsv $ci.OutCsv -maxHops $MaxHops -timeoutSec $TimeoutSec
return
}
$cfg = Get-DomainProfileConfig $DomainProfile
$domainsToUse = if ($DomainProfile -eq "custom") { $Domains } else { $cfg.Domains }
$canonicalToUse = if ([string]::IsNullOrWhiteSpace($Canonical)) { $cfg.Canonical } else { $Canonical }
$outCsvToUse = if ([string]::IsNullOrWhiteSpace($OutCsv)) { $cfg.OutCsv } else { $OutCsv }
Invoke-RedirectReport -domains $domainsToUse -canonical $canonicalToUse -outCsv $outCsvToUse -maxHops $MaxHops -timeoutSec $TimeoutSec