+ client: Refactor DHCP settings

This commit is contained in:
Artem Baskal
2020-08-19 18:23:05 +03:00
committed by Simon Zolin
parent c9f58ce4a7
commit 1d35d73fc5
49 changed files with 2953 additions and 1660 deletions

View File

@@ -0,0 +1,175 @@
import { handleActions } from 'redux-actions';
import * as actions from '../actions';
import { areEqualVersions } from '../helpers/version';
import { STANDARD_DNS_PORT, STANDARD_WEB_PORT } from '../helpers/constants';
const dashboard = handleActions(
{
[actions.setDnsRunningStatus]: (state, { payload }) => (
{
...state,
isCoreRunning: payload,
}
),
[actions.dnsStatusRequest]: (state) => ({
...state,
processing: true,
}),
[actions.dnsStatusFailure]: (state) => ({
...state,
processing: false,
}),
[actions.dnsStatusSuccess]: (state, { payload }) => {
const {
version,
dns_port: dnsPort,
dns_addresses: dnsAddresses,
protection_enabled: protectionEnabled,
http_port: httpPort,
language,
} = payload;
const newState = {
...state,
isCoreRunning: true,
processing: false,
dnsVersion: version,
dnsPort,
dnsAddresses,
protectionEnabled,
language,
httpPort,
};
return newState;
},
[actions.getVersionRequest]: (state) => ({
...state,
processingVersion: true,
}),
[actions.getVersionFailure]: (state) => ({
...state,
processingVersion: false,
}),
[actions.getVersionSuccess]: (state, { payload }) => {
const currentVersion = state.dnsVersion === 'undefined' ? 0 : state.dnsVersion;
if (!payload.disabled && !areEqualVersions(currentVersion, payload.new_version)) {
const {
announcement_url: announcementUrl,
new_version: newVersion,
can_autoupdate: canAutoUpdate,
} = payload;
const newState = {
...state,
announcementUrl,
newVersion,
canAutoUpdate,
isUpdateAvailable: true,
processingVersion: false,
checkUpdateFlag: !payload.disabled,
};
return newState;
}
return {
...state,
processingVersion: false,
checkUpdateFlag: !payload.disabled,
};
},
[actions.getUpdateRequest]: (state) => ({
...state,
processingUpdate: true,
}),
[actions.getUpdateFailure]: (state) => ({
...state,
processingUpdate: false,
}),
[actions.getUpdateSuccess]: (state) => {
const newState = {
...state,
processingUpdate: false,
};
return newState;
},
[actions.toggleProtectionRequest]: (state) => ({
...state,
processingProtection: true,
}),
[actions.toggleProtectionFailure]: (state) => ({
...state,
processingProtection: false,
}),
[actions.toggleProtectionSuccess]: (state) => {
const newState = {
...state,
protectionEnabled: !state.protectionEnabled,
processingProtection: false,
};
return newState;
},
[actions.getLanguageSuccess]: (state, { payload }) => {
const newState = {
...state,
language: payload,
};
return newState;
},
[actions.getClientsRequest]: (state) => ({
...state,
processingClients: true,
}),
[actions.getClientsFailure]: (state) => ({
...state,
processingClients: false,
}),
[actions.getClientsSuccess]: (state, { payload }) => {
const newState = {
...state,
...payload,
processingClients: false,
};
return newState;
},
[actions.getProfileRequest]: (state) => ({
...state,
processingProfile: true,
}),
[actions.getProfileFailure]: (state) => ({
...state,
processingProfile: false,
}),
[actions.getProfileSuccess]: (state, { payload }) => ({
...state,
name: payload.name,
processingProfile: false,
}),
},
{
processing: true,
isCoreRunning: true,
processingVersion: true,
processingClients: true,
processingUpdate: false,
processingProfile: true,
protectionEnabled: false,
processingProtection: false,
httpPort: STANDARD_WEB_PORT,
dnsPort: STANDARD_DNS_PORT,
dnsAddresses: [],
dnsVersion: '',
clients: [],
autoClients: [],
supportedTags: [],
name: '',
checkUpdateFlag: false,
},
);
export default dashboard;

203
client/src/reducers/dhcp.js Normal file
View File

