Skip to content

Instantly share code, notes, and snippets.

@akansha-oi
Created August 2, 2025 05:35
Show Gist options
  • Select an option

  • Save akansha-oi/e130610fecb6c44b8d819a284cd15f5b to your computer and use it in GitHub Desktop.

Select an option

Save akansha-oi/e130610fecb6c44b8d819a284cd15f5b to your computer and use it in GitHub Desktop.
let macAutoData = [
{ macAddress: 'AA:BB:CC:DD:EE:01', vlanId: 10, port: '1' },
{ macAddress: 'AA:BB:CC:DD:EE:02', vlanId: 20, port: '2' },
{ macAddress: 'AA:BB:CC:DD:EE:03', vlanId: 30, port: '3' },
{ macAddress: 'AA:BB:CC:DD:EE:04', vlanId: 10, port: '1' },
{ macAddress: 'AA:BB:CC:DD:EE:05', vlanId: 20, port: '2' },
{ macAddress: 'AA:BB:CC:DD:EE:06', vlanId: 30, port: '3' },
// Add more as needed
];
// Store applied MAC filters per port
let macFilteredData = {}; // { '1': [ {macAddress, vlanId} ], ... }
this.app.get('/api/mac-filter', (req, res) => {
const { port } = req.query;
if (port && port !== '0') {
const entries = macFilteredData[port] || [];
return res.json(entries);
}
// Combine all port data into a flat array with port info
const allEntries = Object.entries(macFilteredData).flatMap(([port, entries]) =>
entries.map(entry => ({ ...entry, port }))
);
res.json(allEntries);
});
this.app.post('/api/mac-filter', (req, res) => {
const { port, macEntries } = req.body;
if (!port || !Array.isArray(macEntries)) {
return res.status(400).json({ error: 'Invalid request body' });
}
if (!macFilteredData[port]) macFilteredData[port] = [];
// Avoid duplicates
macEntries.forEach(entry => {
const exists = macFilteredData[port].some(
e => e.macAddress === entry.macAddress && e.vlanId === entry.vlanId
);
if (!exists) {
macFilteredData[port].push({ macAddress: entry.macAddress, vlanId: entry.vlanId });
}
});
res.json(macFilteredData[port]);
});
this.app.delete('/api/mac-filter', (req, res) => {
const { port, entries } = req.body;
if (!port || !Array.isArray(entries)) {
return res.status(400).json({ error: 'Invalid request body' });
}
if (!macFilteredData[port]) return res.json([]);
macFilteredData[port] = macFilteredData[port].filter(
e => !entries.some(entry =>
e.macAddress === entry.macAddress && e.vlanId === entry.vlanId
)
);
res.json(macFilteredData[port]);
});
this.app.post('/api/mac-filter-apply', (req, res) => {
const { bindings } = req.body;
if (!Array.isArray(bindings) || bindings.length === 0) {
return res.status(400).json({ error: 'No bindings provided.' });
}
bindings.forEach(({ port, macAddress, vlanId }) => {
if (!port || !macAddress || isNaN(vlanId)) return;
if (!macFilteredData[port]) macFilteredData[port] = [];
const exists = macFilteredData[port].some(
e => e.macAddress === macAddress && e.vlanId === vlanId
);
if (!exists) {
macFilteredData[port].push({ macAddress, vlanId });
}
});
console.log('Filtered MAC entries:', macFilteredData);
res.status(200).json({ message: 'MAC filter applied successfully.' });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment