Skip to content

Commit

Permalink
Use consistent User-Agent Header on all webrequests
Browse files Browse the repository at this point in the history
  • Loading branch information
r15ch13 committed Mar 18, 2018
1 parent 21bf0de commit 12962ac
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bin/checkurls.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function test_dl($url, $cookies) {
$wreq = [net.webrequest]::create($url)
$wreq.timeout = $timeout * 1000
if($wreq -is [net.httpwebrequest]) {
$wreq.useragent = 'Scoop/1.0'
$wreq.useragent = Get-UserAgent
$wreq.referer = strip_filename $url
if($cookies) {
$wreq.headers.add('Cookie', (cookie_header $cookies))
Expand Down
4 changes: 2 additions & 2 deletions bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ $original = use_any_https_protocol

# start all downloads
$queue | ForEach-Object {
$wc = new-object net.webclient
$wc.Headers.Add("user-agent", "Scoop/1.0 (+http://scoop.sh/) (Windows NT 6.1; WOW64)")
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
register-objectevent $wc downloadstringcompleted -ea stop | out-null

$name, $json = $_
Expand Down
4 changes: 3 additions & 1 deletion bin/describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ $apps | ForEach-Object {
}
# get description from homepage
try {
$home_html = (new-object net.webclient).downloadstring($json.homepage)
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$home_html = $wc.downloadstring($json.homepage)
} catch {
write-host "`n$($_.exception.message)" -fore red
return
Expand Down
15 changes: 9 additions & 6 deletions lib/autoupdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ function find_hash_in_rdf([String] $url, [String] $filename) {
$data = $null
try {
# Download and parse RDF XML file
$wc = new-object net.webclient
$wc.headers.add('Referer', (strip_filename $url))
$wc = New-Object Net.Webclient
$wc.Headers.Add('Referer', (strip_filename $url))
$wc.Headers.Add('User-Agent', (Get-UserAgent))
[xml]$data = $wc.downloadstring($url)
} catch [system.net.webexception] {
write-host -f darkred $_
Expand All @@ -32,8 +33,9 @@ function find_hash_in_textfile([String] $url, [String] $basename, [String] $rege
$hashfile = $null

try {
$wc = new-object net.webclient
$wc.headers.add('Referer', (strip_filename $url))
$wc = New-Object Net.Webclient
$wc.Headers.Add('Referer', (strip_filename $url))
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$hashfile = $wc.downloadstring($url)
} catch [system.net.webexception] {
write-host -f darkred $_
Expand Down Expand Up @@ -69,8 +71,9 @@ function find_hash_in_json([String] $url, [String] $basename, [String] $jsonpath
$json = $null

try {
$wc = new-object net.webclient
$wc.headers.add('Referer', (strip_filename $url))
$wc = New-Object Net.Webclient
$wc.Headers.Add('Referer', (strip_filename $url))
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$json = $wc.downloadstring($url)
} catch [system.net.webexception] {
write-host -f darkred $_
Expand Down
8 changes: 6 additions & 2 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function enable-encryptionscheme([Net.SecurityProtocolType]$scheme) {
}
enable-encryptionscheme "Tls12"

function Get-UserAgent() {
return "Scoop/1.0 (+http://scoop.sh/) PowerShell/$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor) (Windows NT $([System.Environment]::OSVersion.Version.Major).$([System.Environment]::OSVersion.Version.Minor); $(if($env:PROCESSOR_ARCHITECTURE -eq 'AMD64'){'Win64; x64; '})$(if($env:PROCESSOR_ARCHITEW6432 -eq 'AMD64'){'WOW64; '})$PSEdition)"
}

# helper functions
function coalesce($a, $b) { if($a) { return $a } $b }

Expand Down Expand Up @@ -154,9 +158,9 @@ function is_local($path) {

# operations
function dl($url,$to) {
$wc = new-object system.net.webClient
$wc.headers.add('User-Agent', 'Scoop/1.0')
$wc = New-Object Net.Webclient
$wc.headers.add('Referer', (strip_filename $url))
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$wc.downloadFile($url,$to)
}

Expand Down
4 changes: 3 additions & 1 deletion lib/description.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function find_description($url, $html, $redir = $false) {
# check <meta http-equiv="refresh"> redirect
$refresh = meta_refresh $meta $url
if($refresh -and !$redir) {
$html = (new-object net.webclient).downloadstring($refresh)
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$html = $wc.downloadstring($refresh)
return find_description $refresh $html $true
}

Expand Down
2 changes: 1 addition & 1 deletion lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function do_dl($url, $to, $cookies) {
function dl($url, $to, $cookies, $progress) {
$wreq = [net.webrequest]::create($url)
if($wreq -is [net.httpwebrequest]) {
$wreq.useragent = 'Scoop/1.0'
$wreq.useragent = Get-UserAgent
if (-not ($url -imatch "https?://downloads.sourceforge.net/")) {
$wreq.referer = strip_filename $url
}
Expand Down
13 changes: 10 additions & 3 deletions lib/manifest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ function parse_json($path) {
function url_manifest($url) {
$str = $null
try {
$str = (new-object net.webclient).downloadstring($url)
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$str = $wc.downloadstring($url)
} catch [system.management.automation.methodinvocationexception] {
warn "error: $($_.exception.innerexception.message)"
} catch {
Expand All @@ -29,8 +31,13 @@ function manifest($app, $bucket, $url) {
}

function save_installed_manifest($app, $bucket, $dir, $url) {
if($url) { (new-object net.webclient).downloadstring($url) > "$dir\manifest.json" }
else { Copy-Item (manifest_path $app $bucket) "$dir\manifest.json" }
if($url) {
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$wc.downloadstring($url) > "$dir\manifest.json"
} else {
Copy-Item (manifest_path $app $bucket) "$dir\manifest.json"
}
}

function installed_manifest($app, $version, $global) {
Expand Down
4 changes: 3 additions & 1 deletion libexec/scoop-virustotal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ $requests = 0
Function Get-VirusTotalResult($hash, $app) {
$hash = $hash.ToLower()
$url = "https://www.virustotal.com/ui/files/$hash"
$result = (new-object net.webclient).downloadstring($url)
$wc = New-Object Net.Webclient
$wc.Headers.Add('User-Agent', (Get-UserAgent))
$result = $wc.downloadstring($url)
$stats = json_path $result '$.data.attributes.last_analysis_stats'
$malicious = json_path $stats '$.malicious'
$suspicious = json_path $stats '$.suspicious'
Expand Down

0 comments on commit 12962ac

Please sign in to comment.