| </div> |
| 9 |
| 10 | <div class="actions-section"> |
| 11 | <ActionButton variant="outline" size="small" @click="refreshData"> |
| 12 | Refresh |
| 13 | </ActionButton> |
| 14 |
| 15 | <ActionButton variant="outline" size="small" danger @click="showClearDialog = true"> |
| 16 | Clear Offline |
| 17 | </ActionButton> |
| 18 | </div> |
| 19 | </div> |
| 20 |
| 21 | <div class="filter-section"> |
| 22 | <div class="search-row"> |
| 23 | <el-input |
| 24 | v-model="searchText" |
| 25 | placeholder="Search proxies..." |
| 26 | :prefix-icon="Search" |
| 27 | clearable |
| 28 | class="main-search" |
| 29 | /> |
| 30 | </div> |
| 31 |
| 32 | <div class="type-tabs"> |
| 33 | <button |
| 34 | v-for="t in proxyTypes" |
| 35 | :key="t.value" |
| 36 | class="type-tab" |
| 37 | :class="{ active: activeType === t.value }" |
| 38 | @click="activeType = t.value" |
| 39 | > |
| 40 | {{ t.label }} |
| 41 | </button> |
| 42 | </div> |
| 43 | </div> |
| 44 | </div> |
| 45 |
| 46 | <div v-loading="loading" class="proxies-content"> |
| 47 | <div v-if="proxies.length > 0" class="proxies-list"> |
| 48 | <ProxyCard |
| 49 | v-for="proxy in proxies" |
| 50 | :key="`${proxy.type}:${proxy.name}`" |
| 51 | :proxy="proxy" |
| 52 | :show-type="activeType === 'all'" |
| 53 | /> |
| 54 | </div> |
| 55 | <div v-else-if="!loading" class="empty-state"> |
| 56 | <el-empty description="No proxies found" /> |
| 57 | </div> |
| 58 | </div> |
| 59 |
| 60 | <div v-if="total > 0" class="pagination-section"> |
| 61 | <ElPagination |
| 62 | :current-page="page" |
| 63 | :page-size="pageSize" |
| 64 | :page-sizes="[10, 20, 50, 100]" |
| 65 | :total="total" |
| 66 | layout="total, sizes, prev, pager, next" |
| 67 | @current-change="onPageChange" |
| 68 | @size-change="onPageSizeChange" |
| 69 | /> |
| 70 | </div> |
| 71 |
| 72 | <ConfirmDialog |
| 73 | v-model="showClearDialog" |
| 74 | title="Clear Offline" |
| 75 | message="Are you sure you want to clear all offline proxies?" |
| 76 | confirm-text="Clear" |
| 77 | danger |
| 78 | @confirm="handleClearConfirm" |
| 79 | /> |
| 80 | </div> |
| 81 | </template> |
| 82 |
| 83 | <script setup lang="ts"> |
| 84 | import { ref, watch, onUnmounted } from 'vue' |
| 85 | import { useRoute, useRouter } from 'vue-router' |
| 86 | import { ElMessage, ElPagination } from 'element-plus' |
| 87 | import { Search } from '@element-plus/icons-vue' |
| 88 | import ActionButton from '@shared/components/ActionButton.vue' |
| 89 | import ConfirmDialog from '@shared/components/ConfirmDialog.vue' |
| 90 | import { |
| 91 | BaseProxy, |
| 92 | TCPProxy, |
| 93 | UDPProxy, |
| 94 | HTTPProxy, |
| 95 | HTTPSProxy, |
| 96 | TCPMuxProxy, |
| 97 | STCPProxy, |
| 98 | SUDPProxy, |
| 99 | } from '../utils/proxy' |
| 100 | import ProxyCard from '../components/ProxyCard.vue' |
| 101 | import { |
| 102 | getProxiesV2, |
| 103 | clearOfflineProxies as apiClearOfflineProxies, |
| 104 | } from '../api/proxy' |
| 105 | import { getServerInfo } from '../api/server' |
| 106 | import type { ProxyStatsInfo } from '../types/proxy' |
| 107 | import type { ServerInfo } from '../types/server' |
| 108 |
| 109 | const route = useRoute() |
| 110 | const router = useRouter() |
| 111 |
| 112 | const proxyTypes = [ |
| 113 | { label: 'All', value: 'all' }, |
| 114 | { label: 'TCP', value: 'tcp' }, |
| 115 | { label: 'UDP', value: 'udp' }, |
| 116 | { label: 'HTTP', value: 'http' }, |
| 117 | { label: 'HTTPS', value: 'https' }, |
| 118 | { label: 'TCPMUX', value: 'tcpmux' }, |
| 119 | { label: 'STCP', value: 'stcp' }, |
| 120 | { label: 'XTCP', value: 'xtcp' }, |
| 121 | { label: 'SUDP', value: 'sudp' }, |
| 122 | ] |
| 123 |
| 124 | const activeType = ref((route.params.type as string) || 'all') |
| 125 | const proxies = ref<BaseProxy[]>([]) |
| 126 | const loading = ref(false) |
| 127 | const searchText = ref('') |
| 128 | const showClearDialog = ref(false) |
| 129 | const page = ref(1) |
| 130 | const pageSize = ref(10) |
| 131 | const total = ref(0) |
| 132 | let requestSeq = 0 |
| 133 | let searchDebounceTimer: number | null = null |
| 134 |
| 135 | // Server info cache - cache the Promise itself so concurrent first calls |
| 136 | // from Promise.all (convertProxies) don't kick off multiple HTTP requests. |
| 137 | let serverInfoPromise: Promise<ServerInfo> | null = null |
| 138 |
| 139 | const fetchServerInfo = (): Promise<ServerInfo> => { |
| 140 | if (!serverInfoPromise) { |
| 141 | serverInfoPromise = getServerInfo().catch((err) => { |
| 142 | // Allow retry after failure |
| 143 | serverInfoPromise = null |
| 144 | throw err |
| 145 | }) |
| 146 | } |
| 147 | return serverInfoPromise |
| 148 | } |
| 149 |
| 150 | const convertProxy = async ( |
| 151 | proxy: ProxyStatsInfo, |
| 152 | ): Promise<BaseProxy | null> => { |
| 153 | const type = proxy.type || activeType.value |
| 154 | if (type === 'tcp') { |
| 155 | return new TCPProxy(proxy) |
| 156 | } |
| 157 | if (type === 'udp') { |
| 158 | return new UDPProxy(proxy) |
| 159 | } |
| 160 | if (type === 'http') { |
| 161 | const info = await fetchServerInfo() |
| 162 | if (info && info.config.vhostHTTPPort) { |
| 163 | return new HTTPProxy( |
| 164 | proxy, |
| 165 | info.config.vhostHTTPPort, |
| 166 | info.config.subdomainHost, |
| 167 | ) |
| 168 | } |
| 169 | return null |
| 170 | } |
| 171 | if (type === 'https') { |
| 172 | const info = await fetchServerInfo() |
| 173 | if (info && info.config.vhostHTTPSPort) { |
| 174 | return new HTTPSProxy( |
| 175 | proxy, |
| 176 | info.config.vhostHTTPSPort, |
| 177 | info.config.subdomainHost, |
| 178 | ) |
| 179 | } |
| 180 | return null |
| 181 | } |
| 182 | if (type === 'tcpmux') { |
| 183 | const info = await fetchServerInfo() |
| 184 | if (info && info.config.tcpmuxHTTPConnectPort) { |
| 185 | return new TCPMuxProxy( |
| 186 | proxy, |
| 187 | info.config.tcpmuxHTTPConnectPort, |
| 188 | info.config.subdomainHost, |
| 189 | ) |
| 190 | } |
| 191 | return null |
| 192 | } |
| 193 | if (type === 'stcp') { |
| 194 | return new STCPProxy(proxy) |
| 195 | } |
| 196 | if (type === 'sudp') { |
| 197 | return new SUDPProxy(proxy) |
| 198 | } |
| 199 | // Fallback for types without a dedicated class (e.g. xtcp). Matches the |
| 200 | // pattern in ProxyDetail.vue so the type tag and meta render correctly. |
| 201 | const bp = new BaseProxy(proxy) |
| 202 | bp.type = type |
| 203 | return bp |
| 204 | } |
| 205 |
| 206 | const convertProxies = async (items: ProxyStatsInfo[]): Promise<BaseProxy[]> => { |
| 207 | const converted = await Promise.all(items.map((item) => convertProxy(item))) |
| 208 | return converted.filter((item): item is BaseProxy => item !== null) |
| 209 | } |
| 210 |
| 211 | const fetchData = async (silent = false) => { |
| 212 | const seq = ++requestSeq |
| 213 | if (!silent) loading.value = true |
| 214 |
| 215 | try { |
| 216 | const q = searchText.value.trim() |
| 217 | const data = await getProxiesV2({ |
| 218 | page: page.value, |
| 219 | pageSize: pageSize.value, |
| 220 | type: activeType.value === 'all' ? undefined : activeType.value, |
| 221 | q: q || undefined, |
| 222 | }) |
| 223 | if (seq !== requestSeq) return |
| 224 |
| 225 | const maxPage = Math.max(1, Math.ceil(data.total / data.pageSize)) |
| 226 | if (data.items.length === 0 && data.total > 0 && data.page > maxPage) { |
| 227 | page.value = maxPage |
| 228 | await fetchData(silent) |
| 229 | return |
| 230 | } |
| 231 |
| 232 | const converted = await convertProxies(data.items) |
| 233 | if (seq !== requestSeq) return |
| 234 |
| 235 | proxies.value = converted |
| 236 | total.value = data.total |
| 237 | page.value = data.page |
| 238 | pageSize.value = data.pageSize |
| 239 | } catch (error: any) { |
| 240 | if (seq !== requestSeq) return |
| 241 | ElMessage({ |
| 242 | showClose: true, |
| 243 | message: 'Failed to fetch proxies: ' + error.message, |
| 244 | type: 'error', |
| 245 | }) |
| 246 | } finally { |
| 247 | if (seq === requestSeq) { |
| 248 | loading.value = false |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 |
| 253 | const clearSearchDebounce = () => { |
| 254 | if (searchDebounceTimer !== null) { |
| 255 | window.clearTimeout(searchDebounceTimer) |
| 256 | searchDebounceTimer = null |
| 257 | } |
| 258 | } |
| 259 |
| 260 | const resetPageAndFetch = () => { |
| 261 | clearSearchDebounce() |
| 262 | page.value = 1 |
| 263 | fetchData() |
| 264 | } |
| 265 |
| 266 | const refreshData = () => { |
| 267 | fetchData() |
| 268 | } |
| 269 |
| 270 | const onPageChange = (value: number) => { |
| 271 | clearSearchDebounce() |
| 272 | page.value = value |
| 273 | fetchData() |
| 274 | } |
| 275 |
| 276 | const onPageSizeChange = (value: number) => { |
| 277 | pageSize.value = value |
| 278 | resetPageAndFetch() |
| 279 | } |
| 280 |
| 281 | const handleClearConfirm = async () => { |
| 282 | showClearDialog.value = false |
| 283 | await clearOfflineProxies() |
| 284 | } |
| 285 |
| 286 | const clearOfflineProxies = async () => { |
| 287 | try { |
| 288 | await apiClearOfflineProxies() |
| 289 | ElMessage({ |
| 290 | message: 'Successfully cleared offline proxies', |
| 291 | type: 'success', |
| 292 | }) |
| 293 | fetchData() |
| 294 | } catch (err: any) { |
| 295 | ElMessage({ |
| 296 | message: 'Failed to clear offline proxies: ' + err.message, |
| 297 | type: 'warning', |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 |
| 302 | const sanitizeClientQuery = () => { |
| 303 | const hasClientQuery = |
| 304 | Object.prototype.hasOwnProperty.call(route.query, 'clientID') || |
| 305 | Object.prototype.hasOwnProperty.call(route.query, 'user') |
| 306 | if (!hasClientQuery) return |
| 307 |
| 308 | const query = { ...route.query } |
| 309 | delete query.clientID |
| 310 | delete query.user |
| 311 | router.replace({ query }) |
| 312 | } |
| 313 |
| 314 | // Watch for type changes |
| 315 | watch(activeType, (newType) => { |
| 316 | clearSearchDebounce() |
| 317 | page.value = 1 |
| 318 | // Update route but preserve query params |
| 319 | router.replace({ params: { type: newType }, query: route.query }) |
| 320 | fetchData() |
| 321 | }) |
| 322 |
| 323 | watch(searchText, () => { |
| 324 | clearSearchDebounce() |
| 325 | page.value = 1 |
| 326 | searchDebounceTimer = window.setTimeout(() => { |
| 327 | searchDebounceTimer = null |
| 328 | fetchData() |
| 329 | }, 300) |
| 330 | }) |
| 331 |
| 332 | watch(() => route.query, sanitizeClientQuery) |
| 333 |
| 334 | onUnmounted(() => { |
| 335 | clearSearchDebounce() |
| 336 | }) |
| 337 |
| 338 | // Initial fetch |
| 339 | sanitizeClientQuery() |
| 340 | fetchData() |
| 341 | </script> |
| 342 |
| 343 | <style scoped> |
| 344 | .proxies-page { |
| 345 | display: flex; |
| 346 | flex-direction: column; |
| 347 | gap: 24px; |
| 348 | } |
| 349 |
| 350 | .page-header { |
| 351 | display: flex; |
| 352 | flex-direction: column; |
| 353 | gap: 24px; |
| 354 | } |
| 355 |
| 356 | .header-top { |
| 357 | display: flex; |
| 358 | justify-content: space-between; |
| 359 | align-items: flex-start; |
| 360 | gap: 20px; |
| 361 | } |
| 362 |
| 363 | .title-section { |
| 364 | display: flex; |
| 365 | flex-direction: column; |
| 366 | gap: 8px; |
| 367 | } |
| 368 |
| 369 | .page-title { |
| 370 | font-size: 28px; |
| 371 | font-weight: 600; |
| 372 | color: var(--el-text-color-primary); |
| 373 | margin: 0; |
| 374 | line-height: 1.2; |
| 375 | } |
| 376 |
| 377 | .page-subtitle { |
| 378 | font-size: 14px; |
| 379 | color: var(--el-text-color-secondary); |
| 380 | margin: 0; |
| 381 | } |
| 382 |
| 383 | .actions-section { |
| 384 | display: flex; |
| 385 | gap: 12px; |
| 386 | } |
| 387 |
| 388 |
| 389 | .filter-section { |
| 390 | display: flex; |
| 391 | flex-direction: column; |
| 392 | gap: 20px; |
| 393 | margin-top: 8px; |
| 394 | } |
| 395 |
| 396 | .search-row { |
| 397 | display: flex; |
| 398 | gap: 16px; |
| 399 | width: 100%; |
| 400 | align-items: center; |
| 401 | } |
| 402 |
| 403 | .main-search { |
| 404 | flex: 1; |
| 405 | } |
| 406 |
| 407 | .main-search :deep(.el-input__wrapper) { |
| 408 | height: 32px; |
| 409 | border-radius: 8px; |
| 410 | } |
| 411 |
| 412 | .type-tabs { |
| 413 | display: flex; |
| 414 | gap: 8px; |
| 415 | overflow-x: auto; |
| 416 | padding-bottom: 4px; |
| 417 | } |
| 418 |
| 419 | .type-tab { |
| 420 | padding: 6px 16px; |
| 421 | border: 1px solid var(--el-border-color-lighter); |
| 422 | border-radius: 12px; |
| 423 | background: var(--el-bg-color); |
| 424 | color: var(--el-text-color-regular); |
| 425 | font-size: 13px; |
| 426 | font-weight: 500; |
| 427 | cursor: pointer; |
| 428 | transition: all 0.2s; |
| 429 | text-transform: uppercase; |
| 430 | } |
| 431 |
| 432 | .type-tab:hover { |
| 433 | background: var(--el-fill-color-light); |
| 434 | } |
| 435 |
| 436 | .type-tab.active { |
| 437 | background: var(--el-fill-color-darker); |
| 438 | color: var(--el-text-color-primary); |
| 439 | border-color: var(--el-fill-color-darker); |
| 440 | } |
| 441 |
| 442 | .proxies-content { |
| 443 | min-height: 200px; |
| 444 | } |
| 445 |
| 446 | .proxies-list { |
| 447 | display: flex; |
| 448 | flex-direction: column; |
| 449 | gap: 16px; |
| 450 | } |
| 451 |
| 452 | .empty-state { |
| 453 | padding: 60px 0; |
| 454 | } |
| 455 |
| 456 | .pagination-section { |
| 457 | display: flex; |
| 458 | justify-content: flex-end; |
| 459 | } |
| 460 |
| 461 | @media (max-width: 768px) { |
| 462 | .search-row { |
| 463 | flex-direction: column; |
| 464 | } |
| 465 |
| 466 | .pagination-section { |
| 467 | justify-content: center; |
| 468 | } |
| 469 | } |
| 470 | </style> |