Skip to content

Commit

Permalink
fix: bat and ps1 source
Browse files Browse the repository at this point in the history
  • Loading branch information
ersanne committed Sep 11, 2024
1 parent 8ba395b commit 0a6c02b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 60 deletions.
18 changes: 11 additions & 7 deletions scripts/thunderstorm-collector.bat
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ SET DEBUG=0

:: Source
SET SOURCE=
IF "%SOURCE%"=="" (
FOR /F "tokens=*" %%i IN ('hostname') DO SET HOSTNAME=%%i
)
IF "%SOURCE%" EQ "" AND "%HOSTNAME%" NEQ "" (
SOURCE="?source=%HOSTNAME%"
)

:: WELCOME -------------------------------------------------------

Expand Down Expand Up @@ -83,10 +77,20 @@ ECHO Cannot find curl in PATH or the current directory. Download it from https:/
ECHO If you're collecting on Windows systems older than Windows Vista, use curl version 7.46.0 from https://bintray.com/vszakats/generic/download_file?file_path=curl-7.46.0-win32-mingw.7z
EXIT /b 1
:CHECKDONE
ECHO Curl has been found. We're ready to go.
ECHO Curl has been found. We're ready to go.

:: COLLECTION --------------------------------------------------

:: SOURCE
IF "%SOURCE%"=="" (
ECHO Getting hostname
FOR /F "tokens=*" %%i IN ('hostname') DO SET SOURCE=%%i
ECHO No Source provided, using hostname=!SOURCE!
)
IF "%SOURCE%" NEQ "" (
SET SOURCE=?source=%SOURCE%
)

:: Directory walk and upload
ECHO Processing %COLLECT_DIRS% with filters MAX_SIZE: %COLLECT_MAX_SIZE% MAX_AGE: %MAX_AGE% days EXTENSIONS: %RELEVANT_EXTENSIONS%
ECHO This could take a while depending on the disk size and number of files. (set DEBUG=1 to see all skips)
Expand Down
110 changes: 57 additions & 53 deletions scripts/thunderstorm-collector.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Date Created: 07.10.2020
# Last Modified: 07.10.2020
##################################################

#Requires -Version 3

<#
Expand Down Expand Up @@ -48,47 +48,47 @@
# Parameters ----------------------------------------------------------
# #####################################################################

