fix(striker-ui): guess network base, mask in create manifest

main
Tsu-ba-me 1 year ago
parent 8b6b1a64a8
commit 24c05e740c
  1. 76
      striker-ui/components/ManageManifest/AnNetworkConfigInputGroup.tsx

@ -1,3 +1,4 @@
import { Netmask } from 'netmask';
import { ReactElement, useCallback, useMemo } from 'react'; import { ReactElement, useCallback, useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
@ -30,9 +31,46 @@ const DEFAULT_DNS_CSV = '8.8.8.8,8.8.4.4';
const NETWORK_TYPE_ENTRIES = Object.entries(NETWORK_TYPES); const NETWORK_TYPE_ENTRIES = Object.entries(NETWORK_TYPES);
const MAP_TO_NETWORK_DEFAULTS: Record<string, { base: string; mask: string }> =
{
bcn: { base: '10.201.0.0', mask: '255.255.0.0' },
mn: { base: '10.199.0.0', mask: '255.255.0.0' },
sn: { base: '10.101.0.0', mask: '255.255.0.0' },
};
const assertIfn = (type: string) => type === 'ifn'; const assertIfn = (type: string) => type === 'ifn';
const assertMn = (type: string) => type === 'mn'; const assertMn = (type: string) => type === 'mn';
const guessNetworkMinIp = ({
entries,
type,
}: {
entries: [string, ManifestNetwork][];
type: string;
}): { base?: string; mask?: string } => {
const last = entries
.filter(([, { networkType }]) => networkType === type)
.sort(([, { networkNumber: a }], [, { networkNumber: b }]) =>
a > b ? 1 : -1,
)
.pop();
if (!last) {
return MAP_TO_NETWORK_DEFAULTS[type] ?? {};
}
const [, { networkMinIp, networkSubnetMask }] = last;
try {
const block = new Netmask(`${networkMinIp}/${networkSubnetMask}`);
const { base, mask } = block.next();
return { base, mask };
} catch (error) {
return {};
}
};
const AnNetworkConfigInputGroup = < const AnNetworkConfigInputGroup = <
M extends MapToInputTestID & { M extends MapToInputTestID & {
[K in [K in
@ -105,17 +143,25 @@ const AnNetworkConfigInputGroup = <
}: Partial<ManifestNetwork> = {}): { }: Partial<ManifestNetwork> = {}): {
network: ManifestNetwork; network: ManifestNetwork;
networkId: string; networkId: string;
} => ({ } => {
const { base = networkMinIp, mask = networkSubnetMask } =
guessNetworkMinIp({
entries: networkListEntries,
type: networkType,
});
return {
network: { network: {
networkGateway, networkGateway,
networkMinIp, networkMinIp: base,
networkNumber, networkNumber,
networkSubnetMask, networkSubnetMask: mask,
networkType, networkType,
}, },
networkId: uuidv4(), networkId: uuidv4(),
}), };
[getNetworkNumber], },
[getNetworkNumber, networkListEntries],
); );
const setNetwork = useCallback( const setNetwork = useCallback(
@ -156,7 +202,13 @@ const AnNetworkConfigInputGroup = <
const newList = networkListEntries.reduce<ManifestNetworkList>( const newList = networkListEntries.reduce<ManifestNetworkList>(
(previous, [networkId, networkValue]) => { (previous, [networkId, networkValue]) => {
const { networkNumber: initnn, networkType: initnt } = networkValue; const {
networkNumber: initnn,
networkType: initnt,
networkMinIp: initbase,
networkSubnetMask: initmask,
...restNetworkValue
} = networkValue;
let networkNumber = initnn; let networkNumber = initnn;
let networkType = initnt; let networkType = initnt;
@ -180,8 +232,18 @@ const AnNetworkConfigInputGroup = <
networkNumber -= 1; networkNumber -= 1;
} }
const {
base: networkMinIp = initbase,
mask: networkSubnetMask = initmask,
} = guessNetworkMinIp({
entries: networkListEntries,
type: networkType,
});
previous[networkId] = { previous[networkId] = {
...networkValue, ...restNetworkValue,
networkMinIp,
networkSubnetMask,
networkNumber, networkNumber,
networkType, networkType,
}; };

Loading…
Cancel
Save