-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntrypoint.ps1
More file actions
222 lines (177 loc) · 7.72 KB
/
Entrypoint.ps1
File metadata and controls
222 lines (177 loc) · 7.72 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env pwsh
#Requires -Modules Logging
#Requires -Modules OverlayFS
#Requires -Modules Hooks
#Requires -Modules AptTools
Write-Log "Starting entry script..."
$runUser = $env:USER
$runGroup = $env:GROUP
$runUid = $env:UID
$runGid = $env:GID
Write-Log "Setting up modules and hooks"
if (-not (Test-Path -Path $Env:USER_MODULES)) {
New-Item -ItemType Directory -Path $Env:USER_MODULES | Out-Null
}
if (-not (Test-Path -Path $Env:USER_HOOKS)) {
New-Item -ItemType Directory -Path $Env:USER_HOOKS | Out-Null
}
Copy-Item -Path "$Env:BASE_MODULES/*" -Destination "$Env:USER_MODULES" -Recurse -Force
Copy-Item -Path "$Env:BASE_HOOKS/*" -Destination "$Env:USER_HOOKS" -Recurse -Force
Install-Module -Name PSCompression -Scope AllUsers -Force -AllowClobber -ErrorAction SilentlyContinue
Invoke-Hook "PreInitialization"
Write-Log "Mounting OverlayFS"
if (-not (Test-Path -Path $Env:OVERLAY_DIR)) {
New-Item -ItemType Directory -Path $Env:OVERLAY_DIR | Out-Null
}
if (-not (Test-Path -Path $Env:SERVER_DIR)) {
New-Item -ItemType Directory -Path $Env:SERVER_DIR | Out-Null
}
if (-not (Test-Path -Path $Env:SERVER_ROOT)) {
New-Item -ItemType Directory -Path $Env:SERVER_ROOT | Out-Null
}
if (Test-Path -Path $Env:WORK_DIR) {
Write-Log "Removing existing work directory: $Env:WORK_DIR"
Remove-Item -Path $Env:WORK_DIR -Recurse -Force -ErrorAction SilentlyContinue
}
if (-not (Test-Path -Path $Env:WORK_DIR)) {
New-Item -ItemType Directory -Path $Env:WORK_DIR | Out-Null
Write-Log "Created work directory: $Env:WORK_DIR"
}
try {
Mount-Overlay -MountPoint $Env:SERVER_ROOT -LowerDir $Env:SERVER_DIR -UpperDir $Env:OVERLAY_DIR -WorkDir $Env:WORK_DIR -RequireEmptyWorkDir -OverlayOption "xino=on"
Write-Log "OverlayFS mounted successfully"
} catch {
Write-Log -Level Error "Failed to mount OverlayFS: $($_.Exception.Message)"
}
Write-Log "Chowning files to ${runUser}:${runGroup} (${runUid}:${runGid})"
chown -R "${runUser}:${runGroup}" /config
Invoke-Hook "PostInitialization"
# Setup nginx if enabled
if ($Env:HTTP_FILESERVER_ENABLED -eq "true" -or $Env:HTTP_FILESERVER_ENABLED -eq "1") {
Write-Log "HTTP fileserver is enabled, setting up web server..."
# Ensure web root directory exists
if (-not (Test-Path -Path $Env:HTTP_FILESERVER_WEB_ROOT)) {
New-Item -ItemType Directory -Path $Env:HTTP_FILESERVER_WEB_ROOT -Force | Out-Null
Write-Log "Created nginx web root directory: $Env:HTTP_FILESERVER_WEB_ROOT"
}
# Ensure nginx log directories exist
$nginxLogDir = "/var/log/nginx"
if (-not (Test-Path -Path $nginxLogDir)) {
New-Item -ItemType Directory -Path $nginxLogDir -Force | Out-Null
}
# Generate nginx configuration from template
$nginxConfigPath = "/etc/nginx/nginx.conf"
$nginxConfigTemplate = Get-Content -Path "/usr/local/share/nginx.conf.template" -Raw -ErrorAction SilentlyContinue
if ($nginxConfigTemplate) {
$nginxConfig = $nginxConfigTemplate -replace '\{\{HTTP_FILESERVER_WEB_ROOT\}\}', $Env:HTTP_FILESERVER_WEB_ROOT
# Add file pattern filter if configured
$filePatternFilter = ""
if ($Env:HTTP_FILESERVER_FILE_PATTERN) {
# Convert glob pattern to nginx regex
# Support patterns like: *.zip, *.exe, file*.txt, etc.
$pattern = $Env:HTTP_FILESERVER_FILE_PATTERN.Trim()
# If it's a simple extension pattern like *.ext, convert to regex
if ($pattern -match '^\*\.(.+)$') {
$extension = $Matches[1]
$escapedExt = [regex]::Escape($extension)
$regexPattern = "\.$escapedExt$"
}
# If it starts with * (like *.zip), convert to regex
elseif ($pattern.StartsWith('*.')) {
$extension = $pattern.Substring(2)
$escapedExt = [regex]::Escape($extension)
$regexPattern = "\.$escapedExt$"
}
# If it's already a regex pattern (contains regex special chars), use as-is
elseif ($pattern -match '[\[\]\(\)\^\$\+\{\}\|\\]') {
$regexPattern = $pattern
}
# Otherwise, treat as literal string and escape it
else {
$escapedPattern = [regex]::Escape($pattern)
$regexPattern = $escapedPattern
}
# Create nginx location block that denies files NOT matching the pattern
# Allow directories (ending with /) but deny files that don't match the pattern
$filePatternFilter = @"
# Deny files that don't match the pattern: $pattern
# Allow directories (for directory listing) but filter files
location ~ ^(?!.*/$)(?!.*$regexPattern).*$ {
deny all;
access_log off;
log_not_found off;
}
"@
Write-Log "File pattern filter enabled: $Env:HTTP_FILESERVER_FILE_PATTERN (regex: $regexPattern)"
}
$nginxConfig = $nginxConfig -replace '\{\{HTTP_FILESERVER_FILE_PATTERN_FILTER\}\}', $filePatternFilter
Set-Content -Path $nginxConfigPath -Value $nginxConfig -NoNewline
Write-Log "Generated nginx configuration with web root: $Env:HTTP_FILESERVER_WEB_ROOT"
} else {
Write-Log -Level Warning "Nginx config template not found, using default configuration"
}
# Test nginx configuration
$nginxTest = & nginx -t 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Log "Nginx configuration test passed"
# Start nginx as daemon
& nginx
if ($LASTEXITCODE -eq 0) {
Write-Log "Nginx started successfully on port 80"
} else {
Write-Log -Level Error "Failed to start nginx"
}
} else {
Write-Log -Level Error "Nginx configuration test failed: $nginxTest"
}
} else {
Write-Log "Nginx is disabled (HTTP_FILESERVER_ENABLED not set to 'true' or '1')"
}
Invoke-Hook "ServerStarted"
Set-Location -LiteralPath $Env:SERVER_ROOT
function Test-Int($v) {
$v -match '^\d+$'
}
function Split-CommandLine {
param([string]$CommandLine)
[regex]::Matches($CommandLine, '"[^"]*"|''[^'']*''|\S+') | ForEach-Object {
$v = $_.Value
if ($v -match '^"(.*)"$') { $Matches[1] }
elseif ($v -match "^'(.*)'$") { $Matches[1] }
else { $v }
}
}
function Resolve-StartExe {
param([Parameter(Mandatory)][string]$Exe)
# If it looks like a path (contains / or \) resolve it relative to current location
if ($Exe -match '[/\\]') {
$p = Resolve-Path -LiteralPath $Exe -ErrorAction SilentlyContinue
if ($p) { return $p.Path }
throw "START_EXE path not found (relative to $((Get-Location).Path)): $Exe"
}
# Try direct path existence (covers ./foo or foo in cwd without slashes)
if (Test-Path -LiteralPath $Exe) {
return (Resolve-Path -LiteralPath $Exe).Path
}
# Otherwise treat as PATH command
$cmd = Get-Command $Exe -ErrorAction SilentlyContinue
if ($cmd) { return $cmd.Source }
throw "START_EXE not found as file in cwd or command on PATH: $Exe"
}
$gosuTarget = if ($runUid -and (Test-Int $runUid)) {
if ($runGid -and (Test-Int $runGid)) { "${runUid}:${runGid}" } else { "$runUid" }
} elseif ($runUser) {
if ($runGroup) { "${runUser}:${runGroup}" } else { "$runUser" }
} else {
"lancommander:lancommander"
}
$startArgs = @()
if ($Env:START_ARGS) { $startArgs = Split-CommandLine $Env:START_ARGS }
$startExe = Resolve-StartExe $Env:START_EXE
Write-Host "WorkingDir: $((Get-Location).Path)"
Write-Host "Starting as ${gosuTarget}: $startExe $($startArgs -join ' ')"
& gosu $gosuTarget $startExe @startArgs
$exitCode = $LASTEXITCODE
Write-Log "Server has stopped (exit code: $exitCode)"
Invoke-Hook "ServerStopped"
exit $exitCode