1
+ import ip from 'ip' ;
1
2
import * as React from 'react' ;
2
3
import { sprintf } from 'sprintf-js' ;
3
4
import { colors } from '../../config.json' ;
@@ -91,13 +92,15 @@ interface IState {
91
92
showConfirmBlockWhenDisconnectedAlert : boolean ;
92
93
showAddCustomDns : boolean ;
93
94
invalidDnsIp : boolean ;
95
+ publicDnsIpToConfirm ?: string ;
94
96
}
95
97
96
98
export default class AdvancedSettings extends React . Component < IProps , IState > {
97
99
public state = {
98
100
showConfirmBlockWhenDisconnectedAlert : false ,
99
101
showAddCustomDns : false ,
100
102
invalidDnsIp : false ,
103
+ publicDnsIpToConfirm : undefined ,
101
104
} ;
102
105
103
106
private customDnsSwitchRef = React . createRef < HTMLDivElement > ( ) ;
@@ -470,7 +473,7 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
470
473
< StyledAddCustomDnsLabel tabIndex = { - 1 } >
471
474
{ messages . pgettext ( 'advanced-settings-view' , 'Add a server' ) }
472
475
</ StyledAddCustomDnsLabel >
473
- < Cell . UntintedIcon
476
+ < Cell . Icon
474
477
source = "icon-add"
475
478
width = { 22 }
476
479
height = { 22 }
@@ -496,6 +499,7 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
496
499
497
500
{ this . state . showConfirmBlockWhenDisconnectedAlert &&
498
501
this . renderConfirmBlockWhenDisconnectedAlert ( ) }
502
+ { this . state . publicDnsIpToConfirm && this . renderCustomDnsConfirmationDialog ( ) }
499
503
</ ModalContainer >
500
504
) ;
501
505
}
@@ -543,24 +547,43 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
543
547
} ;
544
548
545
549
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
+ }
549
555
}
550
556
}
551
557
552
558
private addDnsInputChange = ( _value : string ) => {
553
559
this . setState ( { invalidDnsIp : false } ) ;
554
560
} ;
555
561
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 {
564
587
this . setState ( { invalidDnsIp : true } ) ;
565
588
}
566
589
} ;
@@ -600,6 +623,26 @@ export default class AdvancedSettings extends React.Component<IProps, IState> {
600
623
] ;
601
624
} ;
602
625
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
+
603
646
private renderConfirmBlockWhenDisconnectedAlert = ( ) => {
604
647
return (
605
648
< ModalAlert
0 commit comments