357 lines
12 KiB
Vue
357 lines
12 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, onUnmounted, ref } from "vue";
|
|
import { NAlert, NButton, NIcon, NInput, NPagination, NSpin, NTooltip } from "naive-ui";
|
|
import { RefreshOutline } from "@vicons/ionicons5";
|
|
|
|
interface ProxyRow {
|
|
id: number;
|
|
scheme: string;
|
|
host: string;
|
|
port: number;
|
|
country: string;
|
|
anonymity: string;
|
|
source: string;
|
|
status: string;
|
|
status_text: string;
|
|
latency_ms: number | null;
|
|
last_checked_at: string | null;
|
|
note: string;
|
|
}
|
|
|
|
interface StatusOption {
|
|
name: string;
|
|
value: string;
|
|
count: number;
|
|
}
|
|
|
|
const loading = ref(true);
|
|
const errorText = ref("");
|
|
const query = ref("");
|
|
const appliedQuery = ref("");
|
|
const status = ref("");
|
|
const items = ref<ProxyRow[]>([]);
|
|
const total = ref(0);
|
|
const alive = ref(0);
|
|
const dead = ref(0);
|
|
const pending = ref(0);
|
|
const fetchedAt = ref<string | null>(null);
|
|
const page = ref(1);
|
|
const pageSize = ref(50);
|
|
const pageSizes = [20, 50, 100];
|
|
const fetching = ref(false);
|
|
const probingId = ref<number | null>(null);
|
|
const probingAll = ref(false);
|
|
let poll = 0;
|
|
|
|
const kinds = computed<StatusOption[]>(() => [
|
|
{ name: "可用", value: "alive", count: alive.value },
|
|
{ name: "失效", value: "dead", count: dead.value },
|
|
{ name: "待检测", value: "new", count: pending.value },
|
|
]);
|
|
const kindTotal = computed(() => alive.value + dead.value + pending.value);
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
}
|
|
|
|
function textOf(value: unknown) {
|
|
return typeof value === "string" ? value : "";
|
|
}
|
|
|
|
function asRow(value: unknown): ProxyRow | null {
|
|
if (!isRecord(value) || typeof value.id !== "number" || typeof value.host !== "string") return null;
|
|
return {
|
|
id: value.id,
|
|
scheme: textOf(value.scheme),
|
|
host: value.host,
|
|
port: typeof value.port === "number" ? value.port : 0,
|
|
country: textOf(value.country) || "—",
|
|
anonymity: textOf(value.anonymity) || "—",
|
|
source: textOf(value.source) || "—",
|
|
status: textOf(value.status),
|
|
status_text: textOf(value.status_text) || "—",
|
|
latency_ms: typeof value.latency_ms === "number" ? value.latency_ms : null,
|
|
last_checked_at: typeof value.last_checked_at === "string" && value.last_checked_at ? value.last_checked_at : null,
|
|
note: textOf(value.note),
|
|
};
|
|
}
|
|
|
|
function formatTime(value: string | null) {
|
|
if (!value) return "";
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) return value;
|
|
const pad = (part: number) => String(part).padStart(2, "0");
|
|
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
}
|
|
|
|
function formatLatency(value: number | null) {
|
|
if (value == null) return "—";
|
|
return `${value} ms`;
|
|
}
|
|
|
|
const probingHost = computed(() => {
|
|
if (probingId.value == null) return "";
|
|
const row = items.value.find((item) => item.id === probingId.value);
|
|
return row ? `${row.host}:${row.port}` : "";
|
|
});
|
|
|
|
function schemeLabel(value: string) {
|
|
return value ? value.toUpperCase() : "—";
|
|
}
|
|
|
|
function statusClass(row: ProxyRow) {
|
|
if (row.status === "alive") return "ok";
|
|
if (row.status === "dead") return "late";
|
|
return "quiet";
|
|
}
|
|
|
|
async function load() {
|
|
loading.value = items.value.length === 0;
|
|
errorText.value = "";
|
|
try {
|
|
const params = new URLSearchParams({
|
|
q: appliedQuery.value,
|
|
status: status.value,
|
|
offset: String((page.value - 1) * pageSize.value),
|
|
limit: String(pageSize.value),
|
|
});
|
|
const response = await fetch(`/api/admin/proxies?${params}`);
|
|
const payload: unknown = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
errorText.value = isRecord(payload) && typeof payload.detail === "string" ? payload.detail : "代理列表没有获取到。";
|
|
return;
|
|
}
|
|
if (!isRecord(payload) || !Array.isArray(payload.items)) {
|
|
errorText.value = "代理列表没有获取到。";
|
|
return;
|
|
}
|
|
items.value = payload.items.map(asRow).filter((item): item is ProxyRow => item !== null);
|
|
total.value = typeof payload.total === "number" ? payload.total : items.value.length;
|
|
alive.value = typeof payload.alive === "number" ? payload.alive : 0;
|
|
dead.value = typeof payload.dead === "number" ? payload.dead : 0;
|
|
pending.value = typeof payload.pending === "number" ? payload.pending : 0;
|
|
fetchedAt.value = typeof payload.fetched_at === "string" && payload.fetched_at ? payload.fetched_at : null;
|
|
fetching.value = payload.running === true;
|
|
if (typeof payload.message === "string" && payload.message) errorText.value = payload.message;
|
|
if (fetching.value) startPoll();
|
|
else stopPoll();
|
|
} catch {
|
|
errorText.value = "代理列表没有获取到。";
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
function startPoll() {
|
|
if (poll) return;
|
|
poll = window.setInterval(() => void load(), 2000);
|
|
}
|
|
|
|
function stopPoll() {
|
|
window.clearInterval(poll);
|
|
poll = 0;
|
|
}
|
|
|
|
function applyProbed(row: ProxyRow) {
|
|
items.value = items.value.map((item) => (item.id === row.id ? row : item));
|
|
}
|
|
|
|
async function probeOne(row: ProxyRow) {
|
|
if (probingId.value != null || probingAll.value || fetching.value) return;
|
|
probingId.value = row.id;
|
|
errorText.value = "";
|
|
try {
|
|
const response = await fetch(`/api/admin/proxies/${row.id}/probe`, { method: "POST" });
|
|
const payload: unknown = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
errorText.value = isRecord(payload) && typeof payload.detail === "string" ? payload.detail : "没有测出延迟。";
|
|
return;
|
|
}
|
|
const next = asRow(payload);
|
|
if (next) applyProbed(next);
|
|
} catch {
|
|
errorText.value = "没有测出延迟。";
|
|
} finally {
|
|
probingId.value = null;
|
|
}
|
|
}
|
|
|
|
async function probeAll() {
|
|
if (probingId.value != null || probingAll.value || fetching.value || !items.value.length) return;
|
|
probingAll.value = true;
|
|
errorText.value = "";
|
|
try {
|
|
const response = await fetch("/api/admin/proxies/probe", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({}),
|
|
});
|
|
const payload: unknown = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
errorText.value = isRecord(payload) && typeof payload.detail === "string" ? payload.detail : "没有测出延迟。";
|
|
return;
|
|
}
|
|
await load();
|
|
} catch {
|
|
errorText.value = "没有测出延迟。";
|
|
} finally {
|
|
probingAll.value = false;
|
|
}
|
|
}
|
|
|
|
async function refreshNow() {
|
|
errorText.value = "";
|
|
fetching.value = true;
|
|
try {
|
|
const response = await fetch("/api/admin/proxies/refresh", { method: "POST" });
|
|
const payload: unknown = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
fetching.value = false;
|
|
errorText.value = isRecord(payload) && typeof payload.detail === "string" ? payload.detail : "没有更新。";
|
|
return;
|
|
}
|
|
startPoll();
|
|
await load();
|
|
} catch {
|
|
fetching.value = false;
|
|
errorText.value = "没有更新。";
|
|
}
|
|
}
|
|
|
|
function pickStatus(value: string) {
|
|
status.value = value;
|
|
page.value = 1;
|
|
void load();
|
|
}
|
|
|
|
function goPage(next: number) {
|
|
page.value = next;
|
|
void load();
|
|
}
|
|
|
|
function changeSize(size: number) {
|
|
pageSize.value = size;
|
|
page.value = 1;
|
|
void load();
|
|
}
|
|
|
|
function search() {
|
|
appliedQuery.value = query.value.trim();
|
|
page.value = 1;
|
|
void load();
|
|
}
|
|
|
|
onMounted(() => {
|
|
void load();
|
|
});
|
|
|
|
onUnmounted(() => stopPoll());
|
|
</script>
|
|
|
|
<template>
|
|
<section class="page admin-page">
|
|
<header class="page-head">
|
|
<p v-if="!loading">
|
|
可用 {{ alive }} · 失效 {{ dead }}
|
|
<template v-if="fetchedAt"> · 更新于 {{ formatTime(fetchedAt) }}</template>
|
|
</p>
|
|
<form class="course-search" @submit.prevent="search">
|
|
<n-tooltip>
|
|
<template #trigger>
|
|
<n-button quaternary circle attr-type="button" :loading="fetching" :disabled="fetching || probingAll" aria-label="立即更新" @click="refreshNow">
|
|
<template #icon><n-icon><RefreshOutline /></n-icon></template>
|
|
</n-button>
|
|
</template>
|
|
立即更新
|
|
</n-tooltip>
|
|
<n-button size="small" attr-type="button" :loading="probingAll" :disabled="probingAll || fetching || !items.length" @click="probeAll">
|
|
测试全部
|
|
</n-button>
|
|
<n-input v-model:value="query" placeholder="地址" clearable @clear="search" />
|
|
<n-button attr-type="submit">搜索</n-button>
|
|
</form>
|
|
</header>
|
|
<div v-if="kindTotal > 0" class="course-kinds">
|
|
<button type="button" class="kind-chip" :class="{ active: status === '' }" @click="pickStatus('')">全部<span>{{ kindTotal }}</span></button>
|
|
<button
|
|
v-for="item in kinds"
|
|
:key="item.value"
|
|
type="button"
|
|
class="kind-chip"
|
|
:class="{ active: status === item.value }"
|
|
@click="pickStatus(item.value)"
|
|
>
|
|
{{ item.name }}<span>{{ item.count }}</span>
|
|
</button>
|
|
</div>
|
|
<n-alert v-if="errorText" class="admin-alert" type="error">{{ errorText }}</n-alert>
|
|
<n-alert v-if="probingAll" class="admin-alert" type="info">正在测试全部代理延迟</n-alert>
|
|
<n-alert v-else-if="probingId != null" class="admin-alert" type="info">正在测试 {{ probingHost }} 延迟</n-alert>
|
|
<div class="admin-scroll">
|
|
<p v-if="loading" class="admin-status">正在获取</p>
|
|
<p v-else-if="fetching && !items.length" class="admin-status">正在获取</p>
|
|
<p v-else-if="!items.length" class="admin-status">还没有代理</p>
|
|
<table v-else class="admin-table">
|
|
<thead>
|
|
<tr>
|
|
<th>国家</th>
|
|
<th>地址</th>
|
|
<th>端口</th>
|
|
<th>协议</th>
|
|
<th>匿名</th>
|
|
<th>来源</th>
|
|
<th>延迟</th>
|
|
<th>状态</th>
|
|
<th>检测时间</th>
|
|
<th>操作</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="row in items" :key="row.id">
|
|
<td>{{ row.country }}</td>
|
|
<td class="employee">{{ row.host }}</td>
|
|
<td class="when">{{ row.port }}</td>
|
|
<td>{{ schemeLabel(row.scheme) }}</td>
|
|
<td>{{ row.anonymity }}</td>
|
|
<td>{{ row.source }}</td>
|
|
<td class="when">
|
|
<span v-if="probingAll || probingId === row.id" class="probe-loading">
|
|
<n-spin :size="14" />测试中
|
|
</span>
|
|
<template v-else>{{ formatLatency(row.latency_ms) }}</template>
|
|
</td>
|
|
<td :class="statusClass(row)" :title="row.note || undefined">
|
|
{{ row.status_text }}<template v-if="row.note"> · {{ row.note }}</template>
|
|
</td>
|
|
<td class="when">{{ formatTime(row.last_checked_at) || "—" }}</td>
|
|
<td>
|
|
<n-button
|
|
size="tiny"
|
|
:loading="probingId === row.id"
|
|
:disabled="probingAll || fetching || (probingId != null && probingId !== row.id)"
|
|
@click="probeOne(row)"
|
|
>
|
|
测试
|
|
</n-button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<n-pagination
|
|
v-if="total > 0"
|
|
class="course-pager"
|
|
:page="page"
|
|
:page-size="pageSize"
|
|
:item-count="total"
|
|
:page-sizes="pageSizes"
|
|
show-size-picker
|
|
show-quick-jumper
|
|
@update:page="goPage"
|
|
@update:page-size="changeSize"
|
|
>
|
|
<template #prefix>共 {{ total }} 条</template>
|
|
</n-pagination>
|
|
</section>
|
|
</template>
|