Skip to content

Commit

Permalink
fix: 修复 wmic 获取的 mac 地址未格式化导致的描述过滤失效问题(close #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jul 12, 2023
1 parent b0e4722 commit 3b618ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/__test__/getNetworkInteraces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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();
});
});

Expand Down
17 changes: 11 additions & 6 deletions src/getIFacesByExec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,18 +44,23 @@ export async function getNetworkIFacesInfoByWmic() {
stdout = info.stdout;
if (lines[0]) {
let item: Record<string, string> = {};
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);
Expand Down

0 comments on commit 3b618ff

Please sign in to comment.