如何调试 Ngrok 不通过节点模块启动?

Posted

技术标签:

【中文标题】如何调试 Ngrok 不通过节点模块启动?【英文标题】:How to debug Ngrok not starting via node module? 【发布时间】:2021-09-28 17:35:24 【问题描述】:

我正在开发一个使用 Ngrok for dev 的应用程序。从控制台运行 ngrok 可以:

ngrok http 3011

显示“控制台 UI”,指示隧道已启动并提供要使用的 url。

现在我正在尝试设置一些东西,以便开发人员不需要复制粘贴 url 等。但是非常基本的用法对我来说失败了:

// prepare-server.ts

import ngrok from 'ngrok'
import config from '../config'

(async () => 
    try
        const nwhUrl = await ngrok.connect( addr: config.serverPort )
        console.log(nwhUrl)
     catch(e) 
        console.error('problem with ngrok:', e)
    
)()

给我(以ts-node src/prepare-server.ts 运行时):

problem with ngrok: NgrokClientError: 
    at NgrokClient.request (<path to project>\node_modules\ngrok\src\client.js:39:23)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)  
    at connectRetry (<path to project>\node_modules\ngrok\index.js:29:22) 
  response: <ref *1> IncomingMessage 
    _readableState: ReadableState 
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList  head: null, tail: null, length: 0 ,
      length: 0,
      pipes: [],
      flowing: false,
      ended: true,
      endEmitted: true,
      reading: false,
      sync: false,
      needReadable: false,
      emittedReadable: false,
      readableListening: true,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: false,
      destroyed: false,
      errored: false,
      closed: false,
      closeEmitted: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      decoder: [StringDecoder],
      encoding: 'utf8',
      [Symbol(kPaused)]: null
    ,
    _events: [Object: null prototype] 
      end: [Array],
      aborted: [Array],
      error: [Array],
      readable: [Array],
      finish: [Function: onfinish],
      close: [Function: onclose]
    ,
    _eventsCount: 6,
    _maxListeners: undefined,
    socket: Socket 
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: null,
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      parser: null,
      _httpMessage: [ClientRequest],
      _peername: [Object],
      [Symbol(asyncId)]: 15,
      [Symbol(kHandle)]: [TCP],
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0
    ,
    httpVersionMajor: 1,
    httpVersionMinor: 1,
    httpVersion: '1.1',
    complete: true,
    headers: 
      'content-type': 'application/json',
      date: 'Wed, 21 Jul 2021 10:14:40 GMT',
      'content-length': '168',
      connection: 'close'
    ,
    rawHeaders: [
      'Content-Type',
      'application/json',
      'Date',
      'Wed, 21 Jul 2021 10:14:40 GMT',
      'Content-Length',
      '168',
      'Connection',
      'close'
    ],
    trailers: ,
    rawTrailers: [],
    aborted: false,
    upgrade: false,
    url: 'http://127.0.0.1:4041/api/tunnels',
    method: null,
    statusCode: 503,
    statusMessage: 'Service Unavailable',
    client: Socket 
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: null,
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      parser: null,
      _httpMessage: [ClientRequest],
      _peername: [Object],
      [Symbol(asyncId)]: 15,
      [Symbol(kHandle)]: [TCP],
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: null,
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0
    ,
    _consuming: true,
    _dumped: false,
    req: ClientRequest 
      _events: [Object: null prototype],
      _eventsCount: 10,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: null,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [Socket],
      _header: 'POST /api/tunnels HTTP/1.1\r\n' +
        'user-agent: got (https://github.com/sindresorhus/got)\r\n' +    
        'content-type: application/json\r\n' +
        'accept: application/json\r\n' +
        'content-length: 74\r\n' +
        'accept-encoding: gzip, deflate, br\r\n' +
        'Host: 127.0.0.1:4041\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'POST',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/tunnels',
      _ended: true,
      res: [Circular *1],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      timings: [Object],
      emit: [Function (anonymous)],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype],
      [Symbol(reentry)]: true
    ,
    timings: 
      start: 1626862480674,
      socket: 1626862480676,
      lookup: 1626862480677,
      connect: 1626862480677,
      secureConnect: undefined,
      upload: 1626862480677,
      response: 1626862480678,
      end: 1626862480680,
      error: undefined,
      abort: undefined,
      phases: [Object]
    ,
    emit: [Function (anonymous)],
    requestUrl: 'http://127.0.0.1:4041/api/tunnels',
    redirectUrls: [],
    request: Request 
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 16,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      requestInitialized: true,
      redirects: [],
      retryCount: 0,
      _progressCallbacks: [],
      write: [Function: onLockedWrite],
      end: [Function: onLockedWrite],
      options: [Object],
      requestUrl: 'http://127.0.0.1:4041/api/tunnels',
      _cannotHaveBody: false,
      _noPipe: true,
      [Symbol(kCapture)]: false,
      [Symbol(downloadedSize)]: 168,
      [Symbol(uploadedSize)]: 74,
      [Symbol(serverResponsesPiped)]: Set(0) ,
      [Symbol(stopReading)]: true,
      [Symbol(triggerRead)]: false,
      [Symbol(jobs)]: [],
      [Symbol(body)]: '"addr":3011,"proto":"http","name":"2fbfea07-1dc3-4d7b-acfc-20a68b755c10"',
      [Symbol(bodySize)]: 74,
      [Symbol(cancelTimeouts)]: [Function: cancelTimeouts],
      [Symbol(unproxyEvents)]: [Function (anonymous)],
      [Symbol(request)]: [ClientRequest],
      [Symbol(originalResponse)]: [Circular *1],
      [Symbol(isFromCache)]: false,
      [Symbol(responseSize)]: 168,
      [Symbol(response)]: [Circular *1],
      [Symbol(startedReading)]: true
    ,
    isFromCache: false,
    ip: '127.0.0.1',
    retryCount: 0,
    rawBody: <Buffer >,
    body: '',
    [Symbol(kCapture)]: false
  ,
  body: ''

