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

Method getOsNetProbeCmd

src/remote/src/tcpportscanner.ts:255–290  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

253 protected static OSNetProbeCmd = '';
254 protected static OSNetProbeCmdRegexpStr = '';
255 protected static getOsNetProbeCmd(): string {
256 /**
257 * Notes:
258 * `netstat` does not exist on Linux by default. Replacement is `ss`
259 * `netstat` and `ss` are faster than lsof. netstat on mac is super fast.
260 * what program to use is baed of platform and availability
261 */
262 if (TcpPortScanner.OSNetProbeCmd === '') {
263 const commandExistsSync = command_exists.sync;
264 const platform = os.platform();
265 const isWin = platform === 'win32';
266 const isMac = platform === 'darwin';
267 /**
268 * for `netstat` and `ss` We are looking for things that are in the 'local address' field for
269 * ports that are listening. Technically, you can have multiple matches because the local machine
270 * can have multiple addresses
271 */
272 if (!isWin && !isMac && commandExistsSync('ss')) {
273 TcpPortScanner.OSNetProbeCmd = 'ss -nlt';
274 TcpPortScanner.OSNetProbeCmdRegexpStr = 'LISTEN\\s+[^\\n]*:XYZZY\\s+[^\\s]+[^\\n]*\\n';
275 } else if (commandExistsSync('netstat')) {
276 // On windows, if you ask for tcp it will only give you ipv4. On Mac, it gives both, so we have to
277 // use the -f on Mac
278 TcpPortScanner.OSNetProbeCmd = isWin ? 'netstat -nap tcp' : 'netstat -nap tcp -f inet';
279 // netstat output varies wildly, so be careful
280 TcpPortScanner.OSNetProbeCmdRegexpStr = '[tT][cC][pP][^\\n]*[:\\.]XYZZY\\s+[^\\s]+\\s+LISTEN[^\\n]*\\n';
281 } else if (isMac && commandExistsSync('lsof')) {
282 // This is the slowest of all but probably the most consistent
283 TcpPortScanner.OSNetProbeCmd = 'lsof -n -iTCP:XYZZY -sTCP:LISTEN';
284 TcpPortScanner.OSNetProbeCmdRegexpStr = 'IPv4[^\\n]+:XYZZY\\s[^\\n]*\\(LISTEN\\)[^\\n]*\\n';
285 } else {
286 TcpPortScanner.OSNetProbeCmd = '?';
287 }
288 }
289 return TcpPortScanner.OSNetProbeCmd;
290 }
291
292 /**
293 * 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