fix(settings): keep manually set instance public IPs on restart

Closes #11802

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai
2026-10-16 00:16:20 +02:00
co-authored by Claude Opus 5.5
parent 97f451cc2a
commit 70d4dad494
3 changed files with 90 additions and 29 deletions
+28 -27
View File
@@ -2677,38 +2677,39 @@ function deduplicateAllowlist(array $entries): array
return $result;
}
function get_public_ips()
/**
* Detect the instance public IPs with ifconfig.io. Only empty settings are filled, so an address saved in Settings is kept.
*/
function get_public_ips(): void
{
try {
[$first, $second] = Process::concurrently(function (Pool $pool) {
$pool->path(__DIR__)->command('curl -4s https://ifconfig.io');
$pool->path(__DIR__)->command('curl -6s https://ifconfig.io');
});
$ipv4 = $first->output();
if ($ipv4) {
$ipv4 = trim($ipv4);
$validate_ipv4 = filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
if ($validate_ipv4 == false) {
echo "Invalid ipv4: $ipv4\n";
return;
}
InstanceSettings::get()->update(['public_ipv4' => $ipv4]);
$settings = InstanceSettings::get();
$versions = array_filter(
['public_ipv4' => 4, 'public_ipv6' => 6],
fn (string $column) => blank($settings->$column),
ARRAY_FILTER_USE_KEY,
);
if ($versions === []) {
return;
}
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
try {
$ipv6 = $second->output();
if ($ipv6) {
$ipv6 = trim($ipv6);
$validate_ipv6 = filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
if ($validate_ipv6 == false) {
echo "Invalid ipv6: $ipv6\n";
return;
$results = Process::concurrently(function (Pool $pool) use ($versions) {
foreach ($versions as $column => $version) {
$pool->as($column)->path(__DIR__)->command("curl -{$version}s https://ifconfig.io");
}
InstanceSettings::get()->update(['public_ipv6' => $ipv6]);
});
foreach ($versions as $column => $version) {
$ip = trim($results[$column]->output());
if ($ip === '') {
continue;
}
if (filter_var($ip, FILTER_VALIDATE_IP, $version === 4 ? FILTER_FLAG_IPV4 : FILTER_FLAG_IPV6) === false) {
echo "Invalid ipv{$version}: $ip\n";
continue;
}
$settings->update([$column => $ip]);
}
} catch (Throwable $e) {
echo "Error: {$e->getMessage()}\n";
@@ -51,11 +51,11 @@
<div class="grid gap-4 lg:grid-cols-2">
<x-forms.input canGate="update" :canResource="$settings" id="public_ipv4" type="password"
label="Instance public IPv4"
helper="Set this when Coolify cannot detect the correct public IPv4 address."
helper="Coolify detects this address on start only when the field is empty. Set it when the detected address is wrong. Clear it and restart Coolify to detect it again."
placeholder="1.2.3.4" autocomplete="new-password" />
<x-forms.input canGate="update" :canResource="$settings" id="public_ipv6" type="password"
label="Instance public IPv6"
helper="Set this when Coolify cannot detect the correct public IPv6 address."
helper="Coolify detects this address on start only when the field is empty. Set it when the detected address is wrong. Clear it and restart Coolify to detect it again."
placeholder="2001:db8::1" autocomplete="new-password" />
</div>
</x-application.settings-section>
+60
View File
@@ -0,0 +1,60 @@
<?php
use App\Models\InstanceSettings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Process;
uses(RefreshDatabase::class);
beforeEach(function () {
Process::fake([
'curl -4s https://ifconfig.io' => "203.0.113.1\n",
'curl -6s https://ifconfig.io' => "2001:db8::1\n",
]);
});
it('detects the public IPs when the settings are empty', function () {
InstanceSettings::forceCreate(['id' => 0]);
get_public_ips();
$settings = InstanceSettings::find(0);
expect($settings->public_ipv4)->toBe('203.0.113.1')
->and($settings->public_ipv6)->toBe('2001:db8::1');
});
it('keeps public IPs that are already set', function () {
InstanceSettings::forceCreate(['id' => 0, 'public_ipv4' => '203.0.113.21', 'public_ipv6' => '2001:db8::21']);
get_public_ips();
$settings = InstanceSettings::find(0);
expect($settings->public_ipv4)->toBe('203.0.113.21')
->and($settings->public_ipv6)->toBe('2001:db8::21');
Process::assertNothingRan();
});
it('detects only the address family that is empty', function () {
InstanceSettings::forceCreate(['id' => 0, 'public_ipv4' => '203.0.113.21']);
get_public_ips();
$settings = InstanceSettings::find(0);
expect($settings->public_ipv4)->toBe('203.0.113.21')
->and($settings->public_ipv6)->toBe('2001:db8::1');
Process::assertNotRan('curl -4s https://ifconfig.io');
});
it('still detects IPv6 when the IPv4 lookup returns an invalid address', function () {
Process::fake([
'curl -4s https://ifconfig.io' => 'not-an-ip',
'curl -6s https://ifconfig.io' => "2001:db8::1\n",
]);
InstanceSettings::forceCreate(['id' => 0]);
get_public_ips();
$settings = InstanceSettings::find(0);
expect($settings->public_ipv4)->toBeNull()
->and($settings->public_ipv6)->toBe('2001:db8::1');
});