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>
|
||||
@@ -49,9 +49,10 @@ const proxyPort = ref<number | null>(null);
|
||||
const proxyUser = ref("");
|
||||
const proxyPassword = ref("");
|
||||
const proxyHasPassword = ref(false);
|
||||
const proxyOff = computed(() => proxyScheme.value === "off");
|
||||
const proxyManual = computed(() => proxyScheme.value !== "off" && proxyScheme.value !== "pool");
|
||||
const proxyOptions = [
|
||||
{ label: "关闭", value: "off" },
|
||||
{ label: "自动", value: "pool" },
|
||||
{ label: "HTTP", value: "http" },
|
||||
{ label: "HTTPS", value: "https" },
|
||||
{ label: "SOCKS4", value: "socks4" },
|
||||
@@ -388,7 +389,7 @@ function proxyPasswordPayload() {
|
||||
async function saveProxy() {
|
||||
if (proxySaving.value || !proxyIds.value.length) return;
|
||||
proxyError.value = "";
|
||||
if (!proxyOff.value) {
|
||||
if (proxyManual.value) {
|
||||
if (!proxyHost.value.trim()) {
|
||||
proxyError.value = "请填写代理地址";
|
||||
return;
|
||||
@@ -404,16 +405,16 @@ async function saveProxy() {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(
|
||||
proxyOff.value
|
||||
? { employee_ids: proxyIds.value, scheme: "off" }
|
||||
: {
|
||||
proxyManual.value
|
||||
? {
|
||||
employee_ids: proxyIds.value,
|
||||
scheme: proxyScheme.value,
|
||||
host: proxyHost.value.trim(),
|
||||
port: proxyPort.value,
|
||||
username: proxyUser.value.trim(),
|
||||
password: proxyPasswordPayload(),
|
||||
},
|
||||
}
|
||||
: { employee_ids: proxyIds.value, scheme: proxyScheme.value },
|
||||
),
|
||||
});
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
@@ -694,15 +695,15 @@ onMounted(() => {
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-host">地址</label>
|
||||
<n-input id="admin-proxy-host" v-model:value="proxyHost" placeholder="例如 127.0.0.1" :disabled="proxyOff" />
|
||||
<n-input id="admin-proxy-host" v-model:value="proxyHost" placeholder="例如 127.0.0.1" :disabled="!proxyManual" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-port">端口</label>
|
||||
<n-input-number id="admin-proxy-port" v-model:value="proxyPort" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="proxyOff" />
|
||||
<n-input-number id="admin-proxy-port" v-model:value="proxyPort" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="!proxyManual" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-user">账号</label>
|
||||
<n-input id="admin-proxy-user" v-model:value="proxyUser" placeholder="可不填" autocomplete="off" :disabled="proxyOff" />
|
||||
<n-input id="admin-proxy-user" v-model:value="proxyUser" placeholder="可不填" autocomplete="off" :disabled="!proxyManual" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-proxy-password">密码</label>
|
||||
@@ -713,7 +714,7 @@ onMounted(() => {
|
||||
show-password-on="click"
|
||||
:placeholder="proxyIds.length === 1 && proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
autocomplete="new-password"
|
||||
:disabled="proxyOff"
|
||||
:disabled="!proxyManual"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -220,9 +220,10 @@ const proxyPort = ref<number | null>(null);
|
||||
const proxyUser = ref("");
|
||||
const proxyPassword = ref("");
|
||||
const proxyHasPassword = ref(false);
|
||||
const proxyOff = computed(() => proxyScheme.value === "off");
|
||||
const proxyManual = computed(() => proxyScheme.value !== "off" && proxyScheme.value !== "pool");
|
||||
const proxyOptions = [
|
||||
{ label: "关闭", value: "off" },
|
||||
{ label: "自动", value: "pool" },
|
||||
{ label: "HTTP", value: "http" },
|
||||
{ label: "HTTPS", value: "https" },
|
||||
{ label: "SOCKS4", value: "socks4" },
|
||||
@@ -253,7 +254,7 @@ function proxyPasswordPayload() {
|
||||
async function saveProxy() {
|
||||
if (proxySaving.value) return;
|
||||
proxyError.value = "";
|
||||
if (!proxyOff.value) {
|
||||
if (proxyManual.value) {
|
||||
if (!proxyHost.value.trim()) {
|
||||
proxyError.value = "请填写代理地址";
|
||||
return;
|
||||
@@ -269,15 +270,15 @@ async function saveProxy() {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(
|
||||
proxyOff.value
|
||||
? { scheme: "off" }
|
||||
: {
|
||||
proxyManual.value
|
||||
? {
|
||||
scheme: proxyScheme.value,
|
||||
host: proxyHost.value.trim(),
|
||||
port: proxyPort.value,
|
||||
username: proxyUser.value.trim(),
|
||||
password: proxyPasswordPayload(),
|
||||
},
|
||||
}
|
||||
: { scheme: proxyScheme.value },
|
||||
),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
@@ -287,7 +288,7 @@ async function saveProxy() {
|
||||
}
|
||||
proxyPassword.value = "";
|
||||
proxyHasPassword.value = data.has_password === true;
|
||||
if (proxyOff.value) {
|
||||
if (!proxyManual.value) {
|
||||
proxyHost.value = "";
|
||||
proxyPort.value = null;
|
||||
proxyUser.value = "";
|
||||
@@ -424,15 +425,15 @@ onMounted(() => { refresh(); loadTarget(); loadStart(); loadWorkers(); loadProxy
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-host">代理地址</label>
|
||||
<n-input id="proxy-host" v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" :disabled="proxyOff" />
|
||||
<n-input id="proxy-host" v-model:value="proxyHost" size="small" placeholder="例如 127.0.0.1" :disabled="!proxyManual" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-port">代理端口</label>
|
||||
<n-input-number id="proxy-port" v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="proxyOff" />
|
||||
<n-input-number id="proxy-port" v-model:value="proxyPort" size="small" button-placement="both" :min="1" :max="65535" :precision="0" :disabled="!proxyManual" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-user">代理账号</label>
|
||||
<n-input id="proxy-user" v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" :disabled="proxyOff" />
|
||||
<n-input id="proxy-user" v-model:value="proxyUser" size="small" placeholder="可不填" autocomplete="off" :disabled="!proxyManual" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="proxy-password">代理密码</label>
|
||||
@@ -444,7 +445,7 @@ onMounted(() => { refresh(); loadTarget(); loadStart(); loadWorkers(); loadProxy
|
||||
show-password-on="click"
|
||||
:placeholder="proxyHasPassword ? '已保存,留空则保持' : '可不填'"
|
||||
autocomplete="new-password"
|
||||
:disabled="proxyOff"
|
||||
:disabled="!proxyManual"
|
||||
/>
|
||||
</div>
|
||||
<div class="home-proxy-actions">
|
||||
|
||||
@@ -6,6 +6,7 @@ import { isDark, toggleTheme } from "../shared/theme";
|
||||
import { adminNavOptions, type AdminPageKey } from "./adminNavigation";
|
||||
import UsersView from "../features/admin/UsersView.vue";
|
||||
import CoursesView from "../features/admin/CoursesView.vue";
|
||||
import ProxiesView from "../features/admin/ProxiesView.vue";
|
||||
import SettingsView from "../features/admin/SettingsView.vue";
|
||||
|
||||
defineProps<{ username: string }>();
|
||||
@@ -15,7 +16,7 @@ const collapsed = ref(window.innerWidth < 800);
|
||||
|
||||
function pageFromHash(): AdminPageKey {
|
||||
const name = window.location.hash.replace(/^#\/?/, "");
|
||||
if (name === "courses" || name === "settings" || name === "users") return name;
|
||||
if (name === "courses" || name === "proxies" || name === "settings" || name === "users") return name;
|
||||
return "users";
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ onUnmounted(() => {
|
||||
});
|
||||
|
||||
function onMenu(key: string) {
|
||||
if (key === "users" || key === "courses" || key === "settings") {
|
||||
if (key === "users" || key === "courses" || key === "proxies" || key === "settings") {
|
||||
page.value = key;
|
||||
remember(key);
|
||||
}
|
||||
@@ -105,6 +106,7 @@ function onMenu(key: string) {
|
||||
<n-layout class="content" :native-scrollbar="false" content-style="height: 100%; min-height: 0; overflow: hidden; display: flex; flex-direction: column;">
|
||||
<UsersView v-if="page === 'users'" />
|
||||
<CoursesView v-else-if="page === 'courses'" />
|
||||
<ProxiesView v-else-if="page === 'proxies'" />
|
||||
<SettingsView v-else-if="page === 'settings'" @saved="emit('refresh')" />
|
||||
</n-layout>
|
||||
</n-layout>
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import type { Component } from "vue";
|
||||
import { h } from "vue";
|
||||
import { NIcon } from "naive-ui";
|
||||
import { LibraryOutline, PeopleOutline, SettingsOutline } from "@vicons/ionicons5";
|
||||
import { GlobeOutline, LibraryOutline, PeopleOutline, SettingsOutline } from "@vicons/ionicons5";
|
||||
|
||||
function renderIcon(icon: Component) {
|
||||
return () => h(NIcon, null, { default: () => h(icon) });
|
||||
}
|
||||
|
||||
export type AdminPageKey = "users" | "courses" | "settings";
|
||||
export type AdminPageKey = "users" | "courses" | "proxies" | "settings";
|
||||
|
||||
export const adminNavOptions = [
|
||||
{ label: "用户管理", key: "users" as const, icon: renderIcon(PeopleOutline) },
|
||||
{ label: "课程清单", key: "courses" as const, icon: renderIcon(LibraryOutline) },
|
||||
{ label: "代理", key: "proxies" as const, icon: renderIcon(GlobeOutline) },
|
||||
{ label: "系统配置", key: "settings" as const, icon: renderIcon(SettingsOutline) },
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user