@@ -0,0 +1,203 @@
import { handleActions } from 'redux-actions';
import * as actions from '../actions';
import { enrichWithConcatenatedIpAddresses } from '../helpers/helpers';
// todo: figure out if we cat get rid of redux-form state duplication
const dhcp = handleActions(
{
[actions.getDhcpStatusRequest]: (state) => ({
...state,
processing: true,
}),
[actions.getDhcpStatusFailure]: (state) => ({
...state,
processing: false,
}),
[actions.getDhcpStatusSuccess]: (state, { payload }) => {
const { static_leases: staticLeases, ...values } = payload;
const newState = {
...state,
staticLeases,
processing: false,
...values,
};
return newState;
},
[actions.getDhcpInterfacesRequest]: (state) => ({
...state,
processingInterfaces: true,
}),
[actions.getDhcpInterfacesFailure]: (state) => ({
...state,
processingInterfaces: false,
}),
[actions.getDhcpInterfacesSuccess]: (state, { payload }) => {
const newState = {
...state,
interfaces: enrichWithConcatenatedIpAddresses(payload),
processingInterfaces: false,
};
return newState;
},
[actions.findActiveDhcpRequest]: (state) => ({
...state,
processingStatus: true,
}),
[actions.findActiveDhcpFailure]: (state) => ({
...state,
processingStatus: false,
}),
[actions.findActiveDhcpSuccess]: (state, { payload }) => {
const newState = {
...state,
check: payload,
processingStatus: false,
};
return newState;
},
[actions.toggleDhcpRequest]: (state) => ({
...state,
processingDhcp: true,
}),
[actions.toggleDhcpFailure]: (state) => ({
...state,
processingDhcp: false,
}),
[actions.toggleDhcpSuccess]: (state) => {
const { enabled } = state;
const newState = {
...state,
enabled: !enabled,
check: null,
processingDhcp: false,
};
return newState;
},
[actions.setDhcpConfigRequest]: (state) => ({
...state,
processingConfig: true,
}),
[actions.setDhcpConfigFailure]: (state) => ({
...state,
processingConfig: false,
}),
[actions.setDhcpConfigSuccess]: (state, { payload }) => {
const { v4, v6 } = state;
const newConfigV4 = { ...v4, ...payload.v4 };
const newConfigV6 = { ...v6, ...payload.v6 };
const newState = {
...state,
v4: newConfigV4,
v6: newConfigV6,
interface_name: payload.interface_name,
processingConfig: false,
};
return newState;
},
[actions.resetDhcpRequest]: (state) => ({
...state,
processingReset: true,
}),
[actions.resetDhcpFailure]: (state) => ({
...state,
processingReset: false,
}),
[actions.resetDhcpSuccess]: (state) => ({
...state,
processingReset: false,
enabled: false,
v4: {},
v6: {},
interface_name: '',
}),
[actions.toggleLeaseModal]: (state) => {
const newState = {
...state,
isModalOpen: !state.isModalOpen,
};
return newState;
},
[actions.addStaticLeaseRequest]: (state) => ({
...state,
processingAdding: true,
}),
[actions.addStaticLeaseFailure]: (state) => ({
...state,
processingAdding: false,
}),
[actions.addStaticLeaseSuccess]: (state, { payload }) => {
const { ip, mac, hostname } = payload;
const newLease = {
ip,
mac,
hostname: hostname || '',
};
const leases = [...state.staticLeases, newLease];
const newState = {
...state,
staticLeases: leases,
processingAdding: false,
};
return newState;
},
[actions.removeStaticLeaseRequest]: (state) => ({
...state,
processingDeleting: true,
}),
[actions.removeStaticLeaseFailure]: (state) => ({
...state,
processingDeleting: false,
}),
[actions.removeStaticLeaseSuccess]: (state, { payload }) => {
const leaseToRemove = payload.ip;
const leases = state.staticLeases.filter((item) => item.ip !== leaseToRemove);
const newState = {
...state,
staticLeases: leases,
processingDeleting: false,
};
return newState;
},
},
{
processing: true,
processingStatus: false,
processingInterfaces: false,
processingDhcp: false,
processingConfig: false,
processingAdding: false,
processingDeleting: false,
enabled: false,
interface_name: '',
check: null,
v4: {
gateway_ip: '',
subnet_mask: '',
range_start: '',
range_end: '',
lease_duration: 0,
},
v6: {
range_start: '',
lease_duration: 0,
},
leases: [],
staticLeases: [],
isModalOpen: false,
dhcp_available: false,
},
);
export default dhcp;

