Skip to content

Commit ef42942

Browse files
committed
Initial commit
0 parents  commit ef42942

File tree

13 files changed

+234
-0
lines changed

13 files changed

+234
-0
lines changed

.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: [alan-null]
4+
custom: ['https://www.paypal.com/paypalme/plocieniak']

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fake-UserAgent/data/user-agents.json
2+
Fake-UserAgent/data/user-agents.dat

Fake-UserAgent/Fake-UserAgent.psd1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
@{
3+
RootModule = 'Fake-UserAgent.psm1'
4+
ModuleVersion = '0.1.0'
5+
GUID = 'ef6d1c27-66ac-4f6c-8cc6-2bbae0f24543'
6+
Author = 'Alan Plocieniak'
7+
CompanyName = 'Alan Plocieniak'
8+
Copyright = '(c) 2025 Alan Plocieniak. All rights reserved.'
9+
Description = 'PowerShell module for generating random User Agents.'
10+
PowerShellVersion = '5.0'
11+
FunctionsToExport = '*'
12+
PrivateData = @{
13+
PSData = @{
14+
Tags = @('powershell', 'fake', 'ps', 'power-shell', 'fake-useragent', 'useragent', 'user', 'agent' )
15+
LicenseUri = 'https://github.com/PowerShellLibrary/Fake-UserAgent/blob/master/LICENSE.md'
16+
ProjectUri = 'https://github.com/PowerShellLibrary/Fake-UserAgent'
17+
}
18+
}
19+
}

Fake-UserAgent/Fake-UserAgent.psm1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
2+
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
3+
4+
Foreach ($import in @($Public + $Private)) {
5+
try {
6+
. $import.fullname
7+
}
8+
catch {
9+
Write-Error -Message "Failed to import function $($import.fullname): $_"
10+
}
11+
}
12+
Export-ModuleMember -Function $Public.Basename
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function ConvertFrom-Gzip {
2+
Param (
3+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
4+
[ValidateScript( { (Get-Item $_).Name.EndsWith(".gz") })]
5+
[System.IO.FileInfo]
6+
$InputObject,
7+
8+
[Parameter(Mandatory = $false)]
9+
[switch]
10+
$RemoveInputFile
11+
)
12+
Process {
13+
# Create a new file and open a filestream for it
14+
$NewFilename = $InputObject.FullName.Remove($InputObject.FullName.Length - $InputObject.Extension.Length)
15+
$DecompressedFileStream = [System.IO.File]::Create($NewFilename)
16+
17+
# Open the compressed file and copy the file to the decompressed stream
18+
$CompressedFileStream = $InputObject.OpenRead()
19+
$GZipStream = [System.IO.Compression.GZipStream]::new($CompressedFileStream, [System.IO.Compression.CompressionMode]::Decompress)
20+
$GZipStream.CopyTo($DecompressedFileStream)
21+
22+
# Cleanup
23+
$DecompressedFileStream.Dispose()
24+
$GZipStream.Dispose()
25+
$CompressedFileStream.Dispose()
26+
$DecompressedFileStream, $GZipStream, $CompressedFileStream = $null
27+
28+
# Remove the initial file if requested.
29+
if ($PSBoundParameters.ContainsKey('RemoveInputFile')) {
30+
$InputObject.Delete()
31+
}
32+
}
33+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Get-UserAgentData {
2+
[CmdletBinding()]
3+
4+
$datPath = Join-Path -Path $PSScriptRoot -ChildPath "..\data\user-agents.dat"
5+
6+
if (-Not(Test-Path $datPath)) {
7+
Initialize-Dataset
8+
}
9+
10+
Import-Clixml -Path $datPath
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function Initialize-Dataset {
2+
Process {
3+
4+
$gzPath = Join-Path -Path $PSScriptRoot -ChildPath "..\data\user-agents.json.gz"
5+
$jsonPath = Join-Path -Path $PSScriptRoot -ChildPath "..\data\user-agents.json"
6+
$dll = Join-Path -Path $PSScriptRoot -ChildPath "..\tools\UAParser.dll"
7+
8+
if (-Not(Test-Path $jsonPath)) {
9+
ConvertFrom-Gzip -InputObject (Get-Item -Path $gzPath)
10+
}
11+
12+
if (-Not(Test-Path $datPath)) {
13+
Add-Type -Path $dll
14+
$uaParser = [UAParser.Parser]::GetDefault()
15+
16+
17+
$jsonFile = (Get-Item -Path $jsonPath)
18+
$processed = $jsonFile | Get-Content -Raw | ConvertFrom-Json | % {
19+
$ua_result = $uaParser.Parse($_.userAgent)
20+
21+
[PSCustomObject]@{
22+
ua = $_.userAgent
23+
percent = [decimal]($_.weight * 100)
24+
type = $_.deviceCategory
25+
device_brand = $ua_result.Device.Brand
26+
browser = $ua_result.UserAgent.Family
27+
browser_version = [version]::new($ua_result.UserAgent.Major, $ua_result.UserAgent.Minor, $ua_result.UserAgent.Patch, $ua_result.UserAgent.PatchMinor)
28+
browser_version_major_minor = [version]::new($ua_result.UserAgent.Major, $ua_result.UserAgent.Minor)
29+
os = $ua_result.OS.Family
30+
os_version = [version]::new($ua_result.OS.Major, $ua_result.OS.Minor, $ua_result.OS.Patch, $ua_result.OS.PatchMinor)
31+
platform = $_.platform
32+
}
33+
}
34+
35+
$jsonFile | Remove-Item
36+
$processed | Export-Clixml -Path $datPath
37+
}
38+
}
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function Get-UserAgent {
2+
[CmdletBinding()]
3+
param ()
4+
5+
$data = Get-UserAgentData
6+
return $data
7+
8+
}
9+
10+
Export-ModuleMember -Function Get-UserAgent
203 KB
Binary file not shown.

Fake-UserAgent/tools/UAParser.dll

247 KB
Binary file not shown.

0 commit comments

Comments
 (0)