File size: 8,310 Bytes
a21c316 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { request as invoke } from '../../utils/request';
import { Search, AlertTriangle } from 'lucide-react';
interface IpAccessLog {
id: string;
client_ip: string;
timestamp: number;
method?: string;
path?: string;
user_agent?: string;
status?: number;
duration?: number;
api_key_hash?: string;
blocked: boolean;
block_reason?: string;
username?: string;
}
interface IpAccessLogResponse {
logs: IpAccessLog[];
total: number;
}
interface Props {
refreshKey?: number;
}
export const IpAccessLogs: React.FC<Props> = ({ refreshKey }) => {
const { t } = useTranslation();
const [logs, setLogs] = useState<IpAccessLog[]>([]);
const [total, setTotal] = useState(0);
const [loading, setLoading] = useState(false);
const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(50);
const [search, setSearch] = useState('');
const [blockedOnly, setBlockedOnly] = useState(false);
const loadLogs = async () => {
setLoading(true);
try {
const res = await invoke<IpAccessLogResponse>('get_ip_access_logs', {
page,
pageSize: pageSize,
search: search || undefined,
blockedOnly: blockedOnly,
});
setLogs(res.logs);
setTotal(res.total);
} catch (e) {
console.error('Failed to load logs', e);
} finally {
setLoading(false);
}
};
useEffect(() => {
loadLogs();
}, [page, pageSize, blockedOnly, refreshKey]);
// Handle search on enter or blur
const handleSearch = () => {
setPage(1);
loadLogs();
};
return (
<div className="flex flex-col h-full bg-white dark:bg-base-100 rounded-xl">
{/* Toolbar */}
<div className="p-5 border-b border-gray-100 dark:border-base-200 flex flex-wrap items-center gap-6">
<div className="relative flex-1 min-w-[200px] max-w-md">
<Search className="absolute left-3 top-2.5 text-gray-400" size={16} />
<input
type="text"
placeholder={t('security.logs.search_placeholder')}
className="input input-sm input-bordered w-full pl-9"
value={search}
onChange={(e) => setSearch(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleSearch()}
onBlur={handleSearch}
/>
</div>
<label className="label cursor-pointer gap-2 shrink-0">
<span className="label-text text-xs font-bold text-gray-500 uppercase">{t('security.logs.show_blocked_only')}</span>
<input
type="checkbox"
className="toggle toggle-sm toggle-error"
checked={blockedOnly}
onChange={(e) => setBlockedOnly(e.target.checked)}
/>
</label>
<div className="flex-1"></div>
<div className="flex items-center gap-3 shrink-0">
<select
className="select select-sm select-bordered min-w-[100px]"
value={pageSize}
onChange={(e) => setPageSize(Number(e.target.value))}
>
<option value="20">20{t('security.logs.per_page_suffix')}</option>
<option value="50">50{t('security.logs.per_page_suffix')}</option>
<option value="100">100{t('security.logs.per_page_suffix')}</option>
</select>
</div>
</div>
{/* Table */}
<div className="flex-1 overflow-auto">
<table className="table table-xs w-full">
<thead className="sticky top-0 bg-gray-100 dark:bg-base-200 z-10 shadow-sm text-gray-600 dark:text-gray-400">
<tr>
<th className="w-20">{t('security.logs.status')}</th>
<th className="w-32">{t('security.logs.ip_address')}</th>
<th className="w-24">{t('security.logs.username', 'User')}</th>
<th className="w-20">{t('security.logs.method')}</th>
<th className="">{t('security.logs.path')}</th>
<th className="w-24 text-right">{t('security.logs.duration')}</th>
<th className="w-32 text-right">{t('security.logs.time')}</th>
<th className="w-40">{t('security.logs.reason')}</th>
</tr>
</thead>
<tbody>
{logs.map((log) => (
<tr key={log.id} className="hover:bg-gray-50 dark:hover:bg-base-200">
<td>
{log.blocked ? (
<span className="badge badge-xs badge-error gap-1 text-white">
<AlertTriangle size={10} /> {t('security.logs.blocked')}
</span>
) : (
<span className={`badge badge-xs text-white border-none ${log.status && log.status >= 200 && log.status < 400 ? 'badge-success' : 'badge-warning'}`}>
{log.status || '-'}
</span>
)}
</td>
<td className="font-mono font-medium">{log.client_ip}</td>
<td className="font-medium text-blue-600 dark:text-blue-400">{log.username || '-'}</td>
<td className="font-bold text-xs">{log.method || '-'}</td>
<td className="max-w-xs truncate text-gray-600 dark:text-gray-400" title={log.path}>{log.path || '-'}</td>
<td className="text-right font-mono">{log.duration ? `${log.duration}ms` : '-'}</td>
<td className="text-right text-xs text-gray-500">{new Date(log.timestamp * 1000).toLocaleString()}</td>
<td className="text-xs text-red-500 truncate" title={log.block_reason}>{log.block_reason}</td>
</tr>
))}
{!loading && logs.length === 0 && (
<tr>
<td colSpan={8} className="text-center py-10 text-gray-400">
{t('security.logs.no_logs')}
</td>
</tr>
)}
</tbody>
</table>
</div>
{/* Pagination */}
<div className="p-3 border-t border-gray-100 dark:border-base-200 flex items-center justify-between text-xs text-gray-500 bg-gray-50 dark:bg-base-200">
<span>{t('security.logs.total_records', { total })}</span>
<div className="flex gap-2">
<button
className="btn btn-xs"
disabled={page <= 1}
onClick={() => setPage(p => p - 1)}
>
{t('security.logs.prev_page')}
</button>
<button className="btn btn-xs btn-active">{t('security.logs.page_num', { page })}</button>
<button
className="btn btn-xs"
disabled={logs.length < pageSize}
onClick={() => setPage(p => p + 1)}
>
{t('security.logs.next_page')}
</button>
</div>
</div>
</div>
);
};
|