Skip to content

Commit e24a701

Browse files
committed
update
1 parent a72285a commit e24a701

File tree

2 files changed

+109
-44
lines changed

2 files changed

+109
-44
lines changed

web/Modules/Caddy/App/Jobs/CaddyBuild.php

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ protected function generateCaddyfile(): void
162162
{
163163
$getAllDomains = Domain::whereNot('status', '<=>', 'broken')->get();
164164
$caddyBlocks = [];
165+
$wildcardGroups = [];
165166

166167
// Get Apache port settings (non-SSL ports for proxying)
167168
$apacheHttpPort = setting('caddy.apache_proxy_port') ?? setting('general.apache_http_port') ?? '8080';
@@ -174,45 +175,60 @@ protected function generateCaddyfile(): void
174175
$cloudflareApiToken = setting('caddy.cloudflare_api_token');
175176
$zeroSSlApiToken = setting('caddy.zerossl_api_token');
176177

177-
foreach ($getAllDomains as $domain) {
178-
$isBroken = false;
178+
// Check if wildcard is enabled
179+
$useWildcard = setting('caddy.enable_wildcard_ssl', false);
180+
$wildcardDomainSetting = setting('caddy.wildcard_domain');
179181

182+
// First pass - create regular blocks and identify wildcard subdomains
183+
foreach ($getAllDomains as $domain) {
180184
if ($domain->status === 'broken') {
181185
continue;
182186
}
183187

184188
// Check if domain is valid
185189
if (!filter_var($domain->domain, FILTER_VALIDATE_DOMAIN)) {
186-
$isBroken = true;
187-
}
188-
189-
if ($isBroken) {
190190
continue;
191191
}
192+
192193
$domainLog = '/var/log/caddy/' . $domain->domain . '.log';
193194
shell_exec("chown caddy:caddy '/var/log/caddy/");
194195
shell_exec("chmod -R 777 /var/log/caddy/");
195196

196-
197197
shell_exec("sudo setfacl -R -m u:caddy:rx " . $domain->document_root);
198198
shell_exec("sudo setfacl -R -m u:caddy:rx " . $domain->domain_public);
199199
shell_exec("sudo setfacl -R -m u:caddy:rx " . $domain->home_root);
200200

201-
202201
// Set permissions for Caddy to access user directories
203202
shell_exec("chmod o+x {$domain->home_root}");
204203
shell_exec("chmod -R o+rX {$domain->document_root}");
205204

206-
207205
if (!file_exists($domainLog)) {
208206
// Create log file for the domain if it doesn't exist
209207
touch($domainLog);
210208
shell_exec("chown caddy:caddy {$domainLog}");
211209
shell_exec("chmod 777 {$domainLog}");
212210
}
213211

212+
// Check if this domain belongs under a wildcard
213+
$isWildcardSubdomain = false;
214+
$parentDomain = null;
215+
216+
if ($useWildcard && $cloudflareApiToken && !empty($wildcardDomainSetting)) {
217+
if (strpos($domain->domain, '.' . $wildcardDomainSetting) !== false &&
218+
substr_count($domain->domain, '.') > substr_count($wildcardDomainSetting, '.')) {
219+
$isWildcardSubdomain = true;
220+
$parentDomain = $wildcardDomainSetting;
221+
222+
// Add to wildcard group
223+
if (!isset($wildcardGroups[$parentDomain])) {
224+
$wildcardGroups[$parentDomain] = [];
225+
}
226+
$wildcardGroups[$parentDomain][] = $this->createCaddyBlock($domain, $apacheHttpPort);
227+
continue;
228+
}
229+
}
214230

215-
// Create Caddy block for SSL termination and proxy to Apache
231+
// Non-wildcard domain, create regular block
216232
$caddyBlock = $this->createCaddyBlock($domain, $apacheHttpPort);
217233
if ($caddyBlock) {
218234
$caddyBlocks[] = $caddyBlock;
@@ -228,6 +244,16 @@ protected function generateCaddyfile(): void
228244
}
229245
}
230246

247+
// Add wildcard groups to the blocks list
248+
foreach ($wildcardGroups as $parentDomain => $subdomains) {
249+
$caddyBlocks[] = [
250+
'is_wildcard_group' => true,
251+
'parent_domain' => $parentDomain,
252+
'subdomains' => $subdomains,
253+
'cloudflareApiToken' => $cloudflareApiToken
254+
];
255+
}
256+
231257
// Generate Caddyfile
232258
$caddyfile = view('caddy::caddyfile-build', [
233259
'caddyBlocks' => $caddyBlocks,
@@ -255,25 +281,19 @@ private function createCaddyBlock(Domain $domain, $apacheHttpPort): ?array
255281
return null;
256282
}
257283

258-
//gi matcth the wilcard use tls_cloudflare
259-
260284
$useWildcard = setting('caddy.enable_wildcard_ssl', false);
261285
$cloudflareApiToken = setting('caddy.cloudflare_api_token');
262286
$wildcardDomainSettings = setting('caddy.wildcard_domain');
263287
$tls_cloudflare = false;
264288
$use_wildcard = false;
265-
$wildcardDomain = null;
266-
if ($useWildcard && $cloudflareApiToken && !empty($domain->domain)) {
267289

290+
if ($useWildcard && $cloudflareApiToken && !empty($domain->domain)) {
268291
if (!empty($wildcardDomainSettings) && strpos($domain->domain, $wildcardDomainSettings) !== false) {
269292
$tls_cloudflare = true;
270293
$use_wildcard = true;
271294
}
272-
273-
274295
}
275296

276-
277297
return array(
278298
'domain' => $domain->domain,
279299
'proxy_to' => "127.0.0.1:{$apacheHttpPort}",

web/Modules/Caddy/resources/views/caddyfile-build.blade.php

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33
admin off
44

55
@if(isset($cloudflareApiToken) && $cloudflareApiToken)
6-
76
# Set the ACME DNS challenge provider to use Cloudflare for all sites
87
acme_dns cloudflare {{ $cloudflareApiToken }}
9-
10-
118
@endif
12-
@if(isset($zeroSSlApiToken) && $zeroSSlApiToken)
139

10+
@if(isset($zeroSSlApiToken) && $zeroSSlApiToken)
1411
cert_issuer zerossl {{ $zeroSSlApiToken }}
15-
16-
1712
@endif
1813

1914
cert_issuer acme
20-
21-
2215
auto_https prefer_wildcard
2316

2417
# Global options
@@ -28,42 +21,93 @@
2821
}
2922

3023
@foreach($caddyBlocks as $block)
24+
@if(isset($block['is_wildcard_group']) && $block['is_wildcard_group'])
25+
# Wildcard domain configuration for *.{{ $block['parent_domain'] }}
26+
*.{{ $block['parent_domain'] }} {
27+
tls {
28+
dns cloudflare {{ $cloudflareApiToken }}
29+
}
30+
31+
@foreach($block['subdomains'] as $subdomain)
32+
@php
33+
$hostname = $subdomain['domain'];
34+
$matcherName = str_replace('.', '_', $hostname);
35+
@endphp
36+
@matchername_{!! $matcherName !!} host {{ $hostname }}
37+
handle @matchername_{!! $matcherName !!} {
38+
@if(setting('caddy.enable_static_files', false) && !empty($staticPaths) && isset($subdomain['document_root']))
39+
root * {{ $subdomain['document_root'] }}
40+
41+
42+
@staticfiles_{!! $matcherName !!} {
43+
@foreach(explode("\n", trim($staticPaths)) as $path)
44+
path {{ trim($path) }}
45+
@endforeach
46+
}
47+
48+
# Handle static files first
49+
handle @staticfiles_{!! $matcherName !!} {
50+
file_server
51+
}
52+
header @staticfiles_{!! $matcherName !!} {
53+
Cache-Control max-age=5184000
54+
ETag
55+
}
56+
@endif
57+
58+
# Proxy to Apache
59+
reverse_proxy {{ $subdomain['proxy_to'] }} {
60+
header_up Host {host}
61+
header_up X-Real-IP {remote_host}
62+
header_up X-Forwarded-Proto {scheme}
63+
}
64+
65+
# Enable compression
66+
encode zstd gzip
67+
68+
# Security headers
69+
header {
70+
-Server
71+
-X-Powered-By
72+
}
73+
74+
75+
}
76+
@endforeach
77+
78+
# Fallback for otherwise unhandled domains
79+
handle {
80+
respond "Subdomain not configured" 404
81+
}
82+
}
83+
@else
3184
# {{ $block['domain'] }} configuration
3285
{{ $block['domain'] }} {
33-
34-
@if(setting('caddy.enable_static_files', false) && !empty($staticPaths) and isset($block['document_root']))
86+
@if(setting('caddy.enable_static_files', false) && !empty($staticPaths) && isset($block['document_root']))
3587
# Static file paths for domain
3688
root * {{ $block['document_root'] ?? '/var/www/html' }}
3789

38-
@static_files {
39-
@foreach(explode("\n", trim($staticPaths)) as $path)
90+
@staticfiles {
91+
@foreach(explode("\n", trim($staticPaths)) as $path)
4092
path {{ trim($path) }}
41-
@endforeach
93+
@endforeach
4294
}
4395

4496
# Handle static files first
45-
handle @static_files {
97+
handle @staticfiles {
4698
file_server
4799
}
48-
header @static_files {
100+
header @staticfiles {
49101
Cache-Control max-age=5184000
50102
ETag
51103
}
104+
@endif
52105

53-
54-
@endif
55-
56-
57-
58-
@if(isset($block['use_wildcard']) && $block['use_wildcard'] && isset($block['tls_cloudflare']) && $block['tls_cloudflare'] && isset($block['cloudflareApiToken']) && $block['cloudflareApiToken'])
59-
106+
@if(isset($block['use_wildcard']) && $block['use_wildcard'] && isset($block['tls_cloudflare']) && $block['tls_cloudflare'])
60107
tls {
61-
dns cloudflare {
62-
api_token {{ $cloudflareApiToken }}
63-
}
108+
dns cloudflare
64109
}
65-
66-
@endif
110+
@endif
67111

68112
# Proxy remaining requests to Apache
69113
handle {
@@ -102,6 +146,7 @@
102146
redir https://{{ $block['domain'] }}{uri} permanent
103147
}
104148
@endif
149+
@endif
105150
@endforeach
106151

107152
# Catch-all for undefined domains

0 commit comments

Comments
 (0)