View random ngrok URL when run in background

前端 未结 12 1697
旧时难觅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:33

    A Node.js solution.

    Bonus: It copies the url to the clipboard in Windows, Mac and Linux1

    const http = require("http");
    const { execSync } = require("child_process");
    
    const callback = (res) => {
        let data = "";
        res.on("data", (chunk) => (data += chunk));
        res.on("end", () => {
            const resJSON = JSON.parse(data);
            const tunnels = resJSON.tunnels;
    
            const { public_url: url } = tunnels.find(({ proto }) => proto === "https");
    
            console.log(url);
    
            // Copy to clipboard
            switch (process.platform) {
                case "win32":
                    execSync(`echo ${url} | clip`);
                    break;
                
                case "darwin":
                    execSync(`echo ${url} | pbcopy`);
                    break;
                    
                case "linux":
                    // NOTE: this requires xclip to be installed
                    execSync(`echo ${url} | xclip -selection clipboard`);
                    break;
                    
                default:
                    break;
            }
        });
    };
    
    http.get("http://localhost:4040/api/tunnels", callback);
    

    [1] You need to install xclip first:

    sudo apt-get install xclip
    

提交回复
热议问题