-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
232 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* @Author: renxia | ||
* @Date: 2023-12-08 10:11:08 | ||
* @LastEditors: renxia | ||
* @LastEditTime: 2023-12-08 11:40:29 | ||
* @Description: | ||
*/ | ||
import { getArpTable } from '../arpTable'; | ||
// import { getAllMac, getAllPhysicsMac, getMac } from '../getMac'; | ||
// import { isValidMac } from '../utils'; | ||
import { ifacesMock, arpANStdout } from './testData.mock'; | ||
|
||
let platform: keyof typeof arpANStdout = 'win32'; | ||
|
||
jest.mock('os', () => ({ | ||
networkInterfaces: () => ifacesMock, | ||
})); | ||
jest.mock('child_process', () => ({ | ||
execSync() { | ||
return arpANStdout[platform]; | ||
}, | ||
})); | ||
|
||
jest.mock('process', () => { | ||
return { | ||
get platform() { | ||
return platform; | ||
}, | ||
}; | ||
}); | ||
|
||
describe('getArpTable.ts', () => { | ||
beforeAll(() => { | ||
console.debug = () => void 0; | ||
console.error = () => void 0; | ||
process.env.DEBUG = '*'; | ||
}); | ||
|
||
it('getArpTable', async () => { | ||
platform = 'win32'; | ||
let info = await getArpTable(); | ||
expect(Array.isArray(info.table)).toBeTruthy(); | ||
|
||
platform = 'mac'; | ||
info = await getArpTable(); | ||
expect(Array.isArray(info.table)).toBeTruthy(); | ||
expect(info.table.every(d => d.type === 'unknown')).toBeTruthy(); | ||
|
||
platform = 'linux'; | ||
info = await getArpTable(); | ||
expect(Array.isArray(info.table)).toBeTruthy(); | ||
expect(info.table.some(d => d.mac === '<incomplete>')).toBeTruthy(); | ||
expect(info.table.every(d => d.type === 'unknown')).toBeTruthy(); | ||
|
||
info = await getArpTable(arpANStdout.mac); | ||
expect(info.table.some(d => d.mac === '<incomplete>')).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* @Author: renxia | ||
* @Date: 2023-12-08 09:31:39 | ||
* @LastEditors: renxia | ||
* @LastEditTime: 2023-12-08 11:50:52 | ||
* @Description: | ||
*/ | ||
import { execSync } from 'child_process'; | ||
import { isIP } from 'net'; | ||
import process from 'process'; | ||
import { formatMac } from './utils'; | ||
import { getNetworkIFaceOne } from './getNetworkInteraces'; | ||
|
||
export type ArpTableItem = { | ||
/** interface ip address */ | ||
iip: string; | ||
ip: string; | ||
mac: string; | ||
type: 'static' | 'dynamic' | 'unknown'; | ||
}; | ||
|
||
export async function getArpTable(stdout = '') { | ||
const isWindows = process.platform === 'win32'; | ||
if (!stdout) { | ||
if (isWindows) { | ||
const { default: iconv } = await import('iconv-lite'); | ||
stdout = iconv.decode(execSync('arp -a', { windowsHide: true }), 'gbk').trim(); | ||
} else { | ||
stdout = execSync('arp -an', { windowsHide: true }).toString('utf8').trim(); | ||
} | ||
} | ||
|
||
const table: ArpTableItem[] = []; | ||
let iip = ''; | ||
let iface = ''; | ||
|
||
for (const line of stdout.split('\n').map(d => d.trim())) { | ||
if (!line) continue; | ||
let ip = ''; | ||
let mac = ''; | ||
let type = 'unknown'; | ||
|
||
if (isWindows) { | ||
[ip, mac, type = 'unknown'] = line.split(/\s+/); | ||
|
||
if (isIP(mac)) { | ||
iip = mac; | ||
continue; | ||
} | ||
|
||
if (type.includes('动态')) type = 'dynamic'; | ||
else if (type.includes('静态')) type = 'static'; | ||
} else { | ||
const match = line.match(/\(([\d.]+)\) at (\S+) .*on ([\da-z]+)/); | ||
if (!match) continue; | ||
|
||
const preIface = iface; | ||
[, ip, mac, iface] = match; | ||
|
||
if (preIface !== iface) { | ||
const item = await getNetworkIFaceOne(iface); | ||
if (item) iip = item.address; | ||
} | ||
} | ||
|
||
if (isIP(ip)) { | ||
table.push({ iip, ip, mac: formatMac(mac), type: (type || 'unknown') as never }); | ||
} | ||
} | ||
|
||
return { stdout, table }; | ||
} | ||
|
||
export async function getArpMacByIp(ip: string) { | ||
if (!ip) return ''; | ||
const { table } = await getArpTable(); | ||
const item = table.find(d => d.ip === ip); | ||
return item ? item.mac : ''; | ||
} | ||
|
||
export async function getArpIpByMac(mac: string) { | ||
mac = formatMac(mac); | ||
if (!mac) return ''; | ||
const { table } = await getArpTable(); | ||
const item = table.find(d => d.mac.includes(mac)); | ||
return item ? item.ip : ''; | ||
} | ||
|
||
// getArpTable().then(d => console.log(d.table)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './utils'; | ||
export * from './getNetworkInteraces'; | ||
export * from './getMac'; | ||
export * from './arpTable'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters