Files
ServerKit/frontend/src/components/security/FirewallTab.jsx
T
Juan Denis 19c727be4c Add Windows service support and new UI components
Agent: factor run logic into agentMainLoop and detect SCM launch; add Windows service dispatcher (service_windows.go) and POSIX stub (service_other.go) to properly handle SCM lifecycle and avoid Windows SCM error 1053. Frontend: add a set of presentational components (DangerZone, InfoList/InfoItem, JournalControls, LogViewer, MetricRow/MetricItem, ProcessTable, ProgressBar, ServiceCard, StatCard) and wire them into multiple pages and security tabs, replacing many ad-hoc info-list blocks. Also add a route for style-guide/:tab and update AgentFleet to use the new StatCard/StatsGrid layout. These changes improve service reliability on Windows and DRY up UI layouts with reusable components.
2026-04-30 23:11:26 -04:00

533 lines
25 KiB
React

import React, { useState, useEffect } from 'react';
import api from '../../services/api';
import ConfirmDialog from '../ConfirmDialog';
import { useToast } from '../../contexts/ToastContext';
import Modal from '../Modal';
import { InfoList, InfoItem } from '../InfoList';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Badge } from '@/components/ui/badge';
import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from '@/components/ui/select';
const FirewallTab = () => {
const [status, setStatus] = useState(null);
const [rules, setRules] = useState([]);
const [blockedIPs, setBlockedIPs] = useState([]);
const [loading, setLoading] = useState(true);
const [activeSubTab, setActiveSubTab] = useState('status');
const [showBlockIPModal, setShowBlockIPModal] = useState(false);
const [showPortModal, setShowPortModal] = useState(false);
const [showInstallModal, setShowInstallModal] = useState(false);
const [blockIP, setBlockIP] = useState('');
const [newPort, setNewPort] = useState({ port: '', protocol: 'tcp' });
const [selectedFirewall, setSelectedFirewall] = useState('ufw');
const [actionLoading, setActionLoading] = useState(false);
const [confirmDialog, setConfirmDialog] = useState(null);
const toast = useToast();
const commonPorts = [
{ port: 22, name: 'SSH', protocol: 'tcp' },
{ port: 80, name: 'HTTP', protocol: 'tcp' },
{ port: 443, name: 'HTTPS', protocol: 'tcp' },
{ port: 21, name: 'FTP', protocol: 'tcp' },
{ port: 25, name: 'SMTP', protocol: 'tcp' },
{ port: 3306, name: 'MySQL', protocol: 'tcp' },
{ port: 5432, name: 'PostgreSQL', protocol: 'tcp' },
{ port: 6379, name: 'Redis', protocol: 'tcp' },
{ port: 27017, name: 'MongoDB', protocol: 'tcp' },
];
useEffect(() => {
loadData();
}, []);
const loadData = async () => {
setLoading(true);
try {
await Promise.all([loadStatus(), loadRules(), loadBlockedIPs()]);
} catch (error) {
console.error('Failed to load firewall data:', error);
} finally {
setLoading(false);
}
};
const loadStatus = async () => {
try {
const data = await api.getFirewallStatus();
setStatus(data);
} catch (error) {
console.error('Failed to load status:', error);
}
};
const loadRules = async () => {
try {
const data = await api.getFirewallRules();
setRules(data.rules || []);
} catch (error) {
console.error('Failed to load rules:', error);
}
};
const loadBlockedIPs = async () => {
try {
const data = await api.getBlockedIPs();
setBlockedIPs(data.blocked_ips || []);
} catch (error) {
console.error('Failed to load blocked IPs:', error);
}
};
const handleEnable = async () => {
setActionLoading(true);
try {
await api.enableFirewall();
toast.success('Firewall enabled');
await loadStatus();
} catch (error) {
toast.error(`Failed to enable firewall: ${error.message}`);
} finally {
setActionLoading(false);
}
};
const handleDisable = async () => {
setConfirmDialog({
title: 'Disable Firewall',
message: 'Are you sure you want to disable the firewall? This will leave your server unprotected.',
confirmText: 'Disable',
variant: 'danger',
onConfirm: async () => {
setActionLoading(true);
try {
await api.disableFirewall();
toast.success('Firewall disabled');
await loadStatus();
} catch (error) {
toast.error(`Failed to disable firewall: ${error.message}`);
} finally {
setActionLoading(false);
setConfirmDialog(null);
}
},
onCancel: () => setConfirmDialog(null)
});
};
const handleBlockIP = async () => {
if (!blockIP.trim()) return;
setActionLoading(true);
try {
await api.blockIP(blockIP);
toast.success(`IP ${blockIP} blocked`);
setShowBlockIPModal(false);
setBlockIP('');
await loadBlockedIPs();
await loadRules();
} catch (error) {
toast.error(`Failed to block IP: ${error.message}`);
} finally {
setActionLoading(false);
}
};
const handleUnblockIP = async (ip) => {
setConfirmDialog({
title: 'Unblock IP',
message: `Are you sure you want to unblock ${ip}?`,
confirmText: 'Unblock',
variant: 'warning',
onConfirm: async () => {
try {
await api.unblockIP(ip);
toast.success(`IP ${ip} unblocked`);
await loadBlockedIPs();
await loadRules();
} catch (error) {
toast.error(`Failed to unblock IP: ${error.message}`);
}
setConfirmDialog(null);
},
onCancel: () => setConfirmDialog(null)
});
};
const handleAllowPort = async () => {
if (!newPort.port) return;
setActionLoading(true);
try {
await api.allowPort(parseInt(newPort.port), newPort.protocol);
toast.success(`Port ${newPort.port}/${newPort.protocol} allowed`);
setShowPortModal(false);
setNewPort({ port: '', protocol: 'tcp' });
await loadRules();
} catch (error) {
toast.error(`Failed to allow port: ${error.message}`);
} finally {
setActionLoading(false);
}
};
const handleQuickAllowPort = async (port, protocol) => {
setActionLoading(true);
try {
await api.allowPort(port, protocol);
toast.success(`Port ${port}/${protocol} allowed`);
await loadRules();
} catch (error) {
toast.error(`Failed to allow port: ${error.message}`);
} finally {
setActionLoading(false);
}
};
const handleRemovePort = async (port, protocol) => {
setConfirmDialog({
title: 'Remove Port Rule',
message: `Are you sure you want to remove the rule for port ${port}/${protocol}?`,
confirmText: 'Remove',
variant: 'danger',
onConfirm: async () => {
try {
await api.denyPort(parseInt(port), protocol);
toast.success(`Port ${port}/${protocol} rule removed`);
await loadRules();
} catch (error) {
toast.error(`Failed to remove port: ${error.message}`);
}
setConfirmDialog(null);
},
onCancel: () => setConfirmDialog(null)
});
};
const handleInstall = async () => {
setActionLoading(true);
try {
await api.installFirewall(selectedFirewall);
toast.success(`${selectedFirewall.toUpperCase()} installed successfully`);
setShowInstallModal(false);
await loadData();
} catch (error) {
toast.error(`Failed to install firewall: ${error.message}`);
} finally {
setActionLoading(false);
}
};
const isActive = status?.any_active;
const activeFirewall = status?.active_firewall;
if (loading) {
return <div className="loading-sm">Loading firewall status...</div>;
}
return (
<div className="firewall-tab">
{!status?.any_installed ? (
<div className="empty-state">
<svg viewBox="0 0 24 24" width="48" height="48" stroke="currentColor" fill="none" strokeWidth="1">
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
</svg>
<h3>No Firewall Installed</h3>
<p>Install a firewall to protect your server from unauthorized access.</p>
<Button variant="default" onClick={() => setShowInstallModal(true)}>
Install Firewall
</Button>
</div>
) : (
<>
<div className="firewall-header">
<div className="firewall-status-row">
<div className={`status-indicator ${isActive ? 'active' : 'inactive'}`}>
<svg viewBox="0 0 24 24" width="20" height="20" stroke="currentColor" fill="none" strokeWidth="2">
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
</svg>
<span>{isActive ? 'Firewall Active' : 'Firewall Inactive'}</span>
<span className="firewall-type">({activeFirewall?.toUpperCase()})</span>
</div>
<div className="firewall-actions">
<Button variant="outline" size="sm" onClick={() => setShowBlockIPModal(true)}>
Block IP
</Button>
<Button variant="outline" size="sm" onClick={() => setShowPortModal(true)}>
Allow Port
</Button>
{isActive ? (
<Button variant="destructive" size="sm" onClick={handleDisable} disabled={actionLoading}>
Disable
</Button>
) : (
<Button variant="default" size="sm" onClick={handleEnable} disabled={actionLoading}>
Enable
</Button>
)}
</div>
</div>
</div>
<div className="firewall-stats">
<div className="stat-mini">
<span className="stat-value">{rules.length}</span>
<span className="stat-label">Rules</span>
</div>
<div className="stat-mini">
<span className="stat-value">{blockedIPs.length}</span>
<span className="stat-label">Blocked IPs</span>
</div>
<div className="stat-mini">
<span className="stat-value">{rules.filter(r => r.type === 'port' || r.port).length}</span>
<span className="stat-label">Ports Open</span>
</div>
</div>
<div className="subtabs">
<button className={`subtab ${activeSubTab === 'status' ? 'active' : ''}`} onClick={() => setActiveSubTab('status')}>
Status
</button>
<button className={`subtab ${activeSubTab === 'rules' ? 'active' : ''}`} onClick={() => setActiveSubTab('rules')}>
Rules
</button>
<button className={`subtab ${activeSubTab === 'blocked' ? 'active' : ''}`} onClick={() => setActiveSubTab('blocked')}>
Blocked IPs
</button>
<button className={`subtab ${activeSubTab === 'quick' ? 'active' : ''}`} onClick={() => setActiveSubTab('quick')}>
Quick Ports
</button>
</div>
{activeSubTab === 'status' && (
<div className="card">
<div className="card-header">
<h3>Firewall Information</h3>
<Button variant="outline" size="sm" onClick={loadData}>Refresh</Button>
</div>
<div className="card-body">
<InfoList>
<InfoItem label="Type" value={activeFirewall?.toUpperCase()} />
<InfoItem label="Status">
<Badge variant={isActive ? 'success' : 'destructive'}>
{isActive ? 'Active' : 'Inactive'}
</Badge>
</InfoItem>
{activeFirewall === 'firewalld' && status?.firewalld?.default_zone && (
<InfoItem label="Default Zone" value={status.firewalld.default_zone} />
)}
</InfoList>
</div>
</div>
)}
{activeSubTab === 'rules' && (
<div className="card">
<div className="card-header">
<h3>Firewall Rules</h3>
<Button variant="default" size="sm" onClick={() => setShowPortModal(true)}>Add Rule</Button>
</div>
<div className="card-body">
{rules.length === 0 ? (
<p className="text-muted">No rules configured</p>
) : (
<table className="table">
<thead>
<tr>
<th>Type</th>
<th>Target</th>
<th>Protocol</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{rules.map((rule, index) => (
<tr key={index}>
<td><Badge variant="info">{rule.type}</Badge></td>
<td>
{rule.type === 'service' && rule.service}
{rule.type === 'port' && rule.port}
{rule.type === 'rich' && <code>{rule.rule}</code>}
</td>
<td>{rule.protocol || '-'}</td>
<td>
{rule.type === 'port' && (
<Button variant="destructive" size="sm" onClick={() => handleRemovePort(rule.port, rule.protocol)}>
Remove
</Button>
)}
</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
)}
{activeSubTab === 'blocked' && (
<div className="card">
<div className="card-header">
<h3>Blocked IP Addresses</h3>
<Button variant="default" size="sm" onClick={() => setShowBlockIPModal(true)}>Block IP</Button>
</div>
<div className="card-body">
{blockedIPs.length === 0 ? (
<div className="empty-state-sm">
<p>No blocked IPs</p>
</div>
) : (
<div className="blocked-list">
{blockedIPs.map((item, index) => (
<div key={index} className="blocked-item">
<div className="blocked-info">
<span className="blocked-ip">{item.ip}</span>
</div>
<Button variant="secondary" size="sm" onClick={() => handleUnblockIP(item.ip)}>
Unblock
</Button>
</div>
))}
</div>
)}
</div>
</div>
)}
{activeSubTab === 'quick' && (
<div className="card">
<div className="card-header">
<h3>Quick Port Access</h3>
</div>
<div className="card-body">
<p className="text-muted" style={{ marginBottom: '1rem' }}>One-click enable/disable common service ports</p>
<div className="quick-ports-grid">
{commonPorts.map(({ port, name, protocol }) => {
const isAllowed = rules.some(r =>
(r.port === String(port) || r.port === port) && r.protocol === protocol
);
return (
<div key={port} className="quick-port-card">
<div className="port-info">
<span className="port-name">{name}</span>
<span className="port-number">{port}/{protocol}</span>
</div>
{isAllowed ? (
<Button variant="destructive" size="sm" onClick={() => handleRemovePort(port, protocol)} disabled={actionLoading}>
Block
</Button>
) : (
<Button variant="default" size="sm" onClick={() => handleQuickAllowPort(port, protocol)} disabled={actionLoading}>
Allow
</Button>
)}
</div>
);
})}
</div>
</div>
</div>
)}
</>
)}
{/* Block IP Modal */}
<Modal open={showBlockIPModal} onClose={() => setShowBlockIPModal(false)} title="Block IP Address">
<div className="form-group">
<Label>IP Address</Label>
<Input
type="text"
value={blockIP}
onChange={(e) => setBlockIP(e.target.value)}
placeholder="192.168.1.100 or 10.0.0.0/24"
/>
</div>
<p className="text-muted">You can block a single IP or a range using CIDR notation.</p>
<div className="modal-footer">
<Button variant="outline" onClick={() => setShowBlockIPModal(false)}>Cancel</Button>
<Button variant="destructive" onClick={handleBlockIP} disabled={actionLoading || !blockIP.trim()}>
{actionLoading ? 'Blocking...' : 'Block IP'}
</Button>
</div>
</Modal>
{/* Allow Port Modal */}
<Modal open={showPortModal} onClose={() => setShowPortModal(false)} title="Allow Port">
<div className="form-row">
<div className="form-group">
<Label>Port Number</Label>
<Input
type="number"
value={newPort.port}
onChange={(e) => setNewPort({ ...newPort, port: e.target.value })}
placeholder="8080"
min="1"
max="65535"
/>
</div>
<div className="form-group">
<Label>Protocol</Label>
<Select value={newPort.protocol} onValueChange={(value) => setNewPort({ ...newPort, protocol: value })}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="tcp">TCP</SelectItem>
<SelectItem value="udp">UDP</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<div className="modal-footer">
<Button variant="outline" onClick={() => setShowPortModal(false)}>Cancel</Button>
<Button variant="default" onClick={handleAllowPort} disabled={actionLoading || !newPort.port}>
{actionLoading ? 'Adding...' : 'Allow Port'}
</Button>
</div>
</Modal>
{/* Install Firewall Modal */}
<Modal open={showInstallModal} onClose={() => setShowInstallModal(false)} title="Install Firewall">
<div className="form-group">
<Label>Select Firewall</Label>
<Select value={selectedFirewall} onValueChange={setSelectedFirewall}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="ufw">UFW (Recommended for Ubuntu)</SelectItem>
<SelectItem value="firewalld">firewalld (CentOS/RHEL)</SelectItem>
</SelectContent>
</Select>
</div>
<div className="install-info">
{selectedFirewall === 'ufw' ? (
<p><strong>UFW (Uncomplicated Firewall)</strong> is simple and easy to use for Ubuntu/Debian systems.</p>
) : (
<p><strong>firewalld</strong> is a dynamically managed firewall with zone-based configuration for CentOS/RHEL.</p>
)}
</div>
<div className="modal-footer">
<Button variant="outline" onClick={() => setShowInstallModal(false)}>Cancel</Button>
<Button variant="default" onClick={handleInstall} disabled={actionLoading}>
{actionLoading ? 'Installing...' : 'Install'}
</Button>
</div>
</Modal>
{confirmDialog && (
<ConfirmDialog
title={confirmDialog.title}
message={confirmDialog.message}
confirmText={confirmDialog.confirmText}
variant={confirmDialog.variant}
onConfirm={confirmDialog.onConfirm}
onCancel={confirmDialog.onCancel}
/>
)}
</div>
);
};
export default FirewallTab;