2020-09-22 12:04:17 +00:00
|
|
|
import React, { useRef } from 'react';
|
2020-06-10 16:04:19 +00:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2019-03-06 11:45:21 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-06-10 16:04:19 +00:00
|
|
|
import { Field, reduxForm } from 'redux-form';
|
|
|
|
import { Trans, useTranslation } from 'react-i18next';
|
2019-03-06 11:45:21 +00:00
|
|
|
import classnames from 'classnames';
|
2019-03-20 11:24:33 +00:00
|
|
|
import Examples from './Examples';
|
2020-07-15 09:35:37 +00:00
|
|
|
import { renderRadioField, renderTextareaField } from '../../../../helpers/form';
|
2020-09-22 12:04:17 +00:00
|
|
|
import {
|
|
|
|
DNS_REQUEST_OPTIONS,
|
|
|
|
FORM_NAME,
|
|
|
|
isFirefox,
|
|
|
|
UPSTREAM_CONFIGURATION_WIKI_LINK,
|
|
|
|
} from '../../../../helpers/constants';
|
|
|
|
import { testUpstreamWithFormValues } from '../../../../actions';
|
|
|
|
import { removeEmptyLines, trimLinesAndRemoveEmpty } from '../../../../helpers/helpers';
|
|
|
|
import { getTextareaCommentsHighlight, syncScroll } from '../../../../helpers/highlightTextareaComments';
|
|
|
|
import '../../../ui/texareaCommentsHighlight.css';
|
2020-04-22 16:32:07 +00:00
|
|
|
|
2020-09-10 09:54:36 +00:00
|
|
|
const UPSTREAM_DNS_NAME = 'upstream_dns';
|
2020-09-22 12:04:17 +00:00
|
|
|
const UPSTREAM_MODE_NAME = 'upstream_mode';
|
2020-09-10 09:54:36 +00:00
|
|
|
|
2020-09-22 12:04:17 +00:00
|
|
|
const renderField = ({
|
|
|
|
name, component, type, className, placeholder,
|
|
|
|
subtitle, value, normalizeOnBlur, containerClass, onScroll,
|
|
|
|
}) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const processingTestUpstream = useSelector((state) => state.settings.processingTestUpstream);
|
|
|
|
const processingSetConfig = useSelector((state) => state.dnsConfig.processingSetConfig);
|
|
|
|
|
|
|
|
return <div
|
|
|
|
key={placeholder}
|
|
|
|
className={classnames('col-12 mb-4', containerClass)}
|
|
|
|
>
|
|
|
|
<Field
|
|
|
|
id={name}
|
|
|
|
value={value}
|
|
|
|
name={name}
|
|
|
|
component={component}
|
|
|
|
type={type}
|
|
|
|
className={className}
|
|
|
|
placeholder={t(placeholder)}
|
|
|
|
subtitle={t(subtitle)}
|
|
|
|
disabled={processingSetConfig || processingTestUpstream}
|
|
|
|
normalizeOnBlur={normalizeOnBlur}
|
|
|
|
onScroll={onScroll}
|
|
|
|
/>
|
|
|
|
</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
renderField.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
component: PropTypes.element.isRequired,
|
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
className: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string.isRequired,
|
|
|
|
subtitle: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
|
|
|
normalizeOnBlur: PropTypes.func,
|
|
|
|
containerClass: PropTypes.string,
|
|
|
|
onScroll: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderTextareaWithHighlightField = (props) => {
|
|
|
|
const upstream_dns = useSelector((store) => store.form[FORM_NAME.UPSTREAM].values.upstream_dns);
|
|
|
|
const upstream_dns_file = useSelector((state) => state.dnsConfig.upstream_dns_file);
|
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
|
|
const onScroll = (e) => syncScroll(e, ref);
|
|
|
|
|
|
|
|
return <>
|
|
|
|
{renderTextareaField({
|
|
|
|
...props,
|
|
|
|
disabled: !!upstream_dns_file,
|
|
|
|
onScroll,
|
|
|
|
normalizeOnBlur: trimLinesAndRemoveEmpty,
|
|
|
|
})}
|
|
|
|
{getTextareaCommentsHighlight(ref, upstream_dns)}
|
|
|
|
</>;
|
|
|
|
};
|
2020-09-09 18:51:02 +00:00
|
|
|
|
2020-09-22 12:04:17 +00:00
|
|
|
renderTextareaWithHighlightField.propTypes = {
|
|
|
|
className: PropTypes.string.isRequired,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
input: PropTypes.object,
|
|
|
|
meta: PropTypes.object,
|
|
|
|
normalizeOnBlur: PropTypes.func,
|
|
|
|
onScroll: PropTypes.func,
|
|
|
|
placeholder: PropTypes.string.isRequired,
|
|
|
|
subtitle: PropTypes.string.isRequired,
|
|
|
|
type: PropTypes.string.isRequired,
|
2020-09-09 18:51:02 +00:00
|
|
|
};
|
2020-09-08 11:06:19 +00:00
|
|
|
|
2020-09-22 12:04:17 +00:00
|
|
|
const INPUT_FIELDS = [
|
2020-09-08 11:06:19 +00:00
|
|
|
{
|
2020-09-10 09:54:36 +00:00
|
|
|
name: UPSTREAM_DNS_NAME,
|
2020-09-08 11:06:19 +00:00
|
|
|
type: 'text',
|
2020-09-22 12:04:17 +00:00
|
|
|
component: renderTextareaWithHighlightField,
|
|
|
|
className: classnames('form-control form-control--textarea font-monospace text-input', {
|
|
|
|
'text-input--larger': isFirefox,
|
|
|
|
}),
|
|
|
|
containerClass: classnames('text-edit-container', {
|
|
|
|
'mb-4': !isFirefox,
|
|
|
|
'mb-6': isFirefox,
|
|
|
|
}),
|
2020-09-08 11:06:19 +00:00
|
|
|
placeholder: 'upstream_dns',
|
|
|
|
normalizeOnBlur: removeEmptyLines,
|
|
|
|
},
|
|
|
|
{
|
2020-09-22 12:04:17 +00:00
|
|
|
name: UPSTREAM_MODE_NAME,
|
2020-09-08 11:06:19 +00:00
|
|
|
type: 'radio',
|
|
|
|
value: DNS_REQUEST_OPTIONS.LOAD_BALANCING,
|
|
|
|
component: renderRadioField,
|
|
|
|
subtitle: 'load_balancing_desc',
|
|
|
|
placeholder: 'load_balancing',
|
|
|
|
},
|
|
|
|
{
|
2020-09-22 12:04:17 +00:00
|
|
|
name: UPSTREAM_MODE_NAME,
|
2020-09-08 11:06:19 +00:00
|
|
|
type: 'radio',
|
|
|
|
value: DNS_REQUEST_OPTIONS.PARALLEL,
|
|
|
|
component: renderRadioField,
|
|
|
|
subtitle: 'upstream_parallel',
|
|
|
|
placeholder: 'parallel_requests',
|
|
|
|
},
|
|
|
|
{
|
2020-09-22 12:04:17 +00:00
|
|
|
name: UPSTREAM_MODE_NAME,
|
2020-09-08 11:06:19 +00:00
|
|
|
type: 'radio',
|
|
|
|
value: DNS_REQUEST_OPTIONS.FASTEST_ADDR,
|
|
|
|
component: renderRadioField,
|
|
|
|
subtitle: 'fastest_addr_desc',
|
|
|
|
placeholder: 'fastest_addr',
|
|
|
|
},
|
|
|
|
];
|
2019-03-06 11:45:21 +00:00
|
|
|
|
2020-06-10 16:04:19 +00:00
|
|
|
const Form = ({
|
2020-09-08 11:06:19 +00:00
|
|
|
submitting, invalid, handleSubmit,
|
2020-06-10 16:04:19 +00:00
|
|
|
}) => {
|
|
|
|
const dispatch = useDispatch();
|
2020-08-13 15:12:27 +00:00
|
|
|
const { t } = useTranslation();
|
2020-06-10 16:04:19 +00:00
|
|
|
const upstream_dns = useSelector((store) => store.form[FORM_NAME.UPSTREAM].values.upstream_dns);
|
2020-09-08 11:06:19 +00:00
|
|
|
const processingTestUpstream = useSelector((state) => state.settings.processingTestUpstream);
|
|
|
|
const processingSetConfig = useSelector((state) => state.dnsConfig.processingSetConfig);
|
2020-06-10 16:04:19 +00:00
|
|
|
|
2020-09-22 12:04:17 +00:00
|
|
|
const handleUpstreamTest = () => dispatch(testUpstreamWithFormValues());
|
2019-03-06 11:45:21 +00:00
|
|
|
|
2020-06-10 16:04:19 +00:00
|
|
|
const testButtonClass = classnames('btn btn-primary btn-standard mr-2', {
|
|
|
|
'btn-loading': processingTestUpstream,
|
2019-03-06 11:45:21 +00:00
|
|
|
});
|
|
|
|
|
2020-09-22 12:04:17 +00:00
|
|
|
const components = {
|
|
|
|
a: <a href={UPSTREAM_CONFIGURATION_WIKI_LINK} target="_blank"
|
|
|
|
rel="noopener noreferrer" />,
|
|
|
|
};
|
2020-04-22 16:32:07 +00:00
|
|
|
|
2020-09-22 12:04:17 +00:00
|
|
|
return <form onSubmit={handleSubmit} className="form--upstream">
|
2020-05-25 16:41:04 +00:00
|
|
|
<div className="row">
|
2020-09-22 12:04:17 +00:00
|
|
|
<label className="col form__label" htmlFor={UPSTREAM_DNS_NAME}>
|
|
|
|
<Trans components={components}>upstream_dns_help</Trans>
|
|
|
|
{' '}
|
|
|
|
<Trans components={[
|
|
|
|
<a
|
|
|
|
href="https://kb.adguard.com/general/dns-providers"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
key="0"
|
|
|
|
>
|
|
|
|
DNS providers
|
|
|
|
</a>,
|
|
|
|
]}>
|
|
|
|
dns_providers
|
|
|
|
</Trans>
|
|
|
|
</label>
|
|
|
|
{INPUT_FIELDS.map(renderField)}
|
2020-05-25 16:41:04 +00:00
|
|
|
<div className="col-12">
|
|
|
|
<Examples />
|
|
|
|
<hr />
|
2019-03-06 11:45:21 +00:00
|
|
|
</div>
|
2020-05-25 16:41:04 +00:00
|
|
|
<div className="col-12 mb-4">
|
|
|
|
<label
|
|
|
|
className="form__label form__label--with-desc"
|
|
|
|
htmlFor="bootstrap_dns"
|
|
|
|
>
|
|
|
|
<Trans>bootstrap_dns</Trans>
|
|
|
|
</label>
|
|
|
|
<div className="form__desc form__desc--top">
|
|
|
|
<Trans>bootstrap_dns_desc</Trans>
|
2019-03-06 11:45:21 +00:00
|
|
|
</div>
|
2020-05-25 16:41:04 +00:00
|
|
|
<Field
|
|
|
|
id="bootstrap_dns"
|
|
|
|
name="bootstrap_dns"
|
2020-07-15 09:35:37 +00:00
|
|
|
component={renderTextareaField}
|
2020-05-25 16:41:04 +00:00
|
|
|
type="text"
|
|
|
|
className="form-control form-control--textarea form-control--textarea-small font-monospace"
|
|
|
|
placeholder={t('bootstrap_dns')}
|
|
|
|
disabled={processingSetConfig}
|
2020-07-15 09:35:37 +00:00
|
|
|
normalizeOnBlur={removeEmptyLines}
|
2020-05-25 16:41:04 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="card-actions">
|
|
|
|
<div className="btn-list">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className={testButtonClass}
|
2020-06-10 16:04:19 +00:00
|
|
|
onClick={handleUpstreamTest}
|
2020-05-25 16:41:04 +00:00
|
|
|
disabled={!upstream_dns || processingTestUpstream}
|
|
|
|
>
|
|
|
|
<Trans>test_upstream_btn</Trans>
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-success btn-standard"
|
|
|
|
disabled={
|
|
|
|
submitting || invalid || processingSetConfig || processingTestUpstream
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Trans>apply_btn</Trans>
|
|
|
|
</button>
|
2019-03-06 11:45:21 +00:00
|
|
|
</div>
|
2020-05-25 16:41:04 +00:00
|
|
|
</div>
|
|
|
|
</form>;
|
2019-03-06 11:45:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Form.propTypes = {
|
|
|
|
handleSubmit: PropTypes.func,
|
|
|
|
submitting: PropTypes.bool,
|
|
|
|
invalid: PropTypes.bool,
|
|
|
|
initialValues: PropTypes.object,
|
2020-04-22 16:32:07 +00:00
|
|
|
upstream_dns: PropTypes.string,
|
|
|
|
bootstrap_dns: PropTypes.string,
|
2019-03-06 11:45:21 +00:00
|
|
|
};
|
|
|
|
|
2020-06-10 16:04:19 +00:00
|
|
|
export default reduxForm({ form: FORM_NAME.UPSTREAM })(Form);
|