How to build a localtunnel / ngrok clone
Asked Answered
E

1

6

Services like ngrok and localtunnel exist so you can (and I'm not very keen in describing this) put your localhost on the public web for access on a public URL. Localtunnel appears more popular.

My question is, at a general level / high-level overview kind of thing, how do you implement this yourself? Like, how can I create a domain mydomain.com, and then have it such that a cli command like mydomain create-tunnel will return me a unique URL 12345.mydomain.com or whatever, and I can then access my localhost from that unique URL.

I know how to build CLIs and RESTful web apps, but I don't know where the connection is made between localhost and the public URL. I don't see how I can host my localhost on a public URL (https URLs only).

If I try to think through it, I would maybe try searching for HTTP tunnels, which leads to the Tunneling protocol. The HTTP tunnels page doesn't reveal too much. The tunneling protocol page goes too low level I think, I think I'm asking about a higher level.

But if I try and imagine what would happen.... The browser makes a request to the URL on mydomain.com. This then has an already open HTTPS connection with an HTTPS server on my local machine. It proxies the request to the local machine's HTTPS server. This then makes a request to localhost. That is, it proxies the request from the local HTTPS server to whatever localhost server I'm actually concentrated on. It makes the request to the localhost server, then gets a response. Pipes the response back to the remote machine, then that remote machine pipes it back to the browser.

Also, is there anything I need to worry about in terms of security (both in terms of building a secure tunneling app, and also in terms of how you bypass the firewalls so to speak using something like this)?

Maybe I should be looking here :).

Enabling answered 2/10, 2019 at 7:37 Comment(2)
I’m voting to close this question because it seems vague. There are a multitude of ways you could answer this questionJernigan
it's a very useful question. A good responder could list the possibilities. Stackoverflow exists to learn!Austen
A
4

I found this question unanswered and I had the same problem. The concepts on which this is founded are two:

  • reverse port forwarding: the ssh connection through which from a local machine you connect to a public host (you must be authorized) and ask to forward all incoming requests on a particular port to your local machine from where the ssh connection request started. For example, from your local machine you have a web server on port 8080 and you want to make it accessible from a public domain server you know username and password of a user:

    ssh -N -R 80:localhost:8080 [email protected]

Now you can access the local webserver by calling the public server on port 80.

  • subdomains: if you have nginx or apache or some other reverse proxy on the public server, you can create a subdomain on the flight, communicate the address to the local server asking for an ssh reverse connection, and then, knowing the particular host address (comprehending the subdomain), it can start an ssh reverse connection.

That's how I think it works.

Austen answered 20/9, 2021 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.