feat: 维护中国代理池并支持账号自动均匀使用
This commit is contained in:
@@ -0,0 +1,273 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import { NAlert, NButton, NInput, NPagination } from "naive-ui";
|
||||
|
||||
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);
|
||||
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`;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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">
|
||||
<div>
|
||||
<h2>代理</h2>
|
||||
<p v-if="!loading">
|
||||
可用 {{ alive }} · 失效 {{ dead }}
|
||||
<template v-if="fetchedAt"> · 更新于 {{ formatTime(fetchedAt) }}</template>
|
||||
</p>
|
||||
</div>
|
||||
<form class="course-search" @submit.prevent="search">
|
||||
<n-button attr-type="button" :loading="fetching" :disabled="fetching" @click="refreshNow">立即更新</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>
|
||||
<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>
|
||||
</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">{{ formatLatency(row.latency_ms) }}</td>
|
||||
<td :class="statusClass(row)">{{ row.status_text }}</td>
|
||||
<td class="when">{{ formatTime(row.last_checked_at) || "—" }}</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>
|
||||
Reference in New Issue
Block a user