dhcpd.js is a Node.js implementation of a DHCP server. It provides a simple and efficient way to handle DHCP events in your network.
You can install dhcpd.js using npm:
npm install dhcpd.js
const { Server } = require('dhcpd.js');
const s = new Server('192.168.1.1');
s.on("dhcp", (e) => {
console.log(e.packet.debug());
});
s.bind();
const { BOOTMessageType, Server } = require('dhcpd.js');
const s = new Server({
serverId: "192.168.1.1",
gateways: ["192.168.1.1"],
domainServer: ["192.168.1.1"]
});
s.on("listening", () => {
console.log("Server start", s.address);
});
const ips = {};
s.on("discover", (e) => {
const { packet } = e;
console.log(packet.log());
let ip = "0.0.0.0";
if (Object.keys(ips).includes(packet.chaddr)) {
ip = ips[packet.chaddr];
} else {
ip = `192.168.1.${Object.keys(ips).length + 2}`;
}
ips[packet.chaddr] = ip;
const offer = s.createOffer(packet);
offer.yiaddr = ip;
console.log(offer.log());
s.send(offer);
});
s.on("request", (e) => {
const { packet } = e;
console.log(packet.log());
const ack = s.createAck(packet);
ack.yiaddr = ips[packet.chaddr];
console.log(ack.log());
s.send(ack);
});
s.on("release", (e) => {
const { packet } = e;
console.log(packet.log());
delete ips[packet.chaddr];
});
s.bind();
For more information about DHCP, you can refer to the following RFCs: