| createStoreProxy, |
| 9 | updateStoreProxy, |
| 10 | deleteStoreProxy, |
| 11 | } from '../api/frpc' |
| 12 |
| 13 | export const useProxyStore = defineStore('proxy', () => { |
| 14 | const proxies = ref<ProxyStatus[]>([]) |
| 15 | const storeProxies = ref<ProxyDefinition[]>([]) |
| 16 | const storeEnabled = ref(false) |
| 17 | const storeChecked = ref(false) |
| 18 | const loading = ref(false) |
| 19 | const storeLoading = ref(false) |
| 20 | const error = ref<string | null>(null) |
| 21 |
| 22 | const fetchStatus = async () => { |
| 23 | loading.value = true |
| 24 | error.value = null |
| 25 | try { |
| 26 | const json = await getStatus() |
| 27 | const list: ProxyStatus[] = [] |
| 28 | for (const key in json) { |
| 29 | for (const ps of json[key]) { |
| 30 | list.push(ps) |
| 31 | } |
| 32 | } |
| 33 | proxies.value = list |
| 34 | } catch (err: any) { |
| 35 | error.value = err.message |
| 36 | throw err |
| 37 | } finally { |
| 38 | loading.value = false |
| 39 | } |
| 40 | } |
| 41 |
| 42 | const fetchStoreProxies = async () => { |
| 43 | storeLoading.value = true |
| 44 | try { |
| 45 | const res = await listStoreProxies() |
| 46 | storeProxies.value = res.proxies || [] |
| 47 | storeEnabled.value = true |
| 48 | storeChecked.value = true |
| 49 | } catch (err: any) { |
| 50 | if (err?.status === 404) { |
| 51 | storeEnabled.value = false |
| 52 | } |
| 53 | storeChecked.value = true |
| 54 | } finally { |
| 55 | storeLoading.value = false |
| 56 | } |
| 57 | } |
| 58 |
| 59 | const checkStoreEnabled = async () => { |
| 60 | if (storeChecked.value) return storeEnabled.value |
| 61 | await fetchStoreProxies() |
| 62 | return storeEnabled.value |
| 63 | } |
| 64 |
| 65 | const createProxy = async (data: ProxyDefinition) => { |
| 66 | await createStoreProxy(data) |
| 67 | await fetchStoreProxies() |
| 68 | } |
| 69 |
| 70 | const updateProxy = async (name: string, data: ProxyDefinition) => { |
| 71 | await updateStoreProxy(name, data) |
| 72 | await fetchStoreProxies() |
| 73 | } |
| 74 |
| 75 | const deleteProxy = async (name: string) => { |
| 76 | await deleteStoreProxy(name) |
| 77 | await fetchStoreProxies() |
| 78 | } |
| 79 |
| 80 | const toggleProxy = async (name: string, enabled: boolean) => { |
| 81 | const def = await getStoreProxy(name) |
| 82 | const block = (def as any)[def.type] |
| 83 | if (block) { |
| 84 | block.enabled = enabled |
| 85 | } |
| 86 | await updateStoreProxy(name, def) |
| 87 | await fetchStatus() |
| 88 | await fetchStoreProxies() |
| 89 | } |
| 90 |
| 91 | const storeProxyWithStatus = (def: ProxyDefinition): ProxyStatus => { |
| 92 | const block = (def as any)[def.type] |
| 93 | const enabled = block?.enabled !== false |
| 94 |
| 95 | const localIP = block?.localIP || '127.0.0.1' |
| 96 | const localPort = block?.localPort |
| 97 | const local_addr = localPort != null ? `${localIP}:${localPort}` : '' |
| 98 | const remotePort = block?.remotePort |
| 99 | const remote_addr = remotePort != null ? `:${remotePort}` : '' |
| 100 | const plugin = block?.plugin?.type || '' |
| 101 |
| 102 | const status = proxies.value.find((p) => p.name === def.name) |
| 103 | return { |
| 104 | name: def.name, |
| 105 | type: def.type, |
| 106 | status: !enabled ? 'disabled' : (status?.status || 'waiting'), |
| 107 | err: status?.err || '', |
| 108 | local_addr: status?.local_addr || local_addr, |
| 109 | remote_addr: status?.remote_addr || remote_addr, |
| 110 | plugin: status?.plugin || plugin, |
| 111 | source: 'store', |
| 112 | } |
| 113 | } |
| 114 |
| 115 | return { |
| 116 | proxies, |
| 117 | storeProxies, |
| 118 | storeEnabled, |
| 119 | storeChecked, |
| 120 | loading, |
| 121 | storeLoading, |
| 122 | error, |
| 123 | fetchStatus, |
| 124 | fetchStoreProxies, |
| 125 | checkStoreEnabled, |
| 126 | createProxy, |
| 127 | updateProxy, |
| 128 | deleteProxy, |
| 129 | toggleProxy, |
| 130 | storeProxyWithStatus, |
| 131 | } |
| 132 | }) |