-
Notifications
You must be signed in to change notification settings - Fork 43
/
find_pid.js
143 lines (125 loc) · 3.58 KB
/
find_pid.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* @Author: zoujie.wzj
* @Date: 2016-01-22 19:27:17
* @Last Modified by: zoujie.wzj
* @Last Modified time: 2016-10-13 15:17:33
*/
'use strict'
// find pid by port
const utils = require('./utils')
const finders = {
darwin (port) {
return new Promise((resolve, reject) => {
utils.exec('netstat -anv -p TCP && netstat -anv -p UDP', function (err, stdout, stderr) {
if (err) {
reject(err)
} else {
err = stderr.toString().trim()
if (err) {
reject(err)
return
}
// replace header
let data = utils.stripLine(stdout.toString(), 2)
let found = utils.extractColumns(data, [0, 3, 8], 10)
.filter(row => {
return !!String(row[0]).match(/^(udp|tcp)/)
})
.find(row => {
let matches = String(row[1]).match(/\.(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
})
if (found && found[2].length) {
resolve(parseInt(found[2], 10))
} else {
reject(`pid of port (${port}) not found`)
}
}
})
})
},
freebsd: 'darwin',
sunos: 'darwin',
linux (port) {
return new Promise((resolve, reject) => {
// netstat -p need sudo to run
let cmd = 'netstat -tunlp'
if (process.getuid() > 0) {
cmd = 'sudo ' + cmd
}
utils.exec(cmd, function (err, stdout, stderr) {
if (err) {
reject(err)
} else {
err = stderr.toString().trim()
if (err) {
reject(err)
return
}
// replace header
let data = utils.stripLine(stdout.toString(), 2)
let columns = utils.extractColumns(data, [3, 6], 7).find(column => {
let matches = String(column[0]).match(/:(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
})
if (columns && columns[1]) {
let pid = columns[1].split('/', 1)[0]
if (pid.length) {
resolve(parseInt(pid, 10))
} else {
reject(`pid of port (${port}) not found`)
}
} else {
reject(`pid of port (${port}) not found`)
}
}
})
})
},
win32 (port) {
return new Promise((resolve, reject) => {
utils.exec('netstat -ano', function (err, stdout, stderr) {
if (err) {
reject(err)
} else {
err = stderr.toString().trim()
if (err) {
reject(err)
return
}
// replace header
let data = utils.stripLine(stdout.toString(), 4)
let columns = utils.extractColumns(data, [1, 4], 5).find(column => {
let matches = String(column[0]).match(/:(\d+)$/)
if (matches && matches[1] === String(port)) {
return true
}
})
if (columns && columns[1].length) {
resolve(parseInt(columns[1], 10))
} else {
reject(`pid of port (${port}) not found`)
}
}
})
})
}
}
function findPidByPort (port) {
let platform = process.platform
return new Promise((resolve, reject) => {
if (!(platform in finders)) {
reject(new Error(`platform ${platform} is unsupported`))
}
let findPid = finders[platform]
if (typeof findPid === 'string') {
findPid = finders[findPid]
}
findPid(port).then(resolve, reject)
})
}
module.exports = findPidByPort