diff --git a/README.md b/README.md index ac175a4..102d226 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ // 和 IDA 端一致 "port": 5677, // control port // 和 IDA 端一致 + "timeout": 3000, // 连接失败超时 "pythonPath": "${command:python.interpreterPath}", // python path (IDA used) // VSCode 和 IDA 使用同版本的话就不用管这字段了 "logFile": "", // debug log file diff --git a/idavscode/README.md b/idavscode/README.md index daf2018..ea7a34d 100644 --- a/idavscode/README.md +++ b/idavscode/README.md @@ -19,6 +19,7 @@ Install [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.p "program": "${file}", "host": "localhost", // control hostname "port": 5677, // control port + "timeout": 3000, // timeout for connect "pythonPath": "${command:python.interpreterPath}", // python path (IDA used) "logFile": "", // debug log file "debugConfig": { // Python extension debug config diff --git a/idavscode/src/extension.ts b/idavscode/src/extension.ts index 54ef8d0..175614b 100644 --- a/idavscode/src/extension.ts +++ b/idavscode/src/extension.ts @@ -40,6 +40,7 @@ class IDAPythonConfigurationProvider implements vscode.DebugConfigurationProvide "program": "${file}", "host": "localhost", "port": 5677, + "timeout": 3000, "pythonPath": "${command:python.interpreterPath}", "logFile": "", "debugConfig": { @@ -69,6 +70,7 @@ class IDAPythonConfigurationProvider implements vscode.DebugConfigurationProvide if (!config.program) { config.program = '${file}'; } if (!config.pythonPath) { config.pythonPath = '${command:python.interpreterPath}'; } + if (config.timeout === undefined) { config.timeout = 3000; } config.debugConfig.type = 'python'; config.debugConfig.request = 'attach'; config.debugConfig.redirectOutput = true; @@ -86,20 +88,39 @@ class IDAPythonConfigurationProvider implements vscode.DebugConfigurationProvide } } + async function remoteIDAPythonExec(config: DebugConfiguration): Promise { - let r = _remoteIDAPythonExec(config); - if (await r === 'ok') { + try { + await _remoteIDAPythonExec(config); + vscode.commands.executeCommand('workbench.debug.action.focusRepl'); return config.debugConfig; - } else { - vscode.window.showErrorMessage(await r); + } + catch (e: Error | string | any) { + vscode.window.showErrorMessage(e); return undefined; // abort launch } } function _remoteIDAPythonExec(config: DebugConfiguration): Promise { let socket = new WebSocket(`ws://${config.host}:${config.port}/`); + let timer: NodeJS.Timeout | undefined = undefined; + + const clearTimer = () => { + if (timer) { + clearTimeout(timer); + timer = undefined; + } + }; + return new Promise((resolve, reject) => { + + timer = setTimeout(() => { + socket.close(); + reject(new Error(`WebSocket connection timeout after ${config.timeout} ms`)); + }, config.timeout); + socket.on('open', () => { + clearTimer(); socket.send(JSON.stringify({ 'type': MessageType.startDebugServer, 'host': config.debugConfig.connect.host, @@ -126,17 +147,24 @@ function _remoteIDAPythonExec(config: DebugConfiguration): Promise { case MessageType.serverReady: resolve('ok'); + socket.close(); break; + case MessageType.debugFinished: socket.close(); vscode.debug.stopDebugging(); break; + case MessageType.error: - resolve(json.info); + reject(json.info); socket.close(); break; } }); + socket.on('error', (err) => { + clearTimer(); + reject(err); + }); }); } \ No newline at end of file