Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

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

Andras Bacsai70d4dad4941c
diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php
index 6c359212af..136c34589e 100644
--- a/bootstrap/helpers/shared.php
+++ b/bootstrap/helpers/shared.php
@@ -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');
+ $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;
+ }
+
+ $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");
+ }
});
- $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;
+ foreach ($versions as $column => $version) {
+ $ip = trim($results[$column]->output());
+ if ($ip === '') {
+ continue;
}
- InstanceSettings::get()->update(['public_ipv4' => $ipv4]);
- }
- } 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";
+ if (filter_var($ip, FILTER_VALIDATE_IP, $version === 4 ? FILTER_FLAG_IPV4 : FILTER_FLAG_IPV6) === false) {
+ echo "Invalid ipv{$version}: $ip\n";
- return;
+ continue;
}
- InstanceSettings::get()->update(['public_ipv6' => $ipv6]);
+ $settings->update([$column => $ip]);
}
} catch (Throwable $e) {
echo "Error: {$e->getMessage()}\n";
diff --git a/resources/views/livewire/settings/index.blade.php b/resources/views/livewire/settings/index.blade.php
index 848642b6bb..d888145faa 100644
--- a/resources/views/livewire/settings/index.blade.php
+++ b/resources/views/livewire/settings/index.blade.php
@@ -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>
diff --git a/tests/Feature/GetPublicIpsTest.php b/tests/Feature/GetPublicIpsTest.php
new file mode 100644
index 0000000000..477a9d3ed0
--- /dev/null
+++ b/tests/Feature/GetPublicIpsTest.php
@@ -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');
+});