Hosting Element and Matrix .well-known files with Cloudflare Pages™

Using Cloudflare pages, delegate your matrix homserver and serve the element web client for matrix.

Discuss this post at https://matrix.to/#/#minecraftchest1-blog-matrix-elemet-cloudflare:matrix.org

Cloudflare pages is a service by Cloudflare® that allows you to serve static content on Cloudflare’s edge network, allowing you to serve content quickly to users all around the world. It can also be combined with Cloudflare Workers® to make the content somewhat dynamic as well. In this post I would like to share how to use it to serve .well-known files for your Matrix home server, as well as Element web, a Matrix client that runs in your browser.

First off, I assume that you have both a Cloudflare account, as well as an account on either Github or Gitlab. I will be using Gitlab in this tutorial. You will also need to have git installed on your computer. Start off by creating a new blank repository (or project as Gitlab calls it). The name doesn’t matter, but name it something descriptive so you can find it later. The visibility level shouldn’t matter. I flipped it to Public so that it can be helpful for others. Click create if you haven’t already. You should now be taken to your brand new repository. You can use an existing repository if you want as well.

Next, you will want to clone your project locally. I would recommend setting up ssh auth before cloning as this will make it easier to push your changes later. If you have already done this, you can skip this step. If you don’t know how to do so, follow these steps for Github, or these steps for Gitlab. This is especially important if you use 2 Factor Authentication. Now you can clone it. Open a terminal window on Mac or Linux, or a Powershell window on Windows. Next, change to the directory where you want your project to live (creating it if necessary), find the clone button near the top of your project page, copy the ssh link, type git clone ,and paste the link you got. Type cd <your projects name> to go into the projects repository.

.well-known files.

Now you are ready to add content. First thing is to create the .well-known files for your matrix server. Skip this if you just want to host element and use a public homeserver like matrix.org. Create a new folder named .well-known (notice the period at the front), and create a folder in that folder named matrix (no period this time). Using your favorite text editor (I personally like vim), create a file in the .well-known/matrix folder named client with the following content (replace the URL of the homserver with yours).

{
    "m.homeserver": {
        "base_url": "https://minecraftchest1-matrix.eastus.cloudapp.azure.com:443"
    },
    "m.identity_server": {
        "base_url": "https://vector.im"
    }
}

Next create the file .well-known/matrix/server file with the following content, again replacing the URL with your own. That’s all for the .well-known files. Run the command git add * to tell git about the files, git commit -am 'Add .well-known files' to commit the files, and do a git push to push them back to Github or Gitlab.

{ "m.server": "minecraftchest1-matrix.eastus.cloudapp.azure.com:443" }

Adding Element.

Element is also easy to add. It requires slightly more work to get working correctly, but the steps are all very easy. Download the latest release from https://github.com/vector-im/element-web/releases (the file ending in tar.gz) and extract it to the root of your site (it will extract into a new dir). On Linux, you can use tar -xvf <filename>.tar.gz to extract the file. On Windows, you will likely need a tool line 7zip to extract it. Next, rename the extracted folder if desired. You can also contents down one level if you want it at the root of your site.

Now you need to configure Element. Open the folder that Element lives in and copy config.sample.json to config.json. Open it up and change the homeserver baseurl and server name to the correct values. You can change the other settings, but those are the ones that you are most likely going to want to change.

You can now do git add, git commit and git push if you want, but there is one more thing that needs to be done for Element to work right, and that it configuring CORS (Cross Origin Resource Settings) headers that allow Element to talk to the homeserver. At the root of your repository, create a file named _headers with the following content. Once that is done, add the files to git, commit your changes, and perform a git push

