-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (34 loc) · 988 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict'
const netstat = require('node-netstat')
const sh = require('shell-exec')
module.exports = function (port, protocol = 'tcp') {
port = Number.parseInt(port)
return new Promise((resolve, reject) => {
if (!port) {
reject(new Error('Invalid port number.'))
}
if (process.platform === 'win32') {
let found = false
netstat({
filter: { protocol: protocol,
local: {
port: port
}
},
limit: 1,
done: (err) => {
if (!found) {
reject(new Error('Process not exist.'))
}
}
}, (data) => {
found = true
if (data && data.pid) { return sh(`taskkill /PID ${data.pid} /F`) } else { reject(new Error('Something went wrong.')) }
})
} else {
return sh(
`lsof -i ${protocol === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${protocol === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xargs kill -9`
)
}
})
}