firewall: remove comments for now - only causes trouble - add rule type IN or OUT

This commit is contained in:
Crivion
2025-10-22 14:27:11 +03:00
parent f24e8eeab4
commit dd12afe087
5 changed files with 26 additions and 28 deletions

View File

@@ -4,28 +4,22 @@ namespace App\Actions\Firewall;
class BuildUfwRuleSpecAction
{
public function execute(string $protocol, string $from, string $to, int $port, ?string $comment = null): string
public function execute(string $direction, string $protocol, string $from, string $to, int $port): string
{
$direction = strtolower(trim($direction));
$protocol = strtolower(trim($protocol));
$from = trim($from);
$to = trim($to);
$port = (int) $port;
$comment = trim((string) ($comment ?? ''));
$parts = [
$direction,
'proto ' . $protocol,
'from ' . $from,
'to ' . $to,
'port ' . $port,
];
$spec = implode(' ', $parts);
if ($comment !== '') {
$commentEscaped = str_replace("'", "\\'", $comment);
$spec .= " comment '" . $commentEscaped . "'";
}
return $spec;
return implode(' ', $parts);
}
}

View File

@@ -20,4 +20,4 @@ class ToggleUfwAction
}
return trim($proc->output());
}
}
}p

View File

@@ -37,11 +37,11 @@ class FirewallController extends Controller
{
$validated = $request->validated();
$spec = (new BuildUfwRuleSpecAction())->execute(
strtolower($validated['direction']),
strtolower($validated['protocol']),
trim($validated['ip']),
trim($validated['to']),
(int) $validated['port'],
$validated['comment'] ?? ''
(int) $validated['port']
);
if ($validated['type'] === 'allow') {

View File

@@ -15,11 +15,11 @@ class CreateFirewallRuleRequest extends FormRequest
{
return [
'type' => ['required', 'string', 'in:allow,deny'],
'direction' => ['required', 'string', 'in:in,out'],
'protocol' => ['required', 'string', 'in:tcp,udp'],
'port' => ['required', 'integer', 'min:1', 'max:65535'],
'ip' => ['required', 'string'],
'to' => ['required', 'string'],
'comment' => ['nullable', 'string', 'max:150'],
];
}

View File

@@ -21,11 +21,11 @@ export default function CreateFirewallRuleForm() {
clearErrors,
} = useForm({
type: 'allow',
direction: 'in',
protocol: 'tcp',
port: '',
ip: 'any',
to: 'any',
comment: '',
});
const openModal = () => setShowModal(true);
@@ -33,7 +33,7 @@ export default function CreateFirewallRuleForm() {
const closeModal = () => {
setShowModal(false);
clearErrors();
reset({ type: 'allow', protocol: 'tcp', port: '', ip: 'any', to: 'any', comment: '' });
reset({ type: 'allow', direction: 'in', protocol: 'tcp', port: '', ip: 'any', to: 'any' });
};
const submit = (e) => {
@@ -74,6 +74,21 @@ export default function CreateFirewallRuleForm() {
<InputError message={errors.type} className="mt-2" />
</div>
<div>
<InputLabel htmlFor="direction" value="Direction" className='my-2' />
<select
id="direction"
name="direction"
value={data.direction}
onChange={(e) => setData('direction', e.target.value)}
className="mt-1 block w-full flex-1 bg-gray-100 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 p-2 dark:bg-gray-800 dark:border-gray-600 dark:text-white"
>
<option value="in">IN</option>
<option value="out">OUT</option>
</select>
<InputError message={errors.direction} className="mt-2" />
</div>
<div>
<InputLabel htmlFor="protocol" value="Protocol" className='my-2' />
<select
@@ -132,18 +147,7 @@ export default function CreateFirewallRuleForm() {
<InputError message={errors.to} className="mt-2" />
</div>
<div>
<InputLabel htmlFor="comment" value="Comment" className='my-2' />
<TextInput
id="comment"
name="comment"
value={data.comment}
onChange={(e) => setData('comment', e.target.value)}
className="mt-1 block w-full"
placeholder="Optional"
/>
<InputError message={errors.comment} className="mt-2" />
</div>
<div className="flex justify-end pb-6">
<PrimaryButton className="mr-3" disabled={processing}>