|
1 | 1 | import React, { memo, ReactElement, useEffect, useState } from 'react';
|
2 | 2 | import { StyleSheet } from 'react-native';
|
3 | 3 | import { err, ok, Result } from '@synonymdev/result';
|
4 |
| -import Url from 'url-parse'; |
| 4 | +import parseUrl from 'url-parse'; |
5 | 5 | import { useTranslation } from 'react-i18next';
|
6 | 6 | import isEqual from 'lodash/isEqual';
|
7 | 7 | import { EProtocol } from 'beignet';
|
@@ -42,20 +42,35 @@ const radioButtons: RadioButtonItem[] = [
|
42 | 42 | ];
|
43 | 43 |
|
44 | 44 | const isValidURL = (data: string): boolean => {
|
45 |
| - const pattern = new RegExp( |
46 |
| - '^(https?:\\/\\/)?' + // protocol |
47 |
| - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name |
48 |
| - '((\\d{1,3}\\.){3}\\d{1,3}))' + // IP (v4) address |
49 |
| - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*', // port and path |
50 |
| - 'i', |
51 |
| - ); |
52 |
| - |
53 |
| - // wave through everything 'localhost' for development |
54 |
| - if (__DEV__ && data.includes('localhost')) { |
55 |
| - return true; |
| 45 | + // Add 'http://' if the protocol is missing to enable URL parsing |
| 46 | + let normalizedData = data; |
| 47 | + if (!/^https?:\/\//i.test(data)) { |
| 48 | + normalizedData = 'http://' + data; |
56 | 49 | }
|
57 | 50 |
|
58 |
| - return !!pattern.test(data); |
| 51 | + try { |
| 52 | + const url = parseUrl(normalizedData); |
| 53 | + |
| 54 | + // Allow standard domains, custom TLDs like .local, and IPv4 addresses |
| 55 | + const isValidDomainOrIP = !!url.hostname.match( |
| 56 | + /^([a-z\d]([a-z\d-]*[a-z\d])*\.[a-z\d-]+|(\d{1,3}\.){3}\d{1,3})$/i, |
| 57 | + ); |
| 58 | + |
| 59 | + // Always allow .local domains |
| 60 | + if (url.hostname.endsWith('.local')) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + // Allow localhost in development mode |
| 65 | + if (__DEV__ && data.includes('localhost')) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + return isValidDomainOrIP; |
| 70 | + } catch (e) { |
| 71 | + // If URL constructor fails, it's not a valid URL |
| 72 | + return false; |
| 73 | + } |
59 | 74 | };
|
60 | 75 |
|
61 | 76 | const validateInput = (
|
@@ -218,7 +233,7 @@ const ElectrumConfig = ({
|
218 | 233 | protocol: _protocol,
|
219 | 234 | };
|
220 | 235 | } else {
|
221 |
| - const url = new Url(data); |
| 236 | + const url = parseUrl(data); |
222 | 237 |
|
223 | 238 | connectData = {
|
224 | 239 | host: url.hostname,
|
|
0 commit comments