fix: agent page and agent cmd

This commit is contained in:
engigu
2025-12-29 22:22:56 +08:00
parent 1b6ae38cfb
commit 696e523d9d
3 changed files with 183 additions and 109 deletions
+165 -105
View File
@@ -6,11 +6,10 @@ import { Label } from '@/components/ui/label'
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from '@/components/ui/dialog'
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { RefreshCw, Trash2, Edit, Copy, Server, Search, Download, RotateCw, Plus, Ticket, Power, PowerOff, ListTodo } from 'lucide-vue-next'
import { RefreshCw, Trash2, Edit, Copy, Server, Search, Download, RotateCw, Plus, Ticket, Power, PowerOff, ListTodo, Eye } from 'lucide-vue-next'
import { api, type Agent, type AgentRegCode } from '@/api'
import { toast } from 'vue-sonner'
import { useRouter } from 'vue-router'
import TextOverflow from '@/components/TextOverflow.vue'
const router = useRouter()
@@ -25,10 +24,12 @@ const showEditDialog = ref(false)
const showDeleteDialog = ref(false)
const showDownloadDialog = ref(false)
const showRegCodeDialog = ref(false)
const showDetailDialog = ref(false)
const formData = ref({ name: '', description: '' })
const regCodeForm = ref({ remark: '', max_uses: 0, expires_at: '' })
const editingAgent = ref<Agent | null>(null)
const deletingAgent = ref<Agent | null>(null)
const viewingAgent = ref<Agent | null>(null)
let refreshTimer: ReturnType<typeof setInterval> | null = null
const filteredAgents = computed(() => {
@@ -41,13 +42,12 @@ const filteredAgents = computed(() => {
)
})
// 判断 Agent 是否在线(last_seen 在 2 分钟内)
function isOnline(agent: Agent): boolean {
if (!agent.last_seen) return false
const lastSeen = new Date(agent.last_seen)
const now = new Date()
const diffMs = now.getTime() - lastSeen.getTime()
return diffMs < 2 * 60 * 1000 // 2 分钟
return diffMs < 2 * 60 * 1000
}
async function loadAgents() {
@@ -69,6 +69,11 @@ async function loadAgents() {
}
}
function viewDetail(agent: Agent) {
viewingAgent.value = agent
showDetailDialog.value = true
}
function openEditDialog(agent: Agent) {
editingAgent.value = agent
formData.value = { name: agent.name, description: agent.description }
@@ -220,57 +225,55 @@ onUnmounted(() => {
<TabsContent value="agents" class="mt-4">
<div class="rounded-lg border bg-card overflow-x-auto">
<div class="flex items-center gap-4 px-4 py-2 border-b bg-muted/50 text-sm text-muted-foreground font-medium min-w-[900px]">
<span class="w-6"></span>
<span class="w-28">名称</span>
<span class="w-24">IP</span>
<span class="w-24">主机名</span>
<span class="w-16">版本</span>
<span class="w-28">构建时间</span>
<span class="w-36">心跳时间</span>
<span class="flex-1">描述</span>
<span class="w-32 text-center">操作</span>
<div class="flex items-center gap-2 sm:gap-4 px-3 sm:px-4 py-2 border-b bg-muted/50 text-xs sm:text-sm text-muted-foreground font-medium min-w-[360px]">
<span class="w-10 sm:w-12 shrink-0">ID</span>
<span class="w-6 shrink-0"></span>
<span class="flex-1 min-w-[80px] sm:w-32 sm:shrink-0 sm:flex-none">名称</span>
<span class="w-24 sm:w-28 shrink-0 hidden sm:block">IP</span>
<span class="w-20 sm:w-32 shrink-0 hidden md:block">主机名</span>
<span class="w-20 sm:w-36 shrink-0 hidden lg:block">版本</span>
<span class="w-40 shrink-0 hidden xl:block">心跳时间</span>
<span class="flex-1 min-w-[140px] hidden xl:block">创建时间</span>
<span class="w-20 sm:w-36 shrink-0">操作</span>
</div>
<div class="divide-y min-w-[900px]">
<div class="divide-y min-w-[360px]">
<div v-if="filteredAgents.length === 0" class="text-center py-8 text-muted-foreground">
<Server class="h-8 w-8 mx-auto mb-2 opacity-50" />
{{ searchQuery ? '无匹配结果' : '暂无 Agent' }}
</div>
<div v-for="agent in filteredAgents" :key="agent.id" class="flex items-center gap-4 px-4 py-2 hover:bg-muted/50 transition-colors">
<span class="w-6 flex justify-center">
<span
class="relative flex h-2.5 w-2.5"
:title="isOnline(agent) ? '在线' : '离线'"
>
<span v-if="isOnline(agent)" class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
<span :class="isOnline(agent) ? 'bg-green-500' : 'bg-gray-400'" class="relative inline-flex rounded-full h-2.5 w-2.5"></span>
<div v-for="agent in filteredAgents" :key="agent.id" class="flex items-center gap-2 sm:gap-4 px-3 sm:px-4 py-2 hover:bg-muted/50 transition-colors">
<span class="w-10 sm:w-12 shrink-0 text-muted-foreground text-xs sm:text-sm">#{{ agent.id }}</span>
<span class="w-6 shrink-0 flex justify-center">
<span class="relative flex h-2.5 w-2.5" :title="isOnline(agent) ? '在线' : '离线'">
<span v-if="isOnline(agent)" class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
<span :class="isOnline(agent) ? 'bg-green-500' : 'bg-gray-400'" class="relative inline-flex rounded-full h-2.5 w-2.5"></span>
</span>
</span>
</span>
<span class="w-28 font-medium text-sm truncate" :title="agent.machine_id ? '机器ID: ' + agent.machine_id.slice(0, 16) + '...' : ''">{{ agent.name }}</span>
<span class="w-24 text-sm text-muted-foreground truncate">{{ agent.ip || '-' }}</span>
<span class="w-24 text-sm text-muted-foreground truncate">{{ agent.hostname || '-' }}</span>
<span class="w-16 text-sm text-muted-foreground">{{ agent.version || '-' }}</span>
<span class="w-28 text-sm text-muted-foreground truncate">{{ agent.build_time || '-' }}</span>
<span class="w-36 text-sm text-muted-foreground">{{ agent.last_seen || '-' }}</span>
<span class="flex-1 text-sm text-muted-foreground truncate">
<TextOverflow :text="agent.description || '-'" title="描述" />
</span>
<span class="w-32 flex justify-center gap-1">
<Button variant="ghost" size="icon" class="h-7 w-7" @click="toggleEnabled(agent)" :title="agent.enabled ? '禁用' : '启用'">
<Power v-if="agent.enabled" class="h-3.5 w-3.5 text-green-600" />
<PowerOff v-else class="h-3.5 w-3.5 text-gray-400" />
<span class="flex-1 min-w-[80px] sm:w-32 sm:shrink-0 sm:flex-none font-medium text-xs sm:text-sm truncate cursor-pointer hover:text-primary" @click="viewDetail(agent)" :title="agent.name">{{ agent.name }}</span>
<span class="w-24 sm:w-28 shrink-0 text-xs sm:text-sm text-muted-foreground truncate hidden sm:block">{{ agent.ip || '-' }}</span>
<span class="w-20 sm:w-32 shrink-0 text-xs sm:text-sm text-muted-foreground truncate hidden md:block">{{ agent.hostname || '-' }}</span>
<span class="w-20 sm:w-36 shrink-0 text-xs sm:text-sm text-muted-foreground truncate hidden lg:block">{{ agent.version || '-' }}</span>
<span class="w-40 shrink-0 text-xs sm:text-sm text-muted-foreground hidden xl:block">{{ agent.last_seen || '-' }}</span>
<span class="flex-1 min-w-[140px] text-xs sm:text-sm text-muted-foreground hidden xl:block">{{ agent.created_at || '-' }}</span>
<span class="w-20 sm:w-36 shrink-0 flex justify-end gap-0.5 sm:gap-1">
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="toggleEnabled(agent)" :title="agent.enabled ? '禁用' : '启用'">
<Power v-if="agent.enabled" class="h-3 w-3 sm:h-3.5 sm:w-3.5 text-green-600" />
<PowerOff v-else class="h-3 w-3 sm:h-3.5 sm:w-3.5 text-gray-400" />
</Button>
<Button variant="ghost" size="icon" class="h-7 w-7" @click="viewTasks(agent)" title="查看任务">
<ListTodo class="h-3.5 w-3.5" />
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="viewDetail(agent)" title="详情">
<Eye class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
</Button>
<Button variant="ghost" size="icon" class="h-7 w-7" @click="forceUpdate(agent)" title="强制更新">
<RotateCw class="h-3.5 w-3.5" />
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="viewTasks(agent)" title="查看任务">
<ListTodo class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
</Button>
<Button variant="ghost" size="icon" class="h-7 w-7" @click="openEditDialog(agent)" title="编辑">
<Edit class="h-3.5 w-3.5" />
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="forceUpdate(agent)" title="强制更新">
<RotateCw class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
</Button>
<Button variant="ghost" size="icon" class="h-7 w-7 text-destructive" @click="confirmDelete(agent)" title="删除">
<Trash2 class="h-3.5 w-3.5" />
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7" @click="openEditDialog(agent)" title="编辑">
<Edit class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
</Button>
<Button variant="ghost" size="icon" class="h-6 w-6 sm:h-7 sm:w-7 text-destructive" @click="confirmDelete(agent)" title="删除">
<Trash2 class="h-3 w-3 sm:h-3.5 sm:w-3.5" />
</Button>
</span>
</div>
@@ -280,38 +283,38 @@ onUnmounted(() => {
<TabsContent value="regcodes" class="mt-4">
<div class="rounded-lg border bg-card overflow-x-auto">
<div class="flex items-center gap-4 px-4 py-2 border-b bg-muted/50 text-sm text-muted-foreground font-medium min-w-[800px]">
<span class="w-6"></span>
<span class="w-[420px]">令牌</span>
<span class="w-32">备注</span>
<span class="w-20 text-center">使用次数</span>
<span class="flex-1">过期时间</span>
<span class="w-20 flex justify-center">
<div class="flex items-center gap-2 sm:gap-4 px-3 sm:px-4 py-2 border-b bg-muted/50 text-xs sm:text-sm text-muted-foreground font-medium min-w-[500px]">
<span class="w-6 shrink-0"></span>
<span class="flex-1 min-w-[200px]">令牌</span>
<span class="w-24 sm:w-32 shrink-0">备注</span>
<span class="w-16 sm:w-20 shrink-0 text-center">使用次数</span>
<span class="w-28 sm:w-36 shrink-0 hidden sm:block">过期时间</span>
<span class="w-20 shrink-0 flex justify-center">
<Button size="sm" class="h-7" @click="showRegCodeDialog = true">
<Plus class="h-3.5 w-3.5 mr-1" />生成
</Button>
</span>
</div>
<div class="divide-y min-w-[800px]">
<div class="divide-y min-w-[500px]">
<div v-if="regCodes.length === 0" class="text-center py-8 text-muted-foreground">
<Ticket class="h-8 w-8 mx-auto mb-2 opacity-50" />暂无令牌
</div>
<div v-for="code in regCodes" :key="code.id" class="flex items-center gap-4 px-4 py-2 hover:bg-muted/50 transition-colors">
<span class="w-6 flex justify-center">
<div v-for="code in regCodes" :key="code.id" class="flex items-center gap-2 sm:gap-4 px-3 sm:px-4 py-2 hover:bg-muted/50 transition-colors">
<span class="w-6 shrink-0 flex justify-center">
<span class="relative flex h-2.5 w-2.5">
<span v-if="!isRegCodeExpired(code) && !isRegCodeExhausted(code)" class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
<span :class="!isRegCodeExpired(code) && !isRegCodeExhausted(code) ? 'bg-green-500' : 'bg-gray-400'" class="relative inline-flex rounded-full h-2.5 w-2.5"></span>
</span>
</span>
<code class="w-[420px] font-mono text-xs bg-muted px-2 py-0.5 rounded truncate">{{ code.code }}</code>
<span class="w-32 text-sm text-muted-foreground truncate">{{ code.remark || '-' }}</span>
<span class="w-20 text-sm text-muted-foreground text-center">
<code class="flex-1 min-w-[200px] font-mono text-xs bg-muted px-2 py-0.5 rounded truncate">{{ code.code }}</code>
<span class="w-24 sm:w-32 shrink-0 text-xs sm:text-sm text-muted-foreground truncate">{{ code.remark || '-' }}</span>
<span class="w-16 sm:w-20 shrink-0 text-xs sm:text-sm text-muted-foreground text-center">
{{ code.used_count }}/{{ code.max_uses === 0 ? '∞' : code.max_uses }}
</span>
<span class="flex-1 text-sm text-muted-foreground">
<span class="w-28 sm:w-36 shrink-0 text-xs sm:text-sm text-muted-foreground hidden sm:block truncate">
{{ code.expires_at || '永不过期' }}
</span>
<span class="w-20 flex justify-center gap-1">
<span class="w-20 shrink-0 flex justify-center gap-1">
<Button variant="ghost" size="icon" class="h-7 w-7" @click="copyRegCode(code.code)" title="复制">
<Copy class="h-3.5 w-3.5" />
</Button>
@@ -325,20 +328,91 @@ onUnmounted(() => {
</TabsContent>
</Tabs>
<!-- 详情对话框 -->
<Dialog v-model:open="showDetailDialog">
<DialogContent class="sm:max-w-md md:max-w-lg">
<DialogHeader>
<DialogTitle>Agent 详情</DialogTitle>
</DialogHeader>
<div v-if="viewingAgent" class="space-y-3">
<div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">ID</Label>
<div class="text-sm font-medium">#{{ viewingAgent.id }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">名称</Label>
<div class="text-sm font-medium">{{ viewingAgent.name }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">IP 地址</Label>
<div class="text-sm">{{ viewingAgent.ip || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">主机名</Label>
<div class="text-sm">{{ viewingAgent.hostname || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">操作系统</Label>
<div class="text-sm">{{ viewingAgent.os || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">架构</Label>
<div class="text-sm">{{ viewingAgent.arch || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">版本</Label>
<div class="text-sm">{{ viewingAgent.version || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">在线状态</Label>
<div class="flex items-center gap-2">
<span class="relative flex h-2.5 w-2.5">
<span v-if="isOnline(viewingAgent)" class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
<span :class="isOnline(viewingAgent) ? 'bg-green-500' : 'bg-gray-400'" class="relative inline-flex rounded-full h-2.5 w-2.5"></span>
</span>
<span class="text-sm">{{ isOnline(viewingAgent) ? '在线' : '离线' }}</span>
</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">启用状态</Label>
<div class="text-sm">{{ viewingAgent.enabled ? '已启用' : '已禁用' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">最后心跳</Label>
<div class="text-sm">{{ viewingAgent.last_seen || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">注册时间</Label>
<div class="text-sm">{{ viewingAgent.created_at || '-' }}</div>
</div>
<div class="flex items-center justify-between sm:block">
<Label class="text-muted-foreground text-xs">更新时间</Label>
<div class="text-sm">{{ viewingAgent.updated_at || '-' }}</div>
</div>
</div>
<div v-if="viewingAgent.description" class="pt-2 border-t">
<Label class="text-muted-foreground text-xs">描述</Label>
<div class="text-sm mt-1">{{ viewingAgent.description }}</div>
</div>
</div>
</DialogContent>
</Dialog>
<!-- 编辑对话框 -->
<Dialog v-model:open="showEditDialog">
<DialogContent class="sm:max-w-[400px]">
<DialogContent>
<DialogHeader>
<DialogTitle>编辑 Agent</DialogTitle>
</DialogHeader>
<div class="grid gap-4 py-4">
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">名称</Label>
<Input v-model="formData.name" class="col-span-3" />
<div class="space-y-4">
<div>
<Label>名称</Label>
<Input v-model="formData.name" placeholder="Agent 名称" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">描述</Label>
<Input v-model="formData.description" class="col-span-3" />
<div>
<Label>描述</Label>
<Input v-model="formData.description" placeholder="描述信息(可选)" />
</div>
</div>
<DialogFooter>
@@ -348,12 +422,14 @@ onUnmounted(() => {
</DialogContent>
</Dialog>
<!-- 删除确认 -->
<!-- 删除确认对话框 -->
<AlertDialog v-model:open="showDeleteDialog">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>确认删除</AlertDialogTitle>
<AlertDialogDescription>确定要删除 "{{ deletingAgent?.name }}" 此操作不可恢复</AlertDialogDescription>
<AlertDialogDescription>
确定要删除 Agent "{{ deletingAgent?.name }}" 此操作无法撤销
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
@@ -364,56 +440,40 @@ onUnmounted(() => {
<!-- 下载对话框 -->
<Dialog v-model:open="showDownloadDialog">
<DialogContent class="sm:max-w-[500px]">
<DialogContent>
<DialogHeader>
<DialogTitle>下载 Agent</DialogTitle>
<DialogDescription v-if="agentVersion">当前版本: {{ agentVersion }}</DialogDescription>
<DialogDescription>当前版本: {{ agentVersion }}</DialogDescription>
</DialogHeader>
<div class="py-4 space-y-4">
<div v-if="platforms.length === 0" class="text-center py-4 text-muted-foreground">
暂无可用的 Agent 程序
</div>
<div v-else class="grid gap-2">
<Button v-for="p in platforms" :key="p.filename" variant="outline" class="justify-start" @click="downloadAgent(p.os, p.arch)">
<Download class="h-4 w-4 mr-2" />{{ getPlatformLabel(p.os, p.arch) }}
<div class="space-y-2">
<div v-for="platform in platforms" :key="`${platform.os}-${platform.arch}`" class="flex items-center justify-between p-3 border rounded-lg hover:bg-muted/50 transition-colors">
<span class="font-medium">{{ getPlatformLabel(platform.os, platform.arch) }}</span>
<Button size="sm" @click="downloadAgent(platform.os, platform.arch)">
<Download class="h-4 w-4 mr-1.5" />下载
</Button>
</div>
<div class="border-t pt-4">
<p class="text-sm font-medium mb-2">使用说明</p>
<div class="text-xs text-muted-foreground space-y-1.5">
<p>1. 下载对应平台的 Agent 压缩包并解压</p>
<p>2. 修改 config.example.ini config.ini设置 server_url</p>
<p>3. "令牌"标签页生成令牌填入 config.ini token</p>
<p>4. 运行 <code class="bg-muted px-1 rounded">./baihu-agent start</code> 启动</p>
<p>5. 可选: <code class="bg-muted px-1 rounded">./baihu-agent install</code> 设置开机自启</p>
</div>
</div>
</div>
<DialogFooter>
<Button @click="showDownloadDialog = false">关闭</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<!-- 创建令牌对话框 -->
<Dialog v-model:open="showRegCodeDialog">
<DialogContent class="sm:max-w-[400px]">
<DialogContent>
<DialogHeader>
<DialogTitle>生成令牌</DialogTitle>
<DialogDescription>Agent 使用令牌可直接注册无需审核</DialogDescription>
<DialogTitle>生成注册令牌</DialogTitle>
</DialogHeader>
<div class="grid gap-4 py-4">
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">备注</Label>
<Input v-model="regCodeForm.remark" class="col-span-3" placeholder="可选" />
<div class="space-y-4">
<div>
<Label>备注</Label>
<Input v-model="regCodeForm.remark" placeholder="备注信息(可选" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">使用次数</Label>
<Input v-model.number="regCodeForm.max_uses" type="number" min="0" class="col-span-3" placeholder="0 表示无限制" />
<div>
<Label>最大使用次数</Label>
<Input v-model.number="regCodeForm.max_uses" type="number" placeholder="0 表示无限制" />
</div>
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">过期时间</Label>
<Input v-model="regCodeForm.expires_at" type="datetime-local" class="col-span-3" />
<div>
<Label>过期时间</Label>
<Input v-model="regCodeForm.expires_at" type="datetime-local" />
</div>
</div>
<DialogFooter>