-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
323a9e6
commit d518fa9
Showing
8 changed files
with
98 additions
and
2 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * as ip from './ip'; | ||
export * as netstat from './netstat'; |
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,51 @@ | ||
import { execShell } from '@/utils/exec-shell'; | ||
import { removeDuplicate } from '@/utils/array'; | ||
|
||
// ------------------------ | ||
|
||
export interface ActiveConnection { | ||
protocol: string; | ||
program?: string; | ||
recvQ: number; | ||
sendQ: number; | ||
address: string; | ||
port: number; | ||
state?: string; | ||
} | ||
|
||
export async function activeConnections(): Promise<ActiveConnection[]> { | ||
const { error, data } = await execShell(['netstat', '-tuapn']); | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
const lines = data.output.split('\n'); | ||
const ports: ActiveConnection[] = []; | ||
for (const line of lines) { | ||
const match = | ||
/^(\w+)\s+(\d+)\s+(\d+)\s+((?:::)?(?:(?:(?:\d{1,3}\.){3}(?:\d{1,3}){1})?[0-9a-f]{0,4}:{0,2}){1,8}(?:::)?)\s+((?:::)?(?:(?:(?:\d{1,3}\.){3}(?:\d{1,3}){1})?[0-9a-f]{0,4}:{0,2}){1,8}(?:::)?\*?)\s+(\w+)?\s+(.*)$/.exec( | ||
line | ||
); | ||
if (match) { | ||
const port = match[4].split(':').at(-1); | ||
const address = match[4].replace(`:${port}`, ''); | ||
const connection: ActiveConnection = { | ||
protocol: match[1], | ||
recvQ: Number(match[2]), | ||
sendQ: Number(match[3]), | ||
address: address, | ||
port: Number(port), | ||
state: match[6] ? match[6].trim() : undefined, | ||
program: match[7].trim(), | ||
}; | ||
ports.push(connection); | ||
} | ||
} | ||
|
||
return ports; | ||
} | ||
|
||
export async function allocatedPorts(): Promise<number[]> { | ||
const cns = await activeConnections(); | ||
return removeDuplicate(cns.map((cn) => cn.port)); | ||
} |
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,9 @@ | ||
export function removeDuplicate<T>(d: T[]): T[] { | ||
const final: T[] = []; | ||
for (const item of d) { | ||
if (!final.includes(item)) { | ||
final.push(item); | ||
} | ||
} | ||
return final; | ||
} |
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,21 @@ | ||
import { expect } from 'chai'; | ||
import { IPV4_REGEX } from '@/ip'; | ||
import { activeConnections, allocatedPorts } from 'node-netkit/netstat'; | ||
|
||
describe('Active Connections', () => { | ||
it('should get active connections', async () => { | ||
const connections = await activeConnections(); | ||
expect(connections).to.be.an('array'); | ||
expect(connections.length).to.have.greaterThan(0); | ||
|
||
expect(Object.keys(connections[0])).to.have.lengthOf(7); | ||
expect(connections[0].address).to.match(IPV4_REGEX); | ||
}); | ||
|
||
it('should get allocated ports', async () => { | ||
for (const p of await allocatedPorts()) { | ||
expect(p).to.be.greaterThan(0); | ||
expect(p).to.be.lessThan(65535); | ||
} | ||
}); | ||
}); |
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