MCPcopy Create free account
hub / github.com/Marus/cortex-debug / getOsNetProbeCmd

Method getOsNetProbeCmd

src/tcpportscanner.ts:205–240  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

203 protected static OSNetProbeCmd = '';
204 protected static OSNetProbeCmdRegexpStr = '';
205 protected static getOsNetProbeCmd(): string {
206 /**
207 * Notes:
208 * `netstat` does not exist on Linux by default. Replacement is `ss`
209 * `netstat` and `ss` are faster than lsof. netstat on mac is super fast.
210 * what program to use is baed of platform and availability
211 */
212 if (TcpPortScanner.OSNetProbeCmd === '') {
213 const commandExistsSync = command_exists.sync;
214 const platform = os.platform();
215 const isWin = platform === 'win32';
216 const isMac = platform === 'darwin';
217 /**
218 * for `netstat` and `ss` We are looking for things that are in the 'local address' field for
219 * ports that are listening. Technically, you can have multiple matches because the local machine
220 * can have multiple addresses
221 */
222 if (!isWin && !isMac && commandExistsSync('ss')) {
223 TcpPortScanner.OSNetProbeCmd = 'ss -nlt';
224 TcpPortScanner.OSNetProbeCmdRegexpStr = 'LISTEN\\s+[^\\n]*:XYZZY\\s+[^\\s]+[^\\n]*\\n';
225 } else if (commandExistsSync('netstat')) {
226 // On windows, if you ask for tcp it will only give you ipv4. On Mac, it gives both, so we have to
227 // use the -f on Mac
228 TcpPortScanner.OSNetProbeCmd = isWin ? 'netstat -nap tcp' : 'netstat -nap tcp -f inet';
229 // netstat output varies wildly, so be careful
230 TcpPortScanner.OSNetProbeCmdRegexpStr = '[tT][cC][pP][^\\n]*[:\\.]XYZZY\\s+[^\\s]+\\s+LISTEN[^\\n]*\\n';
231 } else if (isMac && commandExistsSync('lsof')) {
232 // This is the slowest of all but probably the most consistent
233 TcpPortScanner.OSNetProbeCmd = 'lsof -n -iTCP:XYZZY -sTCP:LISTEN';
234 TcpPortScanner.OSNetProbeCmdRegexpStr = 'IPv4[^\\n]+:XYZZY\\s[^\\n]*\\(LISTEN\\)[^\\n]*\\n';
235 } else {
236 TcpPortScanner.OSNetProbeCmd = '?';
237 }
238 }
239 return TcpPortScanner.OSNetProbeCmd;
240 }
241
242 /**
243 * This is the most unobtrusive way of figuring out if a port is open. It does not try

Callers 1

waitForPortOpenOSUtlMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected