|
1 |
| -# js-libp2p-hop-relay-server |
2 |
| -A out of the box libp2p relay server with HOP |
| 1 | +# js-libp2p-relay-server <!-- omit in toc --> |
| 2 | + |
| 3 | +[](http://protocol.ai) |
| 4 | +[](http://libp2p.io/) |
| 5 | +[](http://webchat.freenode.net/?channels=%23libp2p) |
| 6 | +[](https://discuss.libp2p.io) |
| 7 | +[](https://github.com/libp2p/js-libp2p-relay-server/actions?query=branch%3Amaster+workflow%3Aci+) |
| 8 | + |
| 9 | +> An out of the box libp2p relay server with HOP |
| 10 | +
|
| 11 | +## Lead Maintainer <!-- omit in toc --> |
| 12 | + |
| 13 | +[Vasco Santos](https://github.com/vasco-santos) |
| 14 | + |
| 15 | +## Table of Contents<!-- omit in toc --> |
| 16 | + |
| 17 | +- [Background](#background) |
| 18 | +- [Usage](#usage) |
| 19 | + - [Install](#install) |
| 20 | + - [CLI](#cli) |
| 21 | + - [Docker](#docker) |
| 22 | + - [SSL](#ssl) |
| 23 | +- [Configuration](#configuration) |
| 24 | + - [Metrics](#metrics) |
| 25 | + - [Discovery](#discovery) |
| 26 | + - [Docker](#docker) |
| 27 | + - [Debug](#debug) |
| 28 | +- [Contribute](#contribute) |
| 29 | +- [License](#license) |
| 30 | + |
| 31 | +## Background |
| 32 | + |
| 33 | +Libp2p nodes acting as circuit relay aim to establish connectivity between libp2p nodes (e.g. IPFS nodes) that wouldn't otherwise be able to establish a direct connection to each other. |
| 34 | + |
| 35 | +A relay is needed in situations where nodes are behind NAT, reverse proxies, firewalls and/or simply don't support the same transports (e.g. go-libp2p vs. browser-libp2p). The circuit relay protocol exists to overcome those scenarios. Nodes with the `auto-relay` feature enabled can automatically bind themselves on a relay to listen for connections on their behalf. |
| 36 | + |
| 37 | +You can read more in its [SPEC](https://github.com/libp2p/specs/tree/master/relay). |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +### Install |
| 42 | + |
| 43 | +```bash |
| 44 | +> npm install --global libp2p-relay-server |
| 45 | +``` |
| 46 | + |
| 47 | +Now you can use the cli command `libp2p-relay-server` to spawn an out of the box libp2p relay server. |
| 48 | + |
| 49 | +### CLI |
| 50 | + |
| 51 | +After installing the relay server, you can use its binary. It accepts several arguments: `--peerId`, `--listenMultiaddrs`, `--announceMultiaddrs`, `--metricsPort`, `--disableMetrics` and `--disablePubsubDiscovery`. |
| 52 | + |
| 53 | +```sh |
| 54 | +libp2p-relay-server [--peerId <jsonFilePath>] [--listenMultiaddrs <ma> ... <ma>] [--announceMultiaddrs <ma> ... <ma>] [--metricsPort <port>] [--disableMetrics] [--disablePubsubDiscovery] |
| 55 | +``` |
| 56 | + |
| 57 | +The main configuration you *should* include are the `PeerId` and `Multiaddrs`, which are detailed next. Using a consistent PeerId will ensure your node's identity is consistent across restarts, and the Multiaddrs will allow you to appropriate bind your local and external addresses so that other peers can connect to you. |
| 58 | + |
| 59 | +#### PeerId |
| 60 | + |
| 61 | +You can create a [PeerId](https://github.com/libp2p/js-peer-id) via its [CLI](https://github.com/libp2p/js-peer-id#cli). |
| 62 | + |
| 63 | +Once you have a generated PeerId json file, you can start the relay with that PeerId by specifying its path via the `--peerId` flag: |
| 64 | + |
| 65 | +```sh |
| 66 | +peer-id --type=ed25519 > id.json |
| 67 | +libp2p-relay-server --peerId ./id.json |
| 68 | +``` |
| 69 | + |
| 70 | +#### Multiaddrs |
| 71 | + |
| 72 | +You can specify the libp2p rendezvous server listen and announce multiaddrs. This server is configured with [libp2p-tcp](https://github.com/libp2p/js-libp2p-tcp) and [libp2p-websockets](https://github.com/libp2p/js-libp2p-websockets), so you will only be able to specify addresses for these transports. |
| 73 | + |
| 74 | +```sh |
| 75 | +libp2p-relay-server --peerId /path/to/peer-id.json --listenMultiaddrs '/ip4/127.0.0.1/tcp/15002/ws' '/ip4/127.0.0.1/tcp/8001' --announceMultiaddrs '/dns4/test.io/tcp' '/dns4/test.io/tcp/443/wss' |
| 76 | +``` |
| 77 | + |
| 78 | +By default it listens on `/ip4/127.0.0.1/tcp/8000` and `/ip4/127.0.0.1/tcp/15003/ws`. It has no announce multiaddrs specified. |
| 79 | + |
| 80 | +### Docker |
| 81 | + |
| 82 | +When running the relay server in Docker, you can configure the same parameters via environment variables, as follows: |
| 83 | + |
| 84 | +```sh |
| 85 | +PEER_ID='/etc/opt/relay/id.json' |
| 86 | +LISTEN_MULTIADDRS='/ip4/127.0.0.1/tcp/15002/ws,/ip4/127.0.0.1/tcp/8001' |
| 87 | +ANNOUNCE_MULTIADDRS='/dns4/test.io/tcp/443/wss,/dns6/test.io/tcp/443/wss' |
| 88 | +``` |
| 89 | + |
| 90 | +Please note that you should expose the listening ports with the docker run command. The default ports used are `8003` for the metrics, `8000` for the tcp listener and `150003` for the websockets listener. |
| 91 | + |
| 92 | +Example: |
| 93 | + |
| 94 | +```sh |
| 95 | +peer-id --type=ed25519 > id.json |
| 96 | +docker build . -t libp2p-relay |
| 97 | +docker run -p 8003:8003 -p 15002:15002 -p 8000:8000 \ |
| 98 | +-e LISTEN_MULTIADDRS='/ip4/127.0.0.1/tcp/8000,/ip4/127.0.0.1/tcp/15002/ws' \ |
| 99 | +-e ANNOUNCE_MULTIADDRS='/dns4/localhost/tcp/8000,/dns4/localhost/tcp/15002/ws' \ |
| 100 | +-e PEER_ID='/etc/opt/relay/id.json' \ |
| 101 | +-v $PWD/id.json:/etc/opt/relay/id.json \ |
| 102 | +-d libp2p-relay |
| 103 | +``` |
| 104 | + |
| 105 | +### SSL |
| 106 | + |
| 107 | +You should setup an SSL certificate with nginx and proxy to the API. You can use a service that already offers an SSL certificate with the server and configure nginx, or you can create valid certificates with for example [Letsencrypt](https://certbot.eff.org/lets-encrypt/osx-nginx). Letsencrypt won’t give you a cert for an IP address (yet) so you need to connect via SSL to a domain name. |
| 108 | + |
| 109 | +With this, you should specify in your relay the announce multiaddrs for your listening transports. This is specially important for browser peers that will leverage this relay, as browser nodes can only dial peers behind a `DNS+WSS` multiaddr. |
| 110 | + |
| 111 | +## Configuration |
| 112 | + |
| 113 | +Besides the `PeerId` and `Multiaddrs`, this server can also have other configurations for fine tuning. |
| 114 | + |
| 115 | +### Metrics |
| 116 | + |
| 117 | +Metrics are enabled by default on `/ip4/127.0.0.1/tcp/8003` via Prometheus. This port can also be modified with: |
| 118 | + |
| 119 | +```sh |
| 120 | +libp2p-relay-server --metricsPort '8008' |
| 121 | +``` |
| 122 | + |
| 123 | +Moreover, metrics can also be disabled with: |
| 124 | + |
| 125 | +```sh |
| 126 | +libp2p-relay-server --disableMetrics |
| 127 | +``` |
| 128 | + |
| 129 | +### Discovery |
| 130 | + |
| 131 | +A discovery module [libp2p/js-libp2p-pubsub-peer-discovery](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery) is configured and enabled by default. It can be disabled with: |
| 132 | + |
| 133 | +```sh |
| 134 | +libp2p-relay-server --disablePubsubDiscovery |
| 135 | +``` |
| 136 | + |
| 137 | +### Docker |
| 138 | + |
| 139 | +On docker you can also specify the configurations above with the following environment variables: |
| 140 | + |
| 141 | +```sh |
| 142 | +METRICS_PORT='8008' |
| 143 | +DISABLE_METRICS='true' |
| 144 | +DISABLE_PUBSUB_DISCOVERY='true' |
| 145 | +``` |
| 146 | + |
| 147 | +Please note that you should expose expose the used ports with the docker run command. |
| 148 | + |
| 149 | +### Debug |
| 150 | + |
| 151 | +You can debug the relay by setting the `DEBUG` environment variable. For instance, you can set it to `libp2p*`. These logs can be noisy so you may wish to tune the namespaces that are logging, see the [Debug module](https://github.com/visionmedia/debug) for detailed usage. |
| 152 | + |
| 153 | +## Contribute |
| 154 | + |
| 155 | +Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-relay-server/issues)! |
| 156 | + |
| 157 | +This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). |
| 158 | + |
| 159 | +[](https://github.com/ipfs/community/blob/master/contributing.md) |
| 160 | + |
| 161 | +## License |
| 162 | + |
| 163 | +MIT - Protocol Labs 2020 |
0 commit comments