Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign unused subnet automatically #418

Merged
merged 6 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
check if ip has been used
  • Loading branch information
sinamics committed May 19, 2024
commit 677b280a1451f0e77871b840732a4b0092fde17c
4 changes: 3 additions & 1 deletion src/server/api/routers/networkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const networkRouter = createTRPCRouter({
if (input.central) {
return await ztController.get_controller_networks(ctx, input.central);
}

const networks = await ctx.prisma.network.findMany({
where: {
authorId: ctx.session.user.id,
Expand Down Expand Up @@ -138,7 +139,7 @@ export const networkRouter = createTRPCRouter({
);

// Generate CIDR options for IP configuration
const { cidrOptions } = IPv4gen(null);
const { cidrOptions } = IPv4gen(null, []);

/**
* Merging logic to ensure that members who only exist in local database ( added manually ) are also included in the response
Expand Down Expand Up @@ -540,6 +541,7 @@ export const networkRouter = createTRPCRouter({
// generate network params
const { ipAssignmentPools, routes, v4AssignMode } = IPv4gen(
input.updateParams.routes[0].target,
[],
);

// prepare update params
Expand Down
12 changes: 11 additions & 1 deletion src/server/api/services/networkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,18 @@ export const networkProvisioningFactory = async ({ ctx, input }) => {
);
}
}
// get used IPs from the database
const usedCidr = await ctx.prisma.network.findMany({
select: {
routes: true,
},
});
// Extract the target from the routes
const usedIPs = usedCidr.map((nw) => nw.routes?.map((r) => r.target));

// Flatten the array
// Generate ipv4 address, cidr, start & end
const ipAssignmentPools = IPv4gen(null);
const ipAssignmentPools = IPv4gen(null, usedIPs);

if (!input?.name) {
// Generate adjective and noun word
Expand All @@ -74,6 +83,7 @@ export const networkProvisioningFactory = async ({ ctx, input }) => {
create: {
name: newNw.name,
nwid: newNw.nwid,
routes: ipAssignmentPools.routes,
},
},
},
Expand Down
17 changes: 12 additions & 5 deletions src/utils/IPv4gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ const cidrOptions = [
"172.25.30.0/24",
];

const generateCidr = () => {
return cidrOptions[Math.floor(Math.random() * cidrOptions.length)];
// const generateCidr = () => {
// return cidrOptions[Math.floor(Math.random() * cidrOptions.length)];
// };
const generateCidr = (usedCidr: string[][]) => {
// Flatten the usedCidr array
const flattenedUsedCidr = usedCidr.flat();

// Filter the available CIDRs
const availableCidr = cidrOptions.filter((cidr) => !flattenedUsedCidr.includes(cidr));
return availableCidr.length > 0 ? availableCidr[0] : cidrOptions[0];
};

export const IPv4gen = (CIDR: string | null) => {
const cidr = CIDR ? CIDR : generateCidr();
export const IPv4gen = (CIDR: string | null, usedCidr) => {
const cidr = CIDR ? CIDR : generateCidr(usedCidr);

const [start, prefix] = cidr.split("/");
const host32 = ((1 << (32 - parseInt(prefix))) - 1) >>> 0;
Expand Down