Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Polkadot Wiki Migration] Setup Secure WebSockets #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added images/infrastructure/general/maintain-wss.webp
Binary file not shown.
124 changes: 124 additions & 0 deletions infrastructure/general/setup-secure-wss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: Setup Secure WebSocket
description: Instructions on setting up a secure socket for remote connections.
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved
---

<!-- TODO: link relevant guides (bootnode, rpc) -->
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved

## Secure a WS Port

A non-secure WebSocket port can be converted to a secure WSS port by placing it behind an SSL-enabled proxy. This can be used to secure a bootnode or secure a RPC server. The SSL-enabled apache2/nginx/other proxy server redirects requests to the internal WebSocket and converts it to a secure (WSS) connection. For this, you will need an SSL certificate for which you can use a service like LetsEncrypt or self-signing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [vale] reported by reviewdog 🐶
[Papermoon.Acronyms] Spell out 'WSS', if it's unfamiliar to the audience.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [vale] reported by reviewdog 🐶
[Papermoon.Acronyms] Spell out 'WSS', if it's unfamiliar to the audience.


### Obtaining an SSL Certificate

One easy way to get a free SSL certificate can be achieved by following the LetsEncrypt instructions
([nginx](https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal){target=_blank}/[apache](https://certbot.eff.org/instructions?ws=apache&os=ubuntufocal){target=_blank}).
dawnkelly09 marked this conversation as resolved.
Show resolved Hide resolved
dawnkelly09 marked this conversation as resolved.
Show resolved Hide resolved
This will auto-generate an SSL certificate and include it in your configuration.

Alternatively, you can generate a self-signed certificate and rely on the raw IP address of your
node when connecting to it. This isn't preferable since you will have to whitelist the certificate
to access it from a browser.

```bash
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/selfsigned.key -out /etc/ssl/certs/selfsigned.crt
sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command can be moved to a code snippet. Also, It might be helpful to include a brief explanation before or after the command to clarify its function.


## Installing a Proxy Server

There are a lot of different implementations of a WebSocket proxy, some of the more widely used are
[nginx](https://www.nginx.com/){target=_blank} and [apache2](https://httpd.apache.org/){target=_blank}, for which configuration
dawnkelly09 marked this conversation as resolved.
Show resolved Hide resolved
dawnkelly09 marked this conversation as resolved.
Show resolved Hide resolved
examples provided below.

### Nginx
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved
dawnkelly09 marked this conversation as resolved.
Show resolved Hide resolved

```bash
apt install nginx
```
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved

In an SSL-enabled virtual host add:

```conf
server {
(...)
location / {
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_pass http://localhost:9944;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
```
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved

Optionally some form of rate limiting can be introduced:

```conf
http {
limit_req_zone "$http_x_forwarded_for" zone=zone:10m rate=2r/s;
(...)
}

location / {
limit_req zone=zone burst=5;
(...)
}
```
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved

### Apache2

CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved
You can run it in different modes such as pre-fork, worker, or event. In this example, the
[event](https://httpd.apache.org/docs/2.4/mod/event.html){target=_blank} works well on higher load
environments, but other modes are also useful depending on the requirements.

```bash
apt install apache2
a2dismod mpm_prefork
a2enmod mpm_event proxy proxy_html proxy_http proxy_wstunnel rewrite ssl
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to a code snippet


The [`mod_proxy_wstunnel`](https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html){target=_blank} provides support for the tunneling of web socket connections to a backend WebSocket server. The connection is automatically upgraded to a WebSocket connection. In an SSL-enabled `virtualhost` add:

```apacheconf
(...)
SSLProxyEngine on
ProxyRequests off

ProxyPass / ws://localhost:9944
ProxyPassReverse / ws://localhost:9944
```

Older versions of `mod_proxy_wstunnel` don't upgrade the connection automatically and will need the
following config added:

```apacheconf
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule /(.*) ws://localhost:9944/$1 [P,L]
RewriteRule /(.*) http://localhost:9944/$1 [P,L]
```

Optionally some form of rate limiting can be introduced:

```bash
apt install libapache2-mod-qos
a2enmod qos
```

And edit `/etc/apache2/mods-available/qos.conf`

```conf
# allows max 50 connections from a single ip address:
QS_SrvMaxConnPerIP 50
```

## Connecting to the Node

Open [Polkadot-JS UI](https://polkadot.js.org/apps){target=_blank} and click the logo in the top left to switch the
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved
node. Activate the "Development" toggle and input your node's address - either the domain or the IP
address. Remember to prefix with `wss://` and if you're using the 443 port, append `:443`, like so:
`wss://example.com:443`.

![A sync-in-progress chain connected to Polkadot-JS UI](/images/infrastructure/general/maintain-wss-image.webp)
CrackTheCode016 marked this conversation as resolved.
Show resolved Hide resolved
Loading