View random ngrok URL when run in background

前端 未结 12 1722
旧时难觅i
旧时难觅i 2021-02-02 08:16

When I start an ngrok client with ./ngrok tcp 22 it runs in the foreground and I can see the randoming generated forwarding URL, such as tcp://0.tcp.ngrok.io:

12条回答
  •  遇见更好的自我
    2021-02-02 08:26

    If it helps anyone I wrote a quick script to extract the generated random url in Node:

    It makes assumption you're only interested in the secure url.

    const fetch = require('node-fetch')
    fetch('http://localhost:4040/api/tunnels')
      .then(res => res.json())
      .then(json => json.tunnels.find(tunnel => tunnel.proto === 'https'))
      .then(secureTunnel => console.log(secureTunnel.public_url))
      .catch(err => {
        if (err.code === 'ECONNREFUSED') {
          return console.error("Looks like you're not running ngrok.")
        }
        console.error(err)
      })
    

    If you wanted all tunnels:

    const fetch = require('node-fetch')
    fetch('http://localhost:4040/api/tunnels')
      .then(res => res.json())
      .then(json => json.tunnels.map(tunnel => tunnel.public_url))
      .then(publicUrls => publicUrls.forEach(url => console.log(url)))
      .catch(err => {
        if (err.code === 'ECONNREFUSED') {
          return console.error(
            "Looks like you're not running ngrok."
          )
        }
        console.error(err)
      })
    

提交回复
热议问题