diff --git a/.snippets/code/develop/smart-contracts/native-evm-contracts/ether-js-server-connection.js b/.snippets/code/develop/smart-contracts/native-evm-contracts/ether-js-server-connection.js index dd630a24e..57e20bc1f 100644 --- a/.snippets/code/develop/smart-contracts/native-evm-contracts/ether-js-server-connection.js +++ b/.snippets/code/develop/smart-contracts/native-evm-contracts/ether-js-server-connection.js @@ -1,5 +1,5 @@ import { JsonRpcProvider } from 'ethers'; const provider = new JsonRpcProvider( - 'https://westend-asset-hub-eth-rpc.polkadot.io', + 'https://westend-asset-hub-eth-rpc.polkadot.io' ); diff --git a/.snippets/code/infrastructure/running-a-node/setup-secure-wss/apache2-config.md b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/apache2-config.md new file mode 100644 index 000000000..5b838b87a --- /dev/null +++ b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/apache2-config.md @@ -0,0 +1,7 @@ +```apacheconf +# (...) +SSLProxyEngine on +ProxyRequests off +ProxyPass / ws://localhost:9944 +ProxyPassReverse / ws://localhost:9944 +``` \ No newline at end of file diff --git a/.snippets/code/infrastructure/running-a-node/setup-secure-wss/install-apache2.md b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/install-apache2.md new file mode 100644 index 000000000..dcc6dae2d --- /dev/null +++ b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/install-apache2.md @@ -0,0 +1,5 @@ +```bash +apt install apache2 +a2dismod mpm_prefork +a2enmod mpm_event proxy proxy_html proxy_http proxy_wstunnel rewrite ssl +``` \ No newline at end of file diff --git a/.snippets/code/infrastructure/running-a-node/setup-secure-wss/install-openssl.md b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/install-openssl.md new file mode 100644 index 000000000..3f47aadcc --- /dev/null +++ b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/install-openssl.md @@ -0,0 +1,4 @@ +```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 +``` \ No newline at end of file diff --git a/.snippets/code/infrastructure/running-a-node/setup-secure-wss/nginx-config.md b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/nginx-config.md new file mode 100644 index 000000000..295d4d723 --- /dev/null +++ b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/nginx-config.md @@ -0,0 +1,14 @@ +```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; + } +} +``` \ No newline at end of file diff --git a/.snippets/code/infrastructure/running-a-node/setup-secure-wss/nginx-rate-limit.md b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/nginx-rate-limit.md new file mode 100644 index 000000000..26a1adeb2 --- /dev/null +++ b/.snippets/code/infrastructure/running-a-node/setup-secure-wss/nginx-rate-limit.md @@ -0,0 +1,10 @@ +```conf +http { + limit_req_zone "$http_x_forwarded_for" zone=zone:10m rate=2r/s; + (...) +} +location / { + limit_req zone=zone burst=5; + (...) +} +``` \ No newline at end of file diff --git a/.snippets/code/tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain/reserve-backed-transfer.js b/.snippets/code/tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain/reserve-backed-transfer.js index a5e77b8ce..397611573 100644 --- a/.snippets/code/tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain/reserve-backed-transfer.js +++ b/.snippets/code/tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain/reserve-backed-transfer.js @@ -25,13 +25,13 @@ import { Binary } from 'polkadot-api'; // Create Polkadot client using WebSocket provider for Polkadot chain const polkadotClient = createClient( - withPolkadotSdkCompat(getWsProvider('ws://127.0.0.1:8001')), + withPolkadotSdkCompat(getWsProvider('ws://127.0.0.1:8001')) ); const dotApi = polkadotClient.getTypedApi(dot); // Create Astar client using WebSocket provider for Astar chain const astarClient = createClient( - withPolkadotSdkCompat(getWsProvider('ws://localhost:8000')), + withPolkadotSdkCompat(getWsProvider('ws://localhost:8000')) ); const astarApi = astarClient.getTypedApi(astar); @@ -42,7 +42,7 @@ const aliceKeyPair = derive('//Alice'); const alice = getPolkadotSigner( aliceKeyPair.publicKey, 'Sr25519', - aliceKeyPair.sign, + aliceKeyPair.sign ); // Define recipient (Dave) address on Astar chain @@ -56,7 +56,7 @@ const polkadotAssetId = 340282366920938463463374607431768211455n; // Fetch asset balance of recipient (Dave) before transaction let assetMetadata = await astarApi.query.Assets.Account.getValue( polkadotAssetId, - daveAddress, + daveAddress ); console.log('Asset balance before tx:', assetMetadata?.balance ?? 0); @@ -65,7 +65,7 @@ const tx = dotApi.tx.XcmPallet.limited_reserve_transfer_assets({ dest: XcmVersionedLocation.V3({ parents: 0, interior: XcmV3Junctions.X1( - XcmV3Junction.Parachain(2006), // Destination is the Astar parachain + XcmV3Junction.Parachain(2006) // Destination is the Astar parachain ), }), beneficiary: XcmVersionedLocation.V3({ @@ -75,7 +75,7 @@ const tx = dotApi.tx.XcmPallet.limited_reserve_transfer_assets({ // Beneficiary address on Astar network: undefined, id: idBenef, - }), + }) ), }), assets: XcmVersionedAssets.V3([ @@ -110,7 +110,7 @@ await new Promise((resolve) => setTimeout(resolve, 20000)); // Fetch asset balance of recipient (Dave) after transaction assetMetadata = await astarApi.query.Assets.Account.getValue( polkadotAssetId, - daveAddress, + daveAddress ); console.log('Asset balance after tx:', assetMetadata?.balance ?? 0); diff --git a/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/Cargo.toml b/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/Cargo.toml deleted file mode 100644 index b1db64bf8..000000000 --- a/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -[package] -name = "custom-pallet" -version = "0.1.0" -license.workspace = true -authors.workspace = true -homepage.workspace = true -repository.workspace = true -edition.workspace = true - -[dependencies] -codec = { features = ["derive"], workspace = true } -scale-info = { features = ["derive"], workspace = true } -frame-support.workspace = true -frame-system.workspace = true - -[dev-dependencies] -frame-support = { workspace = true, default-features = false } -frame-system = { workspace = true, default-features = false } -sp-runtime = { workspace = true, default-features = false } -sp-core = { workspace = true, default-features = false } -sp-io = { workspace = true, default-features = false } -pallet-balances = { workspace = true, default-features = false } - -[features] -default = ["std"] -std = [ - "codec/std", - "frame-support/std", - "frame-system/std", - "scale-info/std", - "sp-runtime/std", - "sp-core/std", - "sp-io/std", - "pallet-balances/std", -] - -[lints] -workspace = true diff --git a/.snippets/code/tutorials/polkadot-sdk/testing/spawn-basic-chain/connect-to-alice-01.js b/.snippets/code/tutorials/polkadot-sdk/testing/spawn-basic-chain/connect-to-alice-01.js index 64681f1f5..325bb0ccc 100644 --- a/.snippets/code/tutorials/polkadot-sdk/testing/spawn-basic-chain/connect-to-alice-01.js +++ b/.snippets/code/tutorials/polkadot-sdk/testing/spawn-basic-chain/connect-to-alice-01.js @@ -12,7 +12,7 @@ async function main() { ]); console.log( - `You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`, + `You are connected to chain ${chain} using ${nodeName} v${nodeVersion}` ); } diff --git a/images/infrastructure/running-a-validator/running-a-node/setup-secure-wss/setup-secure-wss-1.webp b/images/infrastructure/running-a-validator/running-a-node/setup-secure-wss/setup-secure-wss-1.webp new file mode 100644 index 000000000..118e20e29 Binary files /dev/null and b/images/infrastructure/running-a-validator/running-a-node/setup-secure-wss/setup-secure-wss-1.webp differ diff --git a/infrastructure/running-a-node/.pages b/infrastructure/running-a-node/.pages index 524ac6396..bd235e850 100644 --- a/infrastructure/running-a-node/.pages +++ b/infrastructure/running-a-node/.pages @@ -3,4 +3,4 @@ nav: - index.md - 'Set Up a Full Node': setup-full-node.md - 'Set Up a Bootnode': setup-bootnode.md - # - 'Common Node Flags': common-node-flags.md \ No newline at end of file + - 'Set Up Secure Websocket': setup-secure-wss.md \ No newline at end of file diff --git a/infrastructure/running-a-node/setup-secure-wss.md b/infrastructure/running-a-node/setup-secure-wss.md new file mode 100644 index 000000000..527463be4 --- /dev/null +++ b/infrastructure/running-a-node/setup-secure-wss.md @@ -0,0 +1,93 @@ +--- +title: Set Up Secure WebSocket +description: Instructions on enabling SSL for your node and setting up a secure WebSocket proxy server using nginx for remote connections. +--- + +# Setup Secure WebSocket + +## Introduction + +Ensuring secure WebSocket communication is crucial for maintaining the integrity and security of a Polkadot or Kusama node when interacting with remote clients. This guide walks you through setting up a secure WebSocket (WSS) connection for your node by leveraging SSL encryption with popular web server proxies like nginx or Apache. By the end of this guide, you'll be able to secure your node's WebSocket port, enabling safe remote connections without exposing your node to unnecessary risks. + +!!!info + The following instructions are for UNIX-based systems. + +## Secure a WebSocket Port + +You can convert a non-secured WebSocket port to a secure WSS port by placing it behind an SSL-enabled proxy. This approach can be used to secure a bootnode or RPC server. The SSL-enabled apache2/nginx/other proxy server redirects requests to the internal WebSocket and converts it to a secure (WSS) connection. You can use a service like [LetsEncrypt](https://letsencrypt.org/){target=\_blank} to obtain an SSL certificate. + +### Obtain an SSL Certificate + +LetsEncrypt suggests using the [Certbot ACME client](https://letsencrypt.org/getting-started/#with-shell-access/){target=\_blank} for your respective web server implementation to get a free SSL certificate: + +- [nginx](https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal){target=\_blank} +- [apache2](https://certbot.eff.org/instructions?ws=apache&os=ubuntufocal){target=\_blank} + +LetsEncrypt will auto-generate an SSL certificate and include it in your configuration. + +When connecting, you can generate a self-signed certificate and rely on your node's raw IP address. However, self-signed certificates aren't optimal because you must include the certificate in an allowlist to access it from a browser. + +Use the following command to generate a self-signed certificate using OpenSSL: + +--8<-- 'code/infrastructure/running-a-node/setup-secure-wss/install-openssl.md' + +## Install 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}, both of which are commonly used web server implementations. See the following section for configuration examples for both implementations. + +### Use nginx + +1. Install the `nginx` web server: + ```bash + apt install nginx + ``` + +2. In an SSL-enabled virtual host, add: + --8<-- 'code/infrastructure/running-a-node/setup-secure-wss/nginx-config.md' + +3. Optionally, you can introduce some form of rate limiting: + --8<-- 'code/infrastructure/running-a-node/setup-secure-wss/nginx-rate-limit.md' + +### Use Apache2 + +Apache2 can run in various modes, including `prefork`, `worker`, and `event`. In this example, the [`event`](https://httpd.apache.org/docs/2.4/mod/event.html){target=\_blank} mode is recommended for handling higher traffic loads, as it is optimized for performance in such environments. However, depending on the specific requirements of your setup, other modes like `prefork` or `worker` may also be appropriate. + +1. Install the `apache2` web server: + --8<-- 'code/infrastructure/running-a-node/setup-secure-wss/install-apache2.md' + +2. The [`mod_proxy_wstunnel`](https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html){target=\_blank} provides support for the tunneling of WebSocket connections to a backend WebSocket server. The connection is automatically upgraded to a WebSocket connection. In an SSL-enabled virtual host add: + --8<-- 'code/infrastructure/running-a-node/setup-secure-wss/apache2-config.md' + + !!!warning + 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] + ``` + +3. Optionally, some form of rate limiting can be introduced by first running the following command: + + ```bash + apt install libapache2-mod-qos + a2enmod qos + ``` + + Then edit `/etc/apache2/mods-available/qos.conf` as follows: + + ```conf + # allows max 50 connections from a single IP address: + QS_SrvMaxConnPerIP 50 + ``` + +## Connect to the Node + +1. Open [Polkadot.js Apps interface](https://polkadot.js.org/apps){target=\_blank} and click the logo in the top left to switch the node +2. Activate the **Development** toggle and input either your node's domain or IP address. Remember to prefix with `wss://` and, if you're using the 443 port, append `:443` as follows: + + ```bash + wss://example.com:443 + ``` + +![A sync-in-progress chain connected to Polkadot.js UI](/images/infrastructure/running-a-validator/running-a-node/setup-secure-wss/setup-secure-wss-1.webp) \ No newline at end of file