Skip to content

Commit e96015a

Browse files
committed
fix bunch of other crashes and mistakes
1 parent c89b562 commit e96015a

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

src/containers/AddThirdPartyServer/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import { usePersistentServers } from "../../states/servers";
1717
import { useTheme } from "../../states/theme";
1818
import { sc } from "../../utils/sizeScaler";
1919
import { Server } from "../../utils/types";
20-
import { validateServerAddress } from "../../utils/validation";
20+
import {
21+
isValidDomain,
22+
validateServerAddressIPv4,
23+
} from "../../utils/validation";
2124

2225
const AddThirdPartyServerModal = () => {
2326
const { visible, showAddThirdPartyServer } = useAddThirdPartyServerModal();
@@ -36,10 +39,6 @@ const AddThirdPartyServerModal = () => {
3639
}
3740
}, [visible]);
3841

39-
if (!visible) {
40-
return null;
41-
}
42-
4342
const addServer = useCallback(() => {
4443
const serverInfo: Server = {
4544
ip: "",
@@ -66,7 +65,10 @@ const AddThirdPartyServerModal = () => {
6665
serverInfo.port = parseInt(data[1]);
6766
serverInfo.hostname += ` (${serverInfo.ip}:${serverInfo.port})`;
6867
} else {
69-
if (validateServerAddress(serverAddress)) {
68+
if (
69+
validateServerAddressIPv4(serverAddress) ||
70+
isValidDomain(serverAddress)
71+
) {
7072
serverInfo.ip = serverAddress;
7173
serverInfo.port = 7777;
7274
serverInfo.hostname += ` (${serverInfo.ip}:${serverInfo.port})`;
@@ -96,6 +98,10 @@ const AddThirdPartyServerModal = () => {
9698
[height, width, theme]
9799
);
98100

101+
if (!visible) {
102+
return null;
103+
}
104+
99105
return (
100106
<StaticModal onDismiss={() => showAddThirdPartyServer(false)}>
101107
<View style={[styles.container, dynamicStyles.container]}>

src/containers/ExternalServerHandler/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import { useTheme } from "../../states/theme";
1818
import { startGame } from "../../utils/game";
1919
import { sc } from "../../utils/sizeScaler";
2020
import { Server } from "../../utils/types";
21-
import { validateServerAddress } from "../../utils/validation";
21+
import {
22+
isValidDomain,
23+
validateServerAddressIPv4,
24+
} from "../../utils/validation";
2225

2326
const ExternalServerHandler = () => {
2427
const [visible, showModal] = useState(false);
@@ -101,7 +104,10 @@ const ExternalServerHandler = () => {
101104
serverInfo.port = parseInt(data[1]);
102105
serverInfo.hostname += ` (${serverInfo.ip}:${serverInfo.port})`;
103106
} else {
104-
if (validateServerAddress(serverAddress)) {
107+
if (
108+
validateServerAddressIPv4(serverAddress) ||
109+
isValidDomain(serverAddress)
110+
) {
105111
serverInfo.ip = serverAddress;
106112
serverInfo.port = 7777;
107113
serverInfo.hostname += ` (${serverInfo.ip}:${serverInfo.port})`;
@@ -139,7 +145,10 @@ const ExternalServerHandler = () => {
139145
serverInfo.port = parseInt(data[1]);
140146
serverInfo.hostname += ` (${serverInfo.ip}:${serverInfo.port})`;
141147
} else {
142-
if (validateServerAddress(serverAddress)) {
148+
if (
149+
validateServerAddressIPv4(serverAddress) ||
150+
isValidDomain(serverAddress)
151+
) {
143152
serverInfo.ip = serverAddress;
144153
serverInfo.port = 7777;
145154
serverInfo.hostname += ` (${serverInfo.ip}:${serverInfo.port})`;

src/containers/Settings/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ const SettingsModal = () => {
3838
{ label: t("settings_advanced_tab_title"), type: "advanced" },
3939
];
4040

41-
if (!visible) {
42-
return null;
43-
}
44-
4541
const renderTab = () => {
4642
if (selectedTab === "general") return <General />;
4743
else if (selectedTab === "languages") return <Languages />;
4844
else if (selectedTab === "advanced") return <Advanced />;
4945
else return null;
5046
};
5147

48+
if (!visible) {
49+
return null;
50+
}
51+
5252
return (
5353
<StaticModal onDismiss={() => hide()} key={"settings-" + language}>
5454
<View

src/utils/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
Server,
1818
SortType,
1919
} from "./types";
20-
import { validateServerAddress } from "./validation";
20+
import { validateServerAddressIPv4 } from "./validation";
2121

2222
// Server update configuration
2323
const SERVER_UPDATE_CONFIG = {
@@ -177,7 +177,7 @@ export const getIpAddress = async (
177177
}
178178

179179
// Use validation function from validation.ts
180-
if (validateServerAddress(hostname)) {
180+
if (validateServerAddressIPv4(hostname)) {
181181
return hostname;
182182
}
183183

src/utils/validation.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const isValidDomain = (domain: string): boolean => {
1616
return DOMAIN_REGEX.test(domain);
1717
};
1818

19-
export const validateServerAddress = (address: string): boolean => {
19+
export const validateServerAddressIPv4 = (address: string): boolean => {
2020
if (!address || typeof address !== "string") return false;
2121

2222
// Check localhost first (fastest check)
@@ -25,13 +25,27 @@ export const validateServerAddress = (address: string): boolean => {
2525
// Check IPv4
2626
if (isIPv4(address)) return true;
2727

28-
// Check domain
29-
return isValidDomain(address);
28+
// Check domain, return false if it's valid (to resolve hostname later)
29+
return false;
3030
};
3131

3232
export const validateWebUrl = (url: string): boolean => {
3333
if (!url || typeof url !== "string") return false;
34-
return WEB_URL_REGEX.test(url);
34+
35+
// If URL already has protocol, test as-is
36+
if (/^https?:\/\//.test(url)) {
37+
return WEB_URL_REGEX.test(url);
38+
}
39+
40+
// Try with https:// prefix
41+
const httpsUrl = `https://${url}`;
42+
if (WEB_URL_REGEX.test(httpsUrl)) {
43+
return true;
44+
}
45+
46+
// Try with http:// prefix
47+
const httpUrl = `http://${url}`;
48+
return WEB_URL_REGEX.test(httpUrl);
3549
};
3650

3751
export const validatePort = (port: number | string): boolean => {
@@ -43,5 +57,5 @@ export const validateServerEndpoint = (
4357
ip: string,
4458
port: number | string
4559
): boolean => {
46-
return validateServerAddress(ip) && validatePort(port);
60+
return validateServerAddressIPv4(ip) && validatePort(port);
4761
};

0 commit comments

Comments
 (0)