libp2p / go-libp2p-examples

Example libp2p applications
MIT License
339 stars 145 forks source link

Libp2p as reverse proxy for IoT devices #136

Closed StefanGies99 closed 4 years ago

StefanGies99 commented 4 years ago

Hi folks,

I'm currently working on a project where I want to expose my IoT device settings to the internet via a web application. Because I have a large number of devices I wanted to create a maintainable solution for them all. All the devices have different interfaces and settings which are impossible to maintain building a custom solution for them all. So I tried to figure out how I can manage settings from a web portal by creating a (sidecar) proxy to all the individual devices.

Is there any proper solution for this or am I missing a step doing the proxy example? The proxy is doing it in the wrong way which is not useable for my case. How can I do this in reverse?

To make my case more clear I made sort of a diagram where I explain what my thoughts currently are.

LibP2P

hsanjuan commented 4 years ago

That example shows how to tunnel something like http using a libp2p stream.

If every IOT device is going to connect to a server using libp2p, then great, that connection can be used to talk http to each IOT device transported on a libp2p stream.

For that each IOT device should run a webserver that, just as it listens on a normal network address (with a Go tcp listener), should listen on the libp2p stream. The example implements the own server, and it may not be clear for real usage.

Instead, I suggest you look into https://github.com/libp2p/go-libp2p-http and https://godoc.org/github.com/libp2p/go-libp2p-http which shows how the Go webserver can "listen on libp2p", and how a Go http client than use libp2p as transport.

Now, in order to access those websites from your browser at the "Server", you will need to additionally run an actual proxy, that takes your browser client requests and tunnels them (the example covers this part I think):

<webserver><libp2p-gostream-listener>-----libp2p-stream-on-libp2p-connection----<libp2p-reverse-proxy-with-go-libp2p-http><browser>

If your webserver is not written in Go, you will need an additional libp2p-reverse-proxy in front of it.

As a side note, go-ipfs has "stream mounting" (ipfs p2p --help) which does exactly the reverse proxy part for the client. i.e. ipfs p2p forward --allow-custom-protocol /libp2p-http :3000 <peerID> will tunnel anything talking to localhost:3000 as a libp2p stream to a given peerID.

StefanGies99 commented 4 years ago

@hsanjuan Thanks for your explanation! In the next days, I'll continue working on the mentioned solution above.