diff --git a/README.md b/README.md index 3e4407a17..4e6594113 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ SMP server uses in-memory persistence with an optional append-only log of create To enable store log, initialize server using `smp-server -l` command, or modify `smp-server.ini` created during initialization (uncomment `enable: on` option in the store log section). Use `smp-server --help` for other usage tips. +> **Please note:** On initialization SMP server creates a chain of two certificates: a self-signed CA certificate ("offline") and a server certificate used for TLS handshake ("online"). **You should store CA certificate private key securely and delete it from the server. If server TLS credential is compromised this key can be used to sign a new one, keeping the same server identity and established connections.** CA private key location by default is `/etc/opt/simplex/ca.key`. + SMP server implements [SMP protocol](https://github.com/simplex-chat/simplexmq/blob/master/protocol/simplex-messaging.md). #### Running SMP server on MacOS @@ -96,10 +98,12 @@ Deployment on Linode is performed via StackScripts, which serve as recipes for L - Create a Linode account or login with an already existing one. - Open [SMP server StackScript](https://cloud.linode.com/stackscripts/748014) and click "Deploy New Linode". - You can optionally configure the following parameters: - - [SMP Server store log](#SMP-server) flag for queue persistence on server restart (recommended). - - [Linode API token](https://www.linode.com/docs/guides/getting-started-with-the-linode-api#get-an-access-token) for attaching server info as tags to Linode (server address, fingerprint, version) and adding A record to your 2nd level domain (Note: 2nd level e.g. `example.com` domain should be [created](https://cloud.linode.com/domains/create) in your account prior to deployment). The API token access scope should be read/write access to "linodes" (to create tags), and "domains" (to add A record for the 3rd level domain, e.g. `smp`). - - Domain name to use instead of Linode ip address, e.g. `smp.example.com`. -- Choose the region and plan according to your requirements (for regular use Shared CPU Nanode should be sufficient). + - SMP Server store log flag for queue persistence on server restart, recommended. + - [Linode API token](https://www.linode.com/docs/guides/getting-started-with-the-linode-api#get-an-access-token) to attach server address etc. as tags to Linode and to add A record to your 2nd level domain (e.g. `example.com` [domain should be created](https://cloud.linode.com/domains/create) in your account prior to deployment). The API token access scopes: + - read/write for "linodes" + - read/write for "domains" + - Domain name to use instead of Linode IP address, e.g. `smp1.example.com`. +- Choose the region and plan, Shared CPU Nanode with 1Gb is sufficient. - Provide ssh key to be able to connect to your Linode via ssh. If you haven't provided a Linode API token this step is required to login to your Linode and get the server's fingerprint either from the welcome message or from the file `/etc/opt/simplex/fingerprint` after server starts. See [Linode's guide on ssh](https://www.linode.com/docs/guides/use-public-key-authentication-with-ssh/) . - Deploy your Linode. After it starts wait for SMP server to start and for tags to appear (if a Linode API token was provided). It may take up to 5 minutes depending on the connection speed on the Linode. Connecting Linode IP address to provided domain name may take some additional time. - Get `address` and `fingerprint` either from Linode tags (click on a tag and copy it's value from the browser search panel) or via ssh. @@ -121,11 +125,18 @@ To deploy SMP server use [SimpleX Server 1-click app](https://marketplace.digita - Click 'Create SimpleX server Droplet' button. - Choose the region and plan according to your requirements (Basic plan should be sufficient). - Finalize Droplet creation. -- Open "Console" on your Droplet management page to get SMP server fingerprint - either from the welcome message or from `/etc/opt/simplex/fingerprint`. Alternatively you can manually SSH to created Droplet, see [instruction](https://docs.digitalocean.com/products/droplets/how-to/connect-with-ssh/). +- Open "Console" on your Droplet management page to get SMP server fingerprint - either from the welcome message or from `/etc/opt/simplex/fingerprint`. Alternatively you can manually SSH to created Droplet, see [DigitalOcean instruction](https://docs.digitalocean.com/products/droplets/how-to/connect-with-ssh/). - Great, your own SMP server is ready! Use `smp://@` as SMP server address in the client. Please submit an [issue](https://github.com/simplex-chat/simplexmq/issues) if any problems occur. +> **Please note:** SMP server uses server address as a Common Name for server certificate generated during initialization. If you would like your server address to be FQDN instead of IP address, you can log in to your Droplet and run the commands below to re-initialize the server. Alternatively you can use [Linode StackScript](https://cloud.linode.com/stackscripts/748014) which allows this parameterization. + +```sh +smp-server delete +smp-server init [-l] -n +``` + ## SMP server design ![SMP server design](https://raw.githubusercontent.com/simplex-chat/simplexmq/master/design/server.svg) diff --git a/scripts/smp-server-digitalocean-droplet/README.md b/scripts/smp-server-digitalocean-droplet/README.md index 7d58ad0f7..84a483e9c 100644 --- a/scripts/smp-server-digitalocean-droplet/README.md +++ b/scripts/smp-server-digitalocean-droplet/README.md @@ -1,10 +1,4 @@ -# Server image for DigitalOcean - - +# SMP server image for DigitalOcean ## How to build an image diff --git a/scripts/smp-server-digitalocean-droplet/files/opt/simplex/initialize_server.sh b/scripts/smp-server-digitalocean-droplet/files/opt/simplex/initialize_server.sh new file mode 100644 index 000000000..4b66e2a5c --- /dev/null +++ b/scripts/smp-server-digitalocean-droplet/files/opt/simplex/initialize_server.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Download latest release +bin_dir="/opt/simplex/bin" +binary="$bin_dir/smp-server" +mkdir -p $bin_dir +curl -L -o $binary https://github.com/simplex-chat/simplexmq/releases/latest/download/smp-server-ubuntu-20_04-x86-64 +chmod +x $binary + +# / Add to PATH +cat > /etc/profile.d/simplex.sh << EOF +#!/bin/bash + +export PATH="$PATH:$bin_dir" + +EOF +# Add to PATH / + +# Source and test PATH +source /etc/profile.d/simplex.sh +smp-server --version + +# Initialize server +ip_address=$(curl ifconfig.me) +smp-server init -l --ip $ip_address + +# Server fingerprint +fingerprint=$(cat /etc/opt/simplex/fingerprint) + +# Set up welcome script +echo "bash /opt/simplex/on_login.sh $fingerprint $ip_address" >> /root/.bashrc + +# / Create systemd service for SMP server +cat > /etc/systemd/system/smp-server.service << EOF +[Unit] +Description=SMP server + +[Service] +Type=simple +ExecStart=/bin/sh -c "exec $binary start >> /var/opt/simplex/smp-server.log 2>&1" + +[Install] +WantedBy=multi-user.target + +EOF +# Create systemd service for SMP server / + +# Start systemd service for SMP server +chmod 644 /etc/systemd/system/smp-server.service +sudo systemctl enable smp-server +sudo systemctl start smp-server diff --git a/scripts/smp-server-digitalocean-droplet/files/opt/simplex/server_bootstrap.sh b/scripts/smp-server-digitalocean-droplet/files/opt/simplex/server_bootstrap.sh new file mode 100644 index 000000000..136a32754 --- /dev/null +++ b/scripts/smp-server-digitalocean-droplet/files/opt/simplex/server_bootstrap.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -eu + +if [[ ! -f /opt/simplex/do_initialize_server ]]; then + touch /opt/simplex/do_initialize_server +elif [[ ! -f /etc/opt/simplex/smp-server.ini ]]; then + chmod +x /opt/simplex/initialize_server.sh + /opt/simplex/initialize_server.sh +else + echo "SMP server already initialized" +fi diff --git a/scripts/smp-server-digitalocean-droplet/scripts/03-init-start.sh b/scripts/smp-server-digitalocean-droplet/scripts/03-init-start.sh index e88fc0920..3bb2aa8a0 100644 --- a/scripts/smp-server-digitalocean-droplet/scripts/03-init-start.sh +++ b/scripts/smp-server-digitalocean-droplet/scripts/03-init-start.sh @@ -1,51 +1,23 @@ #!/bin/bash -# Download latest release -bin_dir="/opt/simplex/bin" -binary="$bin_dir/smp-server" -mkdir -p $bin_dir -curl -L -o $binary https://github.com/simplex-chat/simplexmq/releases/latest/download/smp-server-ubuntu-20_04-x86-64 -chmod +x $binary +chmod +x /opt/simplex/server_bootstrap.sh -# / Add to PATH -cat <> /etc/profile.d/simplex.sh -#!/bin/bash - -export PATH="$PATH:$bin_dir" - -EOT -# Add to PATH / - -# Source and test PATH -source /etc/profile.d/simplex.sh -smp-server --version - -# Initialize server -ip_address=$(curl ifconfig.me) -smp-server init -l --ip @ip_address - -# Server fingerprint -fingerprint=$(cat /etc/opt/simplex/fingerprint) - -# Set up welcome script -echo "bash /opt/simplex/on_login.sh $fingerprint $ip_address" >> /root/.bashrc - -# / Create systemd service -cat <> /etc/systemd/system/smp-server.service +# / Create systemd service for server bootstrap script +cat > /etc/systemd/system/server-bootstrap.service << EOF [Unit] -Description=SMP server systemd service +Description=Server bootstrap script that downloads and initializes SMP server from the latest release [Service] -Type=simple -ExecStart=/bin/sh -c "$binary start" +Type=oneshot +ExecStart=/opt/simplex/server_bootstrap.sh [Install] WantedBy=multi-user.target -EOT -# Create systemd service / +EOF +# Create systemd service for server bootstrap script / -# Start systemd service -chmod 644 /etc/systemd/system/smp-server.service -sudo systemctl enable smp-server -sudo systemctl start smp-server +# Start systemd service for server bootstrap script +chmod 644 /etc/systemd/system/server-bootstrap.service +sudo systemctl enable server-bootstrap +sudo systemctl start server-bootstrap