-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpage_with_proxy.ps1
185 lines (145 loc) · 5.61 KB
/
page_with_proxy.ps1
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
#Copyright (c) 2014 Serguei Kouzmine
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
param(
[string]$username = '',
[string]$url = 'https://haldev.service-now.com/api/now/table/change_request',
[switch]$use_proxy,
[string]$password = ''
)
function log_message {
param(
[Parameter(Position = 0)]
[string]$message,
[Parameter(Position = 1)]
[string]$logfile
)
Write-Output -InputObject $message
Write-Output -InputObject $message | Out-File -FilePath $logfile -Encoding ascii -Force -Append
}
function page_content {
param(
[string]$username = $env:USERNAME,
[string]$url = '',
[string]$password = '',
[string]$use_proxy
)
if ($url -eq $null -or $url -eq '') {
# $url = ('https://github.com/{0}' -f $username)
$url = 'https://api.github.com/user'
}
$sleep_interval = 10
$max_retries = 5
# LEGACY log file for loading step status into Jenkins
$build_status = 'test.properties'
# this is a test
$expected_status = 200
# ($build_status) | ForEach-Object { Set-Content -Path $_ -Value '' }
Set-Content -Value '' -Path $build_status
$log_file = 'healthcheck.txt'
if ($PSBoundParameters['use_proxy']) {
# Use current user NTLM credentials do deal with corporate firewall
$proxy_address = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
if ($proxy_address -eq $null) {
$proxy_address = (Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoConfigURL
}
if ($proxy_address -eq $null) {
# write a hard coded proxy address here
$proxy_address = 'http://proxy.carnival.com:8080/array.dll?Get.Routing.Script'
}
$proxy = New-Object System.Net.WebProxy
$proxy.Address = $proxy_address
Write-Host ("Probing {0}" -f $proxy.Address)
$proxy.useDefaultCredentials = $true
}
<#
request.Credentials = new NetworkCredential(xxx,xxx);
CookieContainer myContainer = new CookieContainer();
request.CookieContainer = myContainer;
request.PreAuthenticate = true;
#>
[system.Net.WebRequest]$request = [system.Net.WebRequest]::Create($url)
try {
[string]$encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::GetEncoding('ASCII').GetBytes(($username + ':' + $password)))
Write-Debug $encoded
$request.Headers.Add('Authorization','Basic ' + $encoded)
} catch [argumentexception]{
}
if ($PSBoundParameters['use_proxy']) {
Write-Host ('Use Proxy: "{0}"' -f $proxy.Address)
$request.proxy = $proxy
$request.useDefaultCredentials = $true
}
# Note github returns a json result saying that it requires authentication
# standard server response is a "classic" 401 html page
Write-Host ('Open {0}' -f $url)
for ($i = 0; $i -ne $max_retries; $i++) {
Write-Host ('Try {0}' -f $i)
try {
$response = $request.GetResponse()
} catch [System.Net.WebException]{
$response = $_.Exception.Response
}
$int_status = [int]$response.StatusCode
$time_stamp = (Get-Date -Format 'yyyy/MM/dd hh:mm')
$status = $response.StatusCode # not casting
Write-Host "$time_stamp`t$url`t$int_status`t$status"
# | Tee-Object -FilePath $log_file -append
if ($int_status -ne $expected_status) {
Write-Host 'Unexpected http status detected. sleep and retry.'
Start-Sleep -Seconds $sleep_interval
# sleep and retry
} else {
break
}
}
$time_stamp = $null
if ($int_status -ne $expected_status) {
# write error status to a log file and exit
#
Write-Host ('Unexpected http status detected. Error reported. {0}, {1} ' -f $int_status)
log_message 'STEP_STATUS=ERROR' $build_status
}
$respstream = $response.GetResponseStream()
$stream_reader = New-Object System.IO.StreamReader $respstream
$result_page = $stream_reader.ReadToEnd()
<#
if ($result_page -match $confirm_page_text) {
$found_expected_status = $true
if ($result_page.size -lt 100 )
{
$result_page_fragment= $result_page
}
write-host "Page Contents:`n${result_page_fragment}"
} else {
$found_expected_status = $false
$result_page = ''
}
#>
Write-Debug $result_page
return $result_page
}
[string]$use_proxy_arg = $null
# TODO pass switches correctly
if ($PSBoundParameters['use_proxy']) {
$use_proxy_arg = @( '-use_proxy',$true) -join ' '
}
Write-Host "page_content -username $username -password $password -url $url $use_proxy_arg"
page_content -UserName $username -password $password -url $url $use_proxy_arg
# write-output ( page_content -username $username -password $password -url $url $use_proxy_arg)