1 |
|
2 | import { networkInterfaces } from 'https://deno.land/std/node/os.ts'
|
3 | const macRegex = /(?:[a-z0-9]{1,2}[:-]){5}[a-z0-9]{1,2}/i
|
4 | const zeroRegex = /(?:[0]{1,2}[:-]){5}[0]{1,2}/
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export default function getMAC(iface?: string): string {
|
11 | const list = networkInterfaces()
|
12 | if (iface) {
|
13 | const parts = list[iface]
|
14 | if (!parts) {
|
15 | throw new Error(`interface ${iface} was not found`)
|
16 | }
|
17 | for (const part of parts) {
|
18 | if (zeroRegex.test(part.mac) === false) {
|
19 | return part.mac
|
20 | }
|
21 | }
|
22 | throw new Error(`interface ${iface} had no valid mac addresses`)
|
23 | } else {
|
24 | for (const [key, parts] of Object.entries(list)) {
|
25 |
|
26 |
|
27 | if (!parts) continue
|
28 | for (const part of parts) {
|
29 | if (zeroRegex.test(part.mac) === false) {
|
30 | return part.mac
|
31 | }
|
32 | }
|
33 | }
|
34 | }
|
35 | throw new Error('failed to get the MAC address')
|
36 | }
|
37 |
|
38 |
|
39 | export function isMAC(macAddress: string) {
|
40 | return macRegex.test(macAddress)
|
41 | }
|