| 201 | } |
| 202 | |
| 203 | function parseTarget(input) { |
| 204 | let host = String(input || '').split('#')[0].trim(); |
| 205 | let port = 443; |
| 206 | |
| 207 | if (host.startsWith('[')) { |
| 208 | const ipv6PortIndex = host.lastIndexOf(']:'); |
| 209 | if (ipv6PortIndex !== -1) { |
| 210 | const maybePort = Number(host.slice(ipv6PortIndex + 2)); |
| 211 | if (Number.isInteger(maybePort) && maybePort >= 1 && maybePort <= 65535) { |
| 212 | port = maybePort; |
| 213 | host = host.slice(0, ipv6PortIndex + 1); |
| 214 | } |
| 215 | } |
| 216 | return { host, port }; |
| 217 | } |
| 218 | |
| 219 | const colonMatches = host.match(/:/g) || []; |
| 220 | if (colonMatches.length === 1) { |
| 221 | const separatorIndex = host.lastIndexOf(':'); |
| 222 | const maybePort = Number(host.slice(separatorIndex + 1)); |
| 223 | if (Number.isInteger(maybePort) && maybePort >= 1 && maybePort <= 65535) { |
| 224 | port = maybePort; |
| 225 | host = host.slice(0, separatorIndex); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return { host, port }; |
| 230 | } |
| 231 | |
| 232 | function isIPv4(value) { |
| 233 | const parts = value.split('.'); |