Skip to content

Commit 87dd20b

Browse files
committed
Add public dns ip confirmation dialog
1 parent ab3dfaf commit 87dd20b

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

gui/src/renderer/components/AdvancedSettings.tsx

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ip from 'ip';
12
import * as React from 'react';
23
import { sprintf } from 'sprintf-js';
34
import { colors } from '../../config.json';
@@ -91,13 +92,15 @@ interface IState {
9192
showConfirmBlockWhenDisconnectedAlert: boolean;
9293
showAddCustomDns: boolean;
9394
invalidDnsIp: boolean;
95+
publicDnsIpToConfirm?: string;
9496
}
9597

9698
export default class AdvancedSettings extends React.Component<IProps, IState> {
9799
public state = {
98100
showConfirmBlockWhenDisconnectedAlert: false,
99101
showAddCustomDns: false,
100102
invalidDnsIp: false,
103+
publicDnsIpToConfirm: undefined,
101104
};
102105

103106
private customDnsSwitchRef = React.createRef<HTMLDivElement>();
@@ -470,7 +473,7 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
470473
<StyledAddCustomDnsLabel tabIndex={-1}>
471474
{messages.pgettext('advanced-settings-view', 'Add a server')}
472475
</StyledAddCustomDnsLabel>
473-
<Cell.UntintedIcon
476+
<Cell.Icon
474477
source="icon-add"
475478
width={22}
476479
height={22}
@@ -496,6 +499,7 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
496499

497500
{this.state.showConfirmBlockWhenDisconnectedAlert &&
498501
this.renderConfirmBlockWhenDisconnectedAlert()}
502+
{this.state.publicDnsIpToConfirm && this.renderCustomDnsConfirmationDialog()}
499503
</ModalContainer>
500504
);
501505
}
@@ -543,24 +547,43 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
543547
};
544548

545549
private hideAddCustomDnsRow(justAdded: boolean) {
546-
this.setState({ showAddCustomDns: false });
547-
if (!justAdded && this.props.dns.addresses.length === 0) {
548-
consumePromise(this.setCustomDnsEnabled(false));
550+
if (!this.state.publicDnsIpToConfirm) {
551+
this.setState({ showAddCustomDns: false });
552+
if (!justAdded && this.props.dns.addresses.length === 0) {
553+
consumePromise(this.setCustomDnsEnabled(false));
554+
}
549555
}
550556
}
551557

552558
private addDnsInputChange = (_value: string) => {
553559
this.setState({ invalidDnsIp: false });
554560
};
555561

556-
private addDnsAddress = async (address: string) => {
557-
try {
558-
await this.props.setDnsOptions({
559-
custom: this.props.dns.custom,
560-
addresses: [...this.props.dns.addresses, address],
561-
});
562-
this.hideAddCustomDnsRow(true);
563-
} catch (_e) {
562+
private hideCustomDnsConfirmationDialog = () => {
563+
this.setState({ publicDnsIpToConfirm: undefined });
564+
};
565+
566+
private confirmPublicDnsAddress = () => {
567+
consumePromise(this.addDnsAddress(this.state.publicDnsIpToConfirm!, true));
568+
this.hideCustomDnsConfirmationDialog();
569+
};
570+
571+
private addDnsAddress = async (address: string, confirmed?: boolean) => {
572+
if (ip.isV4Format(address) || ip.isV6Format(address)) {
573+
if (ip.isPublic(address) && !confirmed) {
574+
this.setState({ publicDnsIpToConfirm: address });
575+
} else {
576+
try {
577+
await this.props.setDnsOptions({
578+
custom: this.props.dns.custom,
579+
addresses: [...this.props.dns.addresses, address],
580+
});
581+
this.hideAddCustomDnsRow(true);
582+
} catch (_e) {
583+
this.setState({ invalidDnsIp: true });
584+
}
585+
}
586+
} else {
564587
this.setState({ invalidDnsIp: true });
565588
}
566589
};
@@ -600,6 +623,26 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
600623
];
601624
};
602625

626+
private renderCustomDnsConfirmationDialog = () => {
627+
return (
628+
<ModalAlert
629+
type={ModalAlertType.info}
630+
buttons={[
631+
<AppButton.RedButton key="confirm" onClick={this.confirmPublicDnsAddress}>
632+
{messages.pgettext('advanced-settings-view', 'Add anyway')}
633+
</AppButton.RedButton>,
634+
<AppButton.BlueButton key="back" onClick={this.hideCustomDnsConfirmationDialog}>
635+
{messages.gettext('Back')}
636+
</AppButton.BlueButton>,
637+
]}
638+
close={this.hideCustomDnsConfirmationDialog}
639+
message={messages.pgettext(
640+
'advanced-settings-view',
641+
'The DNS server you are trying to add might not work because it is public. Currently we only support local DNS servers.',
642+
)}></ModalAlert>
643+
);
644+
};
645+
603646
private renderConfirmBlockWhenDisconnectedAlert = () => {
604647
return (
605648
<ModalAlert

0 commit comments

Comments
 (0)