wangzhibo
2023-10-31 9da39812105bfc387db3b3b7c4896983e03d247d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { PROXIES_ORDERING_TYPE } from '~/constants'
import { latencyQualityMap } from '~/signals'
 
export const formatProxyType = (type = '') => {
  const t = type.toLowerCase()
 
  if (t.includes('shadowsocks')) {
    return t.replace('shadowsocks', 'ss') // for both ss and ssr
  }
 
  if (t === 'hysteria') {
    return 'hy'
  }
 
  if (t === 'wireguard') {
    return 'wg'
  }
 
  return t
}
 
export const filterSpecialProxyType = (type = '') => {
  const t = type.toLowerCase()
  const conditions = [
    'selector',
    'direct',
    'reject',
    'urltest',
    'loadbalance',
    'fallback',
    'relay',
  ]
 
  return !conditions.includes(t)
}
 
export const sortProxiesByOrderingType = (
  proxyNames: string[],
  proxyLatencyMap: Record<string, number>,
  orderingType: PROXIES_ORDERING_TYPE,
) => {
  if (orderingType === PROXIES_ORDERING_TYPE.NATURAL) {
    return proxyNames
  }
 
  return proxyNames.sort((a, b) => {
    const prevLatency = proxyLatencyMap[a]
    const nextLatency = proxyLatencyMap[b]
 
    switch (orderingType) {
      case PROXIES_ORDERING_TYPE.LATENCY_ASC:
        if (prevLatency === latencyQualityMap().NOT_CONNECTED) return 1
 
        if (nextLatency === latencyQualityMap().NOT_CONNECTED) return -1
 
        return prevLatency - nextLatency
 
      case PROXIES_ORDERING_TYPE.LATENCY_DESC:
        if (prevLatency === latencyQualityMap().NOT_CONNECTED) return 1
 
        if (nextLatency === latencyQualityMap().NOT_CONNECTED) return -1
 
        return nextLatency - prevLatency
 
      case PROXIES_ORDERING_TYPE.NAME_ASC:
        return a.localeCompare(b)
 
      case PROXIES_ORDERING_TYPE.NAME_DESC:
        return b.localeCompare(a)
 
      default:
        return 0
    }
  })
}
 
export const filterProxiesByAvailability = (
  proxyNames: string[],
  proxyLatencyMap: Record<string, number>,
  enabled?: boolean,
) =>
  enabled
    ? proxyNames.filter(
        (name) => proxyLatencyMap[name] !== latencyQualityMap().NOT_CONNECTED,
      )
    : proxyNames