[修改]优化了dns地址添加方法,增添了错误提示消息
This commit is contained in:
@@ -6,10 +6,12 @@ use App\Services\NetworkService;
|
||||
use App\Settings\NetworkSettings;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Pages\SettingsPage;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Fieldset;
|
||||
use Filament\Forms\Components\TagsInput;
|
||||
|
||||
class ManageNetwork extends SettingsPage
|
||||
{
|
||||
@@ -44,15 +46,16 @@ class ManageNetwork extends SettingsPage
|
||||
$set('ip', $interfaces[$state]['ip'] ?? '无IP地址');
|
||||
$set('mask', $interfaces[$state]['mask'] ?? '无子网掩码');
|
||||
$set('gateway', $interfaces[$state]['gateway']);
|
||||
$set('dns_server_1', $interfaces[$state]['dns'][0] ?? null);
|
||||
$set('dns_server_2', $interfaces[$state]['dns'][1] ?? null);
|
||||
// $set('dns_server_1', $interfaces[$state]['dns'][0] ?? null);
|
||||
// $set('dns_server_2', $interfaces[$state]['dns'][1] ??
|
||||
$set('dns_servers', $interfaces[$state]['dns'] ?? []);
|
||||
|
||||
error_log('state:' . $state);
|
||||
error_log('dhcp_enabled: ' . $dhcp_status);
|
||||
error_log("ip " . $interfaces[$state]['ip']);
|
||||
error_log("mask " . $interfaces[$state]['mask']);
|
||||
error_log("gateway " . $interfaces[$state]['gateway']);
|
||||
error_log("dns_server_1 " . print_r($interfaces[$state]['dns'], true));
|
||||
error_log("dns_servers " . print_r($interfaces[$state]['dns'], true));
|
||||
}
|
||||
else{
|
||||
error_log("未找到接口:" . $state);
|
||||
@@ -73,6 +76,7 @@ class ManageNetwork extends SettingsPage
|
||||
TextInput::make('ip')
|
||||
->label("IP地址")
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required()
|
||||
->reactive(),
|
||||
Select::make('mask')
|
||||
->label('子网掩码')
|
||||
@@ -108,16 +112,38 @@ class ManageNetwork extends SettingsPage
|
||||
->reactive(),
|
||||
TextInput::make('gateway')
|
||||
->label('网关')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required(),
|
||||
TextInput::make('dns_server_1')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true'),
|
||||
TagsInput::make('dns_servers')
|
||||
->label('DNS地址')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required(),
|
||||
TextInput::make('dns_server_2')
|
||||
->label('DNS备用地址')
|
||||
->disabled(fn($get) => $get('dhcp_enabled') === 'true')
|
||||
->required(),
|
||||
->reactive()
|
||||
->afterStateUpdated(function ($state, callable $set) {
|
||||
// 保留合法的DNS地址
|
||||
$validDns = array_filter($state, function ($dns) {
|
||||
// 验证是否是合法的IPv4地址
|
||||
return filter_var($dns, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
|
||||
});
|
||||
|
||||
// 如果有非法的DNS地址,显示提示并移除它们
|
||||
if (count($validDns) !== count($state)) {
|
||||
Notification::make()
|
||||
->title('无效的DNS地址 已自动移除')
|
||||
->warning() // 设置消息类型为警告
|
||||
->send();
|
||||
}
|
||||
|
||||
// 限制最多只能有3个合法的DNS地址
|
||||
if (count($validDns) > 3) {
|
||||
$validDns = array_slice($validDns, 0, 3);
|
||||
Notification::make()
|
||||
->title('最多只能输入3个DNS地址')
|
||||
->warning()
|
||||
->send();
|
||||
}
|
||||
|
||||
// 更新字段状态
|
||||
$set('dns_servers', $validDns);
|
||||
})
|
||||
])
|
||||
->visible(fn($get) => !is_null($get('network_interface'))) // 检查是否有选择的接口
|
||||
->reactive(),
|
||||
@@ -128,6 +154,30 @@ class ManageNetwork extends SettingsPage
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
// 获取用户输入的网络配置
|
||||
$ipAddress = $this->form->getState()['ip'] ?? '';
|
||||
$gateway = $this->form->getState()['gateway'] ?? '';
|
||||
|
||||
// 验证 IP 地址格式
|
||||
if (!filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
Notification::make()
|
||||
->title('无效的 IP 地址格式')
|
||||
->warning()
|
||||
->send();
|
||||
|
||||
return; // 返回,不执行保存操作
|
||||
}
|
||||
|
||||
// 验证网关地址格式
|
||||
if (!filter_var($gateway, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
Notification::make()
|
||||
->title('无效的网关地址格式')
|
||||
->warning()
|
||||
->send();
|
||||
|
||||
return; // 返回,不执行保存操作
|
||||
}
|
||||
|
||||
// 调用父类的保存方法以持久化设置
|
||||
parent::save();
|
||||
|
||||
@@ -144,11 +194,15 @@ class ManageNetwork extends SettingsPage
|
||||
error_log('接口名称:' . $networkInterface);
|
||||
error_log('dhcp:' . $dhcpEnabled);
|
||||
|
||||
$ipAddress = $this->form->getState()['ip_address'] ?? '';
|
||||
$ipAddress = $this->form->getState()['ip'] ?? '';
|
||||
$mask = $this->form->getState()['mask'] ?? '';
|
||||
$gateway = $this->form->getState()['gateway'] ?? '';
|
||||
$dns1 = $this->form->getState()['dns_server_1'] ?? '';
|
||||
$dns2 = $this->form->getState()['dns_server_2'] ?? '';
|
||||
// $dns1 = $this->form->getState()['dns_server_1'] ?? '';
|
||||
// $dns2 = $this->form->getState()['dns_server_2'] ?? '';
|
||||
|
||||
$dnsServers = $this->form->getState()['dns_servers'] ?? [];
|
||||
$dnsString = implode(',', $dnsServers); // 合并 DNS 地址为字符串
|
||||
|
||||
if ($dhcpEnabled=='true') {
|
||||
|
||||
// 如果启用 DHCP,设置为使用 DHCP
|
||||
@@ -176,7 +230,7 @@ class ManageNetwork extends SettingsPage
|
||||
} else {
|
||||
// 根据接口和用户输入构建命令
|
||||
$commands = [
|
||||
"nmcli con mod '$networkInterface' ipv4.method manual ipv4.addresses $ipAddress/$mask ipv4.gateway $gateway ipv4.dns '$dns1 $dns2'",
|
||||
"nmcli con mod '$networkInterface' ipv4.method manual ipv4.addresses $ipAddress/$mask ipv4.gateway $gateway ipv4.dns $dnsString",
|
||||
"nmcli con down '$networkInterface'",
|
||||
"nmcli con up '$networkInterface'", // 重新激活连接
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user