View File

@@ -1,9 +1,9 @@
import { handleActions } from 'redux-actions';
import * as actions from '../actions/dnsConfig';
import { BLOCKING_MODES } from '../helpers/constants';
import { ALL_INTERFACES_IP, BLOCKING_MODES } from '../helpers/constants';
const DEFAULT_BLOCKING_IPV4 = '0.0.0.0';
const DEFAULT_BLOCKING_IPV4 = ALL_INTERFACES_IP;
const DEFAULT_BLOCKING_IPV6 = '::';
const dnsConfig = handleActions(

View File

@@ -1,9 +1,6 @@
import { combineReducers } from 'redux';
import { handleActions } from 'redux-actions';
import { loadingBarReducer } from 'react-redux-loading-bar';
import { reducer as formReducer } from 'redux-form';
import * as actions from '../actions';
import toasts from './toasts';
import encryption from './encryption';
import clients from './clients';
@@ -14,296 +11,9 @@ import stats from './stats';
import queryLogs from './queryLogs';
import dnsConfig from './dnsConfig';
import filtering from './filtering';
import { areEqualVersions } from '../helpers/version';
const settings = handleActions(
{
[actions.initSettingsRequest]: (state) => ({ ...state, processing: true }),
[actions.initSettingsFailure]: (state) => ({ ...state, processing: false }),
[actions.initSettingsSuccess]: (state, { payload }) => {
const { settingsList } = payload;
const newState = { ...state, settingsList, processing: false };
return newState;
},
[actions.toggleSettingStatus]: (state, { payload }) => {
const { settingsList } = state;
const { settingKey } = payload;
const setting = settingsList[settingKey];
const newSetting = { ...setting, enabled: !setting.enabled };
const newSettingsList = { ...settingsList, [settingKey]: newSetting };
return { ...state, settingsList: newSettingsList };
},
[actions.testUpstreamRequest]: (state) => ({ ...state, processingTestUpstream: true }),
[actions.testUpstreamFailure]: (state) => ({ ...state, processingTestUpstream: false }),
[actions.testUpstreamSuccess]: (state) => ({ ...state, processingTestUpstream: false }),
},
{
processing: true,
processingTestUpstream: false,
processingDhcpStatus: false,
settingsList: {},
},
);
const dashboard = handleActions(
{
[actions.setDnsRunningStatus]: (state, { payload }) => (
{ ...state, isCoreRunning: payload }
),
[actions.dnsStatusRequest]: (state) => ({ ...state, processing: true }),
[actions.dnsStatusFailure]: (state) => ({ ...state, processing: false }),
[actions.dnsStatusSuccess]: (state, { payload }) => {
const {
version,
dns_port: dnsPort,
dns_addresses: dnsAddresses,
protection_enabled: protectionEnabled,
http_port: httpPort,
language,
} = payload;
const newState = {
...state,
isCoreRunning: true,
processing: false,
dnsVersion: version,
dnsPort,
dnsAddresses,
protectionEnabled,
language,
httpPort,
};
return newState;
},
[actions.getVersionRequest]: (state) => ({ ...state, processingVersion: true }),
[actions.getVersionFailure]: (state) => ({ ...state, processingVersion: false }),
[actions.getVersionSuccess]: (state, { payload }) => {
const currentVersion = state.dnsVersion === 'undefined' ? 0 : state.dnsVersion;
if (!payload.disabled && !areEqualVersions(currentVersion, payload.new_version)) {
const {
announcement_url: announcementUrl,
new_version: newVersion,
can_autoupdate: canAutoUpdate,
} = payload;
const newState = {
...state,
announcementUrl,
newVersion,
canAutoUpdate,
isUpdateAvailable: true,
processingVersion: false,
checkUpdateFlag: !payload.disabled,
};
return newState;
}
return {
...state,
processingVersion: false,
checkUpdateFlag: !payload.disabled,
};
},
[actions.getUpdateRequest]: (state) => ({ ...state, processingUpdate: true }),
[actions.getUpdateFailure]: (state) => ({ ...state, processingUpdate: false }),
[actions.getUpdateSuccess]: (state) => {
const newState = { ...state, processingUpdate: false };
return newState;
},
[actions.toggleProtectionRequest]: (state) => ({ ...state, processingProtection: true }),
[actions.toggleProtectionFailure]: (state) => ({ ...state, processingProtection: false }),
[actions.toggleProtectionSuccess]: (state) => {
const newState = {
...state,
protectionEnabled: !state.protectionEnabled,
processingProtection: false,
};
return newState;
},
[actions.getLanguageSuccess]: (state, { payload }) => {
const newState = { ...state, language: payload };
return newState;
},
[actions.getClientsRequest]: (state) => ({ ...state, processingClients: true }),
[actions.getClientsFailure]: (state) => ({ ...state, processingClients: false }),
[actions.getClientsSuccess]: (state, { payload }) => {
const newState = {
...state,
...payload,
processingClients: false,
};
return newState;
},
[actions.getProfileRequest]: (state) => ({ ...state, processingProfile: true }),
[actions.getProfileFailure]: (state) => ({ ...state, processingProfile: false }),
[actions.getProfileSuccess]: (state, { payload }) => ({
...state,
name: payload.name,
processingProfile: false,
}),
},
{
processing: true,
isCoreRunning: true,
processingVersion: true,
processingClients: true,
processingUpdate: false,
processingProfile: true,
protectionEnabled: false,
processingProtection: false,
httpPort: 80,
dnsPort: 53,
dnsAddresses: [],
dnsVersion: '',
clients: [],
autoClients: [],
supportedTags: [],
name: '',
checkUpdateFlag: false,
},
);
const dhcp = handleActions(
{
[actions.getDhcpStatusRequest]: (state) => ({ ...state, processing: true }),
[actions.getDhcpStatusFailure]: (state) => ({ ...state, processing: false }),
[actions.getDhcpStatusSuccess]: (state, { payload }) => {
const { static_leases: staticLeases, ...values } = payload;
const newState = {
...state,
staticLeases,
processing: false,
...values,
};
return newState;
},
[actions.getDhcpInterfacesRequest]: (state) => ({ ...state, processingInterfaces: true }),
[actions.getDhcpInterfacesFailure]: (state) => ({ ...state, processingInterfaces: false }),
[actions.getDhcpInterfacesSuccess]: (state, { payload }) => {
const newState = {
...state,
interfaces: payload,
processingInterfaces: false,
};
return newState;
},
[actions.findActiveDhcpRequest]: (state) => ({ ...state, processingStatus: true }),
[actions.findActiveDhcpFailure]: (state) => ({ ...state, processingStatus: false }),
[actions.findActiveDhcpSuccess]: (state, { payload }) => {
const { other_server: otherServer, static_ip: staticIP } = payload;
const newState = {
...state,
check: {
otherServer,
staticIP,
},
processingStatus: false,
};
return newState;
},
[actions.toggleDhcpRequest]: (state) => ({ ...state, processingDhcp: true }),
[actions.toggleDhcpFailure]: (state) => ({ ...state, processingDhcp: false }),
[actions.toggleDhcpSuccess]: (state) => {
const { config } = state;
const newConfig = { ...config, enabled: !config.enabled };
const newState = {
...state,
config: newConfig,
check: null,
processingDhcp: false,
};
return newState;
},
[actions.setDhcpConfigRequest]: (state) => ({ ...state, processingConfig: true }),
[actions.setDhcpConfigFailure]: (state) => ({ ...state, processingConfig: false }),
[actions.setDhcpConfigSuccess]: (state, { payload }) => {
const { config } = state;
const newConfig = { ...config, ...payload };
const newState = { ...state, config: newConfig, processingConfig: false };
return newState;
},
[actions.resetDhcpRequest]: (state) => ({ ...state, processingReset: true }),
[actions.resetDhcpFailure]: (state) => ({ ...state, processingReset: false }),
[actions.resetDhcpSuccess]: (state) => ({
...state,
processingReset: false,
config: {
enabled: false,
},
}),
[actions.toggleLeaseModal]: (state) => {
const newState = {
...state,
isModalOpen: !state.isModalOpen,
};
return newState;
},
[actions.addStaticLeaseRequest]: (state) => ({ ...state, processingAdding: true }),
[actions.addStaticLeaseFailure]: (state) => ({ ...state, processingAdding: false }),
[actions.addStaticLeaseSuccess]: (state, { payload }) => {
const { ip, mac, hostname } = payload;
const newLease = {
ip,
mac,
hostname: hostname || '',
};
const leases = [...state.staticLeases, newLease];
const newState = {
...state,
staticLeases: leases,
processingAdding: false,
};
return newState;
},
[actions.removeStaticLeaseRequest]: (state) => ({ ...state, processingDeleting: true }),
[actions.removeStaticLeaseFailure]: (state) => ({ ...state, processingDeleting: false }),
[actions.removeStaticLeaseSuccess]: (state, { payload }) => {
const leaseToRemove = payload.ip;
const leases = state.staticLeases.filter((item) => item.ip !== leaseToRemove);
const newState = {
...state,
staticLeases: leases,
processingDeleting: false,
};
return newState;
},
},
{
processing: true,
processingStatus: false,
processingInterfaces: false,
processingDhcp: false,
processingConfig: false,
processingAdding: false,
processingDeleting: false,
config: {
enabled: false,
},
check: null,
leases: [],
staticLeases: [],
isModalOpen: false,
},
);
import settings from './settings';
import dashboard from './dashboard';
import dhcp from './dhcp';
export default combineReducers({
settings,

View File

@@ -4,7 +4,9 @@ import { reducer as formReducer } from 'redux-form';
import * as actions from '../actions/install';
import toasts from './toasts';
import { INSTALL_FIRST_STEP } from '../helpers/constants';
import {
ALL_INTERFACES_IP, INSTALL_FIRST_STEP, STANDARD_DNS_PORT, STANDARD_WEB_PORT,
} from '../helpers/constants';
const install = handleActions({
[actions.getDefaultAddressesRequest]: (state) => ({ ...state, processingDefault: true }),
@@ -45,14 +47,14 @@ const install = handleActions({
processingSubmit: false,
processingCheck: false,
web: {
ip: '0.0.0.0',
port: 80,
ip: ALL_INTERFACES_IP,
port: STANDARD_WEB_PORT,
status: '',
can_autofix: false,
},
dns: {
ip: '0.0.0.0',
port: 53,
ip: ALL_INTERFACES_IP,
port: STANDARD_DNS_PORT,
status: '',
can_autofix: false,
},

View File

@@ -0,0 +1,63 @@
import { handleActions } from 'redux-actions';
import * as actions from '../actions';
const settings = handleActions(
{
[actions.initSettingsRequest]: (state) => ({
...state,
processing: true,
}),
[actions.initSettingsFailure]: (state) => ({
...state,
processing: false,
}),
[actions.initSettingsSuccess]: (state, { payload }) => {
const { settingsList } = payload;
const newState = {
...state,
settingsList,
processing: false,
};
return newState;
},
[actions.toggleSettingStatus]: (state, { payload }) => {
const { settingsList } = state;
const { settingKey } = payload;
const setting = settingsList[settingKey];
const newSetting = {
...setting,
enabled: !setting.enabled,
};
const newSettingsList = {
...settingsList,
[settingKey]: newSetting,
};
return {
...state,
settingsList: newSettingsList,
};
},
[actions.testUpstreamRequest]: (state) => ({
...state,
processingTestUpstream: true,
}),
[actions.testUpstreamFailure]: (state) => ({
...state,
processingTestUpstream: false,
}),
[actions.testUpstreamSuccess]: (state) => ({
...state,
processingTestUpstream: false,
}),
},
{
processing: true,
processingTestUpstream: false,
processingDhcpStatus: false,
settingsList: {},
},
);
export default settings;

View File

@@ -8,9 +8,12 @@ import { removeToast } from '../actions';
const toasts = handleActions({
[addErrorToast]: (state, { payload }) => {
const message = payload.error.toString();
console.error(message);
const errorToast = {
id: nanoid(),
message: payload.error.toString(),
message,
type: 'error',
};