请注意 503 服务不可用代码。如果 CLI 版本可以工作,那又如何呢?

任何想法如何调试这个?我也试过这个没有选项(await ngrok.connect()),输出没有太大变化。这是一个绝对基本的例子(见docs),所以我真的不能做更多来简化代码,看起来像是关于Ngrok内部的东西......节点包是v4.0.1

PS this issue 可能相关或相同。虽然我的问题发生在节点 14.1.0 上,但它不会发生在节点 15.14.0 上。

【问题讨论】:

那么你的节点版本是 4.0.1 吗?尝试更新您的软件包。在 v16.4.2 上为我工作。我有ts-node最新版本v10.1.0,ngrok 2.3.40 javascript包。 @BasvanderLinden 不,这不是我使用的 node.js 版本(实际上是 14.1.0),4.0.1 是 ngrok 节点包的版本。但是,是的,你是对的,我已经尝试过节点 15.14.0(通过 nvm 切换)并且它有效!谢谢!随意把你的建议作为答案,我会给你赏金。不过,我将接受作为答案可能对如何调试此类问题更有指导意义。 @BasvanderLinden 为什么你删除了你的答案?看起来很有帮助;不过我没有仔细审查过。 虽然更新节点有效,但我建议的它不起作用的原因以及建议的最低版本似乎不正确。所以这就是我删除它的原因。 Github issue for context. 【参考方案1】:

鉴于您提供的详细信息,这似乎是一个内部 ngork 错误或与某些特定节点版本的一些模糊不兼容。

尝试更新(有时甚至更旧!)版本的包和/或节点实际上并不太难,如果不能完全解决问题(就像您的情况一样),可能有助于阐明问题的性质。

如果更新没有解决问题,IMO 最好的办法是在他们的 Github 存储库上打开一个问题,因为看起来问题在 ngork 内部工作中已经足够深了,我不希望有很多人能够提供帮助。

https://github.com/bubenshchykov/ngrok/issues

【讨论】:

以上是关于如何调试 Ngrok 不通过节点模块启动?的主要内容,如果未能解决你的问题,请参考以下文章

VSCode - 如何使用启动配置调试启动自己的调试器的节点程序?

✗ 'shopify node serve' 导致 - ngrok 隧道已超时,正在重新启动......

如何在不重新启动的情况下调试 mod_perl2 模块?

无法写入TTD跟踪 - 是否已完成同步模块加载? nodejs调试节点chakkara

ngrok - vs2017 - 尝试启动 ngrok 隧道时遇到问题

Ngrok url 每天都会更改我启动 Ngrok