-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetScaler-GenerateRedirects.ps1
More file actions
375 lines (246 loc) · 12.9 KB
/
NetScaler-GenerateRedirects.ps1
File metadata and controls
375 lines (246 loc) · 12.9 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
370
371
372
373
374
375
<#
.SYNOPSIS
Generates a NetScaler batch configuration file, for configuring redirects based on a list of redirect rules.
Request URLs containing wilcards ("*") are considered fallback redirect rules, and will be the last rules to be added
.PARAMETER CsvPath
Required. The path to the CSV file specifying the redirect rules.
- Headers must be: "Domain", "RequestUrl", "RedirectUrl"
- Fields must be semicolon-separated (or manually change character for the 'Import-Csv' command)
- The file format must be in UTF8 (Excel: Save as "CSV UTF-8")
- No blank lines (could probably easily be ignored in a future version)
- RequestUrls are automatically URL encoded - otherwise the NetScaler rules may not work
.PARAMETER RedirUrlPrefix
Required. The text string to prefix the redirected URLs with (before "RedirectUrl"), e.g. "https://www.newdomain.tld" or "http://www.anotherdomain.tld".
.PARAMETER HttpVserver
Required. The name of the content switch virtual server for HTTP requests
.PARAMETER HttpsVserver
Required. The name of the content switch virtual server for HTTPS requests
.PARAMETER SpecificRuleNumberBegin
Optional. The starting number for naming the specific (not containing wildcards) responder actions and policies, e.g. RespAct_1000. Defaults to 1000.
.PARAMETER FallbackRuleNumberBegin
Optional. The starting number for naming the fallback (wildcard) responder actions and policies, e.g. RespPol_9000. Defaults to 9000.
.PARAMETER RuleNumberIncrement
Optional. The number to increment by for each rule name, e.g. RespPol_1000, RespPol_1010. Defaults to 1.
.PARAMETER PriorityBegin
Optional. The starting priority for the policy bindings. Defaults to 100, to leave room for higher priority bindings.
.PARAMETER PriorityIncrement
Optional. The number to increment the priority of each policy binding by. Defaults to 10, to leave room for other bindings inbetween.
.PARAMETER OutputPath
Optional. The path where batch files are output to. Defaults to current working directory.
.EXAMPLE
.\NetScaler-GenerateRedirects.ps1 -CsvPath .\Redirects_Example.csv -RedirUrlPrefix "https://www.newdomain.tld/" -HttpVserver CSW_VIP_HTTP-Redirects -HttpsVserver CSW_VIP_HTTPS-Redirects
"HTTP.REQ.URL.SET_TEXT_MODE(IGNORECASE).REGEX_MATCH(re#^/another-old-path/?$#)" ==> https://www.newdomain.tld/brand/new/path/
"HTTP.REQ.URL.SET_TEXT_MODE(IGNORECASE).REGEX_MATCH(re#^/another-old-path/sub-path/?$#)" ==> https://www.newdomain.tld/brand/new/path/
"HTTP.REQ.URL.SET_TEXT_MODE(IGNORECASE).REGEX_MATCH(re#^/another-old-path/sub-path/old-page/?$#)" ==> https://www.newdomain.tld/brand/new/path/new-page
"HTTP.REQ.URL.EQ(\"/\")" ==> https://www.newdomain.tld/new/path/
"HTTP.REQ.URL.SET_TEXT_MODE(IGNORECASE).STARTSWITH(\"/some-old-path/\")" ==> https://www.newdomain.tld/brand/new/path/
NetScaler batch configuration output: C:\code\private\NetScaler-GenerateRedirects\redirects_20181205-125626.txt
.EXAMPLE
.\NetScaler-GenerateRedirects.ps1 -CsvPath .\Redirects_Example.csv -RedirUrlPrefix "https://www.newdomain.tld/" -HttpVserver CSW_VIP_HTTP-Redirects -HttpsVserver CSW_VIP_HTTPS-Redirects -SpecificRuleNumberBegin 2000 -FallbackRuleNumberBegin 8000
#>
Param (
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$CsvPath,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$RedirUrlPrefix,
[ValidateNotNullOrEmpty()]
[string]$OutputPath = $PWD,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$HttpVserver,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$HttpsVserver,
[int]$SpecificRuleNumberBegin = 1000,
[int]$FallbackRuleNumberBegin = 9000,
[int]$RuleNumberIncrement = 1,
[int]$PriorityBegin = 100,
[int]$PriorityIncrement = 10
)
# --------------------------------------------------
# Functions
# --------------------------------------------------
Function New-RedirectConfig {
Param (
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$Domain,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$RedirUrlPrefix,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$RequestUrl,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$RedirectUrl,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[int]$RuleNumber,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[int]$Priority,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[string]$HttpVserver = $HttpVserver,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[string]$HttpsVserver = $HttpsVserver
)
# Fix-up
$DomainFixup = $Domain
$DomainCriteria = "(HTTP.REQ.HOSTNAME.SET_TEXT_MODE(IGNORECASE).EQ(\""$DomainFixup\""))"
# --------------------------------------------------
# Process request URL
# --------------------------------------------------
$RequestUrlEncoded = [System.Web.HttpUtility]::UrlEncode($RequestUrl).Replace('%2f', '/').Replace('%3f', '?')
If ($RequestUrl -match "\*$") {
# Request URL ends with a wildcard (*), so this will be a fall-back rule. Specfic rules will take precedence
$RequestUrlFixup = "/$($RequestUrlEncoded.ToLower().TrimStart('/').TrimEnd('*'))"
$RequestUrlCriteria = "HTTP.REQ.URL.SET_TEXT_MODE(IGNORECASE).STARTSWITH(\""$RequestUrlFixup\"")"
}
Else {
# Fixup request URL - remove explicit leading and trailing slashes, explicitly prefix with a slash, thow-away query (after '?')
$RequestUrlFixup = "/$($RequestUrlEncoded.ToLower().Trim('/'))"
If ($RequestUrlFixup.Contains('?')) {
$RequestUrlFixup = $RequestUrlFixup.Substring(0, $RequestUrlFixup.IndexOf('?'))
}
If ($RequestUrlFixup -eq '/') {
# Requesting root of site (/)
$RequestUrlCriteria = "HTTP.REQ.URL.EQ(\""/\"")"
}
Else {
# Match requests for paths with or without trailing slash
$RequestUrlCriteria = "HTTP.REQ.URL.PATH.SET_TEXT_MODE(IGNORECASE).REGEX_MATCH(re#^$($RequestUrlFixup)/?$#)"
}
}
$ResponderPolicyRequest = @($DomainCriteria, $RequestUrlCriteria) -join ' && '
# --------------------------------------------------
# Process redirect URL
# --------------------------------------------------
$RedirectUrlFixup = $RedirectUrl.TrimStart('/')
$FullRedirectUrl = "$($RedirUrlPrefix.TrimEnd('/'))/$($RedirectUrlFixup)? + HTTP.REQ.URL.QUERY.HTTP_URL_SAFE"
# --------------------------------------------------
# Output NetScaler config lines
# --------------------------------------------------
@"
add responder action RespAct_$($RuleNumber.ToString("0000")) redirect "\"$FullRedirectUrl\"" -responseStatusCode 301
add responder policy RespPol_$($RuleNumber.ToString("0000")) "$ResponderPolicyRequest" RespAct_$($RuleNumber.ToString("0000"))
bind cs vserver $HttpVserver -policyName RespPol_$($RuleNumber.ToString("0000")) -priority $Priority -gotoPriorityExpression END -type REQUEST
bind cs vserver $HttpsVserver -policyName RespPol_$($RuleNumber.ToString("0000")) -priority $Priority -gotoPriorityExpression END -type REQUEST
"@
$RequestUrlCriteria | ForEach-Object {
# Write-Verbose """$_"" ==> $FullRedirectUrl" -Verbose
Write-Host """$_"" ==> $FullRedirectUrl" -ForegroundColor DarkGray
}
}
Function New-RollbackConfig {
Param (
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[int]$RuleNumber,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[int]$Priority,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[string]$HttpVserver = $HttpVserver,
[Parameter(Mandatory = $False)]
[ValidateNotNullOrEmpty()]
[string]$HttpsVserver = $HttpsVserver,
[switch]$UnbindOnly
)
@"
unbind cs vserver $HttpVserver -policyName RespPol_$($RuleNumber.ToString("0000"))
unbind cs vserver $HttpsVserver -policyName RespPol_$($RuleNumber.ToString("0000"))
"@
If (-Not($UnbindOnly)) {
@"
rm responder policy RespPol_$($RuleNumber.ToString("0000"))
rm responder action RespAct_$($RuleNumber.ToString("0000"))
"@
}
}
Function Output-NetScalerCmd {
Param (
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$FileName,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string[]]$Rules
)
$WriterAppend = $false
$WriterEncoding = New-Object System.Text.UTF8Encoding $False
$writer = New-Object System.IO.StreamWriter $FileName, $WriterAppend, $WriterEncoding
$writer.NewLine = "`n"
$Rules -split "`r`n" | ForEach {
If ($_.Length -gt 0) {
$writer.WriteLine($_)
}
}
$writer.Dispose()
}
# --------------------------------------------------
# Begin script
# --------------------------------------------------
Add-Type -AssemblyName System.Web
$RedirectCsv = Import-Csv -Path $CsvPath -Delimiter ';' | Sort-Object Domain, RequestUrl
Remove-Variable Rules -ErrorAction SilentlyContinue
# Specific redirects (separate rules for each redirect)
$RuleNumber = $SpecificRuleNumberBegin
$Priority = $PriorityBegin
$RedirectCsv | Where-Object {$_.RequestUrl -notmatch '\*'} | ForEach {
$RedirectCmd += New-RedirectConfig -Domain $_.Domain.ToString().Trim() -RedirUrlPrefix $RedirUrlPrefix.Trim() -RequestUrl $_.RequestUrl.ToString().Trim() -RedirectUrl $_.RedirectUrl.ToString().Trim() -RuleNumber $RuleNumber -Priority $Priority
$UnbindCmd += New-RollbackConfig -RuleNumber $RuleNumber -Priority $Priority -UnbindOnly
$RollBackCmd += New-RollbackConfig -RuleNumber $RuleNumber -Priority $Priority
$RuleNumber = $RuleNumber + $RuleNumberIncrement
$Priority = $Priority + $PriorityIncrement
}
# Fallback redirects (separate rules for each redirect)
$RuleNumber = $FallbackRuleNumberBegin
$RedirectCsv | Where-Object {$_.RequestUrl -match '\*'} | ForEach {
$RedirectCmd += New-RedirectConfig -Domain $_.Domain.ToString().Trim() -RedirUrlPrefix $RedirUrlPrefix.Trim() -RequestUrl $_.RequestUrl.ToString().Trim() -RedirectUrl $_.RedirectUrl.ToString().Trim() -RuleNumber $RuleNumber -Priority $Priority
$UnbindCmd += New-RollbackConfig -RuleNumber $RuleNumber -Priority $Priority -UnbindOnly
$RollBackCmd += New-RollbackConfig -RuleNumber $RuleNumber -Priority $Priority
$RuleNumber = $RuleNumber + $RuleNumberIncrement
$Priority = $Priority + $PriorityIncrement
}
# Output to batch file (UTF8, LF EOL)
If (Test-Path -Path $OutputPath) {
$TimeStamp = Get-Date -Format "yyyyMMdd-HHmmss"
# --------------------------------------------------
# Duplicate input file
# --------------------------------------------------
$OutputInput = "$OutputPath\$($TimeStamp)_input.csv"
Copy-Item -Path $CsvPath -Destination $OutputInput
# --------------------------------------------------
# Redirects
# --------------------------------------------------
$OutputRedirects = "$OutputPath\$($TimeStamp)_redirects.txt"
Output-NetScalerCmd -FileName $OutputRedirects -Rules $RedirectCmd
# --------------------------------------------------
# Unbind
# --------------------------------------------------
# Syntax: unbind cs vserver CSW_VIP_HTTPS-Redirects -policyName RespPol_9000 -type REQUEST -priority 140
$OutputUnbind = "$OutputPath\$($TimeStamp)_unbind.txt"
Output-NetScalerCmd -FileName $OutputUnbind -Rules $UnbindCmd
# --------------------------------------------------
# Rollback
# --------------------------------------------------
$OutputRollback = "$OutputPath\$($TimeStamp)_rollback.txt"
Output-NetScalerCmd -FileName $OutputRollback -Rules $RollBackCmd
# --------------------------------------------------
# Print result
# --------------------------------------------------
Write-Host "NetScaler batch configuration ($($RedirectCsv.Count) rules):" -ForegroundColor White
Write-Host " Input: $OutputInput" -ForegroundColor Cyan
Write-Host " Redirects: $OutputRedirects" -ForegroundColor Green
Write-Host " Unbind: $OutputUnbind" -ForegroundColor Yellow
Write-Host " Rollback: $OutputRollback" -ForegroundColor Red
}
Else {
Throw "Output path ""$OutputPath"" not found. No files generated."
}