param
(
[Parameter(
HelpMessage='Server name (FQDN) or IP address of your Thunderstorm instance')]
[ValidateNotNullOrEmpty()]
param
(
[Parameter(
HelpMessage='Server name (FQDN) or IP address of your Thunderstorm instance')]
[ValidateNotNullOrEmpty()]
[Alias('TS')]
[string]$ThunderstormServer,
[string]$ThunderstormServer,

[Parameter(HelpMessage="Port number on which the Thunderstorm service is listening (default: 8080)")]
[ValidateNotNullOrEmpty()]
[Alias('TP')]
[Parameter(HelpMessage="Port number on which the Thunderstorm service is listening (default: 8080)")]
[ValidateNotNullOrEmpty()]
[Alias('TP')]
[int]$ThunderstormPort = 8080,

[Parameter(HelpMessage="")]
[Alias('S')]
[Parameter(HelpMessage="Source of the submission (default: hostname of the system)")]
[Alias('S')]
[string]$Source=$env:COMPUTERNAME,

[Parameter(HelpMessage="Folder to process (default: C:\)")]
[ValidateNotNullOrEmpty()]
[Parameter(HelpMessage="Folder to process (default: C:\)")]
[ValidateNotNullOrEmpty()]
[Alias('F')]
[string]$Folder = "C:\",
[Parameter(
HelpMessage='Select files based on the number of days in which the file has been create or modified (default: 0 = no age selection)')]
[ValidateNotNullOrEmpty()]
[Alias('MA')]
[int]$MaxAge,

[Parameter(
HelpMessage='Select only files smaller than the given number in MegaBytes (default: 20MB) ')]
[ValidateNotNullOrEmpty()]
[Alias('MS')]
[int]$MaxSize,

[Parameter(HelpMessage='Extensions to select for submission (default: all of them)')]
[ValidateNotNullOrEmpty()]
[Alias('E')]
[string[]]$Extensions,

[Parameter(HelpMessage='Enables debug output and skips cleanup at the end of the scan')]
[ValidateNotNullOrEmpty()]

[Parameter(
HelpMessage='Select files based on the number of days in which the file has been create or modified (default: 0 = no age selection)')]
[ValidateNotNullOrEmpty()]
[Alias('MA')]
[int]$MaxAge,

[Parameter(
HelpMessage='Select only files smaller than the given number in MegaBytes (default: 20MB) ')]
[ValidateNotNullOrEmpty()]
[Alias('MS')]
[int]$MaxSize,

[Parameter(HelpMessage='Extensions to select for submission (default: all of them)')]
[ValidateNotNullOrEmpty()]
[Alias('E')]
[string[]]$Extensions,

[Parameter(HelpMessage='Enables debug output and skips cleanup at the end of the scan')]
[ValidateNotNullOrEmpty()]
[Alias('D')]
[switch]$Debugging = $False
)
Expand Down Expand Up @@ -150,19 +150,19 @@ if ( $Args.Count -eq 0 -and $ThunderstormServer -eq "" ) {
function Write-Log {
param (
[Parameter(Mandatory=$True, Position=0, HelpMessage="Log entry")]
[ValidateNotNullOrEmpty()]
[ValidateNotNullOrEmpty()]
[String]$Entry,

[Parameter(Position=1, HelpMessage="Log file to write into")]
[ValidateNotNullOrEmpty()]
[Alias('SS')]
[Parameter(Position=1, HelpMessage="Log file to write into")]
[ValidateNotNullOrEmpty()]
[Alias('SS')]
[IO.FileInfo]$LogFile = "thunderstorm-collector.log",

[Parameter(Position=3, HelpMessage="Level")]
[ValidateNotNullOrEmpty()]
[ValidateNotNullOrEmpty()]
[String]$Level = "Info"
)

# Indicator
$Indicator = "[+]"
if ( $Level -eq "Warning" ) {
Expand All @@ -185,7 +185,7 @@ function Write-Log {
} else {
Write-Host "$($Indicator) $($Entry)"
}

# Log File
if ( $global:NoLog -eq $False ) {
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff') $($env:COMPUTERNAME): $Entry" | Out-File -FilePath $LogFile -Append
Expand Down Expand Up @@ -225,27 +225,31 @@ if ( $AutoDetectPlatform -ne "" ) {
}

# URL Creation
$Url = "http://$($ThunderstormServer):$($ThunderstormPort)/api/checkAsync"
if ( $Source -ne "" ) {
Write-Log "Using Source: $($Source)"
$SourceParam = "?Source=$Source"
}
$Url = "http://$($ThunderstormServer):$($ThunderstormPort)/api/checkAsync$($SourceParam)"
Write-Log "Sending to URI: $($Url)" -Level "Debug"

# ---------------------------------------------------------------------
# Run THOR Thunderstorm Collector -------------------------------------
# ---------------------------------------------------------------------
$ProgressPreference = "SilentlyContinue"
try {
Get-ChildItem -Path $Folder -File -Recurse -ErrorAction SilentlyContinue |
Get-ChildItem -Path $Folder -File -Recurse -ErrorAction SilentlyContinue |
ForEach-Object {
# -------------------------------------------------------------
# Filter ------------------------------------------------------
# Filter ------------------------------------------------------
# Size Check
if ( ( $_.Length / 1MB ) -gt $($MaxSize) ) {
Write-Log "$_ skipped due to size filter" -Level "Debug"
Write-Log "$_ skipped due to size filter" -Level "Debug"
return
}
# Age Check
# Age Check
if ( $($MaxAge) -gt 0 ) {
if ( $_.LastWriteTime -lt (Get-Date).AddDays(-$($MaxAge)) ) {
Write-Log "$_ skipped due to age filter" -Level "Debug"
Write-Log "$_ skipped due to age filter" -Level "Debug"
return
}
}
Expand All @@ -259,7 +263,7 @@ try {

# -------------------------------------------------------------
# Submission --------------------------------------------------

Write-Log "Processing $($_.FullName) ..." -Level "Debug"
# Reading the file data & preparing the request
try {
Expand All @@ -270,12 +274,12 @@ try {
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString();
$LF = "`r`n";
$bodyLines = (
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$($_.FullName)`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary--$LF"
"--$boundary--$LF"
) -join $LF

# Submitting the request
Expand All @@ -286,7 +290,7 @@ try {
Write-Log "Submitting to Thunderstorm server: $($_.FullName) ..." -Level "Info"
$Response = Invoke-WebRequest -uri $($Url) -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
$StatusCode = [int]$Response.StatusCode
}
}
# Catch all non 200 status codes
catch {
$StatusCode = $_.Exception.Response.StatusCode.value__
Expand All @@ -310,8 +314,8 @@ try {
}
}
}
} catch {
Write-Log "Unknown error during Thunderstorm Collection $_" -Level "Error"
} catch {
Write-Log "Unknown error during Thunderstorm Collection $_" -Level "Error"
}

# ---------------------------------------------------------------------
Expand Down

0 comments on commit 0a6c02b

Please sign in to comment.