public NodeRemoteDebugProcess(NodeRemoteDebugPort port, string exe, string username, string version) {
     _port = port;
     _id = RemoteId;
     _username = username;
     _exe = string.IsNullOrEmpty(exe) ? "<node>" : exe;
     _version = version;
 }
Beispiel #2
0
 public NodeRemoteDebugProcess(NodeRemoteDebugPort port, string exe, string username, string version)
 {
     this._port     = port;
     this._id       = RemoteId;
     this._username = username;
     this._exe      = string.IsNullOrEmpty(exe) ? "<node>" : exe;
     this._version  = version;
 }
Beispiel #3
0
        // Qualifier for our transport is parsed either as a tcp://, ws:// or ws:// URI,
        // or as 'hostname:port', where ':port' is optional.
        public int AddPort(IDebugPortRequest2 pRequest, out IDebugPort2 ppPort)
        {
            ppPort = null;

            string name;

            pRequest.GetPortName(out name);

            // Support old-style 'hostname:port' format, as well.
            if (!name.Contains("://"))
            {
                name = "tcp://" + name;
            }

            var uri = new Uri(name, UriKind.Absolute);

            switch (uri.Scheme)
            {
            case "tcp":
                // tcp:// URI should only specify host and optionally port, path has no meaning and is invalid.
                if (uri.PathAndQuery != "/")
                {
                    return(new FormatException().HResult);
                }
                // Set default port if not specified.
                if (uri.Port < 0)
                {
                    uri = new UriBuilder(uri)
                    {
                        Port = NodejsConstants.DefaultDebuggerPort
                    }.Uri;
                }
                break;

            case "ws":
            case "wss":
                // WebSocket URIs are used as is
                break;

            default:
                // Anything else is not a valid debugger endpoint
                return(new FormatException().HResult);
            }

            ppPort = new NodeRemoteDebugPort(this, pRequest, uri);
            return(VSConstants.S_OK);
        }
        // Qualifier for our transport is parsed either as a tcp://, ws:// or ws:// URI,
        // or as 'hostname:port', where ':port' is optional.
        public int AddPort(IDebugPortRequest2 pRequest, out IDebugPort2 ppPort) {
            ppPort = null;

            string name;
            pRequest.GetPortName(out name);

            // Support old-style 'hostname:port' format, as well.
            if (!name.Contains("://")) {
                name = "tcp://" + name;
            }

            var uri = new Uri(name, UriKind.Absolute);
            switch (uri.Scheme) {
                case "tcp":
                    // tcp:// URI should only specify host and optionally port, path has no meaning and is invalid.
                    if (uri.PathAndQuery != "/") {
                        return new FormatException().HResult;
                    }
                    // Set default port if not specified.
                    if (uri.Port < 0) {
                        uri = new UriBuilder(uri) { Port = NodejsConstants.DefaultDebuggerPort }.Uri;
                    }
                    break;

                case "ws":
                case "wss":
                    // WebSocket URIs are used as is
                    break;

                default:
                    // Anything else is not a valid debugger endpoint
                    return new FormatException().HResult;
            }

            ppPort = new NodeRemoteDebugPort(this, pRequest, uri);
            return VSConstants.S_OK;
        }
        // Connect to the remote debugging server. If any errors occur, display an error dialog, and keep
        // trying for as long as user clicks "Retry".
        private static NodeRemoteDebugProcess Connect(NodeRemoteDebugPort port, INetworkClientFactory networkClientFactory)
        {
            if (port.Uri.Fragment == "#ping=0")
            {
                return(new NodeRemoteDebugProcess(port, "node.exe", String.Empty, String.Empty));
            }

            NodeRemoteDebugProcess process = null;

            while (true)
            {
                Exception exception = null;
                try {
                    LiveLogger.WriteLine("NodeRemoteEnumDebugProcesses pinging remote host ...");
                    using (var client = networkClientFactory.CreateNetworkClient(port.Uri))
                        using (var stream = client.GetStream()) {
                            // https://nodejstools.codeplex.com/workitem/578

                            // Node.js (V8) debugger is fragile during attach, and it's easy to put it into a bad state where it refuses
                            // future connections altogether, or accepts them but send responses that it queued up for another client.
                            // To avoid this, our ping needs to look like a proper debug session to the debuggee. For this, we need to
                            // do the following steps in order:
                            //
                            // 1. Receive the debugger's greeting message.
                            // 2. Send the "disconnect" request.
                            // 3. Receive the "disconnect" response.
                            //
                            // Only then can the socket be closed safely without disrupting V8.

                            // Receive greeting.
                            var    buffer   = new byte[1024];
                            int    len      = stream.ReadAsync(buffer, 0, buffer.Length, new CancellationTokenSource(5000).Token).GetAwaiter().GetResult();
                            string response = Encoding.UTF8.GetString(buffer, 0, len);
                            LiveLogger.WriteLine("NodeRemoteEnumDebugProcesses debugger greeting: " + response);

                            // There's no error code, so we have to do the string comparison. Luckily, it is hardcoded into V8 and is not localized.
                            if (response == "Remote debugging session already active\r\n")
                            {
                                throw new DebuggerAlreadyAttachedException();
                            }

                            // Send "disconnect" request.
                            string request = @"{""command"":""disconnect"",""seq"":1,""type"":""request"",""arguments"":null}";
                            request = string.Format(CultureInfo.InvariantCulture, "Content-Length: {0}\r\n\r\n{1}", request.Length, request);
                            buffer  = Encoding.UTF8.GetBytes(request);
                            stream.WriteAsync(buffer, 0, buffer.Length, new CancellationTokenSource(5000).Token).GetAwaiter().GetResult();

                            // Receive "disconnect" response.
                            buffer   = new byte[1024];
                            len      = stream.ReadAsync(buffer, 0, buffer.Length, new CancellationTokenSource(5000).Token).GetAwaiter().GetResult();
                            response = Encoding.UTF8.GetString(buffer, 0, len);
                            LiveLogger.WriteLine("NodeRemoteEnumDebugProcesses debugger response: " + response);

                            // If we got to this point, the debuggee is behaving as expected, and we can report it as a valid Node.js process.
                            process = new NodeRemoteDebugProcess(port, "node.exe", String.Empty, String.Empty);
                            LiveLogger.WriteLine("NodeRemoteEnumDebugProcesses ping successful.");
                            break;
                        }
                } catch (OperationCanceledException) {
                    LiveLogger.WriteLine("NodeRemoteEnumDebugProcesses ping timed out.");
                } catch (DebuggerAlreadyAttachedException ex) {
                    LiveLogger.WriteLine("DebuggerAlreadyAttachedException connecting to remote debugger");
                    exception = ex;
                } catch (AggregateException ex) {
                    LiveLogger.WriteLine("AggregateException connecting to remote debugger");
                    exception = ex;
                } catch (IOException ex) {
                    LiveLogger.WriteLine("IOException connecting to remote debugger");
                    exception = ex;
                } catch (InvalidOperationException ex) {
                    LiveLogger.WriteLine("InvalidOperationException connecting to remote debugger");
                    exception = ex;
                } catch (SocketException ex) {
                    LiveLogger.WriteLine("SocketException connecting to remote debugger");
                    exception = ex;
                } catch (WebSocketException ex) {
                    LiveLogger.WriteLine("WebSocketException connecting to remote debugger");
                    exception = ex;
                } catch (PlatformNotSupportedException) {
                    LiveLogger.WriteLine("PlatformNotSupportedException connecting to remote debugger");
                    MessageBox.Show(
                        "Remote debugging of node.js Microsoft Azure applications is only supported on Windows 8 and above.",
                        null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(null);
                }

                if (exception != null)
                {
                    while (exception.InnerException != null)
                    {
                        exception = exception.InnerException;
                    }
                }

                string errText = string.Format(CultureInfo.CurrentCulture,
                                               "Could not attach to Node.js process at {0}{1}\r\n\r\n",
                                               port.Uri,
                                               exception != null ? ":\r\n\r\n" + exception.Message : ".");
                if (!(exception is DebuggerAlreadyAttachedException))
                {
                    if (port.Uri.Scheme == "ws" || port.Uri.Scheme == "wss")
                    {
                        errText +=
                            "Make sure that the Azure web site is deployed in the Debug configuration, and web sockets " +
                            "are enabled for it in the Azure management portal.";
                    }
                    else
                    {
                        errText += string.Format(CultureInfo.CurrentCulture,
                                                 "Make sure that the process is running behind the remote debug proxy (RemoteDebug.js), " +
                                                 "and the debugger port (default {0}) is open on the target host.",
                                                 NodejsConstants.DefaultDebuggerPort);
                    }
                }

                DialogResult dlgRes = MessageBox.Show(errText, null, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                if (dlgRes != DialogResult.Retry)
                {
                    break;
                }
            }

            return(process);
        }
 public NodeRemoteEnumDebugProcesses(NodeRemoteDebugPort port, INetworkClientFactory networkClientFactory)
     : base(Connect(port, networkClientFactory))
 {
 }