/*
        Access-Control-Allow-Origin: * 
        X-Frame-Options: SAMEORIGIN
        X-Content-Type-Options: nosniff
        X-XSS-Protection: "1; mode=block"
        Content-Security-Policy: "frame-ancestors 'none'"

Serving your repository with Cloudflare pages.

Now you have everything in your repository, we need to serve the content. Login to the Cloudflare dashboard and go to the pages tab on the side, then Create a Project in the top of the middle pane. Select the tab for Github or Gitlab and Signin if you aren’t already. Select the project you want to use, then begin setup at the bottom. Change the project name (which will affect the pages.dev subdomain you get) if desired. You should be able to keep everything else at the defaults, then click save and deploy at the bottom. You should then be taken to the project deployment page. The project will likely take a few minutes to build.

Once it is done, you should get a success message. If not, read through the logs and go back through this post to see of you can figure it out. You can also comment down below and I will be happy to help. Click the Continue to Project button when done and you will be taken to your sites dashboard. And that’s all. I hope this post was helpful for you. Pleas comment below with any feedback you have. Thanks for reading.

Are you on Matrix? Discuss this post at https://matrix.to/#/#minecraftchest1-blog-matrix-elemet-cloudflare:matrix.org

How to Install and Setup Matterbridge

For those of you who do not know, Matterbridge (https://github.com/42wim/matterbridge/) is an application that bridges two or more channels/rooms from 1 or more chat platforms to each other so that users in each can talk to each other.

Being a statically linked, single binary, installation is fairly easy. Configuration is also fairly simple. However some people know more then others. This guide will is intended to help anyone host their own bridge.


Table of contents

Install on Windows

Go to https://github.com/42wim/matterbridge/releases/ and Download the binary for your system. This is most likely going to be windows-64bit.exe. Put it into the folder of your choice. I would recommend making an empty folder, but it is not required that you do so. Open a command Windows by clicking an empty part of the windows to make sure nothing is selected. Them press shift+right-click and select either “Open in Windows Terminal”, or “Open Powershell Window Here”.

This should open a command window that you can run the app in. Run .\matterbridge-1.22.2-windows-64bit.exe in the command window. If all is well, you should see an error like the one below saying that it cannot load the config file. This is normal as we have not created it yet. Proceed to the Configuring section to build the config file.

Installing under Linux

In your favorite browser, https://github.com/42wim/matterbridge/releases and download the binary for your platform. In my case, it is matterbridge-1.22.2-linux-armv6. Put it in the folder of your choice. I prefer to put it in a dedicated directory, but that is up to you.

From a command shell, make it executable by running chmod +x matterbridge-1.22.2-linux-armv6, replacing matterbridge-1.22.2-linux-armv6 with the file you downloaded. Run it with ./matterbridge-1.22.2-linux-armv6, substituting as necessary. If all is well, you should get an error saying that it cannot read the config file. We will build it next.

Other platforms

I do not have other systems to test it on. However, the commands are likely to be similar to the ones for Linux. I also expect that you know more about your system that I do, and that you know how to download a file and run and application on it.

Building your config.

Steps for all possible platforms are available at https://github.com/42wim/matterbridge/wiki/How-to-create-your-config.

Matterbridge uses gateways to bridge different protocols together. Gateways are like a hub where messages are sent to, then copied by each protocol driver. Multiple gateways can be configured, and one instance can have multiple gateways running. A protocol can be setup multiple times. Each configuration on a protocol is called an instance throughout this guide.

Discord

Based off of https://github.com/42wim/matterbridge/wiki/Section-Discord-%28basic%29

Discord messages are relayed using bots. You can create a discord bot by going to https://discord.com/developers/applications and clicking new application. Name the app when prompted.

One the app is created, you will be taken to the general information page. Here you can Rename your app or give the app an optional Description and image. You can change the information here at any time. Once you are ready, click the Bot link on the left side to open the Bot page.

On the Bot page, click Add Bot, then Yes, do it!. This will create a new bot. You can also create a new bot on an existing app as well.

We are now ready to create the configure Matterbridge to use the bot. Create a config file if you haven’t already. Paste the following code snippet near the top of the file.

[discord.mydiscord]
# You can get your token by following the instructions on
# https://github.com/42wim/matterbridge/wiki/Discord-bot-setup
# in particular that the "Members Intent" checkbox is important.
# If you want roles/groups mentions to be shown with names instead of ID, 
# you'll need to give your bot the "Manage Roles" permission.
Token="YOUR_TOKEN_HERE"
Server="name or uid of guild" # picked from guilds the bot is connected to
RemoteNickFormat="[{PROTOCOL}] <{NICK}> "

Replace YOUR_TOKEN_HERE with the bot token. You can get it by clicking Copy button which will copy the token to your clipboard. Paste it into the config file. We will get the server id next.

In order for the bot to work, you need to enable Server Members Intent. Scroll down to Privileged Gateway Intents and enable the Server Members Intent switch.

Now you will need to add the bot to the server you want to bridge. Click the OAuth2 button and select bot under scopes, then click the Copy button. This will give you the URL to add the bot to the server with. Paste it in a browser tab and press enter to go to the page.

Select the server that you want to add the bot to from the dropdown and click Authorize. Note: You will need to have Manage Server permission on the server to add the bot to it.

Once the bot is added, you will need to get the server ID. To do so, go to your user setting > Advanced and enable Developer Mode.

Close the Settings page and open the server you added the bot to. Right+Click on the server name and click Copy ID. Just clicking on the Server name will not work. Go back to the config file and set the Server field to the ID you just copied. You are now ready to add the instance to a Gateway. Set the Channel in the gateway to the Channel name in the server.

TODO: Write out a detailed tutorial for Telegram.

Telegram

Based off of https://github.com/42wim/matterbridge/wiki/Section-Telegram-%28basic%29

Telegram messages are relayed using bots. Full instructions to create a bot are at https://core.telegram.org/bots#6-botfather.

To create a bot, start a chat with @botfather in Telegram. Click start to begin a conversation. Send /newbot. Give it a name and a username. Usernames are 5-32 characters long and are case insensitive, but may only include Latin characters, numbers, and underscores. Your bot’s username must end in ‘bot’, e.g. ‘tetris_bot’ or ‘TetrisBot’.

Creating a bot with BotFather. Using Unigram, an unofficial Telegram client.

Next, you need to set the Privacy mode of the bot to disabled for Mattermost to work properly with the bot. Otherwise it will only relay mentions. Do so by sending /setprivacy. Select Disable when prompted.

Setting the bots privacy mode with BotFather. Using Unigram, an unofficial Telegram client.

Add the following template to your config. Replace Yourtokenhere with the token you got from BotFather.

[telegram.mytelegram]
#See https://core.telegram.org/bots#6-botfather 
#and https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau
Token="Yourtokenhere"
RemoteNickFormat="({PROTOCOL}) {NICK} "
MessageFormat="HTMLNick"

Matrix

Creating a Matrix bridge could not be more simple. First, create a dedicated user for your bot. This is not required, but Matterbirdge will not relay messages from the account it is logged in as.

Second, paste the following code block into your config, filling in the appropriate fields

[matrix.mymatrix]
#Server is your homeserver (eg https://matrix.org)
Server="https://matrix.org"
#login/pass of your bot.
#Use a dedicated user for this and not your own!
#Messages sent from this user will not be relayed to avoid loops.
Login="yourlogin"
Password="yourpass"
RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
#Whether to send the homeserver suffix. eg ":matrix.org" in @username:matrix.org
#to other bridges, or only send "username".(true only sends username)
#OPTIONAL (default false)
NoHomeServerSuffix=false

Last, you will need to get the room ID. While the room name sometimes works, I have not had much success with it. To find it in Click on the room name > Advanced, and save the Internal room ID field. This is what you will use for the channel later.

Configuring your gateway(s)

One of the nice things about Matterbridge is that is supports having multiple gateways, and multiple bridges in each gateway.

To create a gateway, go to the bottom of the config and create a gateway, using the following as a sample. Replace protocol with the protocol name, and myprotocol with the bridge name. Repeat with as many gateways as you want. Just make sure to give each gateway a uniqure name.

[[gateway]]
name="gateway1"
enable=true
[[gateway.inout]]
account="protocol.myprotocol"
channel="prtocolchannel"
[[gateway.inout]]
account="protocol2.myprotocol2"
channel="protocol2channel"

TODO: Write out a detailed tutorial for Mattermost.

TODO: Write out a detailed tutorial for IRC.

Additional help?

We have discussions at the Github repro. https://github.com/42wim/matterbridge/discussions. You can also read and open bugs on the issue tracker at https://github.com/42wim/matterbridge/issues. You can also chat with community members on any of several chat apps. https://github.com/42wim/matterbridge/#chat-with-us. Each room is bridged to each other using Matterbridge.

Design a site like this with WordPress.com
Get started