From 79877c43d58dd45c76dd059a854a85f5862221f8 Mon Sep 17 00:00:00 2001 From: Steve Dinn Date: Thu, 6 Apr 2023 15:24:09 -0300 Subject: [PATCH] initial commit --- docker-compose.yml | 31 +++++++++++++++ source/CaddyLog-To-AccessLog.ps1 | 68 ++++++++++++++++++++++++++++++++ source/Dockerfile | 10 +++++ 3 files changed, 109 insertions(+) create mode 100644 docker-compose.yml create mode 100644 source/CaddyLog-To-AccessLog.ps1 create mode 100644 source/Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9671439 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: "3" +services: + caddy: + image: stevedinn/caddy-log-converter:latest + restart: unless-stopped + volumes: + - /srv/docker/caddy/data/log:/caddylogs + - ./logs:/accesslogs + environment: + INFILE: "/caddylogs/caddy.log" + OUTFILE: "/accesslogs/caddy.log" + + social: + image: stevedinn/caddy-log-converter:latest + restart: unless-stopped + volumes: + - /srv/docker/caddy/data/log:/caddylogs + - ./logs:/accesslogs + environment: + INFILE: "/caddylogs/social.log" + OUTFILE: "/accesslogs/social.log" + + pelvichealth: + image: stevedinn/caddy-log-converter:latest + restart: unless-stopped + volumes: + - /srv/docker/caddy/data/log:/caddylogs + - ./logs:/accesslogs + environment: + INFILE: "/caddylogs/pelvichealthclinic.log" + OUTFILE: "/accesslogs/pelvichealthclinic.log" diff --git a/source/CaddyLog-To-AccessLog.ps1 b/source/CaddyLog-To-AccessLog.ps1 new file mode 100644 index 0000000..b60fdee --- /dev/null +++ b/source/CaddyLog-To-AccessLog.ps1 @@ -0,0 +1,68 @@ +param ( + [Parameter(Mandatory = $false)][string] $inFile, + [Parameter(Mandatory = $false)][string] $outFile, + [Parameter(Mandatory = $false)][int] $lines = 0 +) + +if ([string]::IsNullOrWhiteSpace($inFile)) { + $inFile = [System.Environment]::GetEnvironmentVariable("INFILE") +} + +if ([string]::IsNullOrWhiteSpace($outFile)) { + $outFile = [System.Environment]::GetEnvironmentVariable("OUTFILE") +} + +$baseDate = (New-Object System.DateTime -ArgumentList @(1970, 1, 1, 0, 0, 0, [DateTimeKind]::Utc)).ToLocalTime() +$builder = New-Object System.Text.StringBuilder + +$outputStream = [IO.File]::Open($outFile, [IO.FileMode]::Append, [IO.FileAccess]::Write, [IO.FileShare]::Read) +$outputWriter = New-Object -TypeName System.IO.StreamWriter -ArgumentList @($outputStream, [Text.Encoding]::UTF8) + +Get-Content -Tail $lines -Wait -Path $inFile | ForEach-Object { + $l = ConvertFrom-Json -InputObject $_ + $r = $l.request; + $ua = $r.headers.'User-Agent' + + $null = $builder.Clear() + $null = $builder.Append($r.remote_ip) + $null = $builder.Append(" - [") + $null = $builder.Append($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff")) + $null = $builder.Append("] """) + $null = $builder.Append($r.host) + $null = $builder.Append(""" ") + $null = $builder.Append($r.method) + $null = $builder.Append(" ") + $null = $builder.Append($r.uri) + $null = $builder.Append(" ") + $null = $builder.Append($r.proto) + $null = $builder.Append(" ") + $null = $builder.Append($l.status) + $null = $builder.Append(" ") + $null = $builder.Append($l.size) + $null = $builder.Append(" """) + $null = $builder.Append($r.headers.Referer) + $null = $builder.Append(""" """) + $null = $builder.Append("$ua") + $null = $builder.Append("""") + + $commonLog = $builder.ToString() + + Write-Output $commonLog + $outputWriter.WriteLine($commonLog) + $outputWriter.Flush() + + #[system.string]::Join( + # " ", + # @( + # "$($r.remote_ip) - [$($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff"))]", + # """$($r.host)""", + # $r.method, + # $r.uri, + # $r.proto, + # $l.status, + # $l.size, + # """$($r.headers.Referer)""", + # """$ua""" + # )) +} + diff --git a/source/Dockerfile b/source/Dockerfile new file mode 100644 index 0000000..2f65704 --- /dev/null +++ b/source/Dockerfile @@ -0,0 +1,10 @@ +FROM mcr.microsoft.com/dotnet/sdk + +RUN mkdir /app +COPY ./CaddyLog-To-AccessLog.ps1 /app +WORKDIR /app + +ENV INFILE="caddy.log" +ENV OUTFILE="access.log" + +ENTRYPOINT ["pwsh", "-command", "/app/CaddyLog-To-AccessLog.ps1", "-inFile", "$INFILE", "-outFile", "$OUTFILE"]