feat(settings): add Detect button for instance public IPs

Closes #11802

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai
2026-10-16 00:24:13 +02:00
co-authored by Claude Opus 5.5
parent f5c88f7e4f
commit ea483d179b
4 changed files with 150 additions and 25 deletions
+25
View File
@@ -201,6 +201,31 @@ class Index extends Component
}
}
/**
* Look up the public IPs of this host again and save them.
*/
public function detectPublicIps()
{
try {
$this->authorize('update', $this->settings);
$detected = detect_public_ips();
if ($detected === []) {
$this->dispatch('error', 'Could not detect a public IP address.');
return;
}
$this->settings->fill($detected);
$this->saveAndAudit();
$this->public_ipv4 = $this->settings->public_ipv4;
$this->public_ipv6 = $this->settings->public_ipv6;
$this->dispatch('success', 'Detected '.implode(' and ', $detected).'.');
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function buildHelperImage()
{
try {
+31 -23
View File
@@ -2684,38 +2684,46 @@ function get_public_ips(): void
{
try {
$settings = InstanceSettings::get();
$versions = array_filter(
['public_ipv4' => 4, 'public_ipv6' => 6],
fn (string $column) => blank($settings->$column),
ARRAY_FILTER_USE_KEY,
);
if ($versions === []) {
$columns = array_values(array_filter(['public_ipv4', 'public_ipv6'], fn (string $column) => blank($settings->$column)));
if ($columns === []) {
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");
}
});
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]);
$detected = detect_public_ips($columns);
if ($detected !== []) {
$settings->update($detected);
}
} catch (Throwable $e) {
echo "Error: {$e->getMessage()}\n";
}
}
/**
* Look up the public IPs of this host with ifconfig.io, without saving them.
*
* @param list<'public_ipv4'|'public_ipv6'> $columns
* @return array<'public_ipv4'|'public_ipv6', string> valid addresses only
*/
function detect_public_ips(array $columns = ['public_ipv4', 'public_ipv6']): array
{
$versions = array_intersect_key(['public_ipv4' => 4, 'public_ipv6' => 6], array_flip($columns));
$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");
}
});
$detected = [];
foreach ($versions as $column => $version) {
$ip = trim($results[$column]->output());
if (filter_var($ip, FILTER_VALIDATE_IP, $version === 4 ? FILTER_FLAG_IPV4 : FILTER_FLAG_IPV6) !== false) {
$detected[$column] = $ip;
}
}
return $detected;
}
function isAnyDeploymentInprogress(bool $showAll = false)
{
$runningJobs = ApplicationDeploymentQueue::where('horizon_job_worker', gethostname())->where('status', ApplicationDeploymentStatus::IN_PROGRESS->value)->get();
@@ -48,14 +48,20 @@
</x-application.settings-section>
<x-application.settings-section title="Network addresses">
<x-slot:actions>
<x-forms.button canGate="update" :canResource="$settings" type="button"
wire:click="detectPublicIps">
Detect
</x-forms.button>
</x-slot:actions>
<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="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."
helper="Coolify detects this address on start only when the field is empty. Set it when the detected address is wrong. Detect looks it up again and saves it."
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="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."
helper="Coolify detects this address on start only when the field is empty. Set it when the detected address is wrong. Detect looks it up again and saves it."
placeholder="2001:db8::1" autocomplete="new-password" />
</div>
</x-application.settings-section>
@@ -0,0 +1,86 @@
<?php
use App\Livewire\Settings\Index as SettingsIndex;
use App\Models\InstanceSettings;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Once;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
config()->set('constants.coolify.self_hosted', false);
InstanceSettings::forceCreate(['id' => 0, 'public_ipv4' => '203.0.113.21', 'public_ipv6' => '2001:db8::21']);
Once::flush();
$this->rootTeam = Team::factory()->create(['id' => 0]);
$this->user = User::factory()->create();
$this->rootTeam->members()->attach($this->user->id, ['role' => 'owner']);
$this->actingAs($this->user);
session(['currentTeam' => $this->rootTeam]);
Process::fake([
'curl -4s https://ifconfig.io' => "203.0.113.1\n",
'curl -6s https://ifconfig.io' => "2001:db8::1\n",
]);
});
it('saves the detected public IPs and shows them in the fields', function () {
Livewire::test(SettingsIndex::class)
->call('detectPublicIps')
->assertSet('public_ipv4', '203.0.113.1')
->assertSet('public_ipv6', '2001:db8::1')
->assertDispatched('success');
$settings = InstanceSettings::find(0);
expect($settings->public_ipv4)->toBe('203.0.113.1')
->and($settings->public_ipv6)->toBe('2001:db8::1');
});
it('keeps the current value of an address family that cannot be detected', function () {
Process::fake([
'curl -4s https://ifconfig.io' => "203.0.113.1\n",
'curl -6s https://ifconfig.io' => '',
]);
Livewire::test(SettingsIndex::class)
->call('detectPublicIps')
->assertSet('public_ipv4', '203.0.113.1')
->assertSet('public_ipv6', '2001:db8::21');
expect(InstanceSettings::find(0)->public_ipv6)->toBe('2001:db8::21');
});
it('shows an error when no address can be detected', function () {
Process::fake([
'curl -4s https://ifconfig.io' => '',
'curl -6s https://ifconfig.io' => '',
]);
Livewire::test(SettingsIndex::class)
->call('detectPublicIps')
->assertSet('public_ipv4', '203.0.113.21')
->assertSet('public_ipv6', '2001:db8::21')
->assertDispatched('error')
->assertNotDispatched('success');
});
it('does not let a user who is no longer an instance admin detect public IPs', function () {
$component = Livewire::test(SettingsIndex::class);
$this->rootTeam->members()->updateExistingPivot($this->user->id, ['role' => 'member']);
$this->actingAs($this->user->fresh());
Cache::flush();
Once::flush();
$component->call('detectPublicIps')
->assertDispatched('error')
->assertSet('public_ipv4', '203.0.113.21');
expect(InstanceSettings::find(0)->public_ipv4)->toBe('203.0.113.21');
Process::assertNothingRan();
});