如何在节点js中获取客户端的mac地址?

问题描述

是否可以从传入的请求对象中获取 mac 地址? 我的应用程序将在同一子网上本地使用,并在 Nginx 代理后面运行。

解决方法

您需要一个访问 ARP 表(包含本地网络中 IP 的 MAC 地址的表)的工具。像这样的东西arp-lookup package。您可以提取传入的请求ip。然后使用这个 IP 通过 arp-lookup 提取 MAC 地址。

代码应该是这样的:

const{ toMAC } = require('@network-utils/arp-lookup');

// ...

async function handleIncomingRequest( req,res ) {
    const ip = req.connection.remoteAddress;
    const mac = await toMAC(ip);

    // do stomething with the MAC address...
}