In order to proxy an HTTP request, we create a local peer which listens on `localhost:9900`. HTTP requests performed to that address are tunneled via a libp2p stream to a remote peer, which then performs the HTTP requests and sends the response back to the local peer, which relays it
to the user.
In order to proxy an HTTP request, we create a local peer which listens on `localhost:9900`. HTTP requests performed to that address are tunneled via a libp2p stream to a remote peer, which then performs the HTTP requests and sends the response back to the local peer, which relays it to the user.
Note that this is a very simple approach to a proxy, and does not perform any header management, nor supports HTTPS. The `proxy.go` code is thoroughly commeted, detailing what is happening in every step.
...
...
@@ -37,7 +34,7 @@ From `go-libp2p` base folder:
First run the "remote" peer as follows. It will print a local peer address. If you would like to run this on a separate machine, please replace the IP accordingly:
For most applications, the host is the basic building block you'll need to get
started. This guide will show how to construct and use a simple host.
The host is an abstraction that manages services on top of a swarm. It provides
a clean interface to connect to a service on a given remote peer.
For most applications, the host is the basic building block you'll need to get started. This guide will show how to construct and use a simple host.
The host is an abstraction that manages services on top of a swarm. It provides a clean interface to connect to a service on a given remote peer.
First, you'll need an ID, and a place to store that ID. To generate an ID, you can do the following:
First, you'll need an ID, and a place to store that ID. To generate an
ID, you can do the following:
```go
import(
"crypto/rand"
...
...
@@ -35,8 +34,8 @@ ps.AddPrivKey(pid, priv)
ps.AddPubKey(pid,priv)
```
Next, you'll need at least one address that you want to listen on. You can go
from a string to a multiaddr like this:
Next, you'll need at least one address that you want to listen on. You can go from a string to a multiaddr like this:
```go
importma"github.com/multiformats/go-multiaddr"
...
...
@@ -48,10 +47,8 @@ if err != nil {
}
```
Now you know who you are, and where you live (in a manner of speaking). The
next step is setting up a 'swarm network' to handle all the peers you will
connect to. The swarm handles incoming connections from other peers, and
handles the negotiation of new outbound connections.
Now you know who you are, and where you live (in a manner of speaking). The next step is setting up a 'swarm network' to handle all the peers you will connect to. The swarm handles incoming connections from other peers, and handles the negotiation of new outbound connections.
```go
import(
"context"
...
...
@@ -68,20 +65,16 @@ if err != nil {
}
```
At this point, we have everything needed to finally construct a host. That call
is the simplest one so far:
At this point, we have everything needed to finally construct a host. That call is the simplest one so far:
And thats it, you have a libp2p host and you're ready to start doing some
awesome p2p networking!
And thats it, you have a libp2p host and you're ready to start doing some awesome p2p networking!
In future guides we will go over ways to use hosts, configure them differently
(hint: there are a huge number of ways to set these up), and interesting ways
to apply this technology to various applications you might want to build.
In future guides we will go over ways to use hosts, configure them differently (hint: there are a huge number of ways to set these up), and interesting ways to apply this technology to various applications you might want to build.
To see this code all put together, take a look at the `host.go` file in this
directory.
To see this code all put together, take a look at the `host.go` file in this directory.