diff --git a/src/utils.ts b/src/utils.ts index d62e2aa..f5219ff 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,7 +8,20 @@ export function shortenAddress( } export function sanitizeURLProtocol(protocol: 'ws' | 'http', endpoint: string) { - return location.protocol.startsWith('https') + return location.protocol.startsWith('https') && + endpoint.indexOf('localhost') === -1 && + !isStringIpAddress(endpoint) ? `${protocol}s://${endpoint}` : `${protocol}://${endpoint}` } + +function isStringIpAddress(value: string) { + if ( + /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test( + value + ) + ) { + return true + } + return false +}