|
1 | 1 | import _ from 'lodash'; |
2 | 2 | import YAML from '@/utils/yaml'; |
| 3 | +import { isIPv4, isIPv6 } from '@/utils'; |
3 | 4 |
|
4 | 5 | export class Result { |
5 | 6 | constructor(proxy) { |
@@ -65,6 +66,81 @@ export function supportsShadowsocksV2rayPluginMode(proxy, supportedModes) { |
65 | 66 | return supportedModes.includes(normalizedMode); |
66 | 67 | } |
67 | 68 |
|
| 69 | +function parseWireGuardCIDR(cidr, max) { |
| 70 | + if (cidr == null) return undefined; |
| 71 | + const normalized = `${cidr}`.trim(); |
| 72 | + if (!/^\d+$/.test(normalized)) return undefined; |
| 73 | + const parsed = parseInt(normalized, 10); |
| 74 | + if (parsed < 0 || parsed > max) return undefined; |
| 75 | + return parsed; |
| 76 | +} |
| 77 | + |
| 78 | +function parseWireGuardInterfaceAddress(value, family) { |
| 79 | + if (value == null) return null; |
| 80 | + const raw = `${value}`.trim(); |
| 81 | + if (!raw) return null; |
| 82 | + const [, hostRaw = raw, cidrRaw] = /^(.*?)(?:\/(\d+))?$/.exec(raw) || []; |
| 83 | + const host = `${hostRaw}`.trim().replace(/^\[/, '').replace(/\]$/, ''); |
| 84 | + const isIPv4Family = family === 'ipv4'; |
| 85 | + const isValid = isIPv4Family ? isIPv4(host) : isIPv6(host); |
| 86 | + if (!isValid) return null; |
| 87 | + const max = isIPv4Family ? 32 : 128; |
| 88 | + return { |
| 89 | + address: host, |
| 90 | + cidr: parseWireGuardCIDR(cidrRaw, max), |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +function normalizeWireGuardInterfaceAddress(proxy, config) { |
| 95 | + const { addressKey, cidrKey, family, defaultCIDR } = config; |
| 96 | + const parsed = parseWireGuardInterfaceAddress(proxy[addressKey], family); |
| 97 | + if (!parsed) { |
| 98 | + if ( |
| 99 | + proxy[addressKey] == null || |
| 100 | + `${proxy[addressKey]}`.trim().length === 0 |
| 101 | + ) { |
| 102 | + delete proxy[cidrKey]; |
| 103 | + } |
| 104 | + return; |
| 105 | + } |
| 106 | + proxy[addressKey] = parsed.address; |
| 107 | + const normalizedCIDR = parseWireGuardCIDR(proxy[cidrKey], defaultCIDR); |
| 108 | + proxy[cidrKey] = normalizedCIDR ?? parsed.cidr ?? defaultCIDR; |
| 109 | +} |
| 110 | + |
| 111 | +export function normalizeWireGuardInterface(proxy = {}) { |
| 112 | + normalizeWireGuardInterfaceAddress(proxy, { |
| 113 | + addressKey: 'ip', |
| 114 | + cidrKey: 'ip-cidr', |
| 115 | + family: 'ipv4', |
| 116 | + defaultCIDR: 32, |
| 117 | + }); |
| 118 | + normalizeWireGuardInterfaceAddress(proxy, { |
| 119 | + addressKey: 'ipv6', |
| 120 | + cidrKey: 'ipv6-cidr', |
| 121 | + family: 'ipv6', |
| 122 | + defaultCIDR: 128, |
| 123 | + }); |
| 124 | + return proxy; |
| 125 | +} |
| 126 | + |
| 127 | +export function getWireGuardAddressWithCIDR(proxy = {}, family = 'ipv4') { |
| 128 | + const config = |
| 129 | + family === 'ipv6' |
| 130 | + ? { addressKey: 'ipv6', cidrKey: 'ipv6-cidr', defaultCIDR: 128 } |
| 131 | + : { addressKey: 'ip', cidrKey: 'ip-cidr', defaultCIDR: 32 }; |
| 132 | + const parsed = parseWireGuardInterfaceAddress( |
| 133 | + proxy[config.addressKey], |
| 134 | + family, |
| 135 | + ); |
| 136 | + if (!parsed) return undefined; |
| 137 | + const normalizedCIDR = parseWireGuardCIDR( |
| 138 | + proxy[config.cidrKey], |
| 139 | + config.defaultCIDR, |
| 140 | + ); |
| 141 | + return `${parsed.address}/${normalizedCIDR ?? parsed.cidr ?? config.defaultCIDR}`; |
| 142 | +} |
| 143 | + |
68 | 144 | export function produceProxyListOutput(list, type, opts = {}) { |
69 | 145 | if (type === 'internal') return list; |
70 | 146 |
|
|
0 commit comments