| ProxyV2Info, |
| 9 | ProxyV2Spec, |
| 10 | ProxyV2SpecBlocks, |
| 11 | ProxyV2Type, |
| 12 | TrafficResponse, |
| 13 | } from '../types/proxy' |
| 14 |
| 15 | export interface SystemPruneResponse { |
| 16 | type: 'offline_proxies' |
| 17 | cleared: number |
| 18 | total: number |
| 19 | } |
| 20 |
| 21 | export const getProxiesByType = (type: string) => { |
| 22 | return http.get<GetProxyResponse>(`../api/proxy/${type}`) |
| 23 | } |
| 24 |
| 25 | export const getProxiesV2 = async (params: ProxyListV2Params = {}) => { |
| 26 | const page = await http.getV2<V2Page<ProxyV2Info>>( |
| 27 | `../api/v2/proxies${buildQueryString({ |
| 28 | page: params.page, |
| 29 | pageSize: params.pageSize, |
| 30 | status: |
| 31 | params.status && params.status !== 'all' ? params.status : undefined, |
| 32 | q: params.q || undefined, |
| 33 | type: params.type || undefined, |
| 34 | user: params.user, |
| 35 | clientID: params.clientID || undefined, |
| 36 | })}`, |
| 37 | ) |
| 38 |
| 39 | return { |
| 40 | ...page, |
| 41 | items: page.items.map(toLegacyProxyStats), |
| 42 | } |
| 43 | } |
| 44 |
| 45 | const getActiveProxySpec = ( |
| 46 | spec: ProxyV2Spec, |
| 47 | ): ProxyV2SpecBlocks[ProxyV2Type] => { |
| 48 | switch (spec.type) { |
| 49 | case 'tcp': |
| 50 | return spec.tcp |
| 51 | case 'udp': |
| 52 | return spec.udp |
| 53 | case 'http': |
| 54 | return spec.http |
| 55 | case 'https': |
| 56 | return spec.https |
| 57 | case 'tcpmux': |
| 58 | return spec.tcpmux |
| 59 | case 'stcp': |
| 60 | return spec.stcp |
| 61 | case 'sudp': |
| 62 | return spec.sudp |
| 63 | case 'xtcp': |
| 64 | return spec.xtcp |
| 65 | default: |
| 66 | return assertNever(spec) |
| 67 | } |
| 68 | } |
| 69 |
| 70 | const assertNever = (value: never): never => { |
| 71 | throw new Error(`Unsupported proxy spec: ${JSON.stringify(value)}`) |
| 72 | } |
| 73 |
| 74 | export const toLegacyProxyStats = (proxy: ProxyV2Info): ProxyStatsInfo => { |
| 75 | const type = proxy.spec.type |
| 76 | const activeSpec = getActiveProxySpec(proxy.spec) |
| 77 |
| 78 | return { |
| 79 | name: proxy.name, |
| 80 | type, |
| 81 | conf: proxy.status.phase === 'offline' ? null : activeSpec, |
| 82 | user: proxy.user, |
| 83 | clientID: proxy.clientID, |
| 84 | todayTrafficIn: proxy.status.todayTrafficIn, |
| 85 | todayTrafficOut: proxy.status.todayTrafficOut, |
| 86 | curConns: proxy.status.curConns, |
| 87 | lastStartTime: formatUnixSeconds(proxy.status.lastStartAt), |
| 88 | lastCloseTime: formatUnixSeconds(proxy.status.lastCloseAt), |
| 89 | status: proxy.status.phase, |
| 90 | } |
| 91 | } |
| 92 |
| 93 | export const getProxy = (type: string, name: string) => { |
| 94 | return http.get<ProxyStatsInfo>(`../api/proxy/${type}/${name}`) |
| 95 | } |
| 96 |
| 97 | export const getProxyByNameV2 = async (name: string) => { |
| 98 | const proxy = await http.getV2<ProxyV2Info>( |
| 99 | `../api/v2/proxies/${encodeURIComponent(name)}`, |
| 100 | ) |
| 101 | return toLegacyProxyStats(proxy) |
| 102 | } |
| 103 |
| 104 | export const getProxyByName = (name: string) => { |
| 105 | return http.get<ProxyStatsInfo>(`../api/proxies/${name}`) |
| 106 | } |
| 107 |
| 108 | export const getProxyTraffic = (name: string) => { |
| 109 | return http.getV2<TrafficResponse>( |
| 110 | `../api/v2/proxies/${encodeURIComponent(name)}/traffic`, |
| 111 | ) |
| 112 | } |
| 113 |
| 114 | export const clearOfflineProxies = () => { |
| 115 | return http.postV2<SystemPruneResponse>( |
| 116 | '../api/v2/system/prune?type=offline_proxies', |
| 117 | ) |
| 118 | } |