README.md 3.05 KB
Newer Older
Hector Sanjuan's avatar
Hector Sanjuan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# HTTP proxy service with libp2p

This examples shows how to create a simple HTTP proxy service with libp2p:


```
                                                                                                                 XXX
                                                                                                               XX  XXXXXX
                                                                                                              X         XX
                                                                                                    XXXXXXX  XX          XX XXXXXXXXXX
                    +---------------------+                  +---------------------+              XXX      XXX            XXX        XXX
 HTTP Request       |                     |                  |                     |             XX                                    XX
+------------------->                     |  libp2p stream   |                     |  HTTP       X                                      X
                    |    Local peer       <------------------>   Remote peer       <------------->     HTTP SERVER - THE INTERNET      XX
<-------------------+                     |                  |                     | Req & Resp   XX                                   X
    HTTP Response   |   libp2p host       |                  |    libp2p host      |               XXXX XXXX XXXXXXXXXXXXXXXXXXXX   XXXX
                    +---------------------+                  +---------------------+                                            XXXXX

```

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.

## Build

From `go-libp2p` base folder:

```
> make deps
Eric Tang's avatar
typo    
Eric Tang committed
32
> go build ./examples/http-proxy
Hector Sanjuan's avatar
Hector Sanjuan committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
```

## Usage

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:

```sh
$ ./http-proxy
Proxy server is ready
libp2p-peer addresses:
/ip4/127.0.0.1/tcp/12000/ipfs/QmddTrQXhA9AkCpXPTkcY7e22NK73TwkUms3a44DhTKJTD
```

The run the local peer, indicating that it will need to forward http requests to the remote peer as follows:

```
$ ./http-proxy -d /ip4/127.0.0.1/tcp/12000/ipfs/QmddTrQXhA9AkCpXPTkcY7e22NK73TwkUms3a44DhTKJTD
Proxy server is ready
libp2p-peer addresses:
/ip4/127.0.0.1/tcp/12001/ipfs/Qmaa2AYTha1UqcFVX97p9R1UP7vbzDLY7bqWsZw1135QvN
proxy listening on  127.0.0.1:9900
```

As you can see, the proxy prints the listening address `127.0.0.1:9900`. You can now use this address as proxy, for example with `curl`:

```
$ curl -x "127.0.0.1:9900" "http://ipfs.io/ipfs/QmfUX75pGRBRDnjeoMkQzuQczuCup2aYbeLxz5NzeSu9G6"
it works!

```