localtunnel / server

server for localtunnel.me
https://localtunnel.me
MIT License
2.99k stars 967 forks source link

LocalTunnel Server Does Not Detect Dead TunnelAgent Connections or Free Up Stale Subdomains #119

Open jonmchan opened 4 years ago

jonmchan commented 4 years ago

If the localtunnel client is not properly closed, localtunnel may reserve an instance indefinitely and never close the connection. This can happen if wifi dies, internet connection dies, a computer was shut down improperly or set to sleep, or even by just suspending the process while it is running. Any of these scenarios can leave localtunnel server in a state where it is keeping the TunnelAgent sockets open and not allowing the subdomain to be reused.

Repro steps:

lt -s jchan-test -p 3000
your url is: https://jchan-test.localtunnel.me
^Z
[1]+  Stopped     

Disconnect wifi, kill the process, and reconnect to the internet.

Next time when you try to connect, the subdomain is no longer available.

lt -s jchan-test -p 3000
your url is: https://fluffy-walrus-72.localtunnel.me
^C
jonmchan commented 4 years ago

For anyone else experiencing this issue on their own localtunnel instance, I've written a hack to get it working for myself - a delete endpoint:

    router.get('/api/tunnels/:id/delete', async (ctx, next) => {
        const clientId = ctx.params.id;
        const client = manager.getClient(clientId);
        if (!client) {
            ctx.throw(404);
            return;
        }

        manager.removeClient(clientId);
        ctx.body = {
            delete_status: "success",
        };
    });

https://github.com/jonmchan/localtunnel-server/blob/delete-endpoint/server.js#L53-L65

This isn't ideal for a public instance of localtunnel as malicious users can kick someone off and steal their subdomain, but for a private instance of localtunnel, this works well instead of having my developers contact me to constantly restart the server.

StyleT commented 3 years ago

Added pings in this fork https://github.com/StyleT/mytunnel-server so should be fixed now. However you need to use https://github.com/StyleT/mytunnel client to make it work.