From 3b618ff89c943f4eb461f05bb44102572a53c330 Mon Sep 17 00:00:00 2001 From: renxia Date: Wed, 12 Jul 2023 11:59:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20wmic=20=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9A=84=20mac=20=E5=9C=B0=E5=9D=80=E6=9C=AA=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E5=AF=BC=E8=87=B4=E7=9A=84=E6=8F=8F=E8=BF=B0?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=A4=B1=E6=95=88=E9=97=AE=E9=A2=98(close=20?= =?UTF-8?q?#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__test__/getNetworkInteraces.spec.ts | 7 +++++++ src/getIFacesByExec.ts | 17 +++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/__test__/getNetworkInteraces.spec.ts b/src/__test__/getNetworkInteraces.spec.ts index 3c8e0a7..baf1845 100644 --- a/src/__test__/getNetworkInteraces.spec.ts +++ b/src/__test__/getNetworkInteraces.spec.ts @@ -39,7 +39,11 @@ describe('getIFacesByExec', () => { platform = 'win32'; info = await getNetworkIFacesInfoByIpconfig(); + expect(Object.keys(info.config).length > 0).toBeTruthy(); + // 应当转换为小写 + expect('00:1d:7d:71:a8:d6' in info.config).toBeTruthy(); + expect(info.stdout.includes('00-1D-7D-71-A8-D6')).toBeTruthy(); }); it('getNetworkIFacesInfoByWmic', async () => { @@ -50,6 +54,9 @@ describe('getIFacesByExec', () => { platform = 'win32'; info = await getNetworkIFacesInfoByWmic(); expect(Object.keys(info.config).includes(ifacesMock.en0[1].mac)).toBeTruthy(); + // 应当转换为小写 + expect('1c:1b:b5:9b:ff:cc' in info.config).toBeTruthy(); + expect(info.stdout.includes('1C:1B:B5:9B:FF:CC')).toBeTruthy(); }); }); diff --git a/src/getIFacesByExec.ts b/src/getIFacesByExec.ts index bf0f75f..aea501c 100644 --- a/src/getIFacesByExec.ts +++ b/src/getIFacesByExec.ts @@ -1,7 +1,7 @@ import { exec, type ExecException, type ExecOptions } from 'child_process'; import { ObjectEncodingOptions } from 'fs'; import process from 'process'; -import { isVirtualMac, logDebug } from './utils'; +import { formatMac, isVirtualMac, logDebug } from './utils'; export interface IpconfigNIFItem { desc?: string; @@ -44,18 +44,23 @@ export async function getNetworkIFacesInfoByWmic() { stdout = info.stdout; if (lines[0]) { let item: Record = {}; + const setToConfig = () => { + if (item.mac) { + item.mac = formatMac(item.mac); + if (!config[item.mac] || !isVirtualMac('', item.desc)) config[item.mac] = item; + } + item = {}; + }; for (const line of lines) { let [key, value] = line.split('=').map(d => d.trim()); key = keyMap[key] || key.toLowerCase(); - if (item[key]) { - if (item.mac && (!config[item.mac] || !isVirtualMac('', item.desc))) config[item.mac] = item; - item = {}; - } + if (item[key]) setToConfig(); + item[key] = value; } - if (item.mac) config[item.mac] = item; + setToConfig(); } if (stdout) logDebug(`[getNetworkIFacesInfoByWmic]`, stdout, config);